/[escript]/trunk/escript/test/python/UnaryOps.py
ViewVC logotype

Diff of /trunk/escript/test/python/UnaryOps.py

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

revision 545 by jgs, Tue Dec 6 00:55:22 2005 UTC revision 546 by jgs, Tue Feb 21 01:59:07 2006 UTC
# Line 1  Line 1 
1    """
2    
3    Test unary ops on Data objects.
4    
5    Version $Id$
6    
7    """
8    
9  import sys  import sys
10  import unittest  import unittest
11  import os  import os
# Line 7  from esys import bruce Line 15  from esys import bruce
15    
16  import numarray  import numarray
17    
 """  
   
 Test unary ops on Data objects.  
   
 Version $Id$  
   
 """  
   
18  from numarray import array,Float64,ones,greater  from numarray import array,Float64,ones,greater
19    
20  Tag1=10  Tag1=10
21  Tag2=11  Tag2=15
22    
23  tol=1.E-15  tol=1.E-15
24    
# Line 36  arglist = [ \ Line 36  arglist = [ \
36  [ 3.0, 6.0, 3 ] \  [ 3.0, 6.0, 3 ] \
37  ]  ]
38    
 # these are used to test slicing:  
 a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]  
 a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ]  
 a_r4=[ \  
 [ [ [[ 1,2,3],[11,12,13]], [[21,22,23],[31,32,33]], [[41,42,43],[51,52,53]]  ], [ [[101,102,103],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]]  ], [ [[201,202,203],[211,212,213]], [[221,222,223],[231,232,233]], [[241,242,243],[251,252,253]]  ] ], \  
 [ [ [[ -1,-2,-3],[-11,-12,-13]], [[-21,-22,-23],[-31,-32,-33]], [[-41,-42,-43],[-51,-52,-53]]  ], [ [[-101,-102,-103],[-111,-112,-113]], [[-121,-122,-123],[-131,-132,-133]], [[-141,-142,-143],[-151,-152,-153]]  ], [ [[-201,-202,-203],[-211,-212,-213]], [[-221,-222,-223],[-231,-232,-233]], [[-241,-242,-243],[-251,-252,-253]]  ] ], \  
 [ [[[ 11,12,13],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]]  ], [ [[1101,1102,1103],[1111,1112,1113]], [[1121,1122,1123],[1131,1132,1133]], [[1141,1142,1143],[1151,1152,1153]]  ], [ [[1201,1202,1203],[1211,1212,1213]], [[1221,1222,1223],[1231,1232,1233]], [[1241,1242,1243],[1251,1252,1253]]  ] ] ]  
 a_r4_in=[ \  
 [ [ [[ 1./1,1./2,1./3],[1./11,1./12,1./13]], [[1./21,1./22,1./23],[1./31,1./32,1./33]], [[1./41,1./42,1./43],[1./51,1./52,1./53]]  ], [ [[1./101,1./102,1./103],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]]  ], [ [[1./201,1./202,1./203],[1./211,1./212,1./213]], [[1./221,1./222,1./223],[1./231,1./232,1./233]], [[1./241,1./242,1./243],[1./251,1./252,1./253]]  ] ], \  
 [ [ [[ -1./1,-1./2,-1./3],[-1./11,-1./12,-1./13]], [[-1./21,-1./22,-1./23],[-1./31,-1./32,-1./33]], [[-1./41,-1./42,-1./43],[-1./51,-1./52,-1./53]]  ], [ [[-1./101,-1./102,-1./103],[-1./111,-1./112,-1./113]], [[-1./121,-1./122,-1./123],[-1./131,-1./132,-1./133]], [[-1./141,-1./142,-1./143],[1./-151,-1./152,-1./153]]  ], [ [[-1./201,-1./202,-1./203],[-1./211,-1./212,-1./213]], [[-1./221,-1./222,-1./223],[-1./231,-1./232,-1./233]], [[-1./241,-1./242,-1./243],[-1./251,-1./252,-1./253]]  ] ], \  
 [ [[[ 1./11,1./12,1./13],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]]  ], [ [[1./1101,1./1102,1./1103],[1./1111,1./1112,1./1113]], [[1./1121,1./1122,1./1123],[1./1131,1./1132,1./1133]], [[1./1141,1./1142,1./1143],[1./1151,1./1152,1./1153]]  ], [ [[1./1201,1./1202,1./1203],[1./1211,1./1212,1./1213]], [[1./1221,1./1222,1./1223],[1./1231,1./1232,1./1233]], [[1./1241,1./1242,1./1243],[1./1251,1./1252,1./1253]]  ] ] ]  
   
 def turnToArray(val,tagged):  
      if tagged=="Tagged1":  
          out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]  
      elif tagged=="Tagged2":  
          out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]  
      else:  
          out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]  
      return out  
   
