Can't convert expression to float
Asked Answered
P

2

5

I'm trying to learn the ins and outs of symbolic manipulation in python (I'm a beginner).

I have the following basic code, and the output is giving me an error telling me that it "can't convert expression to float".

What's wrong with this code:

from sympy import *
from math import *

def h(x):
    return log(0.75392 * x)

x = symbols('x')
hprime = h(x).diff(x)

print(hprime)
Putdown answered 30/5, 2017 at 13:38 Comment(2)
just remove from math import *Kierakieran
@user46944 I believe the problem is being caused because math is being included after sympy. Just swap the first two lines and you're good to go. Read this for more info.Ship
K
12

This is a classic example of what is said in PEP-8 about wildcard imports:

Wildcard imports ( from <module> import * ) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.

The problem is that you need to work with sympy.log class, but using math.log function instead which works on float objects, not Symbol objects.

When you write

from sympy import *

you are importing in your module namespace everything that sympy package providing at the top level (and there are a lot of stuff, much of that you don't need at all), including sympy.log class.

After next statement

from math import *

you are importing everything in math module, including math.log, which overwrites previously imported sympy.log class.

Considering this your example may be written like

import sympy


def h(x):
    return sympy.log(0.485022 * x)


x = sympy.symbols('x')
h_x = h(x)
hprime = h_x.diff(x)

print(hprime)

gives us

1.0/x

P. S.: I've removed math import since it is not used in given example.

Kierakieran answered 30/5, 2017 at 13:50 Comment(5)
Yes, it does. It is like redefining a function. The last definition will stick.Granoff
I wish it was that easy, but it is not. I have not looked into the packages, but imagine that math defines a function ln whereas sympy does not. Then you do import math as * and import sympy as *. Now if you call log, then python will use sympy.log since it was called last. But if you call ln, then it will use math.ln, because there is no sympy.ln. You see, importing as * gets confusing really fast.Granoff
@user46944: Python import system is a hard thing at first, but you will get used to it and then understand its awesomeness, i guessKierakieran
@user46944: docs is a good place to startKierakieran
This answer is also applied to the Euler constant. Instead of math.exp use sympy.expNarghile
G
3

The problem here is that both the sympy and the math package define a function called log.

Importing them as from sympy import * and then from math import * overrides the sympy.log with math.log.

Better always use import sympy and then call your functions sympy.log or (if as lazy as me) do import sympy as sym and then sym.log. Be sure to do so with the math package as well. This method will save you a lot of hassle in the future and makes your code easier to understand for others.

Granoff answered 30/5, 2017 at 13:55 Comment(1)
Yes, you can. You can actually do import sympy as np and import numpy as sym. It's valid python code and the python interpreter will not complain, but it will for sure confuse anyone reading your code (including yourself at some point). So you can, but my advice is DON'T!Granoff

© 2022 - 2024 — McMap. All rights reserved.