Skip to content

Commit 94efa15

Browse files
Alex VongAlex Vong
authored andcommitted
Python header: Add more 2D sym functions.
Add more 2D sym functions to abstract over the idea of 2D sym so that we can choose to represent non-Matrix 2D sym to be something other than an Array in the future. For example, we could use TableForm. WIP: This is incomplete. More functions need to use these 2D sym functions. Not sure if this is needed anymore as TableForm was shown to be lacking various features such as indexing and taking transpose. No other candidates are being considered at the moment. * inst/private/{python_header.py,python_ipc_native.m}: Add new 2D sym functions 'is_2d_sym', 'is_matrix', 'is_non_matrix_2d_sym', 'list_from_2d_sym' and 'shape_of_2d_sym'. * inst/@sym/private/mat_rclist_{access,asgn}.m: Use them. * inst/@sym/{transpose.m,vertcat.m}: Use them.
1 parent 7ba9370 commit 94efa15

File tree

6 files changed

+54
-18
lines changed

6 files changed

+54
-18
lines changed

inst/@sym/private/mat_rclist_access.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
end
3535

3636
cmd = {'(A, rr, cc) = _ins'
37-
'AA = A.tolist() if isinstance(A, (MatrixBase, NDimArray)) else [[A]]'
37+
'AA = list_from_2d_sym(A) if is_2d_sym(A) else [[A]]'
3838
'MM = [[AA[i][j]] for i, j in zip(rr, cc)]'
3939
'M = make_2d_sym(MM)'
4040
'return M,'};

inst/@sym/private/mat_rclist_asgn.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@
6565
'if A == []:'
6666
' AA = []'
6767
' (nrows_A, ncols_A) = (0, 0)'
68-
'elif isinstance(A, (MatrixBase, NDimArray)):'
69-
' AA = A.tolist()'
70-
' (nrows_A, ncols_A) = A.shape'
68+
'elif is_2d_sym(A):'
69+
' AA = list_from_2d_sym(A)'
70+
' (nrows_A, ncols_A) = shape_of_2d_sym(A)'
7171
'else:'
7272
' AA = [[A]]'
7373
' (nrows_A, ncols_A) = (1, 1)'
74-
'bb = b.tolist() if isinstance(b, (MatrixBase, NDimArray)) else [[b]]'
74+
'bb = list_from_2d_sym(b) if is_2d_sym(b) else [[b]]'
7575
'entries = dict(zip(zip(rr, cc), flatten(bb, levels=1)))'
7676
'def entry(i, j):'
7777
' if (i, j) in entries:'

inst/@sym/transpose.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@
6666
print_usage ();
6767
end
6868

69-
cmd = {'def is_matrix_or_array(x):'
70-
' return isinstance(x, (MatrixBase, NDimArray))'
71-
'x, = _ins'
72-
'return transpose(x) if is_matrix_or_array(x) else x'};
69+
cmd = {'x, = _ins'
70+
'return transpose(x) if is_2d_sym(x) else x'};
7371

7472
z = pycall_sympy__ (cmd, x);
7573

inst/@sym/vertcat.m

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@
4545