39  def prepareArg(val,ex,wh):  def prepareArg(val,ex,wh):
40       if ex=="Array":      if ex=="Array":
41          out=val[0]          out=val[0]
42       else:      else:
43          if ex=="Expanded":          if ex=="Expanded":
44              exx=True              exx=True
45          else:          else:
46              exx=False              exx=False
47          out=Data(val[0],what=wh,expand=exx)          out=Data(val[0],what=wh,expand=exx)
48          if ex=="Tagged1":          if ex=="Tagged1":
49             out.setTaggedValue(Tag1,val[1])              out.setTaggedValue(Tag1,val[1])
50          elif ex=="Tagged2":          elif ex=="Tagged2":
51             out.setTaggedValue(Tag1,val[1])              out.setTaggedValue(Tag1,val[1])
52             out.setTaggedValue(Tag2,val[2])              out.setTaggedValue(Tag2,val[2])
53       return out      return out
54    
55    def turnToArray(val,tagged):
56        if tagged=="Tagged1":
57            out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
58        elif tagged=="Tagged2":
59            out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
60        else:
61            out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
62        return out
63    
64  def checkResult(text,res,val0,val1,val2,wh):  def checkResult(text,res,val0,val1,val2,wh):
65       ref=Data(val0,what=wh,expand=False)      ref=Data(val0,what=wh,expand=False)
66       ref.setTaggedValue(Tag1,val1)      ref.setTaggedValue(Tag1,val1)
67       ref.setTaggedValue(Tag2,val2)      ref.setTaggedValue(Tag2,val2)
68       norm=Lsup(ref)+tol      norm=Lsup(ref)+tol
69       error=Lsup(ref-res)/norm      error=Lsup(ref-res)/norm
70       print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)      print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
71       if error>tol:      if error>tol:
72         print "**** %s: error is too large"%(text)          print "**** %s: error is too large"%(text)
73         raise SystemError,"@@ %s at %s: error is too large"%(text,wh)          raise SystemError,"@@ %s at %s: error is too large"%(text,wh)
74         sys.exit(1)          sys.exit(1)
75    
76  def getRank(arg):  def getRank(arg):
77      if isinstance(arg,Data):      if isinstance(arg,Data):
78         return arg.getRank()          return arg.getRank()
79      else:      else:
80          g=array(arg)          g=array(arg)
81          if g.rank==0:          if g.rank==0:
82             return 1              return 1
83          else:          else:
84             return g.rank              return g.rank
85    
86  def isScalar(arg):  def isScalar(arg):
87      if isinstance(arg,Data):      if isinstance(arg,Data):
88         if arg.getRank()==1 and arg.getShape()[0]==1:          if arg.getRank()==1 and arg.getShape()[0]==1:
89           return not None              return not None
90         else:          else:
91           return None              return None
92      else:      else:
93          g=array(arg)          g=array(arg)
94          if g.rank==0:          if g.rank==0:
95             return not None              return not None
96          else:          else:
97             if g.rank==1 and g.shape[0]==1:              if g.rank==1 and g.shape[0]==1:
98                return not None                  return not None
99             else:              else:
100                return None                  return None
101    
102  #  #
103  # ==============================================================  # ==============================================================
# Line 122  for wh in [ContinuousFunction(msh),Funct Line 110  for wh in [ContinuousFunction(msh),Funct
110    
111    print wh    print wh
112    
113    #for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]:    for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]:
   for ex1 in ["Constant","Expanded"]:  
