matlabFunction Equivalent?

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Jan 20 17:48:48 EST 2014


On 20/01/2014 22:09, rpi.baldum at gmail.com wrote:
> Hey all,
>
> I'm new at Python, so if you see any mistakes feel free to let me know.
>
> I'm trying to take a symbolic expression and turn it into a variable equation or function. I think that just an expression of variables would be preferable.
>
> I have a range equation which I form using symbols and then take various derivatives of it. I then want to have these derivatives on hand to use for various functions, but short of using sub every time or just copy pasting from the console output (I don't want to do that), I can't find an efficient way to do this. Matlab had matlabFunction which was really handy, but I don't think Python has an equivalent.
>
> ########
> import numpy as np
> import scipy as sp
> import sympy as sy
> import math as ma
>
> x, y, z, x_s, y_s, z_s, theta, theta_dot, x_dot, y_dot, z_dot = sy.symbols('x y z x_s y_s z_s theta theta_dot x_dot y_dot z_dot')
>
> rho = (x**2 + y**2 + z**2 + x_s**2 + y_s**2 + z_s**2 - 2*(x*x_s + y*y_s)*sy.cos(theta) + 2*(x*y_s - y*x_s)*sy.sin(theta) - 2*z*z_s)**(0.5)
>
> rho_dot = (x*x_dot + y*y_dot + z*z_dot - (x_dot*x_s + y_dot*y_s)*sy.cos(theta) + theta_dot*(x*x_s + y*y_s)*sy.sin(theta) + (x_dot*y_s - y_dot*x_s)*sy.sin(theta) + theta_dot*(x*y_s - y*x_s)*sy.cos(theta) - z_dot*z_s)/rho
>
> drho_dx = sy.diff(rho, x)
>
> drho_dy = sy.diff(rho, y)
>
> drho_dz = sy.diff(rho, z)
>
> #I then want drho_dx, etc to be variable expressions with x, y, z, etc as variables instead of symbols or numbers. I could do:
>
> x, y, z = 1200, 1300, 1400 #m
>
> drho_dx = subs([x, x], [y, y], [z, z])
>
> #but this seems inefficient to do multiple times. Thoughts?
>

There are references to MatlabFunction in code here 
https://github.com/scipy/scipy/tree/master/scipy/io/matlab but I haven't 
the faintest idea as to whether or not it does what you want, sorry :(

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list