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

revision 123 by jgs, Fri Jul 8 04:08:13 2005 UTC revision 124 by jgs, Wed Jul 20 06:14:58 2005 UTC
# Line 13  TODO for Data: Line 13  TODO for Data:
13
14    * implementation of outer outer(a,b)[:,*]=a[:]*b[*]    * implementation of outer outer(a,b)[:,*]=a[:]*b[*]
15    * trace: trace(arg,axis0=a0,axis1=a1)(:,&,*)=sum_i trace(:,i,&,i,*) (i are at index a0 and a1)    * trace: trace(arg,axis0=a0,axis1=a1)(:,&,*)=sum_i trace(:,i,&,i,*) (i are at index a0 and a1)

16  """  """
17
18  import numarray  import numarray
19  import escript  import escript
20
21  #  #
22  #   escript constants (have to be consistent witj utilC.h  #   escript constants (have to be consistent with utilC.h )
23  #  #
24  UNKNOWN=-1  UNKNOWN=-1
25  EPSILON=1.e-15  EPSILON=1.e-15
# Line 59  TIFF=7 Line 59  TIFF=7
59  OPENINVENTOR=8  OPENINVENTOR=8
60  RENDERMAN=9  RENDERMAN=9
61  PNM=10  PNM=10
62
63  #===========================================================  #===========================================================
64  #  a simple tool box to deal with _differentials of functions  # a simple tool box to deal with _differentials of functions
65  #===========================================================  #===========================================================
66
67  class Symbol:  class Symbol:
# Line 165  class Symbol: Line 166  class Symbol:
166         else:         else:
167           return self           return self
168

169     def __str__(self):     def __str__(self):
170         """returns a string representation of the symbol"""         """returns a string representation of the symbol"""
171         return self.__name         return self.__name
# Line 178  class Symbol: Line 178  class Symbol:
178            a=_matchShape([self,other])            a=_matchShape([self,other])
180

182         """adds other to symbol self. if _testForZero(other) self is returned."""         """adds other to symbol self. if _testForZero(other) self is returned."""
183         return self+other         return self+other
# Line 301  class Tensor4Symbol(Symbol): Line 300  class Tensor4Symbol(Symbol):
300             d=dim             d=dim
301        Symbol.__init__(self,shape=(d,d,d,d),dim=d,name=name)        Symbol.__init__(self,shape=(d,d,d,d),dim=d,name=name)
302