114    
115      print "Unary Ops:", ex1      print "Unary Ops:", ex1
116    
# Line 154  for wh in [ContinuousFunction(msh),Funct Line 141  for wh in [ContinuousFunction(msh),Funct
141    
142        # where positive:        # where positive:
143        ref=checkResult("where positive("+ex1+")", \        ref=checkResult("where positive("+ex1+")", \
144                        (arg1-3)._wherePositive(), \                        arg1._wherePositive(), \
145                        numarray.greater(arrays1[0],3.), \                        numarray.greater(arrays1[0],0.), \
146                        numarray.greater(arrays1[1],3.), \                        numarray.greater(arrays1[1],0.), \
147                        numarray.greater(arrays1[2],3.), \                        numarray.greater(arrays1[2],0.), \
148                        wh)                        wh)
149    
150        # where negative:        # where negative:
151        ref=checkResult("where negative("+ex1+")", \        #ref=checkResult("where negative("+ex1+")", \
152                        (arg1-3)._whereNegative(), \        #                (arg1-3)._whereNegative(), \
153                        numarray.greater(3.,arrays1[0]), \        #                numarray.greater(3.,arrays1[0]), \
154                        numarray.greater(3.,arrays1[1]), \        #                numarray.greater(3.,arrays1[1]), \
155                        numarray.greater(3.,arrays1[2]), \        #                numarray.greater(3.,arrays1[2]), \
156                        wh)        #                wh)
157    
158        # where non-negative:        # where non-negative:
159        ref=checkResult("where nonnegative("+ex1+")", \        #ref=checkResult("where nonnegative("+ex1+")", \
160                        (arg1-3)._whereNonNegative(), \        #                (arg1-3)._whereNonNegative(), \
161                        numarray.greater_equal(arrays1[0],3.), \        #                numarray.greater_equal(arrays1[0],3.), \
162                        numarray.greater_equal(arrays1[1],3.), \        #                numarray.greater_equal(arrays1[1],3.), \
163                        numarray.greater_equal(arrays1[2],3.), \        #                numarray.greater_equal(arrays1[2],3.), \
164                        wh)        #                wh)
165    
166        # where non-positive:        # where non-positive:
167        ref=checkResult("where nonpositive("+ex1+")", \        #ref=checkResult("where nonpositive("+ex1+")", \
168                        (arg1-3)._whereNonPositive(), \        #                (arg1-3)._whereNonPositive(), \
169                        numarray.greater_equal(3.,arrays1[0]), \        #                numarray.greater_equal(3.,arrays1[0]), \
170                        numarray.greater_equal(3.,arrays1[1]), \        #                numarray.greater_equal(3.,arrays1[1]), \
171                        numarray.greater_equal(3.,arrays1[2]), \        #                numarray.greater_equal(3.,arrays1[2]), \
172                        wh)        #                wh)
173    
174        # where zero:        # where zero:
175        ref=checkResult("where zero("+ex1+")", \        #ref=checkResult("where zero("+ex1+")", \
176                        (arg1-3)._whereZero(), \        #                (arg1-3)._whereZero(), \
177                        numarray.less_equal(numarray.abs(arrays1[0]-3.),0.0), \        #                numarray.less_equal(numarray.abs(arrays1[0]-3.),0.0), \
178                        numarray.less_equal(numarray.abs(arrays1[1]-3.),0.0), \        #                numarray.less_equal(numarray.abs(arrays1[1]-3.),0.0), \
179                        numarray.less_equal(numarray.abs(arrays1[2]-3.),0.0), \        #                numarray.less_equal(numarray.abs(arrays1[2]-3.),0.0), \
180                        wh)        #                wh)
181    
182        # where non-zero:        # where non-zero:
183        ref=checkResult("where nonzero("+ex1+")", \        #ref=checkResult("where nonzero("+ex1+")", \
184                        (arg1-3)._whereNonZero(), \        #                (arg1-3)._whereNonZero(), \
185                        numarray.greater(numarray.abs(arrays1[0]-3.),0.0), \        #                numarray.greater(numarray.abs(arrays1[0]-3.),0.0), \
186                        numarray.greater(numarray.abs(arrays1[1]-3.),0.0), \        #                numarray.greater(numarray.abs(arrays1[1]-3.),0.0), \
187                        numarray.greater(numarray.abs(arrays1[2]-3.),0.0), \        #                numarray.greater(numarray.abs(arrays1[2]-3.),0.0), \
188                        wh)        #                wh)
189    
190        # exponential function:        # exponential function:
191        ref=checkResult("exp("+ex1+")", \        ref=checkResult("exp("+ex1+")", \

Legend:
Removed from v.545  
changed lines
  Added in v.546

  ViewVC Help
Powered by ViewVC 1.1.26