symengine.py icon indicating copy to clipboard operation
symengine.py copied to clipboard

2π is not a number

Open Wrzlprmft opened this issue 6 years ago • 3 comments

The long-expected sequel to the popular π is not a number:

from symengine import pi
print((2*pi).is_number)

is_number returns None for 2*pi, though it should be True.

Also, there seem to be is_number and is_Number, which work differently is at least confusing.

Wrzlprmft avatar May 15 '19 08:05 Wrzlprmft

is_number and is_Number difference is consistent with sympy. Names with capitals indicate a class and the attribute is true only if the expression is an instance of that class

I guess is_number can be defined as an expression without Symbol, FunctionSymbol instances in the expression tree.

isuruf avatar May 15 '19 15:05 isuruf

There is a difference between sympy and symengine here:

> import symengine
> (2*symengine.pi).is_number
None

but

> import sympy
> (2*sympy.pi).is_number
True

I think symengine should do what sympy does.

rikardn avatar Jul 21 '21 14:07 rikardn

The SymPy is_number attribute is confusingly named but basically means "this expression can be evaluated using .evalf() to give a complex floating point number". There is a related also confusingly named attribute "is_comparable" which means "this expression can be evaluated to a real floating point number".

The usage is that you can check with something like:

if expr.is_number:
    expr_approx = expr.evalf()
    ...

It is not enough just to check for symbols in the expression tree because it should also represent a number e.g.:

In [2]: ImmutableMatrix([[1, 2], [3, 4]]).is_number
Out[2]: False

oscarbenjamin avatar Sep 01 '21 16:09 oscarbenjamin