/[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 3518 - (hide annotations)
Fri May 20 06:29:31 2011 UTC (8 years ago) by caltinay
File MIME type: text/x-python
File size: 5292 byte(s)
Some cleanup and added tests for symbolic div and grad.

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 caltinay 3512 class grad_n(sympy.Function):
182     """Returns the spatial gradient of the argument
183     """
184     nargs = (2,3)
185    
186     @classmethod
187     def eval(cls, *args):
188     if args[0].is_zero: return S.Zero
189    
190 caltinay 3496 class eigenvalues(sympy.Function):
191     """Returns the Eigenvalues of the argument
192     """
193 caltinay 3512 pass
194 caltinay 3496
195     class eigenvalues_and_eigenvectors(sympy.Function):
196     """Returns the Eigenvalues and Eigenvectors of the argument
197     """
198 caltinay 3512 pass
199 caltinay 3496
200 caltinay 3512 class minval(sympy.Function):
201     """Returns the minimum value over all components of the argument
202     """
203     pass
204    
205     class maxval(sympy.Function):
206     """Returns the maximum value over all components of the argument
207     """
208     pass
209    
210     class maximum(sympy.Function):
211     """Returns the maximum over the arguments
212     """
213     pass
214    
215     class minimum(sympy.Function):
216     """Returns the minimum over the arguments
217     """
218     pass
219    
220     class integrate(sympy.Function):
221     """Returns the integral of the argument
222     """
223     pass
224    
225     class interpolate(sympy.Function):
226     """Returns the argument interpolated on the function space provided
227     """
228     pass
229    
230     class L2(sympy.Function):
231     """Returns the L2 norm of the argument
232     """
233     pass
234    
235 caltinay 3473 #
236     # vim: expandtab shiftwidth=4:

  ViewVC Help
Powered by ViewVC 1.1.26