Diff of /trunk/escript/py_src/util.py

revision 104 by jgs, Fri Dec 17 07:43:12 2004 UTC revision 108 by jgs, Thu Jan 27 06:21:59 2005 UTC
# Line 64  def grad(arg,where=None): Line 64  def grad(arg,where=None):
64      @param where: FunctionSpace in which the gradient will be. If None Function(dom) where dom is the      @param where: FunctionSpace in which the gradient will be. If None Function(dom) where dom is the
65                    domain of the Data object arg.                    domain of the Data object arg.
66      """      """
67      if where==None:      if isinstance(arg,escript.Data):
68         return arg.grad()         if where==None:
70           else:
72      else:      else:
73         return arg.grad(where)         return arg*0.
74
75  def integrate(arg):  def integrate(arg,what=None):
76      """      """
77      @brief return the integral if the function represented by Data object arg over its domain.      @brief return the integral if the function represented by Data object arg over its domain.
78
79      @param arg      @param arg
80      """      """
81      return arg.integrate()      if not what==None:
82           arg2=escript.Data(arg,what)
83        else:
84           arg2=arg
85        if arg2.getRank()==0:
86            return arg2.integrate()[0]
87        else:
88            return arg2.integrate()
89
90  def interpolate(arg,where):  def interpolate(arg,where):
91      """      """
# Line 84  def interpolate(arg,where): Line 94  def interpolate(arg,where):
94      @param arg      @param arg
95      @param where      @param where
96      """      """
97      return arg.interpolate(where)      if isinstance(arg,escript.Data):
98           return arg.interpolate(where)
99        else:
100           return arg
101
102  # functions returning Data objects:  # functions returning Data objects:
103
# Line 95  def transpose(arg,axis=None): Line 108  def transpose(arg,axis=None):
108      @param arg      @param arg
109      """      """
110      if isinstance(arg,escript.Data):      if isinstance(arg,escript.Data):
111           # hack for transpose
112           r=arg.getRank()
113           if r!=2: raise ValueError,"Tranpose only avalaible for rank 2 objects"
114           s=arg.getShape()
115           out=escript.Data(0.,(s[1],s[0]),arg.getFunctionSpace())
116           for i in range(s[0]):
117              for j in range(s[1]):
118                 out[j,i]=arg[i,j]
119           return out
120           # end hack for transpose
121         if axis==None: axis=arg.getRank()/2         if axis==None: axis=arg.getRank()/2
122         return arg.transpose(axis)         return arg.transpose(axis)
123      else:      else:
# Line 108  def trace(arg): Line 131  def trace(arg):
131      @param arg      @param arg
132      """      """
133      if isinstance(arg,escript.Data):      if isinstance(arg,escript.Data):
134           # hack for trace
135           r=arg.getRank()
136           if r!=2: raise ValueError,"trace only avalaible for rank 2 objects"
137           s=arg.getShape()
138           out=escript.Scalar(0,arg.getFunctionSpace())
139           for i in range(min(s)):
140                 out+=arg[i,i]
141           return out
142           # end hack for trace
143         return arg.trace()         return arg.trace()
144      else:      else:
145         return numarray.trace(arg)         return numarray.trace(arg)
# Line 175  def maxval(arg): Line 207  def maxval(arg):
207      """      """
208      if isinstance(arg,escript.Data):      if isinstance(arg,escript.Data):
209         return arg.maxval()         return arg.maxval()
210        elif isinstance(arg,float) or isinstance(arg,int):
211           return arg
212      else:      else:
213         return arg.max()         return arg.max()
214
# Line 186  def minval(arg): Line 220  def minval(arg):
220      """      """
221      if isinstance(arg,escript.Data):      if isinstance(arg,escript.Data):
222         return arg.minval()         return arg.minval()
223        elif isinstance(arg,float) or isinstance(arg,int):
224           return arg
225      else:      else:
226         return arg.max()         return arg.min()
227
228  def length(arg):  def length(arg):
229      """      """
# Line 196  def length(arg): Line 232  def length(arg):
232      @param arg      @param arg
233      """      """
234      if isinstance(arg,escript.Data):      if isinstance(arg,escript.Data):
235         if arg.getRank()==1:         if arg.isEmpty(): return escript.Data()
236           if arg.getRank()==0:
237              return abs(arg)
238           elif arg.getRank()==1:
239            sum=escript.Scalar(0,arg.getFunctionSpace())            sum=escript.Scalar(0,arg.getFunctionSpace())
240            for i in range(arg.getShape()[0]):            for i in range(arg.getShape()[0]):
241               sum+=arg[i]**2               sum+=arg[i]**2
242            return sqrt(sum)            return sqrt(sum)
243           elif arg.getRank()==2:
244              sum=escript.Scalar(0,arg.getFunctionSpace())
245              for i in range(arg.getShape()[0]):
246                 for j in range(arg.getShape()[1]):
247                    sum+=arg[i,j]**2
248              return sqrt(sum)
249           elif arg.getRank()==3:
250              sum=escript.Scalar(0,arg.getFunctionSpace())
251              for i in range(arg.getShape()[0]):
252                 for j in range(arg.getShape()[1]):
253                    for k in range(arg.getShape()[2]):
254                       sum+=arg[i,j,k]**2
255              return sqrt(sum)
256           elif arg.getRank()==4:
257              sum=escript.Scalar(0,arg.getFunctionSpace())
258              for i in range(arg.getShape()[0]):
259                 for j in range(arg.getShape()[1]):
260                    for k in range(arg.getShape()[2]):
261                       for l in range(arg.getShape()[3]):
262                          sum+=arg[i,j,k,l]**2
263              return sqrt(sum)
264         else:         else:
265            raise SystemError,"length is not been implemented yet"            raise SystemError,"length is not been implemented yet"
266         # return arg.length()         # return arg.length()
# Line 236  def sup(arg): Line 296  def sup(arg):
296      """      """
297      if isinstance(arg,escript.Data):      if isinstance(arg,escript.Data):
298         return arg.sup()         return arg.sup()
299        elif isinstance(arg,float) or isinstance(arg,int):
300           return arg
301      else:      else:
302         return arg.max()         return arg.max()
303
# Line 247  def inf(arg): Line 309  def inf(arg):
309      """      """
310      if isinstance(arg,escript.Data):      if isinstance(arg,escript.Data):
311         return arg.inf()         return arg.inf()
312        elif isinstance(arg,float) or isinstance(arg,int):
313           return arg
314      else:      else:
315         return arg.min()         return arg.min()
316
# Line 256  def L2(arg): Line 320  def L2(arg):
320
321      @param arg      @param arg
322      """      """
323      return arg.L2()      if isinstance(arg,escript.Data):
324           return arg.L2()
325        elif isinstance(arg,float) or isinstance(arg,int):
326           return abs(arg)
327        else:
328           return numarry.sqrt(dot(arg,arg))
329
330  def Lsup(arg):  def Lsup(arg):
331      """      """
# Line 266  def Lsup(arg): Line 335  def Lsup(arg):
335      """      """
336      if isinstance(arg,escript.Data):      if isinstance(arg,escript.Data):
337         return arg.Lsup()         return arg.Lsup()
338        elif isinstance(arg,float) or isinstance(arg,int):
339           return abs(arg)
340      else:      else:
341         return arg.max(numarray.abs(arg))         return max(numarray.abs(arg))
342
343  def dot(arg1,arg2):  def dot(arg1,arg2):
344      """      """
# Line 281  def dot(arg1,arg2): Line 352  def dot(arg1,arg2):
352         return arg2.dot(arg1)         return arg2.dot(arg1)
353      else:      else:
354         return numarray.dot(arg1,arg2)         return numarray.dot(arg1,arg2)
#
# \$Log\$
# Revision 1.6  2004/12/17 07:43:10  jgs
# *** empty log message ***
#
# Revision 1.2.2.5  2004/12/17 00:06:53  gross
# mk sets ESYS_ROOT is undefined
#
# Revision 1.2.2.4  2004/12/07 03:19:51  gross
# options for GMRES and PRES20 added
#
# Revision 1.2.2.3  2004/12/06 04:55:18  gross
# function wraper extended
#
# Revision 1.2.2.2  2004/11/22 05:44:07  gross
# a few more unitary functions have been added but not implemented in Data yet
#
# Revision 1.2.2.1  2004/11/12 06:58:15  gross
# a lot of changes to get the linearPDE class running: most important change is that there is no matrix format exposed to the user anymore. the format is chosen by the Domain according to the solver and symmetry
#
# Revision 1.2  2004/10/27 00:23:36  jgs
# fixed minor syntax error
#
# Revision 1.1.1.1  2004/10/26 06:53:56  jgs
# initial import of project esys2
#
# Revision 1.1.2.3  2004/10/26 06:43:48  jgs
# committing Lutz's and Paul's changes to brach jgs
#
# Revision 1.1.4.1  2004/10/20 05:32:51  cochrane
# Added incomplete Doxygen comments to files, or merely put the docstrings that already exist into Doxygen form.
#
# Revision 1.1  2004/08/05 03:58:27  gross
# Bug in Assemble_NodeCoordinates fixed
#
#

Legend:
 Removed from v.104 changed lines Added in v.108