3

I am trying to do partial derivatives using sympy and I want to convert it to a function so that I can substitute values and estimate the derivatives at some values of t_1, t_2. The code I am using is as follows:

import sympy as sp
import numpy as np
from sympy import init_printing
init_printing()
t_1,t_2,X_1,X_2,Y_1,Y_2,X_c1,X_c2,Y_c1,Y_c2,a_1,a_2,psi_1,psi_2,b_1,b_2= sp.symbols('t_1 t_2 X_1 X_2 Y_1 Y_2 X_c1 X_c2 Y_c1 Y_c2 a_1 a_2 psi_1 psi_2 b_1 b_2')

X_1=X_c1 + (a_1 * sp.cos(t_1) * sp.cos(psi_1)) - ((b_1) * sp.sin(t_1)* sp.sin(psi_1))

X_2=X_c2 + (a_2 * sp.cos(t_2) * sp.cos(psi_2)) - ((b_2) * sp.sin(t_2)* sp.sin(psi_2))

Y_1=Y_c1 + (a_1 * sp.cos(t_1) * sp.sin(psi_1)) + ((b_1) * sp.sin(t_1)* sp.cos(psi_1))

Y_2=Y_c2 + (a_2 * sp.cos(t_2) * sp.sin(psi_2)) + ((b_2) * sp.sin(t_2)* sp.sin(psi_2))

D=(((X_2-X_1)**2) + ((Y_2-Y_1)**2))**0.5

y_1=sp.diff(D,t_1)

y_2=sp.diff(D,t_2)

f=sp.lambdify(t_1, y_1, "numpy")

g=sp.lambdify(t_2, y_2, "numpy")

When I try to substitute a value for t_1 using,

f(np.pi/2)

I get the following error:

AttributeError   Traceback (most recent call last)
<ipython-input-26-f37892b21c8b> in <module>()
----> 1 f(np.pi/2)

/users/vishnu/anaconda3/lib/python3.5/site-packages/numpy    /__init__.py in <lambda>(_Dummy_23)

AttributeError: 'Symbol' object has no attribute 'cos'

I referred to the following links:

What causes this error (AttributeError: 'Mul' object has no attribute 'cos') in Python?

Python AttributeError:cos

but I think my imports of numpy and sympy are not clashing unlike the cases mentioned in those links. Any help is appreciated.

Community
  • 1
  • 1
Vishnu
  • 499
  • 1
  • 5
  • 23
  • 2
    Expression `y_1` is a function of many variables, however, your `lambdify` definition implies only a single input argument (`t_1`), resulting in error. Either define the `lambdify` function as having multiple input arguments, or substitute the other symbolic variables by numbers. – Stelios Sep 28 '16 at 17:05
  • Hi Stelios, thanks for replying. Is it not possible that we calculate the partial derivative treating other variables as symbols and only substitute for t_1? For example: if t_1=np.pi/2 then partial derivative of the expression 'cos(t_1)*X_c1' with respect to t_1 equals to '-X_c1'. – Vishnu Sep 28 '16 at 17:27
  • The notion of "symbol" as used in `Sympy` is not valid in `Numpy`. `Numpy` only understands functions with (multiple) numerical arguments. If you only need to replace `t_1` with numbers, treating the other variables as symbols, you should stick with `Sympy` (e.g., using the `subs` method) – Stelios Sep 28 '16 at 17:33
  • For my complete program, I am interested to substitute values for all the variables and then calculate the derivatives. Could you please give me an example using lambdify as to how I could substitute for all variables and then use numpy? Is it simply f=sp.lambdify(t_1,X_c1,a_1,b_1,..... , y_1, "numpy") – Vishnu Sep 28 '16 at 17:50
  • Check the [documentation](http://docs.sympy.org/latest/modules/utilities/lambdify.html#sympy.utilities.lambdify.lambdify) examples. – Stelios Sep 28 '16 at 17:54

1 Answers1

5

This type of error occurs when you call np.cos(a_symbol), which apparently translates under-the-hood in numpy to a_symbol.cos().

lambdify is for numeric calculations - it replaces all sp calls with np calls. But what you're doing is symbolic. This is enough for your problem:

f1 = lambda t: y_1.subs({t_1: t})
f2 = lambda t: y_2.subs({t_2: t})
Eric
  • 95,302
  • 53
  • 242
  • 374