4646
% special case for 0x0 but other empties should be checked for
4747
% compatibilty
48-
cmd = {'def is_matrix_or_array(x):'
49-
' return isinstance(x, (MatrixBase, NDimArray))'
50-
'def number_of_columns(x):'
51-
' return x.shape[1] if is_matrix_or_array(x) else 1'
48+
cmd = {'def number_of_columns(x):'
49+
' return shape_of_2d_sym(x)[1] if is_2d_sym(x) else 1'
5250
'def all_equal(*ls):'
5351
' return True if ls == [] else all(ls[0] == x for x in ls[1:])'
5452
'def as_list_of_list(x):'
55-
' return x.tolist() if is_matrix_or_array(x) else [[x]]'
53+
' return list_from_2d_sym(x) if is_2d_sym(x) else [[x]]'
5654
'args = [x for x in _ins if x != zeros(0, 0)] # remove 0x0 matrices'
5755
'ncols = [number_of_columns(x) for x in args]'
5856
'if not all_equal(*ncols):'

inst/private/python_header.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ def octoutput(x, et):
241241

242242

243243
try:
244+
# begin: 2D sym funcs
245+
# 2D sym funcs defined in inst/private/python_ipc_native.m
246+
# and inst/private/python_header.py should be kept in sync
244247
def make_2d_sym(it_of_it, dbg_matrix_only=False):
245-
# should be kept in sync with the same function
246-
# defined in inst/private/python_ipc_native.m
247248
# FIXME: dbg_matrix_only is used for debugging, remove
248249
# it once sympy drops non-Expr support in Matrix
249250
"""
@@ -259,7 +260,26 @@ def make_2d_sym(it_of_it, dbg_matrix_only=False):
259260
return Matrix(ls_of_ls)
260261
else:
261262
dbout(f"make_2d_sym: constructing 2D sym...")
263+
# FIXME: should we use Array or TableForm?
262264
return Array(ls_of_ls)
265+
def is_2d_sym(x):
266+
types = (MatrixBase, NDimArray, TableForm)
267+
return isinstance(x, types)
268+
def is_matrix(x):
269+
return isinstance(x, MatrixBase)
270+
def is_non_matrix_2d_sym(x):
271+
return isinstance(x, (NDimArray, TableForm))
272+
def list_from_2d_sym(X):
273+
if isinstance(X, TableForm):
274+
return [[x for x in tup] for tup in X._lines]
275+
else:
276+
return X.tolist()
277+
def shape_of_2d_sym(X):
278+
if isinstance(X, TableForm):
279+
return (X._h, X._w)
280+
else:
281+
return X.shape
282+
# end: 2D sym funcs
263283
except:
264284
echo_exception_stdout("in python_header defining fcns block 5")
265285
raise

inst/private/python_ipc_native.m

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@
111111
' # should be kept in sync with the same function'
112112
' # defined in inst/private/python_header.py'
113113
' sys.stderr.write("pydebug: " + str(l) + "\n")'
114+
'# begin: 2D sym funcs'
115+
'# 2D sym funcs defined in inst/private/python_ipc_native.m'
116+
'# and inst/private/python_header.py should be kept in sync'
114117
'def make_2d_sym(it_of_it, dbg_matrix_only=False):'
115-
' # should be kept in sync with the same function'
116-
' # defined in inst/private/python_header.py'
117118
' # FIXME: dbg_matrix_only is used for debugging, remove'
118119
' # it once sympy drops non-Expr support in Matrix'
119120
' """'
@@ -129,7 +130,26 @@
129130
' return Matrix(ls_of_ls)'
130131
' else:'
131132
' dbout(f"make_2d_sym: constructing 2D sym...")'
133+
' # FIXME: should we use Array or TableForm?'
132134
' return Array(ls_of_ls)'
135+
'def is_2d_sym(x):'
136+
' types = (MatrixBase, NDimArray, TableForm)'
137+
' return isinstance(x, types)'
138+
'def is_matrix(x):'
139+
' return isinstance(x, MatrixBase)'
140+
'def is_non_matrix_2d_sym(x):'
141+
' return isinstance(x, (NDimArray, TableForm))'
142+
'def list_from_2d_sym(X):'
143+
' if isinstance(X, TableForm):'
144+
' return [[x for x in tup] for tup in X._lines]'
145+
' else:'
146+
' return X.tolist()'
147+
'def shape_of_2d_sym(X):'
148+
' if isinstance(X, TableForm):'
149+
' return (X._h, X._w)'
150+
' else:'
151+
' return X.shape'
152+
'# end: 2D sym funcs'
133153
}, newl))
134154
have_headers = true;
135155
end

0 commit comments

Comments
 (0)