Numeric Approximation with Symbolics

171 views
Skip to first unread message

Pstrang Rzekle

unread,
Feb 26, 2018, 7:06:37 PM2/26/18
to sage-support
Is there a way to trick SageMath into presenting numeric approximations with symbolics? In this specific case, handling numbers with units attached.

For example:

┌────────────────────────────────────────────────────────────────────┐
SageMath version 8.1, Release Date: 2017-12-07                    
Type "notebook()" for the browser-based notebook interface.        
Type "help()" for help.                                            
└────────────────────────────────────────────────────────────────────┘
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


Thank you for any suggestions!

Ralf Stephan

unread,
Feb 27, 2018, 1:49:50 AM2/27/18
to sage-support
On Tuesday, February 27, 2018 at 1:06:37 AM UTC+1, Pstrang Rzekle wrote:
Is there a way to trick SageMath into presenting numeric approximations with symbolics? In this specific case, handling numbers with units attached.

You will have to use an internal method and convert the number of digits to bits yourself, until that is fixed:

sage: x._convert({'parent':RealField(10)})
(1.0e-6)*centimeter

Regards,

Raghukul Raman

unread,
May 16, 2018, 3:36:42 PM5/16/18
to sage-support
Hi I was working on adding a fix for this but:

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?

Ralf Stephan

unread,
May 19, 2018, 4:32:27 AM5/19/18
to sage-support
There is a function is_unit() in symbolic/units.py, so you just need to walk the expression tree using the ExpressionTreeWalker class in symbolic/expression_conversions.py.  I would first check for symbol with ex.is_symbol() which is very fast. See DefiniteSumExpander for example.

Regards,

Pstrang

unread,
May 19, 2018, 2:33:35 PM5/19/18
to sage-s...@googlegroups.com
I know next to nothing about python. But I see the challenge here.
We'd need to separate the expression into 3 parts, 1) numeric value, 2) symbol (i.e. pi, e) 3) and unit.  The numeric approximation function needs to be applied to the numeric value multiplied by the symbol value (if any) and rounded to specified digits, but the unit needs to remain unchanged and attached.   But I can also see a case where the numeric value needs rounded to digits, but the symbol and unit need to remain unchanged.  This is tricky to accommodate all cases and user requirements.  And there would be restrictions, more than I can think of now.

Sorry I can't be more helpful.  Thank you for your efforts!


On Sat, May 19, 2018 at 2:32 AM, Ralf Stephan <gtr...@gmail.com> wrote:
There is a function is_unit() in symbolic/units.py, so you just need to walk the expression tree using the ExpressionTreeWalker class in symbolic/expression_conversions.py.  I would first check for symbol with ex.is_symbol() which is very fast. See DefiniteSumExpander for example.

Regards,

--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/3eCHzmJ1MXY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-support+unsubscribe@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

slelievre

unread,
May 19, 2018, 5:12:57 PM5/19/18
to sage-support
Sat 2018-05-19 08:32:27 UTC, Ralf Stephan:
Is there a tutorial about walking the expression tree and doing various
operations there? That would be a tutorial I would be happy to study
and to recommend in the many occasions when users ask questions which
would be best solved by an approach of this kind.

Samuel

Ralf Stephan

unread,
May 21, 2018, 1:44:42 AM5/21/18
to sage-support
On Saturday, May 19, 2018 at 11:12:57 PM UTC+2, slelievre wrote:
Is there a tutorial about walking the expression tree and doing various
operations there?

There isn't even a description of the possible parts of an expression in Sage. That's in the GiNaC tutorial but C++ centric.
The ExpressionTreeWalker class was also added only recently. In general,


Regards, 

cs16...@smail.iitm.ac.in

unread,
Jan 17, 2019, 4:43:13 AM1/17/19
to sage-support


Hi,
   I am interested in solving this issue as a beginner start in applying for GSoC 2019.
   Can anyone please suggest a starting point?

Thanks and Regards
Pranav
3rd yr CS undergrad
IIT MADRAS
Reply all
Reply to author
Forward
0 new messages