/[escript]/trunk/escript/py_src/util.py
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 588 by gross, Fri Mar 10 04:45:04 2006 UTC revision 912 by gross, Wed Dec 6 03:29:49 2006 UTC
# Line 1  Line 1 
1  # $Id$  # $Id$
 #  
 #      COPYRIGHT ACcESS 2004 -  All Rights Reserved  
 #  
 #   This software is the property of ACcESS.  No part of this code  
 #   may be copied in any form or by any means without the expressed written  
 #   consent of ACcESS.  Copying, use or modification of this software  
 #   by any unauthorised person is illegal unless that  
 #   person has a software license agreement with ACcESS.  
 #  
2    
3  """  """
4  Utility functions for escript  Utility functions for escript
5    
 @remark:  This module is under construction and is still tested!!!  
   
6  @var __author__: name of author  @var __author__: name of author
7  @var __licence__: licence agreement  @var __copyright__: copyrights
8    @var __license__: licence agreement
9  @var __url__: url entry point on documentation  @var __url__: url entry point on documentation
10  @var __version__: version  @var __version__: version
11  @var __date__: date of the version  @var __date__: date of the version
12  """  """
13                                                                                                                                                                                                                                                                                                                                                                                                            
14  __author__="Lutz Gross, l.gross@uq.edu.au"  __author__="Lutz Gross, l.gross@uq.edu.au"
15  __licence__="contact: esys@access.uq.edu.au"  __copyright__="""  Copyright (c) 2006 by ACcESS MNRF
16                        http://www.access.edu.au
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__="http://www.iservo.edu.au/esys/escript"  __url__="http://www.iservo.edu.au/esys/escript"
21  __version__="$Revision$"  __version__="$Revision$"
22  __date__="$Date$"  __date__="$Date$"
# Line 30  __date__="$Date$" Line 24  __date__="$Date$"
24    
25  import math  import math
26  import numarray  import numarray
 import numarray.linear_algebra  
27  import escript  import escript
28  import os  import os
29    from esys.escript import C_GeneralTensorProduct
 # missing tests:  
   
 # def pokeShape(arg):  
 # def pokeDim(arg):  
 # def commonShape(arg0,arg1):  
 # def commonDim(*args):  
 # def testForZero(arg):  
 # def matchType(arg0=0.,arg1=0.):  
 # def matchShape(arg0,arg1):  
   
 # def reorderComponents(arg,index):  
   
 #  
 # slicing: get  
 #          set  
 #  
 # and derivatives  
30    
31  #=========================================================  #=========================================================
32  #   some helpers:  #   some helpers:
# Line 59  def saveVTK(filename,domain=None,**data) Line 35  def saveVTK(filename,domain=None,**data)
35      """      """
36      writes a L{Data} objects into a files using the the VTK XML file format.      writes a L{Data} objects into a files using the the VTK XML file format.
37    
38      Example:      Example::
39    
40         tmp=Scalar(..)         tmp=Scalar(..)
41         v=Vector(..)         v=Vector(..)
# Line 87  def saveDX(filename,domain=None,**data): Line 63  def saveDX(filename,domain=None,**data):
63      """      """
64      writes a L{Data} objects into a files using the the DX file format.      writes a L{Data} objects into a files using the the DX file format.
65    
66      Example:      Example::
67    
68         tmp=Scalar(..)         tmp=Scalar(..)
69         v=Vector(..)         v=Vector(..)
# Line 118  def kronecker(d=3): Line 94  def kronecker(d=3):
94     @param d: dimension or an object that has the C{getDim} method defining the dimension     @param d: dimension or an object that has the C{getDim} method defining the dimension
95     @type d: C{int}, L{escript.Domain} or L{escript.FunctionSpace}     @type d: C{int}, L{escript.Domain} or L{escript.FunctionSpace}
96     @return: the object u of rank 2 with M{u[i,j]=1} for M{i=j} and M{u[i,j]=0} otherwise     @return: the object u of rank 2 with M{u[i,j]=1} for M{i=j} and M{u[i,j]=0} otherwise
97     @rtype d: L{numarray.NumArray} or L{escript.Data} of rank 2.     @rtype: L{numarray.NumArray} or L{escript.Data} of rank 2.
98     """     """
99     return identityTensor(d)     return identityTensor(d)
100    
# Line 154  def identityTensor(d=3): Line 130  def identityTensor(d=3):
130     @param d: dimension or an object that has the C{getDim} method defining the dimension     @param d: dimension or an object that has the C{getDim} method defining the dimension
131     @type d: C{int}, L{escript.Domain} or L{escript.FunctionSpace}     @type d: C{int}, L{escript.Domain} or L{escript.FunctionSpace}
132     @return: the object u of rank 2 with M{u[i,j]=1} for M{i=j} and M{u[i,j]=0} otherwise     @return: the object u of rank 2 with M{u[i,j]=1} for M{i=j} and M{u[i,j]=0} otherwise
133     @rtype d: L{numarray.NumArray} or L{escript.Data} of rank 2     @rtype: L{numarray.NumArray} or L{escript.Data} of rank 2
134     """     """
135     if isinstance(d,escript.FunctionSpace):     if isinstance(d,escript.FunctionSpace):
136         return escript.Data(identity((d.getDim(),)),d)         return escript.Data(identity((d.getDim(),)),d)
# Line 170  def identityTensor4(d=3): Line 146  def identityTensor4(d=3):
146     @param d: dimension or an object that has the C{getDim} method defining the dimension     @param d: dimension or an object that has the C{getDim} method defining the dimension
147     @type d: C{int} or any object with a C{getDim} method     @type d: C{int} or any object with a C{getDim} method
148     @return: the object u of rank 4 with M{u[i,j,k,l]=1} for M{i=k and j=l} and M{u[i,j,k,l]=0} otherwise     @return: the object u of rank 4 with M{u[i,j,k,l]=1} for M{i=k and j=l} and M{u[i,j,k,l]=0} otherwise
149     @rtype d: L{numarray.NumArray} or L{escript.Data} of rank 4.     @rtype: L{numarray.NumArray} or L{escript.Data} of rank 4.
150     """     """
151     if isinstance(d,escript.FunctionSpace):     if isinstance(d,escript.FunctionSpace):
152         return escript.Data(identity((d.getDim(),d.getDim())),d)         return escript.Data(identity((d.getDim(),d.getDim())),d)
# Line 188  def unitVector(i=0,d=3): Line 164  def unitVector(i=0,d=3):
164     @param d: dimension or an object that has the C{getDim} method defining the dimension     @param d: dimension or an object that has the C{getDim} method defining the dimension
165     @type d: C{int}, L{escript.Domain} or L{escript.FunctionSpace}     @type d: C{int}, L{escript.Domain} or L{escript.FunctionSpace}
166     @return: the object u of rank 1 with M{u[j]=1} for M{j=i} and M{u[i]=0} otherwise     @return: the object u of rank 1 with M{u[j]=1} for M{j=i} and M{u[i]=0} otherwise
167     @rtype d: L{numarray.NumArray} or L{escript.Data} of rank 1     @rtype: L{numarray.NumArray} or L{escript.Data} of rank 1
168     """     """
169     return kronecker(d)[i]     return kronecker(d)[i]
170    
# Line 244  def inf(arg): Line 220  def inf(arg):
220    
221      @param arg: argument      @param arg: argument
222      @type arg: C{float}, C{int}, L{escript.Data}, L{numarray.NumArray}.      @type arg: C{float}, C{int}, L{escript.Data}, L{numarray.NumArray}.
223      @return : minimum value of arg over all components and all data points      @return: minimum value of arg over all components and all data points
224      @rtype: C{float}      @rtype: C{float}
225      @raise TypeError: if type of arg cannot be processed      @raise TypeError: if type of arg cannot be processed
226      """      """
# Line 263  def inf(arg): Line 239  def inf(arg):
239  #=========================================================================  #=========================================================================
240  #   some little helpers  #   some little helpers
241  #=========================================================================  #=========================================================================
242  def pokeShape(arg):  def getRank(arg):
243        """
244        identifies the rank of its argument
245    
246        @param arg: a given object
247        @type arg: L{numarray.NumArray},L{escript.Data},C{float}, C{int}, C{Symbol}
248        @return: the rank of the argument
249        @rtype: C{int}
250        @raise TypeError: if type of arg cannot be processed
251        """
252    
253        if isinstance(arg,numarray.NumArray):
254            return arg.rank
255        elif isinstance(arg,escript.Data):
256            return arg.getRank()
257        elif isinstance(arg,float):
258            return 0
259        elif isinstance(arg,int):
260            return 0
261        elif isinstance(arg,Symbol):
262            return arg.getRank()
263        else:
264          raise TypeError,"getShape: cannot identify shape"
265    def getShape(arg):
266      """      """
267      identifies the shape of its argument      identifies the shape of its argument
268    
# Line 285  def pokeShape(arg): Line 284  def pokeShape(arg):
284      elif isinstance(arg,Symbol):      elif isinstance(arg,Symbol):
285          return arg.getShape()          return arg.getShape()
286      else:      else:
287        raise TypeError,"pokeShape: cannot identify shape"        raise TypeError,"getShape: cannot identify shape"
288    
289  def pokeDim(arg):  def pokeDim(arg):
290      """      """
# Line 308  def commonShape(arg0,arg1): Line 307  def commonShape(arg0,arg1):
307      """      """
308      returns a shape to which arg0 can be extendent from the right and arg1 can be extended from the left.      returns a shape to which arg0 can be extendent from the right and arg1 can be extended from the left.
309    
310      @param arg0: an object with a shape (see L{pokeShape})      @param arg0: an object with a shape (see L{getShape})
311      @param arg1: an object with a shape (see L{pokeShape})      @param arg1: an object with a shape (see L{getShape})
312      @return: the shape of arg0 or arg1 such that the left port equals the shape of arg0 and the right end equals the shape of arg1.      @return: the shape of arg0 or arg1 such that the left port equals the shape of arg0 and the right end equals the shape of arg1.
313      @rtype: C{tuple} of C{int}      @rtype: C{tuple} of C{int}
314      @raise ValueError: if no shape can be found.      @raise ValueError: if no shape can be found.
315      """      """
316      sh0=pokeShape(arg0)      sh0=getShape(arg0)
317      sh1=pokeShape(arg1)      sh1=getShape(arg1)
318      if len(sh0)<len(sh1):      if len(sh0)<len(sh1):
319         if not sh0==sh1[:len(sh0)]:         if not sh0==sh1[:len(sh0)]:
320               raise ValueError,"argument 0 cannot be extended to the shape of argument 1"               raise ValueError,"argument 0 cannot be extended to the shape of argument 1"
# Line 333  def commonDim(*args): Line 332  def commonDim(*args):
332      """      """
333      identifies, if possible, the spatial dimension across a set of objects which may or my not have a spatial dimension.      identifies, if possible, the spatial dimension across a set of objects which may or my not have a spatial dimension.
334    
335      @param *args: given objects      @param args: given objects
336      @return: the spatial dimension of the objects with identifiable dimension (see L{pokeDim}). If none the objects has      @return: the spatial dimension of the objects with identifiable dimension (see L{pokeDim}). If none the objects has
337               a spatial dimension C{None} is returned.               a spatial dimension C{None} is returned.
338      @rtype: C{int} or C{None}      @rtype: C{int} or C{None}
# Line 355  def testForZero(arg): Line 354  def testForZero(arg):
354    
355      @param arg: a given object      @param arg: a given object
356      @type arg: typically L{numarray.NumArray},L{escript.Data},C{float}, C{int}      @type arg: typically L{numarray.NumArray},L{escript.Data},C{float}, C{int}
357      @return : True if the argument is identical to zero.      @return: True if the argument is identical to zero.
358      @rtype : C{bool}      @rtype: C{bool}
359      """      """
360      if isinstance(arg,numarray.NumArray):      if isinstance(arg,numarray.NumArray):
361         return not Lsup(arg)>0.         return not Lsup(arg)>0.
# Line 459  def matchType(arg0=0.,arg1=0.): Line 458  def matchType(arg0=0.,arg1=0.):
458    
459  def matchShape(arg0,arg1):  def matchShape(arg0,arg1):
460      """      """
461            return representations of arg0 amd arg1 which ahve the same shape
462    
463      If shape is not given the shape "largest" shape of args is used.      @param arg0: a given object
464        @type arg0: L{numarray.NumArray},L{escript.Data},C{float}, C{int}, L{Symbol}
465      @param args: a given ob      @param arg1: a given object
466      @type arg: typically L{numarray.NumArray},L{escript.Data},C{float}, C{int}      @type arg1: L{numarray.NumArray},L{escript.Data},C{float}, C{int}, L{Symbol}
467      @return: True if the argument is identical to zero.      @return: C{arg0} and C{arg1} where copies are returned when the shape has to be changed.
468      @rtype: C{list} of C{int}      @rtype: C{tuple}
469      """      """
470      sh=commonShape(arg0,arg1)      sh=commonShape(arg0,arg1)
471      sh0=pokeShape(arg0)      sh0=getShape(arg0)
472      sh1=pokeShape(arg1)      sh1=getShape(arg1)
473      if len(sh0)<len(sh):      if len(sh0)<len(sh):
474         return outer(arg0,numarray.ones(sh[len(sh0):],numarray.Float64)),arg1         return outer(arg0,numarray.ones(sh[len(sh0):],numarray.Float64)),arg1
475      elif len(sh1)<len(sh):      elif len(sh1)<len(sh):
# Line 494  class Symbol(object): Line 493  class Symbol(object):
493         Creates an instance of a symbol of a given shape. The symbol may depending on a list of arguments args which may be         Creates an instance of a symbol of a given shape. The symbol may depending on a list of arguments args which may be
494         symbols or any other object.         symbols or any other object.
495    
496         @param arg: the arguments of the symbol.         @param args: the arguments of the symbol.
497         @type arg: C{list}         @type args: C{list}
498         @param shape: the shape         @param shape: the shape
499         @type shape: C{tuple} of C{int}         @type shape: C{tuple} of C{int}
500         @param dim: spatial dimension of the symbol. If dim=C{None} the spatial dimension is undefined.           @param dim: spatial dimension of the symbol. If dim=C{None} the spatial dimension is undefined.  
# Line 538  class Symbol(object): Line 537  class Symbol(object):
537         """         """
538         the shape of the symbol.         the shape of the symbol.
539    
540         @return : the shape of the symbol.         @return: the shape of the symbol.
541         @rtype: C{tuple} of C{int}         @rtype: C{tuple} of C{int}
542         """         """
543         return self.__shape         return self.__shape
# Line 547  class Symbol(object): Line 546  class Symbol(object):
546         """         """
547         the spatial dimension         the spatial dimension
548    
549         @return : the spatial dimension         @return: the spatial dimension
550         @rtype: C{int} if the dimension is defined. Otherwise C{None} is returned.         @rtype: C{int} if the dimension is defined. Otherwise C{None} is returned.
551         """         """
552         return self.__dim         return self.__dim
# Line 571  class Symbol(object): Line 570  class Symbol(object):
570         """         """
571         substitutes symbols in the arguments of this object and returns the result as a list.         substitutes symbols in the arguments of this object and returns the result as a list.
572    
573         @param argvals: L{Symbols} and their substitutes. The L{Symbol} u in the expression defining this object is replaced by argvals[u].         @param argvals: L{Symbol} and their substitutes. The L{Symbol} u in the expression defining this object is replaced by argvals[u].
574         @type argvals: C{dict} with keywords of type L{Symbol}.         @type argvals: C{dict} with keywords of type L{Symbol}.
575         @rtype: C{list} of objects         @rtype: C{list} of objects
576         @return: list of the object assigned to the arguments through substitution or for the arguments which are not L{Symbols} the value assigned to the argument at instantiation.         @return: list of the object assigned to the arguments through substitution or for the arguments which are not L{Symbol} the value assigned to the argument at instantiation.
577         """         """
578         out=[]         out=[]
579         for a in self.getArgument():         for a in self.getArgument():
# Line 598  class Symbol(object): Line 597  class Symbol(object):
597            if isinstance(a,Symbol):            if isinstance(a,Symbol):
598               out.append(a.substitute(argvals))               out.append(a.substitute(argvals))
599            else:            else:
600                s=pokeShape(s)+arg.getShape()                s=getShape(s)+arg.getShape()
601                if len(s)>0:                if len(s)>0:
602                   out.append(numarray.zeros(s),numarray.Float64)                   out.append(numarray.zeros(s),numarray.Float64)
603                else:                else:
# Line 698  class Symbol(object): Line 697  class Symbol(object):
697         """         """
698         returns -self.         returns -self.
699    
700         @return:  a S{Symbol} representing the negative of the object         @return:  a L{Symbol} representing the negative of the object
701         @rtype: L{DependendSymbol}         @rtype: L{DependendSymbol}
702         """         """
703         return self*(-1.)         return self*(-1.)
# Line 707  class Symbol(object): Line 706  class Symbol(object):
706         """         """
707         returns +self.         returns +self.
708    
709         @return:  a S{Symbol} representing the positive of the object         @return:  a L{Symbol} representing the positive of the object
710         @rtype: L{DependendSymbol}         @rtype: L{DependendSymbol}
711         """         """
712         return self*(1.)         return self*(1.)
713    
714     def __abs__(self):     def __abs__(self):
715         """         """
716         returns a S{Symbol} representing the absolute value of the object.         returns a L{Symbol} representing the absolute value of the object.
717         """         """
718         return Abs_Symbol(self)         return Abs_Symbol(self)
719    
# Line 724  class Symbol(object): Line 723  class Symbol(object):
723    
724         @param other: object to be added to this object         @param other: object to be added to this object
725         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
726         @return:  a S{Symbol} representing the sum of this object and C{other}         @return:  a L{Symbol} representing the sum of this object and C{other}
727         @rtype: L{DependendSymbol}         @rtype: L{DependendSymbol}
728         """         """
729         return add(self,other)         return add(self,other)
# Line 735  class Symbol(object): Line 734  class Symbol(object):
734    
735         @param other: object this object is added to         @param other: object this object is added to
736         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
737         @return: a S{Symbol} representing the sum of C{other} and this object object         @return: a L{Symbol} representing the sum of C{other} and this object object
738         @rtype: L{DependendSymbol}         @rtype: L{DependendSymbol}
739         """         """
740         return add(other,self)         return add(other,self)
# Line 746  class Symbol(object): Line 745  class Symbol(object):
745    
746         @param other: object to be subtracted from this object         @param other: object to be subtracted from this object
747         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
748         @return: a S{Symbol} representing the difference of C{other} and this object         @return: a L{Symbol} representing the difference of C{other} and this object
749         @rtype: L{DependendSymbol}         @rtype: L{DependendSymbol}
750         """         """
751         return add(self,-other)         return add(self,-other)
# Line 757  class Symbol(object): Line 756  class Symbol(object):
756    
757         @param other: object this object is been subtracted from         @param other: object this object is been subtracted from
758         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
759         @return: a S{Symbol} representing the difference of this object and C{other}.         @return: a L{Symbol} representing the difference of this object and C{other}.
760         @rtype: L{DependendSymbol}         @rtype: L{DependendSymbol}
761         """         """
762         return add(-self,other)         return add(-self,other)
# Line 768  class Symbol(object): Line 767  class Symbol(object):
767    
768         @param other: object to be mutiplied by this object         @param other: object to be mutiplied by this object
769         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
770         @return: a S{Symbol} representing the product of the object and C{other}.         @return: a L{Symbol} representing the product of the object and C{other}.
771         @rtype: L{DependendSymbol} or 0 if other is identical to zero.         @rtype: L{DependendSymbol} or 0 if other is identical to zero.
772         """         """
773         return mult(self,other)         return mult(self,other)
# Line 779  class Symbol(object): Line 778  class Symbol(object):
778    
779         @param other: object this object is multiplied with         @param other: object this object is multiplied with
780         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
781         @return: a S{Symbol} representing the product of C{other} and the object.         @return: a L{Symbol} representing the product of C{other} and the object.
782         @rtype: L{DependendSymbol} or 0 if other is identical to zero.         @rtype: L{DependendSymbol} or 0 if other is identical to zero.
783         """         """
784         return mult(other,self)         return mult(other,self)
# Line 790  class Symbol(object): Line 789  class Symbol(object):
789    
790         @param other: object dividing this object         @param other: object dividing this object
791         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
792         @return: a S{Symbol} representing the quotient of this object and C{other}         @return: a L{Symbol} representing the quotient of this object and C{other}
793         @rtype: L{DependendSymbol}         @rtype: L{DependendSymbol}
794         """         """
795         return quotient(self,other)         return quotient(self,other)
# Line 801  class Symbol(object): Line 800  class Symbol(object):
800    
801         @param other: object dividing this object         @param other: object dividing this object
802         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
803         @return: a S{Symbol} representing the quotient of C{other} and this object         @return: a L{Symbol} representing the quotient of C{other} and this object
804         @rtype: L{DependendSymbol} or 0 if C{other} is identical to zero.         @rtype: L{DependendSymbol} or 0 if C{other} is identical to zero.
805         """         """
806         return quotient(other,self)         return quotient(other,self)
# Line 812  class Symbol(object): Line 811  class Symbol(object):
811    
812         @param other: exponent         @param other: exponent
813         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
814         @return: a S{Symbol} representing the power of this object to C{other}         @return: a L{Symbol} representing the power of this object to C{other}
815         @rtype: L{DependendSymbol} or 1 if C{other} is identical to zero.         @rtype: L{DependendSymbol} or 1 if C{other} is identical to zero.
816         """         """
817         return power(self,other)         return power(self,other)
# Line 823  class Symbol(object): Line 822  class Symbol(object):
822    
823         @param other: basis         @param other: basis
824         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type other: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
825         @return: a S{Symbol} representing the power of C{other} to this object         @return: a L{Symbol} representing the power of C{other} to this object
826         @rtype: L{DependendSymbol} or 0 if C{other} is identical to zero.         @rtype: L{DependendSymbol} or 0 if C{other} is identical to zero.
827         """         """
828         return power(other,self)         return power(other,self)
# Line 834  class Symbol(object): Line 833  class Symbol(object):
833    
834         @param index: defines a         @param index: defines a
835         @type index: C{slice} or C{int} or a C{tuple} of them         @type index: C{slice} or C{int} or a C{tuple} of them
836         @return: a S{Symbol} representing the slice defined by index         @return: a L{Symbol} representing the slice defined by index
837         @rtype: L{DependendSymbol}         @rtype: L{DependendSymbol}
838         """         """
839         return GetSlice_Symbol(self,index)         return GetSlice_Symbol(self,index)
# Line 844  class DependendSymbol(Symbol): Line 843  class DependendSymbol(Symbol):
843     DependendSymbol extents L{Symbol} by modifying the == operator to allow two instances to be equal.     DependendSymbol extents L{Symbol} by modifying the == operator to allow two instances to be equal.
844     Two DependendSymbol are equal if they have the same shape, the same arguments and one of them has an unspecified spatial dimension or the spatial dimension is identical       Two DependendSymbol are equal if they have the same shape, the same arguments and one of them has an unspecified spatial dimension or the spatial dimension is identical  
845        
846     Example:     Example::
847        
848     u1=Symbol(shape=(3,4),dim=2,args=[4.])       u1=Symbol(shape=(3,4),dim=2,args=[4.])
849     u2=Symbol(shape=(3,4),dim=2,args=[4.])       u2=Symbol(shape=(3,4),dim=2,args=[4.])
850     print u1==u2       print u1==u2
851     False       False
852        
853        but       but::
854    
855     u1=DependendSymbol(shape=(3,4),dim=2,args=[4.])       u1=DependendSymbol(shape=(3,4),dim=2,args=[4.])
856     u2=DependendSymbol(shape=(3,4),dim=2,args=[4.])       u2=DependendSymbol(shape=(3,4),dim=2,args=[4.])
857     u3=DependendSymbol(shape=(2,),dim=2,args=[4.])         u3=DependendSymbol(shape=(2,),dim=2,args=[4.])  
858     print u1==u2, u1==u3       print u1==u2, u1==u3
859     True False       True False
860    
861     @note: DependendSymbol should be used as return value of functions with L{Symbol} arguments. This will allow the optimizer to remove redundant function calls.     @note: DependendSymbol should be used as return value of functions with L{Symbol} arguments. This will allow the optimizer to remove redundant function calls.
862     """     """
# Line 947  class GetSlice_Symbol(DependendSymbol): Line 946  class GetSlice_Symbol(DependendSymbol):
946        @type format: C{str}        @type format: C{str}
947        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
948        @rtype: C{str}        @rtype: C{str}
949        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
950        """        """
951        if format=="escript" or format=="str"  or format=="text":        if format=="escript" or format=="str"  or format=="text":
952           return "%s.__getitem__(%s)"%(argstrs[0],argstrs[1])           return "%s.__getitem__(%s)"%(argstrs[0],argstrs[1])
# Line 983  def log10(arg): Line 982  def log10(arg):
982    
983     @param arg: argument     @param arg: argument
984     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
985     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
986     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
987     """     """
988     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1005  def wherePositive(arg): Line 1004  def wherePositive(arg):
1004    
1005     @param arg: argument     @param arg: argument
1006     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1007     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1008     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1009     """     """
1010     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1051  class WherePositive_Symbol(DependendSymb Line 1050  class WherePositive_Symbol(DependendSymb
1050        @type format: C{str}        @type format: C{str}
1051        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1052        @rtype: C{str}        @rtype: C{str}
1053        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1054        """        """
1055        if isinstance(argstrs,list):        if isinstance(argstrs,list):
1056            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 1087  def whereNegative(arg): Line 1086  def whereNegative(arg):
1086    
1087     @param arg: argument     @param arg: argument
1088     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1089     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1090     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1091     """     """
1092     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1133  class WhereNegative_Symbol(DependendSymb Line 1132  class WhereNegative_Symbol(DependendSymb
1132        @type format: C{str}        @type format: C{str}
1133        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1134        @rtype: C{str}        @rtype: C{str}
1135        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1136        """        """
1137        if isinstance(argstrs,list):        if isinstance(argstrs,list):
1138            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 1169  def whereNonNegative(arg): Line 1168  def whereNonNegative(arg):
1168    
1169     @param arg: argument     @param arg: argument
1170     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1171     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1172     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1173     """     """
1174     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1199  def whereNonPositive(arg): Line 1198  def whereNonPositive(arg):
1198    
1199     @param arg: argument     @param arg: argument
1200     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1201     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1202     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1203     """     """
1204     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1231  def whereZero(arg,tol=0.): Line 1230  def whereZero(arg,tol=0.):
1230     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1231     @param tol: tolerance. values with absolute value less then tol are accepted as zero.     @param tol: tolerance. values with absolute value less then tol are accepted as zero.
1232     @type tol: C{float}     @type tol: C{float}
1233     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1234     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1235     """     """
1236     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1239  def whereZero(arg,tol=0.): Line 1238  def whereZero(arg,tol=0.):
1238        if isinstance(out,float): out=numarray.array(out,type=numarray.Float64)        if isinstance(out,float): out=numarray.array(out,type=numarray.Float64)
1239        return out        return out
1240     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1241        if tol>0.:        return arg._whereZero(tol)
          return whereNegative(abs(arg)-tol)  
       else:  
          return arg._whereZero()  
1242     elif isinstance(arg,float):     elif isinstance(arg,float):
1243        if abs(arg)<=tol:        if abs(arg)<=tol:
1244          return 1.          return 1.
# Line 1280  class WhereZero_Symbol(DependendSymbol): Line 1276  class WhereZero_Symbol(DependendSymbol):
1276        @type format: C{str}        @type format: C{str}
1277        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1278        @rtype: C{str}        @rtype: C{str}
1279        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1280        """        """
1281        if format=="escript" or format=="str"  or format=="text":        if format=="escript" or format=="str"  or format=="text":
1282           return "whereZero(%s,tol=%s)"%(argstrs[0],argstrs[1])           return "whereZero(%s,tol=%s)"%(argstrs[0],argstrs[1])
# Line 1314  def whereNonZero(arg,tol=0.): Line 1310  def whereNonZero(arg,tol=0.):
1310    
1311     @param arg: argument     @param arg: argument
1312     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1313     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1314     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1315     """     """
1316     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1322  def whereNonZero(arg,tol=0.): Line 1318  def whereNonZero(arg,tol=0.):
1318        if isinstance(out,float): out=numarray.array(out,type=numarray.Float64)        if isinstance(out,float): out=numarray.array(out,type=numarray.Float64)
1319        return out        return out
1320     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
1321        if tol>0.:        return arg._whereNonZero(tol)
          return 1.-whereZero(arg,tol)  
       else:  
          return arg._whereNonZero()  
1322     elif isinstance(arg,float):     elif isinstance(arg,float):
1323        if abs(arg)>tol:        if abs(arg)>tol:
1324          return 1.          return 1.
# Line 1341  def whereNonZero(arg,tol=0.): Line 1334  def whereNonZero(arg,tol=0.):
1334     else:     else:
1335        raise TypeError,"whereNonZero: Unknown argument type."        raise TypeError,"whereNonZero: Unknown argument type."
1336    
1337    def erf(arg):
1338       """
1339       returns erf of argument arg
1340    
1341       @param arg: argument
1342       @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1343       @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1344       @raises TypeError: if the type of the argument is not expected.
1345       """
1346       if isinstance(arg,escript.Data):
1347          return arg._erf()
1348       else:
1349          raise TypeError,"erf: Unknown argument type."
1350    
1351  def sin(arg):  def sin(arg):
1352     """     """
1353     returns sine of argument arg     returns sine of argument arg
1354    
1355     @param arg: argument     @param arg: argument
1356     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1357     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1358     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1359     """     """
1360     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1385  class Sin_Symbol(DependendSymbol): Line 1392  class Sin_Symbol(DependendSymbol):
1392        @type format: C{str}        @type format: C{str}
1393        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1394        @rtype: C{str}        @rtype: C{str}
1395        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1396        """        """
1397        if isinstance(argstrs,list):        if isinstance(argstrs,list):
1398            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 1437  def cos(arg): Line 1444  def cos(arg):
1444    
1445     @param arg: argument     @param arg: argument
1446     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1447     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1448     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1449     """     """
1450     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1475  class Cos_Symbol(DependendSymbol): Line 1482  class Cos_Symbol(DependendSymbol):
1482        @type format: C{str}        @type format: C{str}
1483        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1484        @rtype: C{str}        @rtype: C{str}
1485        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1486        """        """
1487        if isinstance(argstrs,list):        if isinstance(argstrs,list):
1488            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 1527  def tan(arg): Line 1534  def tan(arg):
1534    
1535     @param arg: argument     @param arg: argument
1536     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1537     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1538     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1539     """     """
1540     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1565  class Tan_Symbol(DependendSymbol): Line 1572  class Tan_Symbol(DependendSymbol):
1572        @type format: C{str}        @type format: C{str}
1573        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1574        @rtype: C{str}        @rtype: C{str}
1575        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1576        """        """
1577        if isinstance(argstrs,list):        if isinstance(argstrs,list):
1578            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 1617  def asin(arg): Line 1624  def asin(arg):
1624    
1625     @param arg: argument     @param arg: argument
1626     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1627     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1628     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1629     """     """
1630     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1655  class Asin_Symbol(DependendSymbol): Line 1662  class Asin_Symbol(DependendSymbol):
1662        @type format: C{str}        @type format: C{str}
1663        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1664        @rtype: C{str}        @rtype: C{str}
1665        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1666        """        """
1667        if isinstance(argstrs,list):        if isinstance(argstrs,list):
1668            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 1707  def acos(arg): Line 1714  def acos(arg):
1714    
1715     @param arg: argument     @param arg: argument
1716     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1717     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1718     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1719     """     """
1720     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1745  class Acos_Symbol(DependendSymbol): Line 1752  class Acos_Symbol(DependendSymbol):
1752        @type format: C{str}        @type format: C{str}
1753        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1754        @rtype: C{str}        @rtype: C{str}
1755        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1756        """        """
1757        if isinstance(argstrs,list):        if isinstance(argstrs,list):
1758            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 1797  def atan(arg): Line 1804  def atan(arg):
1804    
1805     @param arg: argument     @param arg: argument
1806     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1807     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1808     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1809     """     """
1810     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1835  class Atan_Symbol(DependendSymbol): Line 1842  class Atan_Symbol(DependendSymbol):
1842        @type format: C{str}        @type format: C{str}
1843        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1844        @rtype: C{str}        @rtype: C{str}
1845        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1846        """        """
1847        if isinstance(argstrs,list):        if isinstance(argstrs,list):
1848            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 1887  def sinh(arg): Line 1894  def sinh(arg):
1894    
1895     @param arg: argument     @param arg: argument
1896     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1897     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1898     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1899     """     """
1900     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 1925  class Sinh_Symbol(DependendSymbol): Line 1932  class Sinh_Symbol(DependendSymbol):
1932        @type format: C{str}        @type format: C{str}
1933        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
1934        @rtype: C{str}        @rtype: C{str}
1935        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
1936        """        """
1937        if isinstance(argstrs,list):        if isinstance(argstrs,list):
1938            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 1977  def cosh(arg): Line 1984  def cosh(arg):
1984    
1985     @param arg: argument     @param arg: argument
1986     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
1987     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
1988     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
1989     """     """
1990     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2015  class Cosh_Symbol(DependendSymbol): Line 2022  class Cosh_Symbol(DependendSymbol):
2022        @type format: C{str}        @type format: C{str}
2023        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2024        @rtype: C{str}        @rtype: C{str}
2025        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2026        """        """
2027        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2028            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2067  def tanh(arg): Line 2074  def tanh(arg):
2074    
2075     @param arg: argument     @param arg: argument
2076     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2077     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
2078     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2079     """     """
2080     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2105  class Tanh_Symbol(DependendSymbol): Line 2112  class Tanh_Symbol(DependendSymbol):
2112        @type format: C{str}        @type format: C{str}
2113        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2114        @rtype: C{str}        @rtype: C{str}
2115        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2116        """        """
2117        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2118            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2157  def asinh(arg): Line 2164  def asinh(arg):
2164    
2165     @param arg: argument     @param arg: argument
2166     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2167     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
2168     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2169     """     """
2170     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2195  class Asinh_Symbol(DependendSymbol): Line 2202  class Asinh_Symbol(DependendSymbol):
2202        @type format: C{str}        @type format: C{str}
2203        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2204        @rtype: C{str}        @rtype: C{str}
2205        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2206        """        """
2207        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2208            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2247  def acosh(arg): Line 2254  def acosh(arg):
2254    
2255     @param arg: argument     @param arg: argument
2256     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2257     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
2258     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2259     """     """
2260     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2285  class Acosh_Symbol(DependendSymbol): Line 2292  class Acosh_Symbol(DependendSymbol):
2292        @type format: C{str}        @type format: C{str}
2293        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2294        @rtype: C{str}        @rtype: C{str}
2295        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2296        """        """
2297        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2298            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2337  def atanh(arg): Line 2344  def atanh(arg):
2344    
2345     @param arg: argument     @param arg: argument
2346     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2347     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
2348     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2349     """     """
2350     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2375  class Atanh_Symbol(DependendSymbol): Line 2382  class Atanh_Symbol(DependendSymbol):
2382        @type format: C{str}        @type format: C{str}
2383        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2384        @rtype: C{str}        @rtype: C{str}
2385        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2386        """        """
2387        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2388            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2427  def exp(arg): Line 2434  def exp(arg):
2434    
2435     @param arg: argument     @param arg: argument
2436     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2437     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
2438     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2439     """     """
2440     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2465  class Exp_Symbol(DependendSymbol): Line 2472  class Exp_Symbol(DependendSymbol):
2472        @type format: C{str}        @type format: C{str}
2473        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2474        @rtype: C{str}        @rtype: C{str}
2475        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2476        """        """
2477        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2478            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2517  def sqrt(arg): Line 2524  def sqrt(arg):
2524    
2525     @param arg: argument     @param arg: argument
2526     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2527     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
2528     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2529     """     """
2530     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2555  class Sqrt_Symbol(DependendSymbol): Line 2562  class Sqrt_Symbol(DependendSymbol):
2562        @type format: C{str}        @type format: C{str}
2563        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2564        @rtype: C{str}        @rtype: C{str}
2565        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2566        """        """
2567        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2568            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2607  def log(arg): Line 2614  def log(arg):
2614    
2615     @param arg: argument     @param arg: argument
2616     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2617     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
2618     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2619     """     """
2620     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2645  class Log_Symbol(DependendSymbol): Line 2652  class Log_Symbol(DependendSymbol):
2652        @type format: C{str}        @type format: C{str}
2653        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2654        @rtype: C{str}        @rtype: C{str}
2655        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2656        """        """
2657        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2658            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2697  def sign(arg): Line 2704  def sign(arg):
2704    
2705     @param arg: argument     @param arg: argument
2706     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2707     @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
2708     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2709     """     """
2710     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2745  class Abs_Symbol(DependendSymbol): Line 2752  class Abs_Symbol(DependendSymbol):
2752        @type format: C{str}        @type format: C{str}
2753        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2754        @rtype: C{str}        @rtype: C{str}
2755        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2756        """        """
2757        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2758            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2797  def minval(arg): Line 2804  def minval(arg):
2804    
2805     @param arg: argument     @param arg: argument
2806     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2807     @rtype:C{float}, L{escript.Data}, L{Symbol} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol} depending on the type of arg.
2808     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2809     """     """
2810     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2838  class Minval_Symbol(DependendSymbol): Line 2845  class Minval_Symbol(DependendSymbol):
2845        @type format: C{str}        @type format: C{str}
2846        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2847        @rtype: C{str}        @rtype: C{str}
2848        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2849        """        """
2850        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2851            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2874  def maxval(arg): Line 2881  def maxval(arg):
2881    
2882     @param arg: argument     @param arg: argument
2883     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2884     @rtype:C{float}, L{escript.Data}, L{Symbol} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol} depending on the type of arg.
2885     @raises TypeError: if the type of the argument is not expected.     @raises TypeError: if the type of the argument is not expected.
2886     """     """
2887     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
# Line 2915  class Maxval_Symbol(DependendSymbol): Line 2922  class Maxval_Symbol(DependendSymbol):
2922        @type format: C{str}        @type format: C{str}
2923        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
2924        @rtype: C{str}        @rtype: C{str}
2925        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
2926        """        """
2927        if isinstance(argstrs,list):        if isinstance(argstrs,list):
2928            argstrs=argstrs[0]            argstrs=argstrs[0]
# Line 2951  def length(arg): Line 2958  def length(arg):
2958    
2959     @param arg: argument     @param arg: argument
2960     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2961     @rtype:C{float}, L{escript.Data}, L{Symbol} depending on the type of arg.     @rtype: C{float}, L{escript.Data}, L{Symbol} depending on the type of arg.
2962     """     """
2963     return sqrt(inner(arg,arg))     return sqrt(inner(arg,arg))
2964    
# Line 2961  def trace(arg,axis_offset=0): Line 2968  def trace(arg,axis_offset=0):
2968    
2969     @param arg: argument     @param arg: argument
2970     @type arg: L{escript.Data}, L{Symbol}, L{numarray.NumArray}.     @type arg: L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
2971     @param axis_offset: axis_offset to components to sum over. C{axis_offset} must be non-negative and less than the rank of arg +1. The dimensions on component     @param axis_offset: C{axis_offset} to components to sum over. C{axis_offset} must be non-negative and less than the rank of arg +1. The dimensions on component
2972                    axis_offset and axis_offset+1 must be equal.                    C{axis_offset} and axis_offset+1 must be equal.
2973     @type axis_offset: C{int}     @type axis_offset: C{int}
2974     @return: trace of arg. The rank of the returned object is minus 2 of the rank of arg.     @return: trace of arg. The rank of the returned object is minus 2 of the rank of arg.
2975     @rtype: L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.     @rtype: L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
# Line 2970  def trace(arg,axis_offset=0): Line 2977  def trace(arg,axis_offset=0):
2977     if isinstance(arg,numarray.NumArray):     if isinstance(arg,numarray.NumArray):
2978        sh=arg.shape        sh=arg.shape
2979        if len(sh)<2:        if len(sh)<2:
2980          raise ValueError,"trace: rank of argument must be greater than 1"          raise ValueError,"rank of argument must be greater than 1"
2981        if axis_offset<0 or axis_offset>len(sh)-2:        if axis_offset<0 or axis_offset>len(sh)-2:
2982          raise ValueError,"trace: axis_offset must be between 0 and %s"%len(sh)-2          raise ValueError,"axis_offset must be between 0 and %s"%len(sh)-2
2983        s1=1        s1=1
2984        for i in range(axis_offset): s1*=sh[i]        for i in range(axis_offset): s1*=sh[i]
2985        s2=1        s2=1
2986        for i in range(axis_offset+2,len(sh)): s2*=sh[i]        for i in range(axis_offset+2,len(sh)): s2*=sh[i]
2987        if not sh[axis_offset] == sh[axis_offset+1]:        if not sh[axis_offset] == sh[axis_offset+1]:
2988          raise ValueError,"trace: dimensions of component %s and %s must match."%(axis_offset.axis_offset+1)          raise ValueError,"dimensions of component %s and %s must match."%(axis_offset.axis_offset+1)
2989        arg_reshaped=numarray.reshape(arg,(s1,sh[axis_offset],sh[axis_offset],s2))        arg_reshaped=numarray.reshape(arg,(s1,sh[axis_offset],sh[axis_offset],s2))
2990        out=numarray.zeros([s1,s2],numarray.Float64)        out=numarray.zeros([s1,s2],numarray.Float64)
2991        for i1 in range(s1):        for i1 in range(s1):
# Line 2987  def trace(arg,axis_offset=0): Line 2994  def trace(arg,axis_offset=0):
2994        out.resize(sh[:axis_offset]+sh[axis_offset+2:])        out.resize(sh[:axis_offset]+sh[axis_offset+2:])
2995        return out        return out
2996     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
2997        return escript_trace(arg,axis_offset)        if arg.getRank()<2:
2998            raise ValueError,"rank of argument must be greater than 1"
2999          if axis_offset<0 or axis_offset>arg.getRank()-2:
3000            raise ValueError,"axis_offset must be between 0 and %s"%arg.getRank()-2
3001          s=list(arg.getShape())        
3002          if not s[axis_offset] == s[axis_offset+1]:
3003            raise ValueError,"dimensions of component %s and %s must match."%(axis_offset.axis_offset+1)
3004          return arg._trace(axis_offset)
3005     elif isinstance(arg,float):     elif isinstance(arg,float):
3006        raise TypeError,"trace: illegal argument type float."        raise TypeError,"illegal argument type float."
3007     elif isinstance(arg,int):     elif isinstance(arg,int):
3008        raise TypeError,"trace: illegal argument type int."        raise TypeError,"illegal argument type int."
3009     elif isinstance(arg,Symbol):     elif isinstance(arg,Symbol):
3010        return Trace_Symbol(arg,axis_offset)        return Trace_Symbol(arg,axis_offset)
3011     else:     else:
3012        raise TypeError,"trace: Unknown argument type."        raise TypeError,"Unknown argument type."
3013    
 def escript_trace(arg,axis_offset): # this should be escript._trace  
       "arg si a Data objects!!!"  
       if arg.getRank()<2:  
         raise ValueError,"escript_trace: rank of argument must be greater than 1"  
       if axis_offset<0 or axis_offset>arg.getRank()-2:  
         raise ValueError,"escript_trace: axis_offset must be between 0 and %s"%arg.getRank()-2  
       s=list(arg.getShape())          
       if not s[axis_offset] == s[axis_offset+1]:  
         raise ValueError,"escript_trace: dimensions of component %s and %s must match."%(axis_offset.axis_offset+1)  
       out=escript.Data(0.,tuple(s[0:axis_offset]+s[axis_offset+2:]),arg.getFunctionSpace())  
       if arg.getRank()==2:  
          for i0 in range(s[0]):  
             out+=arg[i0,i0]  
       elif arg.getRank()==3:  
          if axis_offset==0:  
             for i0 in range(s[0]):  
                   for i2 in range(s[2]):  
                          out[i2]+=arg[i0,i0,i2]  
          elif axis_offset==1:  
             for i0 in range(s[0]):  
                for i1 in range(s[1]):  
                          out[i0]+=arg[i0,i1,i1]  
       elif arg.getRank()==4:  
          if axis_offset==0:  
             for i0 in range(s[0]):  
                   for i2 in range(s[2]):  
                      for i3 in range(s[3]):  
                          out[i2,i3]+=arg[i0,i0,i2,i3]  
          elif axis_offset==1:  
             for i0 in range(s[0]):  
                for i1 in range(s[1]):  
                      for i3 in range(s[3]):  
                          out[i0,i3]+=arg[i0,i1,i1,i3]  
          elif axis_offset==2:  
             for i0 in range(s[0]):  
                for i1 in range(s[1]):  
                   for i2 in range(s[2]):  
                          out[i0,i1]+=arg[i0,i1,i2,i2]  
       return out  
3014  class Trace_Symbol(DependendSymbol):  class Trace_Symbol(DependendSymbol):
3015     """     """
3016     L{Symbol} representing the result of the trace function     L{Symbol} representing the result of the trace function
# Line 3045  class Trace_Symbol(DependendSymbol): Line 3020  class Trace_Symbol(DependendSymbol):
3020        initialization of trace L{Symbol} with argument arg        initialization of trace L{Symbol} with argument arg
3021        @param arg: argument of function        @param arg: argument of function
3022        @type arg: L{Symbol}.        @type arg: L{Symbol}.
3023        @param axis_offset: axis_offset to components to sum over. C{axis_offset} must be non-negative and less than the rank of arg +1. The dimensions on component        @param axis_offset: C{axis_offset} to components to sum over. C{axis_offset} must be non-negative and less than the rank of arg +1. The dimensions on component
3024                    axis_offset and axis_offset+1 must be equal.                    C{axis_offset} and axis_offset+1 must be equal.
3025        @type axis_offset: C{int}        @type axis_offset: C{int}
3026        """        """
3027        if arg.getRank()<2:        if arg.getRank()<2:
3028          raise ValueError,"Trace_Symbol: rank of argument must be greater than 1"          raise ValueError,"rank of argument must be greater than 1"
3029        if axis_offset<0 or axis_offset>arg.getRank()-2:        if axis_offset<0 or axis_offset>arg.getRank()-2:
3030          raise ValueError,"Trace_Symbol: axis_offset must be between 0 and %s"%arg.getRank()-2          raise ValueError,"axis_offset must be between 0 and %s"%arg.getRank()-2
3031        s=list(arg.getShape())                s=list(arg.getShape())        
3032        if not s[axis_offset] == s[axis_offset+1]:        if not s[axis_offset] == s[axis_offset+1]:
3033          raise ValueError,"Trace_Symbol: dimensions of component %s and %s must match."%(axis_offset.axis_offset+1)          raise ValueError,"dimensions of component %s and %s must match."%(axis_offset.axis_offset+1)
3034        super(Trace_Symbol,self).__init__(args=[arg,axis_offset],shape=tuple(s[0:axis_offset]+s[axis_offset+2:]),dim=arg.getDim())        super(Trace_Symbol,self).__init__(args=[arg,axis_offset],shape=tuple(s[0:axis_offset]+s[axis_offset+2:]),dim=arg.getDim())
3035    
3036     def getMyCode(self,argstrs,format="escript"):     def getMyCode(self,argstrs,format="escript"):
# Line 3068  class Trace_Symbol(DependendSymbol): Line 3043  class Trace_Symbol(DependendSymbol):
3043        @type format: C{str}        @type format: C{str}
3044        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
3045        @rtype: C{str}        @rtype: C{str}
3046        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
3047        """        """
3048        if format=="escript" or format=="str"  or format=="text":        if format=="escript" or format=="str"  or format=="text":
3049           return "trace(%s,axis_offset=%s)"%(argstrs[0],argstrs[1])           return "trace(%s,axis_offset=%s)"%(argstrs[0],argstrs[1])
# Line 3112  class Trace_Symbol(DependendSymbol): Line 3087  class Trace_Symbol(DependendSymbol):
3087    
3088  def transpose(arg,axis_offset=None):  def transpose(arg,axis_offset=None):
3089     """     """
3090     returns the transpose of arg by swaping the first axis_offset and the last rank-axis_offset components.     returns the transpose of arg by swaping the first C{axis_offset} and the last rank-axis_offset components.
3091    
3092     @param arg: argument     @param arg: argument
3093     @type arg: L{escript.Data}, L{Symbol}, L{numarray.NumArray}, C{float}, C{int}     @type arg: L{escript.Data}, L{Symbol}, L{numarray.NumArray}, C{float}, C{int}
3094     @param axis_offset: the first axis_offset components are swapped with rest. If C{axis_offset} must be non-negative and less or equal the rank of arg.     @param axis_offset: the first C{axis_offset} components are swapped with rest. If C{axis_offset} must be non-negative and less or equal the rank of arg.
3095                         if axis_offset is not present C{int(r/2)} where r is the rank of arg is used.                         if C{axis_offset} is not present C{int(r/2)} where r is the rank of arg is used.
3096     @type axis_offset: C{int}     @type axis_offset: C{int}
3097     @return: transpose of arg     @return: transpose of arg
3098     @rtype: L{escript.Data}, L{Symbol}, L{numarray.NumArray},C{float}, C{int} depending on the type of arg.     @rtype: L{escript.Data}, L{Symbol}, L{numarray.NumArray},C{float}, C{int} depending on the type of arg.
# Line 3126  def transpose(arg,axis_offset=None): Line 3101  def transpose(arg,axis_offset=None):
3101        if axis_offset==None: axis_offset=int(arg.rank/2)        if axis_offset==None: axis_offset=int(arg.rank/2)
3102        return numarray.transpose(arg,axes=range(axis_offset,arg.rank)+range(0,axis_offset))        return numarray.transpose(arg,axes=range(axis_offset,arg.rank)+range(0,axis_offset))
3103     elif isinstance(arg,escript.Data):     elif isinstance(arg,escript.Data):
3104        if axis_offset==None: axis_offset=int(arg.getRank()/2)        r=arg.getRank()
3105        return escript_transpose(arg,axis_offset)        if axis_offset==None: axis_offset=int(r/2)
3106          if axis_offset<0 or axis_offset>r:
3107            raise ValueError,"axis_offset must be between 0 and %s"%r
3108          return arg._transpose(axis_offset)
3109     elif isinstance(arg,float):     elif isinstance(arg,float):
3110        if not ( axis_offset==0 or axis_offset==None):        if not ( axis_offset==0 or axis_offset==None):
3111          raise ValueError,"transpose: axis_offset must be 0 for float argument"          raise ValueError,"axis_offset must be 0 for float argument"
3112        return arg        return arg
3113     elif isinstance(arg,int):     elif isinstance(arg,int):
3114        if not ( axis_offset==0 or axis_offset==None):        if not ( axis_offset==0 or axis_offset==None):
3115          raise ValueError,"transpose: axis_offset must be 0 for int argument"          raise ValueError,"axis_offset must be 0 for int argument"
3116        return float(arg)        return float(arg)
3117     elif isinstance(arg,Symbol):     elif isinstance(arg,Symbol):
3118        if axis_offset==None: axis_offset=int(arg.getRank()/2)        if axis_offset==None: axis_offset=int(arg.getRank()/2)
3119        return Transpose_Symbol(arg,axis_offset)        return Transpose_Symbol(arg,axis_offset)
3120     else:     else:
3121        raise TypeError,"transpose: Unknown argument type."        raise TypeError,"Unknown argument type."
3122    
 def escript_transpose(arg,axis_offset): # this should be escript._transpose  
       "arg si a Data objects!!!"  
       r=arg.getRank()  
       if axis_offset<0 or axis_offset>r:  
         raise ValueError,"escript_transpose: axis_offset must be between 0 and %s"%r  
       s=arg.getShape()  
       s_out=s[axis_offset:]+s[:axis_offset]  
       out=escript.Data(0.,s_out,arg.getFunctionSpace())  
       if r==4:  
          if axis_offset==1:  
             for i0 in range(s_out[0]):  
                for i1 in range(s_out[1]):  
                   for i2 in range(s_out[2]):  
                      for i3 in range(s_out[3]):  
                          out[i0,i1,i2,i3]=arg[i3,i0,i1,i2]  
          elif axis_offset==2:  
             for i0 in range(s_out[0]):  
                for i1 in range(s_out[1]):  
                   for i2 in range(s_out[2]):  
                      for i3 in range(s_out[3]):  
                          out[i0,i1,i2,i3]=arg[i2,i3,i0,i1]  
          elif axis_offset==3:  
             for i0 in range(s_out[0]):  
                for i1 in range(s_out[1]):  
                   for i2 in range(s_out[2]):  
                      for i3 in range(s_out[3]):  
                          out[i0,i1,i2,i3]=arg[i1,i2,i3,i0]  
          else:  
             for i0 in range(s_out[0]):  
                for i1 in range(s_out[1]):  
                   for i2 in range(s_out[2]):  
                      for i3 in range(s_out[3]):  
                          out[i0,i1,i2,i3]=arg[i0,i1,i2,i3]  
       elif r==3:  
          if axis_offset==1:  
             for i0 in range(s_out[0]):  
                for i1 in range(s_out[1]):  
                   for i2 in range(s_out[2]):  
                          out[i0,i1,i2]=arg[i2,i0,i1]  
          elif axis_offset==2:  
             for i0 in range(s_out[0]):  
                for i1 in range(s_out[1]):  
                   for i2 in range(s_out[2]):  
                          out[i0,i1,i2]=arg[i1,i2,i0]  
          else:  
             for i0 in range(s_out[0]):  
                for i1 in range(s_out[1]):  
                   for i2 in range(s_out[2]):  
                          out[i0,i1,i2]=arg[i0,i1,i2]  
       elif r==2:  
          if axis_offset==1:  
             for i0 in range(s_out[0]):  
                for i1 in range(s_out[1]):  
                          out[i0,i1]=arg[i1,i0]  
          else:  
             for i0 in range(s_out[0]):  
                for i1 in range(s_out[1]):  
                          out[i0,i1]=arg[i0,i1]  
       elif r==1:  
           for i0 in range(s_out[0]):  
                out[i0]=arg[i0]  
       elif r==0:  
              out=arg+0.  
       return out  
3123  class Transpose_Symbol(DependendSymbol):  class Transpose_Symbol(DependendSymbol):
3124     """     """
3125     L{Symbol} representing the result of the transpose function     L{Symbol} representing the result of the transpose function
# Line 3216  class Transpose_Symbol(DependendSymbol): Line 3130  class Transpose_Symbol(DependendSymbol):
3130    
3131        @param arg: argument of function        @param arg: argument of function
3132        @type arg: L{Symbol}.        @type arg: L{Symbol}.
3133         @param axis_offset: the first axis_offset components are swapped with rest. If C{axis_offset} must be non-negative and less or equal the rank of arg.        @param axis_offset: the first C{axis_offset} components are swapped with rest. If C{axis_offset} must be non-negative and less or equal the rank of arg.
3134                         if axis_offset is not present C{int(r/2)} where r is the rank of arg is used.                         if C{axis_offset} is not present C{int(r/2)} where r is the rank of arg is used.
3135        @type axis_offset: C{int}        @type axis_offset: C{int}
3136        """        """
3137        if axis_offset==None: axis_offset=int(arg.getRank()/2)        if axis_offset==None: axis_offset=int(arg.getRank()/2)
3138        if axis_offset<0 or axis_offset>arg.getRank():        if axis_offset<0 or axis_offset>arg.getRank():
3139          raise ValueError,"escript_transpose: axis_offset must be between 0 and %s"%r          raise ValueError,"axis_offset must be between 0 and %s"%arg.getRank()
3140        s=arg.getShape()        s=arg.getShape()
3141        super(Transpose_Symbol,self).__init__(args=[arg,axis_offset],shape=s[axis_offset:]+s[:axis_offset],dim=arg.getDim())        super(Transpose_Symbol,self).__init__(args=[arg,axis_offset],shape=s[axis_offset:]+s[:axis_offset],dim=arg.getDim())
3142    
# Line 3236  class Transpose_Symbol(DependendSymbol): Line 3150  class Transpose_Symbol(DependendSymbol):
3150        @type format: C{str}        @type format: C{str}
3151        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
3152        @rtype: C{str}        @rtype: C{str}
3153        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
3154        """        """
3155        if format=="escript" or format=="str"  or format=="text":        if format=="escript" or format=="str"  or format=="text":
3156           return "transpose(%s,axis_offset=%s)"%(argstrs[0],argstrs[1])           return "transpose(%s,axis_offset=%s)"%(argstrs[0],argstrs[1])
# Line 3277  class Transpose_Symbol(DependendSymbol): Line 3191  class Transpose_Symbol(DependendSymbol):
3191           return identity(self.getShape())           return identity(self.getShape())
3192        else:        else:
3193           return transpose(self.getDifferentiatedArguments(arg)[0],axis_offset=self.getArgument()[1])           return transpose(self.getDifferentiatedArguments(arg)[0],axis_offset=self.getArgument()[1])
3194    
3195    def swap_axes(arg,axis0=0,axis1=1):
3196       """
3197       returns the swap of arg by swaping the components axis0 and axis1
3198    
3199       @param arg: argument
3200       @type arg: L{escript.Data}, L{Symbol}, L{numarray.NumArray}.
3201       @param axis0: axis. C{axis0} must be non-negative and less than the rank of arg.
3202       @type axis0: C{int}
3203       @param axis1: axis. C{axis1} must be non-negative and less than the rank of arg.
3204       @type axis1: C{int}
3205       @return: C{arg} with swaped components
3206       @rtype: L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.
3207       """
3208       if axis0 > axis1:
3209          axis0,axis1=axis1,axis0
3210       if isinstance(arg,numarray.NumArray):
3211          return numarray.swapaxes(arg,axis0,axis1)
3212       elif isinstance(arg,escript.Data):
3213          return arg._swap_axes(axis0,axis1)
3214       elif isinstance(arg,float):
3215          raise TyepError,"float argument is not supported."
3216       elif isinstance(arg,int):
3217          raise TyepError,"int argument is not supported."
3218       elif isinstance(arg,Symbol):
3219          return SwapAxes_Symbol(arg,axis0,axis1)
3220       else:
3221          raise TypeError,"Unknown argument type."
3222    
3223    class SwapAxes_Symbol(DependendSymbol):
3224       """
3225       L{Symbol} representing the result of the swap function
3226       """
3227       def __init__(self,arg,axis0=0,axis1=1):
3228          """
3229          initialization of swap L{Symbol} with argument arg
3230    
3231          @param arg: argument
3232          @type arg: L{Symbol}.
3233          @param axis0: axis. C{axis0} must be non-negative and less than the rank of arg.
3234          @type axis0: C{int}
3235          @param axis1: axis. C{axis1} must be non-negative and less than the rank of arg.
3236          @type axis1: C{int}
3237          """
3238          if arg.getRank()<2:
3239             raise ValueError,"argument must have at least rank 2."
3240          if axis0<0 or axis0>arg.getRank()-1:
3241             raise ValueError,"axis0 must be between 0 and %s"%arg.getRank()-1
3242          if axis1<0 or axis1>arg.getRank()-1:
3243             raise ValueError,"axis1 must be between 0 and %s"%arg.getRank()-1
3244          if axis0 == axis1:
3245             raise ValueError,"axis indices must be different."
3246          if axis0 > axis1:
3247             axis0,axis1=axis1,axis0
3248          s=arg.getShape()
3249          s_out=[]
3250          for i in range(len(s)):
3251             if i == axis0:
3252                s_out.append(s[axis1])
3253             elif i == axis1:
3254                s_out.append(s[axis0])
3255             else:
3256                s_out.append(s[i])
3257          super(SwapAxes_Symbol,self).__init__(args=[arg,axis0,axis1],shape=tuple(s_out),dim=arg.getDim())
3258    
3259       def getMyCode(self,argstrs,format="escript"):
3260          """
3261          returns a program code that can be used to evaluate the symbol.
3262    
3263          @param argstrs: gives for each argument a string representing the argument for the evaluation.
3264          @type argstrs: C{str} or a C{list} of length 1 of C{str}.
3265          @param format: specifies the format to be used. At the moment only "escript" ,"text" and "str" are supported.
3266          @type format: C{str}
3267          @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
3268          @rtype: C{str}
3269          @raise NotImplementedError: if the requested format is not available
3270          """
3271          if format=="escript" or format=="str"  or format=="text":
3272             return "swap(%s,axis_offset=%s)"%(argstrs[0],argstrs[1])
3273          else:
3274             raise NotImplementedError,"SwapAxes_Symbol does not provide program code for format %s."%format
3275    
3276       def substitute(self,argvals):
3277          """
3278          assigns new values to symbols in the definition of the symbol.
3279          The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.
3280    
3281          @param argvals: new values assigned to symbols
3282          @type argvals: C{dict} with keywords of type L{Symbol}.
3283          @return: result of the substitution process. Operations are executed as much as possible.
3284          @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution
3285          @raise TypeError: if a value for a L{Symbol} cannot be substituted.
3286          """
3287          if argvals.has_key(self):
3288             arg=argvals[self]
3289             if self.isAppropriateValue(arg):
3290                return arg
3291             else:
3292                raise TypeError,"%s: new value is not appropriate."%str(self)
3293          else:
3294             arg=self.getSubstitutedArguments(argvals)
3295             return swap_axes(arg[0],axis0=arg[1],axis1=arg[2])
3296    
3297       def diff(self,arg):
3298          """
3299          differential of this object
3300    
3301          @param arg: the derivative is calculated with respect to arg
3302          @type arg: L{escript.Symbol}
3303          @return: derivative with respect to C{arg}
3304          @rtype: typically L{Symbol} but other types such as C{float}, L{escript.Data}, L{numarray.NumArray}  are possible.
3305          """
3306          if arg==self:
3307             return identity(self.getShape())
3308          else:
3309             return swap_axes(self.getDifferentiatedArguments(arg)[0],axis0=self.getArgument()[1],axis1=self.getArgument()[2])
3310    
3311  def symmetric(arg):  def symmetric(arg):
3312      """      """
3313      returns the symmetric part of the square matrix arg. This is (arg+transpose(arg))/2      returns the symmetric part of the square matrix arg. This is (arg+transpose(arg))/2
# Line 3289  def symmetric(arg): Line 3320  def symmetric(arg):
3320      if isinstance(arg,numarray.NumArray):      if isinstance(arg,numarray.NumArray):
3321        if arg.rank==2:        if arg.rank==2:
3322          if not (arg.shape[0]==arg.shape[1]):          if not (arg.shape[0]==arg.shape[1]):
3323             raise ValueError,"symmetric: argument must be square."             raise ValueError,"argument must be square."
3324        elif arg.rank==4:        elif arg.rank==4:
3325          if not (arg.shape[0]==arg.shape[2] and arg.shape[1]==arg.shape[3]):          if not (arg.shape[0]==arg.shape[2] and arg.shape[1]==arg.shape[3]):
3326             raise ValueError,"symmetric: argument must be square."             raise ValueError,"argument must be square."
3327        else:        else:
3328          raise ValueError,"symmetric: rank 2 or 4 is required."          raise ValueError,"rank 2 or 4 is required."
3329        return (arg+transpose(arg))/2        return (arg+transpose(arg))/2
3330      elif isinstance(arg,escript.Data):      elif isinstance(arg,escript.Data):
3331        return escript_symmetric(arg)        if arg.getRank()==2:
3332            if not (arg.getShape()[0]==arg.getShape()[1]):
3333               raise ValueError,"argument must be square."
3334            return arg._symmetric()
3335          elif arg.getRank()==4:
3336            if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):
3337               raise ValueError,"argument must be square."
3338            return arg._symmetric()
3339          else:
3340            raise ValueError,"rank 2 or 4 is required."
3341      elif isinstance(arg,float):      elif isinstance(arg,float):
3342        return arg        return arg
3343      elif isinstance(arg,int):      elif isinstance(arg,int):
# Line 3305  def symmetric(arg): Line 3345  def symmetric(arg):
3345      elif isinstance(arg,Symbol):      elif isinstance(arg,Symbol):
3346        if arg.getRank()==2:        if arg.getRank()==2:
3347          if not (arg.getShape()[0]==arg.getShape()[1]):          if not (arg.getShape()[0]==arg.getShape()[1]):
3348             raise ValueError,"symmetric: argument must be square."             raise ValueError,"argument must be square."
3349        elif arg.getRank()==4:        elif arg.getRank()==4:
3350          if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):          if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):
3351             raise ValueError,"symmetric: argument must be square."             raise ValueError,"argument must be square."
3352        else:        else:
3353          raise ValueError,"symmetric: rank 2 or 4 is required."          raise ValueError,"rank 2 or 4 is required."
3354        return (arg+transpose(arg))/2        return (arg+transpose(arg))/2
3355      else:      else:
3356        raise TypeError,"symmetric: Unknown argument type."        raise TypeError,"symmetric: Unknown argument type."
3357    
 def escript_symmetric(arg): # this should be implemented in c++  
       if arg.getRank()==2:  
         if not (arg.getShape()[0]==arg.getShape()[1]):  
            raise ValueError,"escript_symmetric: argument must be square."  
         out=escript.Data(0.,arg.getShape(),arg.getFunctionSpace())  
         for i0 in range(arg.getShape()[0]):  
            for i1 in range(arg.getShape()[1]):  
               out[i0,i1]=(arg[i0,i1]+arg[i1,i0])/2.  
       elif arg.getRank()==4:  
         if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):  
            raise ValueError,"escript_symmetric: argument must be square."  
         out=escript.Data(0.,arg.getShape(),arg.getFunctionSpace())  
         for i0 in range(arg.getShape()[0]):  
            for i1 in range(arg.getShape()[1]):  
               for i2 in range(arg.getShape()[2]):  
                  for i3 in range(arg.getShape()[3]):  
                      out[i0,i1,i2,i3]=(arg[i0,i1,i2,i3]+arg[i2,i3,i0,i1])/2.  
       else:  
         raise ValueError,"escript_symmetric: rank 2 or 4 is required."  
       return out  
   
3358  def nonsymmetric(arg):  def nonsymmetric(arg):
3359      """      """
3360      returns the nonsymmetric part of the square matrix arg. This is (arg-transpose(arg))/2      returns the nonsymmetric part of the square matrix arg. This is (arg-transpose(arg))/2
# Line 3356  def nonsymmetric(arg): Line 3375  def nonsymmetric(arg):
3375          raise ValueError,"nonsymmetric: rank 2 or 4 is required."          raise ValueError,"nonsymmetric: rank 2 or 4 is required."
3376        return (arg-transpose(arg))/2        return (arg-transpose(arg))/2
3377      elif isinstance(arg,escript.Data):      elif isinstance(arg,escript.Data):
3378        return escript_nonsymmetric(arg)        if arg.getRank()==2:
3379            if not (arg.getShape()[0]==arg.getShape()[1]):
3380               raise ValueError,"argument must be square."
3381            return arg._nonsymmetric()
3382          elif arg.getRank()==4:
3383            if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):
3384               raise ValueError,"argument must be square."
3385            return arg._nonsymmetric()
3386          else:
3387            raise ValueError,"rank 2 or 4 is required."
3388      elif isinstance(arg,float):      elif isinstance(arg,float):
3389        return arg        return arg
3390      elif isinstance(arg,int):      elif isinstance(arg,int):
# Line 3374  def nonsymmetric(arg): Line 3402  def nonsymmetric(arg):
3402      else:      else:
3403        raise TypeError,"nonsymmetric: Unknown argument type."        raise TypeError,"nonsymmetric: Unknown argument type."
3404    
 def escript_nonsymmetric(arg): # this should be implemented in c++  
       if arg.getRank()==2:  
         if not (arg.getShape()[0]==arg.getShape()[1]):  
            raise ValueError,"escript_nonsymmetric: argument must be square."  
         out=escript.Data(0.,arg.getShape(),arg.getFunctionSpace())  
         for i0 in range(arg.getShape()[0]):  
            for i1 in range(arg.getShape()[1]):  
               out[i0,i1]=(arg[i0,i1]-arg[i1,i0])/2.  
       elif arg.getRank()==4:  
         if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):  
            raise ValueError,"escript_nonsymmetric: argument must be square."  
         out=escript.Data(0.,arg.getShape(),arg.getFunctionSpace())  
         for i0 in range(arg.getShape()[0]):  
            for i1 in range(arg.getShape()[1]):  
               for i2 in range(arg.getShape()[2]):  
                  for i3 in range(arg.getShape()[3]):  
                      out[i0,i1,i2,i3]=(arg[i0,i1,i2,i3]-arg[i2,i3,i0,i1])/2.  
       else:  
         raise ValueError,"escript_nonsymmetric: rank 2 or 4 is required."  
       return out  
   
   
3405  def inverse(arg):  def inverse(arg):
3406      """      """
3407      returns the inverse of the square matrix arg.      returns the inverse of the square matrix arg.
3408    
3409      @param arg: square matrix. Must have rank 2 and the first and second dimension must be equal.      @param arg: square matrix. Must have rank 2 and the first and second dimension must be equal.
3410      @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}      @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
3411      @return: inverse arg_inv of the argument. It will be matrixmul(inverse(arg),arg) almost equal to kronecker(arg.getShape()[0])      @return: inverse arg_inv of the argument. It will be matrix_mult(inverse(arg),arg) almost equal to kronecker(arg.getShape()[0])
3412      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
3413      @remark: for L{escript.Data} objects the dimension is restricted to 3.      @note: for L{escript.Data} objects the dimension is restricted to 3.
3414      """      """
3415        import numarray.linear_algebra # This statement should be after the next statement but then somehow numarray is gone.
3416      if isinstance(arg,numarray.NumArray):      if isinstance(arg,numarray.NumArray):
3417        return numarray.linear_algebra.inverse(arg)        return numarray.linear_algebra.inverse(arg)
3418      elif isinstance(arg,escript.Data):      elif isinstance(arg,escript.Data):
# Line 3498  class Inverse_Symbol(DependendSymbol): Line 3505  class Inverse_Symbol(DependendSymbol):
3505        @type format: C{str}        @type format: C{str}
3506        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
3507        @rtype: C{str}        @rtype: C{str}
3508        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
3509        """        """
3510        if format=="escript" or format=="str"  or format=="text":        if format=="escript" or format=="str"  or format=="text":
3511           return "inverse(%s)"%argstrs[0]           return "inverse(%s)"%argstrs[0]
# Line 3538  class Inverse_Symbol(DependendSymbol): Line 3545  class Inverse_Symbol(DependendSymbol):
3545        if arg==self:        if arg==self:
3546           return identity(self.getShape())           return identity(self.getShape())
3547        else:        else:
3548           return -matrixmult(matrixmult(self,self.getDifferentiatedArguments(arg)[0]),self)           return -matrix_mult(matrix_mult(self,self.getDifferentiatedArguments(arg)[0]),self)
3549    
3550  def eigenvalues(arg):  def eigenvalues(arg):
3551      """      """
# Line 3549  def eigenvalues(arg): Line 3556  def eigenvalues(arg):
3556      @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}      @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
3557      @return: the eigenvalues in increasing order.      @return: the eigenvalues in increasing order.
3558      @rtype: L{numarray.NumArray},L{escript.Data}, L{Symbol} depending on the input.      @rtype: L{numarray.NumArray},L{escript.Data}, L{Symbol} depending on the input.
3559      @remark: for L{escript.Data} and L{Symbol} objects the dimension is restricted to 3.      @note: for L{escript.Data} and L{Symbol} objects the dimension is restricted to 3.
3560      """      """
3561      if isinstance(arg,numarray.NumArray):      if isinstance(arg,numarray.NumArray):
3562        out=numarray.linear_algebra.eigenvalues((arg+numarray.transpose(arg))/2.)        out=numarray.linear_algebra.eigenvalues((arg+numarray.transpose(arg))/2.)
# Line 3619  def eigenvalues_and_eigenvectors(arg): Line 3626  def eigenvalues_and_eigenvectors(arg):
3626               eigenvectors are orthogonal and normalized. If V are the eigenvectors than V[:,i] is               eigenvectors are orthogonal and normalized. If V are the eigenvectors than V[:,i] is
3627               the eigenvector coresponding to the i-th eigenvalue.               the eigenvector coresponding to the i-th eigenvalue.
3628      @rtype: L{tuple} of L{escript.Data}.      @rtype: L{tuple} of L{escript.Data}.
3629      @remark: The dimension is restricted to 3.      @note: The dimension is restricted to 3.
3630      """      """
3631      if isinstance(arg,numarray.NumArray):      if isinstance(arg,numarray.NumArray):
3632        raise TypeError,"eigenvalues_and_eigenvectors is not supporting numarray arguments"        raise TypeError,"eigenvalues_and_eigenvectors is not supporting numarray arguments"
# Line 3676  class Add_Symbol(DependendSymbol): Line 3683  class Add_Symbol(DependendSymbol):
3683         @raise ValueError: if both arguments do not have the same shape.         @raise ValueError: if both arguments do not have the same shape.
3684         @note: if both arguments have a spatial dimension, they must equal.         @note: if both arguments have a spatial dimension, they must equal.
3685         """         """
3686         sh0=pokeShape(arg0)         sh0=getShape(arg0)
3687         sh1=pokeShape(arg1)         sh1=getShape(arg1)
3688         if not sh0==sh1:         if not sh0==sh1:
3689            raise ValueError,"Add_Symbol: shape of arguments must match"            raise ValueError,"Add_Symbol: shape of arguments must match"
3690         DependendSymbol.__init__(self,dim=commonDim(arg0,arg1),shape=sh0,args=[arg0,arg1])         DependendSymbol.__init__(self,dim=commonDim(arg0,arg1),shape=sh0,args=[arg0,arg1])
# Line 3692  class Add_Symbol(DependendSymbol): Line 3699  class Add_Symbol(DependendSymbol):
3699        @type format: C{str}        @type format: C{str}
3700        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
3701        @rtype: C{str}        @rtype: C{str}
3702        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
3703        """        """
3704        if format=="str" or format=="text":        if format=="str" or format=="text":
3705           return "(%s)+(%s)"%(argstrs[0],argstrs[1])           return "(%s)+(%s)"%(argstrs[0],argstrs[1])
# Line 3751  def mult(arg0,arg1): Line 3758  def mult(arg0,arg1):
3758         """         """
3759         args=matchShape(arg0,arg1)         args=matchShape(arg0,arg1)
3760         if testForZero(args[0]) or testForZero(args[1]):         if testForZero(args[0]) or testForZero(args[1]):
3761            return numarray.zeros(pokeShape(args[0]),numarray.Float64)            return numarray.zeros(getShape(args[0]),numarray.Float64)
3762         else:         else:
3763            if isinstance(args[0],Symbol) or isinstance(args[1],Symbol) :            if isinstance(args[0],Symbol) or isinstance(args[1],Symbol) :
3764                return Mult_Symbol(args[0],args[1])                return Mult_Symbol(args[0],args[1])
# Line 3775  class Mult_Symbol(DependendSymbol): Line 3782  class Mult_Symbol(DependendSymbol):
3782         @raise ValueError: if both arguments do not have the same shape.         @raise ValueError: if both arguments do not have the same shape.
3783         @note: if both arguments have a spatial dimension, they must equal.         @note: if both arguments have a spatial dimension, they must equal.
3784         """         """
3785         sh0=pokeShape(arg0)         sh0=getShape(arg0)
3786         sh1=pokeShape(arg1)         sh1=getShape(arg1)
3787         if not sh0==sh1:         if not sh0==sh1:
3788            raise ValueError,"Mult_Symbol: shape of arguments must match"            raise ValueError,"Mult_Symbol: shape of arguments must match"
3789         DependendSymbol.__init__(self,dim=commonDim(arg0,arg1),shape=sh0,args=[arg0,arg1])         DependendSymbol.__init__(self,dim=commonDim(arg0,arg1),shape=sh0,args=[arg0,arg1])
# Line 3791  class Mult_Symbol(DependendSymbol): Line 3798  class Mult_Symbol(DependendSymbol):
3798        @type format: C{str}        @type format: C{str}
3799        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
3800        @rtype: C{str}        @rtype: C{str}
3801        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
3802        """        """
3803        if format=="str" or format=="text":        if format=="str" or format=="text":
3804           return "(%s)*(%s)"%(argstrs[0],argstrs[1])           return "(%s)*(%s)"%(argstrs[0],argstrs[1])
# Line 3851  def quotient(arg0,arg1): Line 3858  def quotient(arg0,arg1):
3858         """         """
3859         args=matchShape(arg0,arg1)         args=matchShape(arg0,arg1)
3860         if testForZero(args[0]):         if testForZero(args[0]):
3861            return numarray.zeros(pokeShape(args[0]),numarray.Float64)            return numarray.zeros(getShape(args[0]),numarray.Float64)
3862         elif isinstance(args[0],Symbol):         elif isinstance(args[0],Symbol):
3863            if isinstance(args[1],Symbol):            if isinstance(args[1],Symbol):
3864               return Quotient_Symbol(args[0],args[1])               return Quotient_Symbol(args[0],args[1])
# Line 3880  class Quotient_Symbol(DependendSymbol): Line 3887  class Quotient_Symbol(DependendSymbol):
3887         @raise ValueError: if both arguments do not have the same shape.         @raise ValueError: if both arguments do not have the same shape.
3888         @note: if both arguments have a spatial dimension, they must equal.         @note: if both arguments have a spatial dimension, they must equal.
3889         """         """
3890         sh0=pokeShape(arg0)         sh0=getShape(arg0)
3891         sh1=pokeShape(arg1)         sh1=getShape(arg1)
3892         if not sh0==sh1:         if not sh0==sh1:
3893            raise ValueError,"Quotient_Symbol: shape of arguments must match"            raise ValueError,"Quotient_Symbol: shape of arguments must match"
3894         DependendSymbol.__init__(self,dim=commonDim(arg0,arg1),shape=sh0,args=[arg0,arg1])         DependendSymbol.__init__(self,dim=commonDim(arg0,arg1),shape=sh0,args=[arg0,arg1])
# Line 3896  class Quotient_Symbol(DependendSymbol): Line 3903  class Quotient_Symbol(DependendSymbol):
3903        @type format: C{str}        @type format: C{str}
3904        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
3905        @rtype: C{str}        @rtype: C{str}
3906        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
3907        """        """
3908        if format=="str" or format=="text":        if format=="str" or format=="text":
3909           return "(%s)/(%s)"%(argstrs[0],argstrs[1])           return "(%s)/(%s)"%(argstrs[0],argstrs[1])
# Line 3957  def power(arg0,arg1): Line 3964  def power(arg0,arg1):
3964         """         """
3965         args=matchShape(arg0,arg1)         args=matchShape(arg0,arg1)
3966         if testForZero(args[0]):         if testForZero(args[0]):
3967            return numarray.zeros(pokeShape(args[0]),numarray.Float64)            return numarray.zeros(getShape(args[0]),numarray.Float64)
3968         elif testForZero(args[1]):         elif testForZero(args[1]):
3969            return numarray.ones(pokeShape(args[1]),numarray.Float64)            return numarray.ones(getShape(args[1]),numarray.Float64)
3970         elif isinstance(args[0],Symbol) or isinstance(args[1],Symbol):         elif isinstance(args[0],Symbol) or isinstance(args[1],Symbol):
3971            return Power_Symbol(args[0],args[1])            return Power_Symbol(args[0],args[1])
3972         elif isinstance(args[0],numarray.NumArray) and not isinstance(args[1],numarray.NumArray):         elif isinstance(args[0],numarray.NumArray) and not isinstance(args[1],numarray.NumArray):
# Line 3982  class Power_Symbol(DependendSymbol): Line 3989  class Power_Symbol(DependendSymbol):
3989         @raise ValueError: if both arguments do not have the same shape.         @raise ValueError: if both arguments do not have the same shape.
3990         @note: if both arguments have a spatial dimension, they must equal.         @note: if both arguments have a spatial dimension, they must equal.
3991         """         """
3992         sh0=pokeShape(arg0)         sh0=getShape(arg0)
3993         sh1=pokeShape(arg1)         sh1=getShape(arg1)
3994         if not sh0==sh1:         if not sh0==sh1:
3995            raise ValueError,"Power_Symbol: shape of arguments must match"            raise ValueError,"Power_Symbol: shape of arguments must match"
3996         d0=pokeDim(arg0)         d0=pokeDim(arg0)
# Line 4000  class Power_Symbol(DependendSymbol): Line 4007  class Power_Symbol(DependendSymbol):
4007        @type format: C{str}        @type format: C{str}
4008        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
4009        @rtype: C{str}        @rtype: C{str}
4010        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
4011        """        """
4012        if format=="escript" or format=="str" or format=="text":        if format=="escript" or format=="str" or format=="text":
4013           return "(%s)**(%s)"%(argstrs[0],argstrs[1])           return "(%s)**(%s)"%(argstrs[0],argstrs[1])
# Line 4082  def minimum(*args): Line 4089  def minimum(*args):
4089            out=add(out,mult(whereNegative(diff),diff))            out=add(out,mult(whereNegative(diff),diff))
4090      return out      return out
4091    
4092  def clip(arg,minval=0.,maxval=1.):  def clip(arg,minval=None,maxval=None):
4093      """      """
4094      cuts the values of arg between minval and maxval      cuts the values of arg between minval and maxval
4095    
4096      @param arg: argument      @param arg: argument
4097      @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}
4098      @param minval: lower range      @param minval: lower range. If None no lower range is applied
4099      @type arg: C{float}      @type minval: C{float} or C{None}
4100      @param maxval: upper range      @param maxval: upper range. If None no upper range is applied
4101      @type arg: C{float}      @type maxval: C{float} or C{None}
4102      @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
4103               less then maxval are unchanged.               less then maxval are unchanged.
4104      @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
4105      @raise ValueError: if minval>maxval      @raise ValueError: if minval>maxval
4106      """      """
4107      if minval>maxval:      if not minval==None and not maxval==None:
4108         raise ValueError,"minval = %s must be less then maxval %s"%(minval,maxval)         if minval>maxval:
4109      return minimum(maximum(minval,arg),maxval)            raise ValueError,"minval = %s must be less then maxval %s"%(minval,maxval)
4110        if minval == None:
4111            tmp=arg
4112        else:
4113            tmp=maximum(minval,arg)
4114        if maxval == None:
4115            return tmp
4116        else:
4117            return minimum(tmp,maxval)
4118    
4119        
4120  def inner(arg0,arg1):  def inner(arg0,arg1):
# Line 4116  def inner(arg0,arg1): Line 4131  def inner(arg0,arg1):
4131      @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}      @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4132      @param arg1: second argument      @param arg1: second argument
4133      @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}      @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4134      @return : the inner product of arg0 and arg1 at each data point      @return: the inner product of arg0 and arg1 at each data point
4135      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float} depending on the input      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float} depending on the input
4136      @raise ValueError: if the shapes of the arguments are not identical      @raise ValueError: if the shapes of the arguments are not identical
4137      """      """
4138      sh0=pokeShape(arg0)      sh0=getShape(arg0)
4139      sh1=pokeShape(arg1)      sh1=getShape(arg1)
4140      if not sh0==sh1:      if not sh0==sh1:
4141          raise ValueError,"inner: shape of arguments does not match"          raise ValueError,"inner: shape of arguments does not match"
4142      return generalTensorProduct(arg0,arg1,axis_offset=len(sh0))      return generalTensorProduct(arg0,arg1,axis_offset=len(sh0))
4143    
4144    def outer(arg0,arg1):
4145        """
4146        the outer product of the two argument:
4147    
4148        out[t,s]=arg0[t]*arg1[s]
4149    
4150        where
4151    
4152            - s runs through arg0.Shape
4153            - t runs through arg1.Shape
4154    
4155        @param arg0: first argument
4156        @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4157        @param arg1: second argument
4158        @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4159        @return: the outer product of arg0 and arg1 at each data point
4160        @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
4161        """
4162        return generalTensorProduct(arg0,arg1,axis_offset=0)
4163    
4164  def matrixmult(arg0,arg1):  def matrixmult(arg0,arg1):
4165      """      """
4166        see L{matrix_mult}
4167        """
4168        return matrix_mult(arg0,arg1)
4169    
4170    def matrix_mult(arg0,arg1):
4171        """
4172      matrix-matrix or matrix-vector product of the two argument:      matrix-matrix or matrix-vector product of the two argument:
4173    
4174      out[s0]=S{Sigma}_{r0} arg0[s0,r0]*arg1[r0]      out[s0]=S{Sigma}_{r0} arg0[s0,r0]*arg1[r0]
4175    
4176            or      or
4177    
4178      out[s0,s1]=S{Sigma}_{r0} arg0[s0,r0]*arg1[r0,s1]      out[s0,s1]=S{Sigma}_{r0} arg0[s0,r0]*arg1[r0,s1]
4179    
4180      The second dimension of arg0 and the length of arg1 must match.      The second dimension of arg0 and the first dimension of arg1 must match.
4181    
4182      @param arg0: first argument of rank 2      @param arg0: first argument of rank 2
4183      @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}      @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
# Line 4146  def matrixmult(arg0,arg1): Line 4187  def matrixmult(arg0,arg1):
4187      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
4188      @raise ValueError: if the shapes of the arguments are not appropriate      @raise ValueError: if the shapes of the arguments are not appropriate
4189      """      """
4190      sh0=pokeShape(arg0)      sh0=getShape(arg0)
4191      sh1=pokeShape(arg1)      sh1=getShape(arg1)
4192      if not len(sh0)==2 :      if not len(sh0)==2 :
4193          raise ValueError,"first argument must have rank 2"          raise ValueError,"first argument must have rank 2"
4194      if not len(sh1)==2 and not len(sh1)==1:      if not len(sh1)==2 and not len(sh1)==1:
4195          raise ValueError,"second argument must have rank 1 or 2"          raise ValueError,"second argument must have rank 1 or 2"
4196      return generalTensorProduct(arg0,arg1,axis_offset=1)      return generalTensorProduct(arg0,arg1,axis_offset=1)
4197    
4198  def outer(arg0,arg1):  def tensormult(arg0,arg1):
4199      """      """
4200      the outer product of the two argument:      use L{tensor_mult}
   
     out[t,s]=arg0[t]*arg1[s]  
   
     where s runs through arg0.Shape  
           t runs through arg1.Shape  
   
     @param arg0: first argument  
     @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}  
     @param arg1: second argument  
     @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}  
     @return: the outer product of arg0 and arg1 at each data point  
     @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input  
4201      """      """
4202      return generalTensorProduct(arg0,arg1,axis_offset=0)      return tensor_mult(arg0,arg1)
   
4203    
4204  def tensormult(arg0,arg1):  def tensor_mult(arg0,arg1):
4205      """      """
4206      the tensor product of the two argument:      the tensor product of the two argument:
   
4207            
4208      for arg0 of rank 2 this is      for arg0 of rank 2 this is
4209    
4210      out[s0]=S{Sigma}_{r0} arg0[s0,r0]*arg1[r0]        out[s0]=S{Sigma}_{r0} arg0[s0,r0]*arg1[r0]  
4211    
4212                   or      or
4213    
4214      out[s0,s1]=S{Sigma}_{r0} arg0[s0,r0]*arg1[r0,s1]      out[s0,s1]=S{Sigma}_{r0} arg0[s0,r0]*arg1[r0,s1]
4215    
# Line 4191  def tensormult(arg0,arg1): Line 4218  def tensormult(arg0,arg1):
4218    
4219      out[s0,s1,s2,s3]=S{Sigma}_{r0,r1} arg0[s0,s1,r0,r1]*arg1[r0,r1,s2,s3]      out[s0,s1,s2,s3]=S{Sigma}_{r0,r1} arg0[s0,s1,r0,r1]*arg1[r0,r1,s2,s3]
4220                                
4221                   or      or
4222    
4223      out[s0,s1,s2]=S{Sigma}_{r0,r1} arg0[s0,s1,r0,r1]*arg1[r0,r1,s2]      out[s0,s1,s2]=S{Sigma}_{r0,r1} arg0[s0,s1,r0,r1]*arg1[r0,r1,s2]
4224    
4225                   or      or
4226    
4227      out[s0,s1]=S{Sigma}_{r0,r1} arg0[s0,s1,r0,r1]*arg1[r0,r1]      out[s0,s1]=S{Sigma}_{r0,r1} arg0[s0,s1,r0,r1]*arg1[r0,r1]
4228    
4229      In the first case the the second dimension of arg0 and the length of arg1 must match and        In the first case the the second dimension of arg0 and the last dimension of arg1 must match and  
4230      in the second case the two last dimensions of arg0 must match the shape of arg1.      in the second case the two last dimensions of arg0 must match the two first dimensions of arg1.
4231    
4232      @param arg0: first argument of rank 2 or 4      @param arg0: first argument of rank 2 or 4
4233      @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}      @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
# Line 4209  def tensormult(arg0,arg1): Line 4236  def tensormult(arg0,arg1):
4236      @return: the tensor product of arg0 and arg1 at each data point      @return: the tensor product of arg0 and arg1 at each data point
4237      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
4238      """      """
4239      sh0=pokeShape(arg0)      sh0=getShape(arg0)
4240      sh1=pokeShape(arg1)      sh1=getShape(arg1)
4241      if len(sh0)==2 and ( len(sh1)==2 or len(sh1)==1 ):      if len(sh0)==2 and ( len(sh1)==2 or len(sh1)==1 ):
4242         return generalTensorProduct(arg0,arg1,axis_offset=1)         return generalTensorProduct(arg0,arg1,axis_offset=1)
4243      elif len(sh0)==4 and (len(sh1)==2 or len(sh1)==3 or len(sh1)==4):      elif len(sh0)==4 and (len(sh1)==2 or len(sh1)==3 or len(sh1)==4):
4244         return generalTensorProduct(arg0,arg1,axis_offset=2)         return generalTensorProduct(arg0,arg1,axis_offset=2)
4245      else:      else:
4246          raise ValueError,"tensormult: first argument must have rank 2 or 4"          raise ValueError,"tensor_mult: first argument must have rank 2 or 4"
4247    
4248  def generalTensorProduct(arg0,arg1,axis_offset=0):  def generalTensorProduct(arg0,arg1,axis_offset=0):
4249      """      """
# Line 4224  def generalTensorProduct(arg0,arg1,axis_ Line 4251  def generalTensorProduct(arg0,arg1,axis_
4251    
4252      out[s,t]=S{Sigma}_r arg0[s,r]*arg1[r,t]      out[s,t]=S{Sigma}_r arg0[s,r]*arg1[r,t]
4253    
4254      where s runs through arg0.Shape[:arg0.Rank-axis_offset]      where
           r runs trough arg0.Shape[:axis_offset]  
           t runs through arg1.Shape[axis_offset:]  
4255    
4256      In the first case the the second dimension of arg0 and the length of arg1 must match and            - s runs through arg0.Shape[:arg0.Rank-axis_offset]
4257      in the second case the two last dimensions of arg0 must match the shape of arg1.          - r runs trough arg0.Shape[:axis_offset]
4258            - t runs through arg1.Shape[axis_offset:]
4259    
4260      @param arg0: first argument      @param arg0: first argument
4261      @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}      @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4262      @param arg1: second argument of shape greater of 1 or 2 depending on rank of arg0      @param arg1: second argument
4263      @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}      @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4264      @return: the general tensor product of arg0 and arg1 at each data point.      @return: the general tensor product of arg0 and arg1 at each data point.
4265      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
# Line 4246  def generalTensorProduct(arg0,arg1,axis_ Line 4272  def generalTensorProduct(arg0,arg1,axis_
4272             return GeneralTensorProduct_Symbol(arg0,arg1,axis_offset)             return GeneralTensorProduct_Symbol(arg0,arg1,axis_offset)
4273         else:         else:
4274             if not arg0.shape[arg0.rank-axis_offset:]==arg1.shape[:axis_offset]:             if not arg0.shape[arg0.rank-axis_offset:]==arg1.shape[:axis_offset]:
4275                 raise ValueError,"generalTensorProduct: dimensions of last %s components in left argument don't match the first %s components in the right argument."%(axis_offset,axis_offset)                 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)
4276             arg0_c=arg0.copy()             arg0_c=arg0.copy()
4277             arg1_c=arg1.copy()             arg1_c=arg1.copy()
4278             sh0,sh1=arg0.shape,arg1.shape             sh0,sh1=arg0.shape,arg1.shape
# Line 4272  def generalTensorProduct(arg0,arg1,axis_ Line 4298  def generalTensorProduct(arg0,arg1,axis_
4298                                    
4299  class GeneralTensorProduct_Symbol(DependendSymbol):  class GeneralTensorProduct_Symbol(DependendSymbol):
4300     """     """
4301     Symbol representing the quotient of two arguments.     Symbol representing the general tensor product of two arguments
4302     """     """
4303     def __init__(self,arg0,arg1,axis_offset=0):     def __init__(self,arg0,arg1,axis_offset=0):
4304         """         """
4305         initialization of L{Symbol} representing the quotient of two arguments         initialization of L{Symbol} representing the general tensor product of two arguments.
4306    
4307         @param arg0: numerator         @param arg0: first argument
4308         @type arg0: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type arg0: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
4309         @param arg1: denominator         @param arg1: second argument
4310         @type arg1: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.         @type arg1: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
4311         @raise ValueError: if both arguments do not have the same shape.         @raise ValueError: illegal dimension
4312         @note: if both arguments have a spatial dimension, they must equal.         @note: if both arguments have a spatial dimension, they must equal.
4313         """         """
4314         sh_arg0=pokeShape(arg0)         sh_arg0=getShape(arg0)
4315         sh_arg1=pokeShape(arg1)         sh_arg1=getShape(arg1)
4316         sh0=sh_arg0[:len(sh_arg0)-axis_offset]         sh0=sh_arg0[:len(sh_arg0)-axis_offset]
4317         sh01=sh_arg0[len(sh_arg0)-axis_offset:]         sh01=sh_arg0[len(sh_arg0)-axis_offset:]
4318         sh10=sh_arg1[:axis_offset]         sh10=sh_arg1[:axis_offset]
# Line 4305  class GeneralTensorProduct_Symbol(Depend Line 4331  class GeneralTensorProduct_Symbol(Depend
4331        @type format: C{str}        @type format: C{str}
4332        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
4333        @rtype: C{str}        @rtype: C{str}
4334        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
4335        """        """
4336        if format=="escript" or format=="str" or format=="text":        if format=="escript" or format=="str" or format=="text":
4337           return "generalTensorProduct(%s,%s,axis_offset=%s)"%(argstrs[0],argstrs[1],argstrs[2])           return "generalTensorProduct(%s,%s,axis_offset=%s)"%(argstrs[0],argstrs[1],argstrs[2])
# Line 4333  class GeneralTensorProduct_Symbol(Depend Line 4359  class GeneralTensorProduct_Symbol(Depend
4359           args=self.getSubstitutedArguments(argvals)           args=self.getSubstitutedArguments(argvals)
4360           return generalTensorProduct(args[0],args[1],args[2])           return generalTensorProduct(args[0],args[1],args[2])
4361    
4362  def escript_generalTensorProduct(arg0,arg1,axis_offset): # this should be escript._generalTensorProduct  def escript_generalTensorProduct(arg0,arg1,axis_offset,transpose=0):
4363      "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!!!"
4364      # calculate the return shape:      return C_GeneralTensorProduct(arg0, arg1, axis_offset, transpose)
4365      shape0=arg0.getShape()[:arg0.getRank()-axis_offset]  
4366      shape01=arg0.getShape()[arg0.getRank()-axis_offset:]  def transposed_matrix_mult(arg0,arg1):
4367      shape10=arg1.getShape()[:axis_offset]      """
4368      shape1=arg1.getShape()[axis_offset:]      transposed(matrix)-matrix or transposed(matrix)-vector product of the two argument:
4369      if not shape01==shape10:  
4370          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)      out[s0]=S{Sigma}_{r0} arg0[r0,s0]*arg1[r0]
4371    
4372      # whatr function space should be used? (this here is not good!)      or
4373      fs=(escript.Scalar(0.,arg0.getFunctionSpace())+escript.Scalar(0.,arg1.getFunctionSpace())).getFunctionSpace()  
4374      # create return value:      out[s0,s1]=S{Sigma}_{r0} arg0[r0,s0]*arg1[r0,s1]
4375      out=escript.Data(0.,tuple(shape0+shape1),fs)  
4376      #      The function call transposed_matrix_mult(arg0,arg1) is equivalent to matrix_mult(transpose(arg0),arg1).
4377      s0=[[]]  
4378      for k in shape0:      The first dimension of arg0 and arg1 must match.
4379            s=[]  
4380            for j in s0:      @param arg0: first argument of rank 2
4381                  for i in range(k): s.append(j+[slice(i,i)])      @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
4382            s0=s      @param arg1: second argument of at least rank 1
4383      s1=[[]]      @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
4384      for k in shape1:      @return: the product of the transposed of arg0 and arg1 at each data point
4385            s=[]      @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
4386            for j in s1:      @raise ValueError: if the shapes of the arguments are not appropriate
4387                  for i in range(k): s.append(j+[slice(i,i)])      """
4388            s1=s      sh0=getShape(arg0)
4389      s01=[[]]      sh1=getShape(arg1)
4390      for k in shape01:      if not len(sh0)==2 :
4391            s=[]          raise ValueError,"first argument must have rank 2"
4392            for j in s01:      if not len(sh1)==2 and not len(sh1)==1:
4393                  for i in range(k): s.append(j+[slice(i,i)])          raise ValueError,"second argument must have rank 1 or 2"
4394            s01=s      return generalTransposedTensorProduct(arg0,arg1,axis_offset=1)
4395    
4396      for i0 in s0:  def transposed_tensor_mult(arg0,arg1):
4397         for i1 in s1:      """
4398           s=escript.Scalar(0.,fs)      the tensor product of the transposed of the first and the second argument
4399           for i01 in s01:      
4400              s+=arg0.__getitem__(tuple(i0+i01))*arg1.__getitem__(tuple(i01+i1))      for arg0 of rank 2 this is
4401           out.__setitem__(tuple(i0+i1),s)  
4402      return out      out[s0]=S{Sigma}_{r0} arg0[r0,s0]*arg1[r0]  
4403    
4404        or
4405    
4406        out[s0,s1]=S{Sigma}_{r0} arg0[r0,s0]*arg1[r0,s1]
4407    
4408      
4409        and for arg0 of rank 4 this is
4410    
4411        out[s0,s1,s2,s3]=S{Sigma}_{r0,r1} arg0[r0,r1,s0,s1]*arg1[r0,r1,s2,s3]
4412                  
4413        or
4414    
4415        out[s0,s1,s2]=S{Sigma}_{r0,r1} arg0[r0,r1,s0,s1]*arg1[r0,r1,s2]
4416    
4417        or
4418    
4419        out[s0,s1]=S{Sigma}_{r0,r1} arg0[r0,r1,s0,s1]*arg1[r0,r1]
4420    
4421        In the first case the the first dimension of arg0 and the first dimension of arg1 must match and  
4422        in the second case the two first dimensions of arg0 must match the two first dimension of arg1.
4423    
4424        The function call transposed_tensor_mult(arg0,arg1) is equivalent to tensor_mult(transpose(arg0),arg1).
4425    
4426        @param arg0: first argument of rank 2 or 4
4427        @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
4428        @param arg1: second argument of shape greater of 1 or 2 depending on rank of arg0
4429        @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
4430        @return: the tensor product of tarnsposed of arg0 and arg1 at each data point
4431        @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
4432        """
4433        sh0=getShape(arg0)
4434        sh1=getShape(arg1)
4435        if len(sh0)==2 and ( len(sh1)==2 or len(sh1)==1 ):
4436           return generalTransposedTensorProduct(arg0,arg1,axis_offset=1)
4437        elif len(sh0)==4 and (len(sh1)==2 or len(sh1)==3 or len(sh1)==4):
4438           return generalTransposedTensorProduct(arg0,arg1,axis_offset=2)
4439        else:
4440            raise ValueError,"first argument must have rank 2 or 4"
4441    
4442    def generalTransposedTensorProduct(arg0,arg1,axis_offset=0):
4443        """
4444        generalized tensor product of transposed of arg0 and arg1:
4445    
4446        out[s,t]=S{Sigma}_r arg0[r,s]*arg1[r,t]
4447    
4448        where
4449    
4450            - s runs through arg0.Shape[axis_offset:]
4451            - r runs trough arg0.Shape[:axis_offset]
4452            - t runs through arg1.Shape[axis_offset:]
4453    
4454        The function call generalTransposedTensorProduct(arg0,arg1,axis_offset) is equivalent
4455        to generalTensorProduct(transpose(arg0,arg0.rank-axis_offset),arg1,axis_offset).
4456    
4457        @param arg0: first argument
4458        @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4459        @param arg1: second argument
4460        @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4461        @return: the general tensor product of transposed(arg0) and arg1 at each data point.
4462        @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
4463        """
4464        if isinstance(arg0,float) and isinstance(arg1,float): return arg1*arg0
4465        arg0,arg1=matchType(arg0,arg1)
4466        # at this stage arg0 and arg0 are both numarray.NumArray or escript.Data or Symbols
4467        if isinstance(arg0,numarray.NumArray):
4468           if isinstance(arg1,Symbol):
4469               return GeneralTransposedTensorProduct_Symbol(arg0,arg1,axis_offset)
4470           else:
4471               if not arg0.shape[:axis_offset]==arg1.shape[:axis_offset]:
4472                   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)
4473               arg0_c=arg0.copy()
4474               arg1_c=arg1.copy()
4475               sh0,sh1=arg0.shape,arg1.shape
4476               d0,d1,d01=1,1,1
4477               for i in sh0[axis_offset:]: d0*=i
4478               for i in sh1[axis_offset:]: d1*=i
4479               for i in sh0[:axis_offset]: d01*=i
4480               arg0_c.resize((d01,d0))
4481               arg1_c.resize((d01,d1))
4482               out=numarray.zeros((d0,d1),numarray.Float64)
4483               for i0 in range(d0):
4484                        for i1 in range(d1):
4485                             out[i0,i1]=numarray.sum(arg0_c[:,i0]*arg1_c[:,i1])
4486               out.resize(sh0[axis_offset:]+sh1[axis_offset:])
4487               return out
4488        elif isinstance(arg0,escript.Data):
4489           if isinstance(arg1,Symbol):
4490               return GeneralTransposedTensorProduct_Symbol(arg0,arg1,axis_offset)
4491           else:
4492               return escript_generalTransposedTensorProduct(arg0,arg1,axis_offset) # this calls has to be replaced by escript._generalTensorProduct(arg0,arg1,axis_offset)
4493        else:      
4494           return GeneralTransposedTensorProduct_Symbol(arg0,arg1,axis_offset)
4495                    
4496    class GeneralTransposedTensorProduct_Symbol(DependendSymbol):
4497       """
4498       Symbol representing the general tensor product of the transposed of arg0 and arg1
4499       """
4500       def __init__(self,arg0,arg1,axis_offset=0):
4501           """
4502           initialization of L{Symbol} representing tensor product of the transposed of arg0 and arg1
4503    
4504           @param arg0: first argument
4505           @type arg0: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
4506           @param arg1: second argument
4507           @type arg1: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
4508           @raise ValueError: inconsistent dimensions of arguments.
4509           @note: if both arguments have a spatial dimension, they must equal.
4510           """
4511           sh_arg0=getShape(arg0)
4512           sh_arg1=getShape(arg1)
4513           sh01=sh_arg0[:axis_offset]
4514           sh10=sh_arg1[:axis_offset]
4515           sh0=sh_arg0[axis_offset:]
4516           sh1=sh_arg1[axis_offset:]
4517           if not sh01==sh10:
4518               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)
4519           DependendSymbol.__init__(self,dim=commonDim(arg0,arg1),shape=sh0+sh1,args=[arg0,arg1,axis_offset])
4520    
4521       def getMyCode(self,argstrs,format="escript"):
4522          """
4523          returns a program code that can be used to evaluate the symbol.
4524    
4525          @param argstrs: gives for each argument a string representing the argument for the evaluation.
4526          @type argstrs: C{list} of length 2 of C{str}.
4527          @param format: specifies the format to be used. At the moment only "escript", "str" and "text" are supported.
4528          @type format: C{str}
4529          @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
4530          @rtype: C{str}
4531          @raise NotImplementedError: if the requested format is not available
4532          """
4533          if format=="escript" or format=="str" or format=="text":
4534             return "generalTransposedTensorProduct(%s,%s,axis_offset=%s)"%(argstrs[0],argstrs[1],argstrs[2])
4535          else:
4536             raise NotImplementedError,"%s does not provide program code for format %s."%(str(self),format)
4537    
4538       def substitute(self,argvals):
4539          """
4540          assigns new values to symbols in the definition of the symbol.
4541          The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.
4542    
4543          @param argvals: new values assigned to symbols
4544          @type argvals: C{dict} with keywords of type L{Symbol}.
4545          @return: result of the substitution process. Operations are executed as much as possible.
4546          @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution
4547          @raise TypeError: if a value for a L{Symbol} cannot be substituted.
4548          """
4549          if argvals.has_key(self):
4550             arg=argvals[self]
4551             if self.isAppropriateValue(arg):
4552                return arg
4553             else:
4554                raise TypeError,"%s: new value is not appropriate."%str(self)
4555          else:
4556             args=self.getSubstitutedArguments(argvals)
4557             return generalTransposedTensorProduct(args[0],args[1],args[2])
4558    
4559    def escript_generalTransposedTensorProduct(arg0,arg1,axis_offset): # this should be escript._generalTransposedTensorProduct
4560        "arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!"
4561        return C_GeneralTensorProduct(arg0, arg1, axis_offset, 1)
4562    
4563    def matrix_transposed_mult(arg0,arg1):
4564        """
4565        matrix-transposed(matrix) product of the two argument:
4566    
4567        out[s0,s1]=S{Sigma}_{r0} arg0[s0,r0]*arg1[s1,r0]
4568    
4569        The function call matrix_transposed_mult(arg0,arg1) is equivalent to matrix_mult(arg0,transpose(arg1)).
4570    
4571        The last dimensions of arg0 and arg1 must match.
4572    
4573        @param arg0: first argument of rank 2
4574        @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
4575        @param arg1: second argument of rank 2
4576        @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
4577        @return: the product of arg0 and the transposed of arg1 at each data point
4578        @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
4579        @raise ValueError: if the shapes of the arguments are not appropriate
4580        """
4581        sh0=getShape(arg0)
4582        sh1=getShape(arg1)
4583        if not len(sh0)==2 :
4584            raise ValueError,"first argument must have rank 2"
4585        if not len(sh1)==2 and not len(sh1)==1:
4586            raise ValueError,"second argument must have rank 1 or 2"
4587        return generalTensorTransposedProduct(arg0,arg1,axis_offset=1)
4588    
4589    def tensor_transposed_mult(arg0,arg1):
4590        """
4591        the tensor product of the first and the transpose of the second argument
4592        
4593        for arg0 of rank 2 this is
4594    
4595        out[s0,s1]=S{Sigma}_{r0} arg0[s0,r0]*arg1[s1,r0]
4596    
4597        and for arg0 of rank 4 this is
4598    
4599        out[s0,s1,s2,s3]=S{Sigma}_{r0,r1} arg0[s0,s1,r0,r1]*arg1[s2,s3,r0,r1]
4600                  
4601        or
4602    
4603        out[s0,s1,s2]=S{Sigma}_{r0,r1} arg0[s0,s1,r0,r1]*arg1[s2,r0,r1]
4604    
4605        In the first case the the second dimension of arg0 and arg1 must match and  
4606        in the second case the two last dimensions of arg0 must match the two last dimension of arg1.
4607    
4608        The function call tensor_transpose_mult(arg0,arg1) is equivalent to tensor_mult(arg0,transpose(arg1)).
4609    
4610        @param arg0: first argument of rank 2 or 4
4611        @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
4612        @param arg1: second argument of shape greater of 1 or 2 depending on rank of arg0
4613        @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
4614        @return: the tensor product of tarnsposed of arg0 and arg1 at each data point
4615        @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
4616        """
4617        sh0=getShape(arg0)
4618        sh1=getShape(arg1)
4619        if len(sh0)==2 and ( len(sh1)==2 or len(sh1)==1 ):
4620           return generalTensorTransposedProduct(arg0,arg1,axis_offset=1)
4621        elif len(sh0)==4 and (len(sh1)==2 or len(sh1)==3 or len(sh1)==4):
4622           return generalTensorTransposedProduct(arg0,arg1,axis_offset=2)
4623        else:
4624            raise ValueError,"first argument must have rank 2 or 4"
4625    
4626    def generalTensorTransposedProduct(arg0,arg1,axis_offset=0):
4627        """
4628        generalized tensor product of transposed of arg0 and arg1:
4629    
4630        out[s,t]=S{Sigma}_r arg0[s,r]*arg1[t,r]
4631    
4632        where
4633    
4634            - s runs through arg0.Shape[:arg0.Rank-axis_offset]
4635            - r runs trough arg0.Shape[arg1.Rank-axis_offset:]
4636            - t runs through arg1.Shape[arg1.Rank-axis_offset:]
4637    
4638        The function call generalTensorTransposedProduct(arg0,arg1,axis_offset) is equivalent
4639        to generalTensorProduct(arg0,transpose(arg1,arg1.Rank-axis_offset),axis_offset).
4640    
4641        @param arg0: first argument
4642        @type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4643        @param arg1: second argument
4644        @type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{float}, C{int}
4645        @return: the general tensor product of transposed(arg0) and arg1 at each data point.
4646        @rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol} depending on the input
4647        """
4648        if isinstance(arg0,float) and isinstance(arg1,float): return arg1*arg0
4649        arg0,arg1=matchType(arg0,arg1)
4650        # at this stage arg0 and arg0 are both numarray.NumArray or escript.Data or Symbols
4651        if isinstance(arg0,numarray.NumArray):
4652           if isinstance(arg1,Symbol):
4653               return GeneralTensorTransposedProduct_Symbol(arg0,arg1,axis_offset)
4654           else:
4655               if not arg0.shape[arg0.rank-axis_offset:]==arg1.shape[arg1.rank-axis_offset:]:
4656                   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)
4657               arg0_c=arg0.copy()
4658               arg1_c=arg1.copy()
4659               sh0,sh1=arg0.shape,arg1.shape
4660               d0,d1,d01=1,1,1
4661               for i in sh0[:arg0.rank-axis_offset]: d0*=i
4662               for i in sh1[:arg1.rank-axis_offset]: d1*=i
4663               for i in sh1[arg1.rank-axis_offset:]: d01*=i
4664               arg0_c.resize((d0,d01))
4665               arg1_c.resize((d1,d01))
4666               out=numarray.zeros((d0,d1),numarray.Float64)
4667               for i0 in range(d0):
4668                        for i1 in range(d1):
4669                             out[i0,i1]=numarray.sum(arg0_c[i0,:]*arg1_c[i1,:])
4670               out.resize(sh0[:arg0.rank-axis_offset]+sh1[:arg1.rank-axis_offset])
4671               return out
4672        elif isinstance(arg0,escript.Data):
4673           if isinstance(arg1,Symbol):
4674               return GeneralTensorTransposedProduct_Symbol(arg0,arg1,axis_offset)
4675           else:
4676               return escript_generalTensorTransposedProduct(arg0,arg1,axis_offset) # this calls has to be replaced by escript._generalTensorProduct(arg0,arg1,axis_offset)
4677        else:      
4678           return GeneralTensorTransposedProduct_Symbol(arg0,arg1,axis_offset)
4679                    
4680    class GeneralTensorTransposedProduct_Symbol(DependendSymbol):
4681       """
4682       Symbol representing the general tensor product of arg0 and the transpose of arg1
4683       """
4684       def __init__(self,arg0,arg1,axis_offset=0):
4685           """
4686           initialization of L{Symbol} representing the general tensor product of arg0 and the transpose of arg1
4687    
4688           @param arg0: first argument
4689           @type arg0: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
4690           @param arg1: second argument
4691           @type arg1: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray}.
4692           @raise ValueError: inconsistent dimensions of arguments.
4693           @note: if both arguments have a spatial dimension, they must equal.
4694           """
4695           sh_arg0=getShape(arg0)
4696           sh_arg1=getShape(arg1)
4697           sh0=sh_arg0[:len(sh_arg0)-axis_offset]
4698           sh01=sh_arg0[len(sh_arg0)-axis_offset:]
4699           sh10=sh_arg1[len(sh_arg1)-axis_offset:]
4700           sh1=sh_arg1[:len(sh_arg1)-axis_offset]
4701           if not sh01==sh10:
4702               raise ValueError,"dimensions of last %s components in left argument don't match the last %s components in the right argument."%(axis_offset,axis_offset)
4703           DependendSymbol.__init__(self,dim=commonDim(arg0,arg1),shape=sh0+sh1,args=[arg0,arg1,axis_offset])
4704    
4705       def getMyCode(self,argstrs,format="escript"):
4706          """
4707          returns a program code that can be used to evaluate the symbol.
4708    
4709          @param argstrs: gives for each argument a string representing the argument for the evaluation.
4710          @type argstrs: C{list} of length 2 of C{str}.
4711          @param format: specifies the format to be used. At the moment only "escript", "str" and "text" are supported.
4712          @type format: C{str}
4713          @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
4714          @rtype: C{str}
4715          @raise NotImplementedError: if the requested format is not available
4716          """
4717          if format=="escript" or format=="str" or format=="text":
4718             return "generalTensorTransposedProduct(%s,%s,axis_offset=%s)"%(argstrs[0],argstrs[1],argstrs[2])
4719          else:
4720             raise NotImplementedError,"%s does not provide program code for format %s."%(str(self),format)
4721    
4722       def substitute(self,argvals):
4723          """
4724          assigns new values to symbols in the definition of the symbol.
4725          The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.
4726    
4727          @param argvals: new values assigned to symbols
4728          @type argvals: C{dict} with keywords of type L{Symbol}.
4729          @return: result of the substitution process. Operations are executed as much as possible.
4730          @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution
4731          @raise TypeError: if a value for a L{Symbol} cannot be substituted.
4732          """
4733          if argvals.has_key(self):
4734             arg=argvals[self]
4735             if self.isAppropriateValue(arg):
4736                return arg
4737             else:
4738                raise TypeError,"%s: new value is not appropriate."%str(self)
4739          else:
4740             args=self.getSubstitutedArguments(argvals)
4741             return generalTensorTransposedProduct(args[0],args[1],args[2])
4742    
4743    def escript_generalTensorTransposedProduct(arg0,arg1,axis_offset): # this should be escript._generalTensorTransposedProduct
4744        "arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!"
4745        return C_GeneralTensorProduct(arg0, arg1, axis_offset, 2)
4746    
4747  #=========================================================  #=========================================================
4748  #  functions dealing with spatial dependency  #  functions dealing with spatial dependency
# Line 4396  def grad(arg,where=None): Line 4764  def grad(arg,where=None):
4764                    If not present or C{None} an appropriate default is used.                    If not present or C{None} an appropriate default is used.
4765      @type where: C{None} or L{escript.FunctionSpace}      @type where: C{None} or L{escript.FunctionSpace}
4766      @return: gradient of arg.      @return: gradient of arg.
4767      @rtype:  L{escript.Data} or L{Symbol}      @rtype: L{escript.Data} or L{Symbol}
4768      """      """
4769      if isinstance(arg,Symbol):      if isinstance(arg,Symbol):
4770         return Grad_Symbol(arg,where)         return Grad_Symbol(arg,where)
# Line 4436  class Grad_Symbol(DependendSymbol): Line 4804  class Grad_Symbol(DependendSymbol):
4804        @type format: C{str}        @type format: C{str}
4805        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
4806        @rtype: C{str}        @rtype: C{str}
4807        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
4808        """        """
4809        if format=="escript" or format=="str"  or format=="text":        if format=="escript" or format=="str"  or format=="text":
4810           return "grad(%s,where=%s)"%(argstrs[0],argstrs[1])           return "grad(%s,where=%s)"%(argstrs[0],argstrs[1])
# Line 4489  def integrate(arg,where=None): Line 4857  def integrate(arg,where=None):
4857                    If not present or C{None} an appropriate default is used.                    If not present or C{None} an appropriate default is used.
4858      @type where: C{None} or L{escript.FunctionSpace}      @type where: C{None} or L{escript.FunctionSpace}
4859      @return: integral of arg.      @return: integral of arg.
4860      @rtype:  C{float}, C{numarray.NumArray} or L{Symbol}      @rtype: C{float}, C{numarray.NumArray} or L{Symbol}
4861      """      """
4862      if isinstance(arg,Symbol):      if isinstance(arg,Symbol):
4863         return Integrate_Symbol(arg,where)         return Integrate_Symbol(arg,where)
# Line 4527  class Integrate_Symbol(DependendSymbol): Line 4895  class Integrate_Symbol(DependendSymbol):
4895        @type format: C{str}        @type format: C{str}
4896        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
4897        @rtype: C{str}        @rtype: C{str}
4898        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
4899        """        """
4900        if format=="escript" or format=="str"  or format=="text":        if format=="escript" or format=="str"  or format=="text":
4901           return "integrate(%s,where=%s)"%(argstrs[0],argstrs[1])           return "integrate(%s,where=%s)"%(argstrs[0],argstrs[1])
# Line 4579  def interpolate(arg,where): Line 4947  def interpolate(arg,where):
4947      @param where: FunctionSpace to be interpolated to      @param where: FunctionSpace to be interpolated to
4948      @type where: L{escript.FunctionSpace}      @type where: L{escript.FunctionSpace}
4949      @return: interpolated argument      @return: interpolated argument
4950      @rtype:  C{escript.Data} or L{Symbol}      @rtype: C{escript.Data} or L{Symbol}
4951      """      """
4952      if isinstance(arg,Symbol):      if isinstance(arg,Symbol):
4953         return Interpolate_Symbol(arg,where)         return Interpolate_Symbol(arg,where)
# Line 4610  class Interpolate_Symbol(DependendSymbol Line 4978  class Interpolate_Symbol(DependendSymbol
4978        @type format: C{str}        @type format: C{str}
4979        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.        @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.
4980        @rtype: C{str}        @rtype: C{str}
4981        @raise: NotImplementedError: if the requested format is not available        @raise NotImplementedError: if the requested format is not available
4982        """        """
4983        if format=="escript" or format=="str"  or format=="text":        if format=="escript" or format=="str"  or format=="text":
4984           return "interpolate(%s,where=%s)"%(argstrs[0],argstrs[1])           return "interpolate(%s,where=%s)"%(argstrs[0],argstrs[1])
# Line 4663  def div(arg,where=None): Line 5031  def div(arg,where=None):
5031                    If not present or C{None} an appropriate default is used.                    If not present or C{None} an appropriate default is used.
5032      @type where: C{None} or L{escript.FunctionSpace}      @type where: C{None} or L{escript.FunctionSpace}
5033      @return: divergence of arg.      @return: divergence of arg.
5034      @rtype:  L{escript.Data} or L{Symbol}      @rtype: L{escript.Data} or L{Symbol}
5035      """      """
5036      if isinstance(arg,Symbol):      if isinstance(arg,Symbol):
5037          dim=arg.getDim()          dim=arg.getDim()
# Line 4685  def jump(arg,domain=None): Line 5053  def jump(arg,domain=None):
5053                     the domain of arg is used. If arg is a L{Symbol} the domain must be present.                     the domain of arg is used. If arg is a L{Symbol} the domain must be present.
5054      @type domain: C{None} or L{escript.Domain}      @type domain: C{None} or L{escript.Domain}
5055      @return: jump of arg      @return: jump of arg
5056      @rtype:  L{escript.Data} or L{Symbol}      @rtype: L{escript.Data} or L{Symbol}
5057      """      """
5058      if domain==None: domain=arg.getDomain()      if domain==None: domain=arg.getDomain()
5059      return interpolate(arg,escript.FunctionOnContactOne(domain))-interpolate(arg,escript.FunctionOnContactZero(domain))      return interpolate(arg,escript.FunctionOnContactOne(domain))-interpolate(arg,escript.FunctionOnContactZero(domain))
# Line 4697  def L2(arg): Line 5065  def L2(arg):
5065      @param arg: function which L2 to be calculated.      @param arg: function which L2 to be calculated.
5066      @type arg: L{escript.Data} or L{Symbol}      @type arg: L{escript.Data} or L{Symbol}
5067      @return: L2 norm of arg.      @return: L2 norm of arg.
5068      @rtype:  L{float} or L{Symbol}      @rtype: L{float} or L{Symbol}
5069      @note: L2(arg) is equivalent to sqrt(integrate(inner(arg,arg)))      @note: L2(arg) is equivalent to sqrt(integrate(inner(arg,arg)))
5070      """      """
5071      return sqrt(integrate(inner(arg,arg)))      return sqrt(integrate(inner(arg,arg)))

Legend:
Removed from v.588  
changed lines
  Added in v.912

  ViewVC Help
Powered by ViewVC 1.1.26