whitespace fix
[sympyx.git] / sympy.py
blob6c8c5c8bd9e4abd04cf10823e0d96f3a98adcb83
1 import sympy_py
2 _mode = 'py'
4 import os
5 if os.getenv('SYMPY_PY', '').lower() in ['y', 'yes']:
6 print 'I: forced to be pure python'
8 else:
9 try:
10 # XXX better not * ?
11 print 'I: import sympy_pyx ...',
12 from sympy_pyx import *
14 # XXX figure out how to put this into sympy_pyx.pyx:
15 BASIC = 0
16 SYMBOL = 1
17 ADD = 2
18 MUL = 3
19 POW = 4
20 INTEGER = 5
22 from sympy_pyx import _Basic
24 # Cython/Pyrex do not support __new__, so we have to define Basic here
25 class Basic(_Basic):
27 def __new__(cls, args):
28 o = _Basic.__new__(cls)
29 o._set_rawargs(tuple(args))
30 return o
33 except ImportError, e:
34 print 'fail (%s)' % e
35 print 'W: can\'t import sympy_pyx -- will be pure python'
37 else:
38 print 'ok'
39 _mode = 'pyx'
42 if _mode == 'pyx':
43 from sympy_pyx import *
44 else:
45 from sympy_py import *
47 def var(s):
48 """
49 Create a symbolic variable with the name *s*.
51 INPUT:
52 s -- a string, either a single variable name, or
53 a space separated list of variable names, or
54 a list of variable names.
56 NOTE: The new variable is both returned and automatically injected into
57 the parent's *global* namespace. It's recommended not to use "var" in
58 library code, it is better to use symbols() instead.
60 EXAMPLES:
61 We define some symbolic variables:
62 >>> var('m')
64 >>> var('n xx yy zz')
65 (n, xx, yy, zz)
66 >>> n
69 """
70 import re
71 import inspect
72 frame = inspect.currentframe().f_back
74 try:
75 if not isinstance(s, list):
76 s = re.split('\s|,', s)
78 res = []
80 for t in s:
81 # skip empty strings
82 if not t:
83 continue
84 sym = Symbol(t)
85 frame.f_globals[t] = sym
86 res.append(sym)
88 res = tuple(res)
89 if len(res) == 0: # var('')
90 res = None
91 elif len(res) == 1: # var('x')
92 res = res[0]
93 # otherwise var('a b ...')
94 return res
96 finally:
97 # we should explicitly break cyclic dependencies as stated in inspect
98 # doc
99 del frame
103 # for debugging
104 x = Symbol('x')
105 y = Symbol('y')
106 z = Symbol('z')
107 i = Integer(2)
109 x2= Symbol('x')
112 e = (x+y)**2
115 class sin(Basic):
117 def __new__(cls, arg):
118 if arg == 0:
119 return Integer(0)
120 else:
121 obj = Basic.__new__(cls, (arg,))
122 return obj
124 def __repr__(self):
125 return "sin(%s)" % self.args[0]