how to specify sympy.Rational when creating sympy.Matrix from string floating point

47 views
Skip to first unread message

chaowen guo

unread,
Apr 11, 2017, 12:52:53 PM4/11/17
to sympy
Hi:

I want to create a sympy matrix from string floating and I want to get exact values not just floating point, so I have to specify sympy.Rational every time as follows:



I want to ask is there any easier way to specify sympy.Rational only once, maybe something like dtype in numpy? say:
sympy.Matrix([['0.8','0.2'],['0.6','0.4']],dtype=sympy.Rational)
Auto Generated Inline Image 1

Aaron Meurer

unread,
Apr 11, 2017, 1:52:34 PM4/11/17
to sy...@googlegroups.com
You can pass the whole expression as a string to sympify(rational=True), like

sympify('Matrix([[0.8, 0.2], [0.6, 0.4]])', rational=True)

You can also use nsimplify() on the expression after it is created, but this may result in different numbers in some cases because of the inexactness of floating point numbers.

Aaron Meurer


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/d6f51c14-3ce8-4fa5-b021-4c50f29f1d77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages