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

revision 804 by gross, Thu Aug 10 01:12:16 2006 UTC revision 881 by gross, Thu Oct 26 02:54:47 2006 UTC
# Line 26  import math Line 26  import math
26  import numarray  import numarray
27  import escript  import escript
28  import os  import os
29    from esys.escript import C_GeneralTensorProduct
30
31  #=========================================================  #=========================================================
32  #   some helpers:  #   some helpers:
# Line 1310  def whereNonZero(arg,tol=0.): Line 1311  def whereNonZero(arg,tol=0.):
1311     else:     else:
1312        raise TypeError,"whereNonZero: Unknown argument type."        raise TypeError,"whereNonZero: Unknown argument type."
1313
1314    def erf(arg):
1315       """
1316       returns erf of argument arg
1317
1318       @param arg: argument
1319       @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1320       @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1321       @raises TypeError: if the type of the argument is not expected.
1322       """
1323       if isinstance(arg,escript.Data):
1324          return arg._erf()
1325       else:
1326          raise TypeError,"erf: Unknown argument type."
1327
1328  def sin(arg):  def sin(arg):
1329     """     """
1330     returns sine of argument arg     returns sine of argument arg
# Line 4051  def minimum(*args): Line 4066  def minimum(*args):
4067      return out      return out
4068
4069  def clip(arg,minval=0.,maxval=1.):  def clip(arg,minval=None,maxval=None):
4070      """      """
4071      cuts the values of arg between minval and maxval      cuts the values of arg between minval and maxval
4072
4073      @param arg: argument      @param arg: argument
4074      @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float}      @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float}
4075      @param minval: lower range      @param minval: lower range. If None no lower range is applied
4076      @type minval: C{float}      @type minval: C{float} or C{None}
4077      @param maxval: upper range      @param maxval: upper range. If None no upper range is applied
4078      @type maxval: C{float}      @type maxval: C{float} or C{None}
4079      @return: is on object with all its value between minval and maxval. value of the argument that greater then minval and      @return: is on object with all its value between minval and maxval. value of the argument that greater then minval and
4080               less then maxval are unchanged.               less then maxval are unchanged.
4081      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} depending on the input      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} depending on the input
4082      @raise ValueError: if minval>maxval      @raise ValueError: if minval>maxval
4083      """      """
4084      if minval>maxval:      if not minval==None and not maxval==None:
4085         raise ValueError,"minval = %s must be less then maxval %s"%(minval,maxval)         if minval>maxval:
4086      return minimum(maximum(minval,arg),maxval)            raise ValueError,"minval = %s must be less then maxval %s"%(minval,maxval)
4087        if minval == None:
4088            tmp=arg
4089        else:
4090            tmp=maximum(minval,arg)
4091        if maxval == None:
4092            return tmp
4093        else:
4094            return minimum(tmp,maxval)
4095
4096
4097  def inner(arg0,arg1):  def inner(arg0,arg1):
# Line 4313  class GeneralTensorProduct_Symbol(Depend Line 4336  class GeneralTensorProduct_Symbol(Depend
4336           args=self.getSubstitutedArguments(argvals)           args=self.getSubstitutedArguments(argvals)
4337           return generalTensorProduct(args[0],args[1],args[2])           return generalTensorProduct(args[0],args[1],args[2])
4338
4339  def escript_generalTensorProductNew(arg0,arg1,axis_offset):  def escript_generalTensorProduct(arg0,arg1,axis_offset,transpose=0):
4340      "arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!"      "arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!"
4341      # calculate the return shape:      return C_GeneralTensorProduct(arg0, arg1, axis_offset, transpose)
shape0=arg0.getShape()[:arg0.getRank()-axis_offset]
shape01=arg0.getShape()[arg0.getRank()-axis_offset:]
shape10=arg1.getShape()[:axis_offset]
shape1=arg1.getShape()[axis_offset:]
if not shape01==shape10:
raise ValueError,"dimensions of last %s components in left argument don't match the first %s components in the right argument."%(axis_offset,axis_offset)
# Figure out which functionspace to use (look at where operator+ is defined maybe in BinaryOp.h to get the logic for this)
# fs=(escript.Scalar(0.,arg0.getFunctionSpace())+escript.Scalar(0.,arg1.getFunctionSpace())).getFunctionSpace()
out=GeneralTensorProduct(arg0, arg1, axis_offset)
return out

def escript_generalTensorProduct(arg0,arg1,axis_offset): # this should be escript._generalTensorProduct
"arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!"
# calculate the return shape:
shape0=arg0.getShape()[:arg0.getRank()-axis_offset]
shape01=arg0.getShape()[arg0.getRank()-axis_offset:]
shape10=arg1.getShape()[:axis_offset]
shape1=arg1.getShape()[axis_offset:]
if not shape01==shape10:
raise ValueError,"dimensions of last %s components in left argument don't match the first %s components in the right argument."%(axis_offset,axis_offset)

# whatr function space should be used? (this here is not good!)
fs=(escript.Scalar(0.,arg0.getFunctionSpace())+escript.Scalar(0.,arg1.getFunctionSpace())).getFunctionSpace()
# create return value:
out=escript.Data(0.,tuple(shape0+shape1),fs)
#
s0=[[]]
for k in shape0:
s=[]
for j in s0:
for i in range(k): s.append(j+[slice(i,i)])
s0=s
s1=[[]]
for k in shape1:
s=[]
for j in s1:
for i in range(k): s.append(j+[slice(i,i)])
s1=s
s01=[[]]
for k in shape01:
s=[]
for j in s01:
for i in range(k): s.append(j+[slice(i,i)])
s01=s

for i0 in s0:
for i1 in s1:
s=escript.Scalar(0.,fs)
for i01 in s01:
s+=arg0.__getitem__(tuple(i0+i01))*arg1.__getitem__(tuple(i01+i1))
out.__setitem__(tuple(i0+i1),s)
return out

4342
4343  def transposed_matrix_mult(arg0,arg1):  def transposed_matrix_mult(arg0,arg1):
4344      """      """
# Line 4565  class GeneralTransposedTensorProduct_Sym Line 4535  class GeneralTransposedTensorProduct_Sym
4535
4536  def escript_generalTransposedTensorProduct(arg0,arg1,axis_offset): # this should be escript._generalTransposedTensorProduct  def escript_generalTransposedTensorProduct(arg0,arg1,axis_offset): # this should be escript._generalTransposedTensorProduct
4537      "arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!"      "arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!"
4538      # calculate the return shape:      return C_GeneralTensorProduct(arg0, arg1, axis_offset, 1)
shape01=arg0.getShape()[:axis_offset]
shape10=arg1.getShape()[:axis_offset]
shape0=arg0.getShape()[axis_offset:]
shape1=arg1.getShape()[axis_offset:]
if not shape01==shape10:
raise ValueError,"dimensions of first %s components in left argument don't match the first %s components in the right argument."%(axis_offset,axis_offset)

# whatr function space should be used? (this here is not good!)
fs=(escript.Scalar(0.,arg0.getFunctionSpace())+escript.Scalar(0.,arg1.getFunctionSpace())).getFunctionSpace()
# create return value:
out=escript.Data(0.,tuple(shape0+shape1),fs)
#
s0=[[]]
for k in shape0:
s=[]
for j in s0:
for i in range(k): s.append(j+[slice(i,i)])
s0=s
s1=[[]]
for k in shape1:
s=[]
for j in s1:
for i in range(k): s.append(j+[slice(i,i)])
s1=s
s01=[[]]
for k in shape01:
s=[]
for j in s01:
for i in range(k): s.append(j+[slice(i,i)])
s01=s

for i0 in s0:
for i1 in s1:
s=escript.Scalar(0.,fs)
for i01 in s01:
s+=arg0.__getitem__(tuple(i01+i0))*arg1.__getitem__(tuple(i01+i1))
out.__setitem__(tuple(i0+i1),s)
return out

4539
4540  def matrix_transposed_mult(arg0,arg1):  def matrix_transposed_mult(arg0,arg1):
4541      """      """
# Line 4788  class GeneralTensorTransposedProduct_Sym Line 4719  class GeneralTensorTransposedProduct_Sym
4719
4720  def escript_generalTensorTransposedProduct(arg0,arg1,axis_offset): # this should be escript._generalTensorTransposedProduct  def escript_generalTensorTransposedProduct(arg0,arg1,axis_offset): # this should be escript._generalTensorTransposedProduct
4721      "arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!"      "arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!"
4722      # calculate the return shape:      return C_GeneralTensorProduct(arg0, arg1, axis_offset, 2)
shape01=arg0.getShape()[arg0.getRank()-axis_offset:]
shape0=arg0.getShape()[:arg0.getRank()-axis_offset]
shape10=arg1.getShape()[arg1.getRank()-axis_offset:]
shape1=arg1.getShape()[:arg1.getRank()-axis_offset]
if not shape01==shape10:
raise ValueError,"dimensions of first %s components in left argument don't match the first %s components in the right argument."%(axis_offset,axis_offset)

# whatr function space should be used? (this here is not good!)
fs=(escript.Scalar(0.,arg0.getFunctionSpace())+escript.Scalar(0.,arg1.getFunctionSpace())).getFunctionSpace()
# create return value:
out=escript.Data(0.,tuple(shape0+shape1),fs)
#
s0=[[]]
for k in shape0:
s=[]
for j in s0:
for i in range(k): s.append(j+[slice(i,i)])
s0=s
s1=[[]]
for k in shape1:
s=[]
for j in s1:
for i in range(k): s.append(j+[slice(i,i)])
s1=s
s01=[[]]
for k in shape01:
s=[]
for j in s01:
for i in range(k): s.append(j+[slice(i,i)])
s01=s

for i0 in s0:
for i1 in s1:
s=escript.Scalar(0.,fs)
for i01 in s01:
s+=arg0.__getitem__(tuple(i0+i01))*arg1.__getitem__(tuple(i1+i01))
out.__setitem__(tuple(i0+i1),s)
return out

4723
4724  #=========================================================  #=========================================================
4725  #  functions dealing with spatial dependency  #  functions dealing with spatial dependency

Legend:
 Removed from v.804 changed lines Added in v.881