Opened 14 months ago

Last modified 11 months ago

#24850 new enhancement

Provide convenient Expression.convert_numeric()

Reported by: rws Owned by:
Priority: trivial Milestone: sage-8.2
Component: symbolics Keywords: beginner
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:



sage: cm = units.length.centimeter;
sage: x = 1e-6*cm;
sage: x
sage: x.n(digits=2)

TypeError                                 Traceback (most recent call last)
<ipython-input-4-3595ea137553> in <module>()
----> 1 x.n(digits=Integer(2))

/home/Scratch/Install/Linux/SageMath/sage-8.1-Fedora_26-x86_64/SageMath/src/sage/structure/element.pyx in sage.structure.element.Element.n (build/cythonized/sage/structure/element.c:8063)()
    861             0.666666666666667
    862         """
--> 863         return self.numerical_approx(prec, digits, algorithm)
    865     N = deprecated_function_alias(13055, n)

/home/Scratch/Install/Linux/SageMath/sage-8.1-Fedora_26-x86_64/SageMath/src/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.numerical_approx (build/cythonized/sage/symbolic/expression.cpp:36128)()
   5782             res = x.pyobject()
   5783         else:
-> 5784             raise TypeError("cannot evaluate symbolic expression numerically")
   5786         # Important -- the  we get might not be a valid output for numerical_approx in

TypeError: cannot evaluate symbolic expression numerically

Simply call _convert() with the parent keyword.

Change History (2)

comment:1 Changed 14 months ago by rws

  • Keywords beginner added
  • Priority changed from major to trivial

comment:2 Changed 11 months ago by raghukul01

The current implementation of numerical_approx has

        x = x = x._convert(kwds)

        if is_a_numeric(x._gobj):
            res = py_object_from_numeric(x._gobj)
        elif  is_a_constant(x._gobj):
            res = x.pyobject()
            raise TypeError("cannot evaluate symbolic expression numerically")

If x is a n number with units (example in ticket) then it would go to the else block, So can you suggest a check for expression which are numbers attached with units?

Note: See TracTickets for help on using tickets.