Can you pass a symbolic variable through a function?

1,323 views
Skip to first unread message

Lucy Jackson

unread,
Jun 4, 2019, 11:36:07 AM6/4/19
to sympy
I have a function that I have written, attached file, and would like to pass theta, thetad, phi and phid through it as symbolic variables. Therefore hoping for an output in terms of these variables that I can then integrate. Is this possible??


C_vm.py

Aaron Meurer

unread,
Jun 5, 2019, 1:07:35 AM6/5/19
to sympy
Yes, you should replace 'math' and 'numpy' with 'sympy', so that the
symbolic functions are used. I also recommend using sympy.Matrix for
the symbolic expressions.

Then, if you want a numeric expression you can use with numpy, use
lambdify() to convert the SymPy expression into a numeric function.
lambdify() will convert sympy.Matrix into a numpy array.

Aaron Meurer

On Tue, Jun 4, 2019 at 9:36 AM Lucy Jackson <lucy.jac...@gmail.com> wrote:
>
> I have a function that I have written, attached file, and would like to pass theta, thetad, phi and phid through it as symbolic variables. Therefore hoping for an output in terms of these variables that I can then integrate. Is this possible??
>
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/1004b533-73fe-4fc6-b051-a91b2a3c3fe9%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Lucy Jackson

unread,
Jun 18, 2019, 7:26:40 AM6/18/19
to sy...@googlegroups.com
When passing symbolic variables through the function i get the error message "can't convert expression to float" 

This does not happen if I pass numerical values through the function.

Any suggestions?


Jean ABOU SAMRA

unread,
Jun 18, 2019, 8:31:45 AM6/18/19
to lucy.jac...@gmail.com, sy...@googlegroups.com
Hi,
Next time, please provide an example code so that people can do their own experiments.

I guess you did something like:

>>> from math import sqrt
>>> from sympy.abc import *
>>> sqrt(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sympy/core/expr.py", line 280, in __float__
    raise TypeError("can't convert expression to float")
TypeError: can't convert expression to float

This happens because you used a function from the standard library, here math.sqrt, and you gave it a SymPy expression as an argument. The problem is that math.sqrt() is for numbers and doesn't understand expressions which are coming from a third party module. Here, it tried to convert your 'x' into a float (hence the message coming from a __float__ method) but SymPy told you that this expression can't be converted to a float because you gave no value to 'x'. Of course when you pass a number, the conversion to float succeeds.
So, just use the sqrt function provided by SymPy:

>>> from sympy import *
>>> from sympy.abc import *
>>> sqrt(x) # this is sympy's sqrt, not math's
sqrt(x)

The sqrt functions defined in the math module and in the SymPy library have the same name, and yet they are completely different because math.sqrt() acts on a real number and returns a real number, whereas sympy.sqrt acts on any number or expression and returns an expression -- which can later be evaluated, or printed, or derivated, and so on.
(Of course perhaps your problem is with a sin() or whatever but I used sqrt() for the demonstration.)

Regards,
Jean Abou Samra

Reply all
Reply to author
Forward
0 new messages