0

I have the following case: I defined a Sympy Matrix (Vector) which is a function of parameters in some, but not all elements. So e.g. take

from sympy import *
a = Symbol('a')
M = Matrix([a,0])

Now I want this to be a function which takes numpy arrays as an element, I used lambdify for this. Actually I want M to be a row vector so I did the following which I found here.

funcM = lambdify([a], M.T.tolist()[0], 'numpy')

Passing a list or an array, e.g. [0,1] to this new function gives me:

In [596]: funcM([0,1])
Out[596]: [[0, 1], 0]

Actually I want the function funcM to work in a way that the output is

[[0,1],[0,0]]

so that the output contains two column vectors, one for each input value in the list, so the column with 0,0 for the input 0 and the column 1,0 for the input 1. Thanks for helping me!

  • 1
    I find `print(funcM.__doc__)` to be helpful in seeing what the resulting `numpy` code is like. – hpaulj Nov 18 '20 at 21:54
  • 1
    The code for your function is `([a, 0])`. – hpaulj Nov 18 '20 at 22:11
  • 1
    `lambdify` does relatively simple lexical translation to python/numpy. If you don't know how to do something, like building the desired array with `numpy`, trying to get there via `sympy` is not likely to work. `numpy` `broadcasting` controls the interaction of 2 or more arrays; it does not come into play when constructing arrays from smaller pieces. – hpaulj Nov 18 '20 at 23:26
  • Okay thanks, that helps me to understand where exactly the problem is. Still I have no idea how to fix this... – maximumphysics Nov 19 '20 at 09:00
  • 1
    While I could write a `numpy` expression that concatenates a row of 0s to a 1d array, I have no idea how this could be generated from a sympy expression. – hpaulj Nov 19 '20 at 09:26

0 Answers0