Convert an expression involving translating a vector into a numeric function with lambdify

I wrote a script in python that uses sympy to calculate a pair of vector / matrix formulas. However, when I try to convert them to functions that I can evaluate with sympy.lambdify, I get

SyntaxError: EOL while scanning a string literal

Here is the code with the same error so you can see what I mean.

import sympy x = sympy.MatrixSymbol('x',3,1) f = sympy.lambdify(x, xT*x) 

Thus, the syntax error is related to the expression "x'.dot (x)" and the conversion of ".T" to '.

How can I get around this in order to correctly determine f from the above lambdify?

+7
python vector sympy
source share
2 answers

The work found, although not the cleanest solution ... but it works.

Use the implement_function () method from sympy to define your function. Read the full documentation here: http://docs.sympy.org/latest/modules/utilities/lambdify.html

Here is the code:

 import sympy import numpy as np from sympy.utilities.lambdify import implemented_function x = sympy.MatrixSymbol('x',3,1) f = implemented_function(sympy.Function('f'), lambda x: xT*x) lam_f= sympy.lambdify(x, f(x)) 

Hope this solves your problem :)

+2
source share

It was resolved in the pretty version> = 1.1

Edit:

Example

when u define this x = sympy.MatrixSymbol('x',3,1) you create a matrix,

you can check its indexing and form using print(sympy.Matrix(x))

Now, when you want to multiply Transpose by x by x, you will need to provide the x matrix of the same shape that you defined before

try the following:

 from sympy import MatrixSymbol, lambdify, Matrix x = MatrixSymbol('x', 3, 1) f = lambdify(x, xT*x) a = Matrix([[1], [2], [3]]) print(f(a)) 

you can check this link to better understand lambdify: http://docs.sympy.org/latest/modules/utilities/lambdify.html

0
source share

All Articles