Numbers
In tasympy, Nunber objects are wrapped around sympy numbers. There are two recomended ways to create tasympy Number objects
Setup and Accuracy
Before importing tasympy and setting up settings, decide how many digit accuracy to use for float numbers. The default number of digits is 15, the same as for sympy. For an accuracy of 15 digits nothing special needs done. For an accuracy of 20 digits, the first two steps must be:
In [1]: from truealgebra.tasympy.utility import snum
In [2]: snum.accuracy = 20
It is recomended the above two lines be at the very beginning of a truealgebra
session, and the snum.accuracy not be changed again. The reason is that a
difference in accuracy can affect the equality of two sympy.Float numbers.
In [3]: import sympy
In [4]: sympy.Float('1.0', 20) == sympy.Float('1.0', 15)
Out[4]: False
In [5]: Number(sympy.Float('1.0', 20)) == Number(sympy.Float('1.0', 15))
Out[5]: False
As shown above, the equality of truealgebra Number objects can be affected
by changes in accuracy. Truealgebra often uses truealggebra expressions
in dictionaries, for
example the NaturalRule. So if the accuracy of float numbers is varied, there
is a chamce of incorrect or unexpected results. Therefore in
a session with truealgebra set snum.accuracy at the very beginning
if it is to be changed and
leave it unchanged throughout the rest of the session.
Next import, set up the tasympy cas, and create a frontend.
In [6]: import truealgebra.tasympy as cas
In [7]: cas.setup_func()
In [8]: fe = cas.create_frontend()
In [9]: import sympy
Parsing Numbers
Parsing is the easiest and recommended way to create Number objects for common and normal needs. Specialized error messages will be diplayed for many errors.
In [10]: fe(' 7.54; 4.21e15 ')
Ex(0): 7.5400000000000000000; 4210000000000000.0000
Rational numbers which includes Integers and fractions can be parsed.
In [11]: fe(' 3; 415/600; 23/1 ')
Ex(1): 3; 83/120; 23
the square root of negative one represented by I can be parsed.
The rule cas.evalnumbu will combine I with other
numbers to represent more complex numbers.
In [12]: fe(' I; 6 * I; 4 + 6 * I ', cas.evalnumbu)
Ex(2): I; 6*I; 4 + 6*I
Special sympy symbols can be parsed:
In [13]: fe(' pi; E; EulerGamma; oo ')
Ex(3): pi; E; EulerGamma; oo
Manual Number Creation
Truealgebra Number objects containing symipy objects can be created manally using methods of the cas.snum object. This is useful for the purposes of testing, exprimenting, or writing new rules.
The cas.snum.float method is recomended for floats with a string argument.
In [14]: float0 = cas.snum.float('2.3005005005')
In [15]: type(float0)
Out[15]: truealgebra.core.expressions.Number
In [16]: type(float0.value)
Out[16]: sympy.core.numbers.Float
In [17]: cas.snum.float(3.3005005005)
Out[17]: 3.3005005005000001006
The first argument of the cas.snum.float method can be a python float. But As explained in this sympy document some python floats are only accurrate to about 15 digits as inputs.
For integers, the first argument of the cas.snum.integer method can be a string or a python integer.
In [18]: int0 = cas.snum.integer('237')
In [19]: type(int0)
Out[19]: truealgebra.core.expressions.Number
In [20]: type(int0.value)
Out[20]: sympy.core.numbers.Integer
For fractions or rational numbers
In [21]: rat0 = cas.snum.rational('7', '9')
In [22]: type(rat0)
Out[22]: truealgebra.core.expressions.Number
In [23]: type(rat0.value)
Out[23]: sympy.core.numbers.Rational
In [24]: aa = cas.parse(' f(345600000.234, 0.000349, 12340.4) ')
In [25]: cas.floatunparse4(aa)
Out[25]: 'f(3.456e+8, 0.0003490, 1.234e+4)'
In [26]: apn = cas.ApplyN(digits=4)
In [27]: apn(aa)
Out[27]: f(3.456e+8, 0.0003490, 1.234e+4)
Reset snum.accuracy
Setting snum.accuracy to None returns the sympy default accuracy
of 15 digits.
In [28]: snum.accuracy = None
This is done here because there are other restructured text files in this document that use the default accuracy.