From 353718dc3eae0be0d1a65a37485101c2906eb011 Mon Sep 17 00:00:00 2001 From: "Colin B. Macdonald" Date: Tue, 13 Sep 2022 00:23:36 -0700 Subject: [PATCH] make_2d_sym: support flat input and shape Fixes #1236. For now, just add a new underscore version that takes a shape kwarg. --- inst/@sym/private/elementwise_op.m | 6 +++--- inst/private/python_header.py | 15 +++++++++++++++ inst/private/python_ipc_native.m | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/inst/@sym/private/elementwise_op.m b/inst/@sym/private/elementwise_op.m index 996bd43d..74c99844 100644 --- a/inst/@sym/private/elementwise_op.m +++ b/inst/@sym/private/elementwise_op.m @@ -96,11 +96,11 @@ '# dbout(f"at least one matrix param, shape={q.shape}")' 'assert len(q.shape) == 2, "non-2D arrays/tensors not yet supported"' 'm, n = q.shape' - 'g = [[0]*n for i in range(m)]' + 'g = []' 'for i in range(m):' ' for j in range(n):' - ' g[i][j] = _op(*[k[i, j] if isinstance(k, (MatrixBase, NDimArray)) else k for k in _ins])' - 'return make_2d_sym(g)' ]; + ' g.append(_op(*[k[i, j] if isinstance(k, (MatrixBase, NDimArray)) else k for k in _ins]))' + 'return _make_2d_sym(g, shape=q.shape)' ]; z = pycall_sympy__ (cmd, varargin{:}); diff --git a/inst/private/python_header.py b/inst/private/python_header.py index c93c8309..ffa6c51b 100644 --- a/inst/private/python_header.py +++ b/inst/private/python_header.py @@ -263,6 +263,21 @@ def make_2d_sym(it_of_it, dbg_matrix_only=False): else: dbout(f"make_2d_sym: constructing 2D sym...") return Array(ls_of_ls) + def _make_2d_sym(flat, shape, dbg_matrix_only=False): + """ + If all elements of FLAT are Expr, construct the + corresponding Matrix. Otherwise, construct the + corresponding non-Matrix 2D sym. + """ + flat = list(flat) + if Version(spver) <= Version("1.11.1"): + # never use Array on older SymPy + dbg_matrix_only = True + if (dbg_matrix_only + or all(isinstance(elt, Expr) for elt in flat)): + return Matrix(*shape, flat) + dbout(f"make_2d_sym: constructing 2D sym...") + return Array(flat, shape) except: echo_exception_stdout("in python_header defining fcns block 5") raise diff --git a/inst/private/python_ipc_native.m b/inst/private/python_ipc_native.m index 5bfa94e5..68f64746 100644 --- a/inst/private/python_ipc_native.m +++ b/inst/private/python_ipc_native.m @@ -133,6 +133,21 @@ ' else:' ' dbout(f"make_2d_sym: constructing 2D sym...")' ' return Array(ls_of_ls)' + ' def _make_2d_sym(flat, shape, dbg_matrix_only=False):' + ' """' + ' If all elements of FLAT are Expr, construct the' + ' corresponding Matrix. Otherwise, construct the' + ' corresponding non-Matrix 2D sym.' + ' """' + ' flat = list(flat)' + ' if Version(spver) <= Version("1.11.1"):' + ' # never use Array on older SymPy' + ' dbg_matrix_only = True' + ' if (dbg_matrix_only' + ' or all(isinstance(elt, Expr) for elt in flat)):' + ' return Matrix(*shape, flat)' + ' dbout(f"make_2d_sym: constructing 2D sym...")' + ' return Array(flat, shape)' }, newl)) have_headers = true; end