Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide convenient Expression.convert_numeric() #24850

Open
rwst opened this issue Feb 27, 2018 · 2 comments
Open

Provide convenient Expression.convert_numeric() #24850

rwst opened this issue Feb 27, 2018 · 2 comments

Comments

@rwst
Copy link

rwst commented Feb 27, 2018

From https://groups.google.com/forum/#!topic/sage-support/3eCHzmJ1MXY:

sage: cm = units.length.centimeter;
sage: x = 1e-6*cm;
sage: x
(1.00000000000000e-6)*centimeter
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)
    864 
    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")
   5785 
   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.

Component: symbolics

Issue created by migration from https://trac.sagemath.org/ticket/24850

@rwst rwst added this to the sage-8.2 milestone Feb 27, 2018
@raghukul01
Copy link

comment:2

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()
        else:
            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?

@DaveWitteMorris
Copy link
Member

comment:3

As suggested in the description of this ticket, and specifically stated in the sage-support discussion that is mentioned there, the _convert() method can deal with the example in this ticket:

sage: cm = units.length.centimeter;
sage: x = 1e-6*cm;
sage: x._convert({'parent':RealField(10)})
(1.0e-6)*centimeter

So this problem will be solved if #12577 is merged. And the case here (or something similar) could be added to the docstring as an example.

However, I don't think that would be a complete solution to the issue that is raised here, because attempting to evaluate sin(x)*cm numerically (when x is a variable) should give a TypeError: cannot evaluate symbolic expression numerically, but we have:

sage: var('x');
sage: (sin(x)*cm)._convert({'parent':RealField(10)})
1.0*centimeter*sin(x)

@mkoeppe mkoeppe removed this from the sage-8.2 milestone Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants