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
> Le 18 juin 2019 à 13:26, Lucy Jackson <[email protected]> a écrit :
>
> 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?
>
>
> On Wed, 5 Jun 2019 at 06:07, Aaron Meurer <[email protected]
> <mailto:[email protected]>> wrote:
> 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 <[email protected]
> <mailto:[email protected]>> 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 [email protected]
> > <mailto:sympy%[email protected]>.
> > To post to this group, send email to [email protected]
> > <mailto:[email protected]>.
> > Visit this group at https://groups.google.com/group/sympy
> > <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
> >
> > <https://groups.google.com/d/msgid/sympy/1004b533-73fe-4fc6-b051-a91b2a3c3fe9%40googlegroups.com>.
> > For more options, visit https://groups.google.com/d/optout
> > <https://groups.google.com/d/optout>.
>
> --
> 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 [email protected]
> <mailto:sympy%[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at https://groups.google.com/group/sympy
> <https://groups.google.com/group/sympy>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2Bwq2e0CQVitLYvbw897nL%3DG9pQH1N8Y9JeO8t0A8c8FQ%40mail.gmail.com
>
> <https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2Bwq2e0CQVitLYvbw897nL%3DG9pQH1N8Y9JeO8t0A8c8FQ%40mail.gmail.com>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> 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 [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at https://groups.google.com/group/sympy
> <https://groups.google.com/group/sympy>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/CAGwk1%3DetdW0k6q%2B2LPBAgqq8o55Ng0DiuR5rp32Rt8EuL0NGFA%40mail.gmail.com
>
> <https://groups.google.com/d/msgid/sympy/CAGwk1%3DetdW0k6q%2B2LPBAgqq8o55Ng0DiuR5rp32Rt8EuL0NGFA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
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 [email protected].
To post to this group, send email to [email protected].
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/9D4E376E-26BB-415E-9637-1D0546B2E254%40abou-samra.fr.
For more options, visit https://groups.google.com/d/optout.