1+ # coding:utf-8
2+
3+ from __future__ import division
4+ from sympy import *
5+ x , y , z , t = symbols ('x y z t' )
6+ k , m , n = symbols ('k m n' , integer = True )
7+ f , g , h = symbols ('f g h' , cls = Function )
8+ init_printing ()
9+
10+ """
11+ Sympy中的数学表达式
12+ """
13+
14+ def math_expression ():
15+ var = ("x0,y0,x1,y1" )
16+ return var
17+
18+ def _is_attr ():
19+ x = symbols ('x' , positive = True )
20+ print (x .is_Symbol )
21+ print (x .is_positive )
22+ print (x .is_imaginary )
23+ print (x .is_complex )
24+ # 使用x.assumptions0来查看所有的假设条件
25+ print (x .assumptions0 )
26+ # 在Sympy中,所有的对象都是从Basic中继承
27+
28+ def Sympy_data_obj ():
29+ # Sympy中的数值对象,S对象
30+ # res为Rational对象
31+ res = (S (1 )/ 2 + S (2 )/ 3 )
32+ return res
33+
34+ # 查看数字的有效位数下的精度
35+ def Precise_data ():
36+ print (N (0.1 , 60 ))
37+ # print(N(Real('0.1',60),60))
38+
39+ # Print_expression
40+ # 一个递归操作
41+ def print_expression (e , level = 0 ):
42+ spaces = " " * level
43+ # 如果e是Symbol或者Number中一个直接输出 return
44+ if isinstance (e , (Symbol ,)):
45+ print spaces + str (e )
46+ return
47+ '''
48+ DIY by Minux~
49+ if isinstance(e, (Number,)):
50+ print spaces + " " + str(e)
51+ return
52+ '''
53+ if len (e .args ) > 0 :
54+ print spaces + e .func .__name__
55+ for arg in e .args :
56+ print_expression (arg , level + 1 )
57+ else :
58+ print spaces + e .func .__name__
59+
60+
61+ if __name__ == '__main__' :
62+ print (math_expression ())
63+ _is_attr ()
64+ Sympy_data_obj ()
65+ Precise_data ()
66+ print_expression (sqrt (x ** 2 + y ** y ))
67+ print_expression (x + 3 )
68+ print_expression (x + y )
0 commit comments