/[escript]/branches/symbolic_from_3470/escript/py_src/symbolic/functions.py
ViewVC logotype

Annotation of /branches/symbolic_from_3470/escript/py_src/symbolic/functions.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3512 - (hide annotations)
Wed May 18 06:22:46 2011 UTC (8 years ago) by caltinay
File MIME type: text/x-python
File size: 6709 byte(s)
Implementation of symbolic grad() and a few fixes.

1 caltinay 3473
2     ########################################################
3     #
4     # Copyright (c) 2003-2010 by University of Queensland
5     # Earth Systems Science Computational Center (ESSCC)
6     # http://www.uq.edu.au/esscc
7     #
8     # Primary Business: Queensland, Australia
9     # Licensed under the Open Software License version 3.0
10     # http://www.opensource.org/licenses/osl-3.0.php
11     #
12     ########################################################
13    
14     __copyright__="""Copyright (c) 2003-2010 by University of Queensland
15     Earth Systems Science Computational Center (ESSCC)
16     http://www.uq.edu.au/esscc
17     Primary Business: Queensland, Australia"""
18     __license__="""Licensed under the Open Software License version 3.0
19     http://www.opensource.org/licenses/osl-3.0.php"""
20     __url__="https://launchpad.net/escript-finley"
21    
22     """
23     extra functions that can be used in symbolic expressions
24     """
25    
26 caltinay 3493 import sympy
27     from sympy.functions import *
28 caltinay 3473 from sympy import S
29    
30 caltinay 3493 class wherePositive(sympy.Function):
31 caltinay 3473 """Returns:
32     1 where expr > 0
33     0 else
34     """
35     nargs = 1
36     is_bounded = True
37     is_negative = False
38     is_real = True
39    
40     @classmethod
41     def eval(cls, arg):
42     if arg is S.NaN:
43     return S.NaN
44     if arg.is_positive: return S.One
45     if arg.is_negative or arg is S.Zero: return S.Zero
46     if arg.is_Function:
47     if arg.func is wherePositive: return arg
48     if arg.func is whereNegative: return arg
49    
50     def _eval_conjugate(self):
51     return self
52    
53     def _eval_derivative(self, x):
54     return S.Zero
55    
56 caltinay 3493 class whereNonPositive(sympy.Function):
57 caltinay 3473 """Returns:
58     0 where expr > 0
59     1 else
60     """
61     nargs = 1
62     is_bounded = True
63     is_negative = False
64     is_real = True
65    
66     @classmethod
67     def eval(cls, arg):
68     return 1-wherePositive(arg)
69    
70     def _eval_conjugate(self):
71     return self
72    
73     def _eval_derivative(self, x):
74     return S.Zero
75    
76 caltinay 3493 class whereNegative(sympy.Function):
77 caltinay 3473 """Returns:
78     1 where expr < 0
79     0 else
80     """
81     nargs = 1
82     is_bounded = True
83     is_negative = False
84     is_real = True
85    
86     @classmethod
87     def eval(cls, arg):
88     if arg is S.NaN:
89     return S.NaN
90     if arg.is_nonnegative: return S.Zero
91     if arg.is_negative: return S.One
92     if arg.is_Function:
93     if arg.func is wherePositive: return S.Zero
94    
95     def _eval_conjugate(self):
96     return self
97    
98     def _eval_derivative(self, x):
99     return S.Zero
100    
101 caltinay 3493 class whereNonNegative(sympy.Function):
102 caltinay 3473 """Returns:
103     0 where expr < 0
104     1 else
105     """
106     nargs = 1
107     is_bounded = True
108     is_negative = False
109     is_real = True
110    
111     @classmethod
112     def eval(cls, arg):
113     return 1-whereNegative(arg)
114    
115     def _eval_conjugate(self):
116     return self
117    
118     def _eval_derivative(self, x):
119     return S.Zero
120    
121 caltinay 3493 class whereZero(sympy.Function):
122 caltinay 3474 """Returns:
123     1 where expr == 0
124     0 else
125     """
126     nargs = 1
127     is_bounded = True
128     is_negative = False
129     is_real = True
130    
131     @classmethod
132     def eval(cls, arg):
133     if arg is S.NaN:
134     return S.NaN
135     if arg.is_zero: return S.One
136     if arg.is_nonzero: return S.Zero
137     if arg.is_Function:
138     if arg.func is whereZero: return 1-arg
139    
140     def _eval_conjugate(self):
141     return self
142    
143     def _eval_derivative(self, x):
144     return S.Zero
145    
146 caltinay 3493 class whereNonZero(sympy.Function):
147 caltinay 3474 """Returns:
148     0 where expr == 0
149     1 else
150     """
151     nargs = 1
152     is_bounded = True
153     is_negative = False
154     is_real = True
155    
156     @classmethod
157     def eval(cls, arg):
158     return 1-whereZero(arg)
159    
160     def _eval_conjugate(self):
161     return self
162    
163     def _eval_derivative(self, x):
164     return S.Zero
165    
166 caltinay 3493 class log10(sympy.Function):
167 caltinay 3474 """Returns the base-10 logarithm of the argument (same as log(x,10))
168     """
169     nargs = 1
170    
171     @classmethod
172     def eval(cls, arg):
173     from sympy.functions.elementary.exponential import log
174     return log(arg,10)
175    
176 caltinay 3493 class clip(sympy.Function):
177     """Returns the argument clipped to a minimum and maximum value
178     """
179     nargs = (1,2,3)
180    
181     class trace(sympy.Function):
182 caltinay 3474 """Returns the trace of the argument with optional axis_offset
183     """
184     nargs = (1,2)
185    
186 caltinay 3493 class transpose(sympy.Function):
187 caltinay 3474 """Returns the transpose of the argument
188     """
189     nargs = (1,2)
190    
191 caltinay 3493 class symmetric(sympy.Function):
192 caltinay 3474 """Returns the symmetric part of the argument
193     """
194     nargs = 1
195    
196 caltinay 3488 @classmethod
197     def eval(cls, arg):
198 caltinay 3507 if isinstance(arg,sympy.Basic):
199     if arg.is_Function:
200     if arg.func is symmetric: return arg
201     if arg.func is nonsymmetric: return S.Zero
202     elif arg.is_Number:
203     return arg
204 caltinay 3488
205 caltinay 3493 class nonsymmetric(sympy.Function):
206 caltinay 3474 """Returns the non-symmetric part of the argument
207     """
208     nargs = 1
209    
210 caltinay 3488 @classmethod
211     def eval(cls, arg):
212 caltinay 3507 if isinstance(arg,sympy.Basic):
213     if arg.is_Function:
214     if arg.func is nonsymmetric: return arg
215     if arg.func is symmetric: return S.Zero
216     elif arg.is_Number:
217     return arg
218 caltinay 3488
219 caltinay 3493 class swap_axes(sympy.Function):
220 caltinay 3474 """Returns the 'swap' of the argument
221     """
222     nargs = (1,2,3)
223    
224 caltinay 3493 class inner(sympy.Function):
225     """Returns the inner product of the arguments
226     """
227     nargs = 2
228    
229     class outer(sympy.Function):
230     """Returns the outer product of the arguments
231     """
232     nargs = 2
233    
234 caltinay 3512 class grad_n(sympy.Function):
235     """Returns the spatial gradient of the argument
236     """
237     nargs = (2,3)
238    
239     @classmethod
240     def eval(cls, *args):
241     if args[0].is_zero: return S.Zero
242    
243 caltinay 3496 class eigenvalues(sympy.Function):
244     """Returns the Eigenvalues of the argument
245     """
246 caltinay 3512 pass
247 caltinay 3496
248     class eigenvalues_and_eigenvectors(sympy.Function):
249     """Returns the Eigenvalues and Eigenvectors of the argument
250     """
251 caltinay 3512 pass
252 caltinay 3496
253 caltinay 3512 class inverse(sympy.Function):
254     """Returns the inverse of the argument
255     """
256     pass
257    
258     class minval(sympy.Function):
259     """Returns the minimum value over all components of the argument
260     """
261     pass
262    
263     class maxval(sympy.Function):
264     """Returns the maximum value over all components of the argument
265     """
266     pass
267    
268     class maximum(sympy.Function):
269     """Returns the maximum over the arguments
270     """
271     pass
272    
273     class minimum(sympy.Function):
274     """Returns the minimum over the arguments
275     """
276     pass
277    
278     class integrate(sympy.Function):
279     """Returns the integral of the argument
280     """
281     pass
282    
283     class interpolate(sympy.Function):
284     """Returns the argument interpolated on the function space provided
285     """
286     pass
287    
288     class L2(sympy.Function):
289     """Returns the L2 norm of the argument
290     """
291     pass
292    
293 caltinay 3473 #
294     # vim: expandtab shiftwidth=4:

  ViewVC Help
Powered by ViewVC 1.1.26