304     """symbol representing the sum of two arguments"""     """symbol representing the sum of two arguments"""
305     def __init__(self,arg0,arg1):     def __init__(self,arg0,arg1):
# Line 446  def _matchShape(args,shape=None): Line 444  def _matchShape(args,shape=None):
444              else:                else:
445                  raise ValueError,"cannot adopt shape of %s to %s"%(str(args[i]),str(shape))                  raise ValueError,"cannot adopt shape of %s to %s"%(str(args[i]),str(shape))
446      return out        return out
447
448  #=========================================================  #=========================================================
449  #   wrapper for various mathematical functions:  #   wrappers for various mathematical functions:
450  #=========================================================  #=========================================================
451  def diff(arg,dep):  def diff(arg,dep):
452      """returns the derivative of arg with respect to dep. If arg is not Symbol object      """returns the derivative of arg with respect to dep. If arg is not Symbol object
# Line 531  class Log_Symbol(Symbol): Line 530  class Log_Symbol(Symbol):
530     def _diff(self,arg):     def _diff(self,arg):
531         return self.getDifferentiatedArguments(arg)[0]/self.getArgument(0)         return self.getDifferentiatedArguments(arg)[0]/self.getArgument(0)
532
533    def ln(arg):
534        """
535        @brief applies the natural logarithmic function to arg
536        @param arg (input): argument
537        """
538        if isinstance(arg,Symbol):
539           return Ln_Symbol(arg)
540        elif hasattr(arg,"ln"):
541           return arg.log()
542        else:
543           return numarray.log(arg)
544
545    class Ln_Symbol(Symbol):
546       """symbol representing natural logarithm of the argument"""
547       def __init__(self,arg):
548           Symbol.__init__(self,shape=arg.getShape(),dim=arg.getDim(),args=[arg])
549       def __str__(self):
550          return "ln(%s)"%str(self.getArgument(0))
551       def eval(self,argval):
552           return ln(self.getEvaluatedArguments(argval)[0])
553       def _diff(self,arg):
554           return self.getDifferentiatedArguments(arg)[0]/self.getArgument(0)
555
556  def sin(arg):  def sin(arg):
557      """      """
558      @brief applies the sinus function to arg      @brief applies the sinus function to arg
# Line 857  def Integral_Symbol(Float_Symbol): Line 879  def Integral_Symbol(Float_Symbol):
879  #=============================  #=============================
880  #  #
881  # wrapper for various functions: if the argument has attribute the function name  # wrapper for various functions: if the argument has attribute the function name
882  # as an argument it calls the correspong methods. Otherwise the coresponsing numarray  # as an argument it calls the corresponding methods. Otherwise the corresponding
883  # function is called.  # numarray function is called.
884  #
885  # functions involving the underlying Domain:  # functions involving the underlying Domain:
#
886
887
888  # functions returning Data objects:  # functions returning Data objects:
# Line 919  def trace(arg,axis0=0,axis1=1): Line 940  def trace(arg,axis0=0,axis1=1):
940      else:      else:
941         return numarray.trace(arg,axis0=axis0,axis1=axis1)         return numarray.trace(arg,axis0=axis0,axis1=axis1)
942

943  def Trace_Symbol(Symbol):  def Trace_Symbol(Symbol):
944      pass      pass
945
# Line 1035  def matrixmult(arg0,arg1): Line 1054  def matrixmult(arg0,arg1):
1054            return out            return out
1055        else:        else:
1056            raise SystemError,"matrixmult is not fully implemented yet!"            raise SystemError,"matrixmult is not fully implemented yet!"
1057
1058  #=========================================================  #=========================================================
1059  # reduction operations:  # reduction operations:
1060  #=========================================================  #=========================================================
# Line 1127  def unit(i,d): Line 1147  def unit(i,d):
1147     e[i] = 1.0     e[i] = 1.0
1148     return e     return e
1149
#
1150  # ============================================  # ============================================
1151  #   testing  #   testing
1152  # ============================================  # ============================================
# Line 1138  if __name__=="__main__": Line 1157  if __name__=="__main__":
1157    v=VectorSymbol(2,"v")    v=VectorSymbol(2,"v")
1158    u=VectorSymbol(2,"u")    u=VectorSymbol(2,"u")
1159

1160    print u+5,(u+5).diff(u)    print u+5,(u+5).diff(u)
1161    print 5+u,(5+u).diff(u)    print 5+u,(5+u).diff(u)
1162    print u+v,(u+v).diff(u)    print u+v,(u+v).diff(u)
# Line 1180  if __name__=="__main__": Line 1198  if __name__=="__main__":
1199    print diff(5*g,g)    print diff(5*g,g)
1200    4*(g+transpose(g))/2+6*trace(g)*kronecker(3)    4*(g+transpose(g))/2+6*trace(g)*kronecker(3)
1201
1202  #  #
1203  # \$Log\$  # \$Log\$
1204    # Revision 1.12  2005/07/20 06:14:58  jgs
1205    # added ln(data) style wrapper for data.ln() - also added corresponding
1206    # implementation of Ln_Symbol class (not sure if this is right though)
1207    #
1208  # Revision 1.11  2005/07/08 04:07:35  jgs  # Revision 1.11  2005/07/08 04:07:35  jgs
1209  # Merge of development branch back to main trunk on 2005-07-08  # Merge of development branch back to main trunk on 2005-07-08
1210  #  #

Legend:
 Removed from v.123 changed lines Added in v.124