1 |
|
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 |
import sympy |
27 |
from sympy.functions import * |
28 |
from sympy import S |
29 |
|
30 |
class wherePositive(sympy.Function): |
31 |
"""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 |
class whereNonPositive(sympy.Function): |
57 |
"""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 |
class whereNegative(sympy.Function): |
77 |
"""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 |
class whereNonNegative(sympy.Function): |
102 |
"""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 |
class whereZero(sympy.Function): |
122 |
"""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 |
class whereNonZero(sympy.Function): |
147 |
"""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 |
class log10(sympy.Function): |
167 |
"""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 |
class inverse(sympy.Function): |
177 |
"""Returns the inverse of the argument |
178 |
""" |
179 |
nargs = 1 |
180 |
|
181 |
@classmethod |
182 |
def eval(cls, arg): |
183 |
if arg.is_Number: |
184 |
return 1./arg |
185 |
|
186 |
class minval(sympy.Function): |
187 |
"""Returns the minimum value over all components of the argument |
188 |
""" |
189 |
nargs = 1 |
190 |
|
191 |
@classmethod |
192 |
def eval(cls, arg): |
193 |
if arg.is_Number: |
194 |
return arg |
195 |
|
196 |
class maxval(sympy.Function): |
197 |
"""Returns the maximum value over all components of the argument |
198 |
""" |
199 |
nargs = 1 |
200 |
|
201 |
@classmethod |
202 |
def eval(cls, arg): |
203 |
if arg.is_Number: |
204 |
return arg |
205 |
|
206 |
class maximum(sympy.Function): |
207 |
"""Returns the maximum over the arguments |
208 |
""" |
209 |
pass |
210 |
|
211 |
class minimum(sympy.Function): |
212 |
"""Returns the minimum over the arguments |
213 |
""" |
214 |
pass |
215 |
|
216 |
class integrate(sympy.Function): |
217 |
"""Returns the integral of the argument |
218 |
""" |
219 |
nargs = (1,2) |
220 |
|
221 |
class interpolate(sympy.Function): |
222 |
"""Returns the argument interpolated on the function space provided |
223 |
""" |
224 |
nargs = 2 |
225 |
|
226 |
class L2(sympy.Function): |
227 |
"""Returns the L2 norm of the argument |
228 |
""" |
229 |
nargs = 1 |
230 |
|
231 |
class clip(sympy.Function): |
232 |
"""Returns the argument clipped to a minimum and maximum value |
233 |
""" |
234 |
nargs = (1,2,3) |
235 |
|
236 |
class trace(sympy.Function): |
237 |
"""Returns the trace of the argument with optional axis_offset |
238 |
""" |
239 |
nargs = (1,2) |
240 |
|
241 |
class transpose(sympy.Function): |
242 |
"""Returns the transpose of the argument |
243 |
""" |
244 |
nargs = (1,2) |
245 |
|
246 |
class symmetric(sympy.Function): |
247 |
"""Returns the symmetric part of the argument |
248 |
""" |
249 |
nargs = 1 |
250 |
|
251 |
@classmethod |
252 |
def eval(cls, arg): |
253 |
if arg.is_Function: |
254 |
if arg.func is symmetric: return arg |
255 |
if arg.func is nonsymmetric: return S.Zero |
256 |
elif arg.is_Number: |
257 |
return arg |
258 |
|
259 |
class nonsymmetric(sympy.Function): |
260 |
"""Returns the non-symmetric part of the argument |
261 |
""" |
262 |
nargs = 1 |
263 |
|
264 |
@classmethod |
265 |
def eval(cls, arg): |
266 |
if arg.is_Function: |
267 |
if arg.func is nonsymmetric: return arg |
268 |
if arg.func is symmetric: return S.Zero |
269 |
elif arg.is_Number: |
270 |
return arg |
271 |
|
272 |
class swap_axes(sympy.Function): |
273 |
"""Returns the 'swap' of the argument |
274 |
""" |
275 |
nargs = (1,2,3) |
276 |
|
277 |
class grad(sympy.Function): |
278 |
"""Returns the spatial gradient of the argument |
279 |
""" |
280 |
nargs = (1,2) |
281 |
|
282 |
class inner(sympy.Function): |
283 |
"""Returns the inner product of the arguments |
284 |
""" |
285 |
nargs = 2 |
286 |
|
287 |
class outer(sympy.Function): |
288 |
"""Returns the outer product of the arguments |
289 |
""" |
290 |
nargs = 2 |
291 |
|
292 |
class eigenvalues(sympy.Function): |
293 |
"""Returns the Eigenvalues of the argument |
294 |
""" |
295 |
nargs = 1 |
296 |
|
297 |
class eigenvalues_and_eigenvectors(sympy.Function): |
298 |
"""Returns the Eigenvalues and Eigenvectors of the argument |
299 |
""" |
300 |
nargs = 1 |
301 |
|
302 |
# |
303 |
# vim: expandtab shiftwidth=4: |