# Contents of /trunk/esys2/escript/py_src/generateutil.py

Revision 153 - (show annotations)
Tue Oct 25 01:51:20 2005 UTC (15 years ago) by jgs
File MIME type: text/x-python
File size: 30878 byte(s)
```Merge of development branch dev-02 back to main trunk on 2005-10-25

```
 1 # \$Id\$ 2 3 """ 4 program generates parts of the util.py and the test_util.py script 5 """ 6 u_prog ="import esys.escript\n" 7 u_prog+="import numarray\n" 8 u_prog+="import math\n" 9 t_prog="" 10 11 func= [ ["atan", [-100.,100.], "math.atan(%a%)"], 12 ["log", [1.e-8,10.], "math.log(%a%)"] 13 ] 14 import random 15 import numarray 16 import math 17 finc=1.e-6 18 19 def makeArray(shape,rng): 20 l=rng[1]-rng[0] 21 out=numarray.zeros(shape,numarray.Float64) 22 if len(shape)==0: 23 out=random.random() 24 elif len(shape)==1: 25 for i0 in range(shape[0]): 26 out[i0]=l*random.random()+rng[0] 27 elif len(shape)==2: 28 for i0 in range(shape[0]): 29 for i1 in range(shape[1]): 30 out[i0,i1]=l*random.random()+rng[0] 31 elif len(shape)==3: 32 for i0 in range(shape[0]): 33 for i1 in range(shape[1]): 34 for i2 in range(shape[2]): 35 out[i0,i1,i2]=l*random.random()+rng[0] 36 elif len(shape)==4: 37 for i0 in range(shape[0]): 38 for i1 in range(shape[1]): 39 for i2 in range(shape[2]): 40 for i3 in range(shape[3]): 41 out[i0,i1,i2,i3]=l*random.random()+rng[0] 42 else: 43 raise SystemError,"rank is restricted to 4" 44 return out 45 46 def makeResult2(val,case): 47 if isinstance(val,float): 48 out=val 49 elif len(val.shape)==0: 50 out=val[0] 51 elif len(val.shape)==1: 52 out=val[0] 53 for i0 in range(val.shape[0]): 54 if case == "Lsup": 55 out=max(out,abs(val[i0])) 56 elif case == "sup": 57 out=max(out,val[i0]) 58 else: 59 out=min(out,val[i0]) 60 elif len(val.shape)==2: 61 out=val[0,0] 62 for i0 in range(val.shape[0]): 63 for i1 in range(val.shape[1]): 64 if case == "Lsup": 65 out=max(out,abs(val[i0,i1])) 66 elif case == "sup": 67 out=max(out,val[i0,i1]) 68 else: 69 out=min(out,val[i0,i1]) 70 elif len(val.shape)==3: 71 out=val[0,0,0] 72 for i0 in range(val.shape[0]): 73 for i1 in range(val.shape[1]): 74 for i2 in range(val.shape[2]): 75 if case == "Lsup": 76 out=max(out,abs(val[i0,i1,i2])) 77 elif case == "sup": 78 out=max(out,val[i0,i1,i2]) 79 else: 80 out=min(out,val[i0,i1,i2]) 81 elif len(val.shape)==4: 82 out=val[0,0,0,0] 83 for i0 in range(val.shape[0]): 84 for i1 in range(val.shape[1]): 85 for i2 in range(val.shape[2]): 86 for i3 in range(val.shape[3]): 87 if case == "Lsup": 88 out=max(out,abs(val[i0,i1,i2,i3])) 89 elif case == "sup": 90 out=max(out,val[i0,i1,i2,i3]) 91 else: 92 out=min(out,val[i0,i1,i2,i3]) 93 else: 94 raise SystemError,"rank is restricted to 4" 95 return out 96 97 def makeResult(val,f): 98 if isinstance(val,float): 99 out=eval(f[2].replace("%a%","val")) 100 elif len(val.shape)==0: 101 out=eval(f[2].replace("%a%","val")) 102 elif len(val.shape)==1: 103 out=numarray.zeros(val.shape,numarray.Float64) 104 for i0 in range(val.shape[0]): 105 out[i0]=eval(f[2].replace("%a%","val[i0]")) 106 elif len(val.shape)==2: 107 out=numarray.zeros(val.shape,numarray.Float64) 108 for i0 in range(val.shape[0]): 109 for i1 in range(val.shape[1]): 110 out[i0,i1]=eval(f[2].replace("%a%","val[i0,i1]")) 111 elif len(val.shape)==3: 112 out=numarray.zeros(val.shape,numarray.Float64) 113 for i0 in range(val.shape[0]): 114 for i1 in range(val.shape[1]): 115 for i2 in range(val.shape[2]): 116 out[i0,i1,i2]=eval(f[2].replace("%a%","val[i0,i1,i2]")) 117 elif len(val.shape)==4: 118 out=numarray.zeros(val.shape,numarray.Float64) 119 for i0 in range(val.shape[0]): 120 for i1 in range(val.shape[1]): 121 for i2 in range(val.shape[2]): 122 for i3 in range(val.shape[3]): 123 out[i0,i1,i2,i3]=eval(f[2].replace("%a%","val[i0,i1,i2,i3]")) 124 else: 125 raise SystemError,"rank is restricted to 4" 126 return out 127 128 for case in ["Lsup", "sup", "inf"]: 129 for args in ["float","array","constData","taggedData","expandedData"]: 130 for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]: 131 if not args=="float" or len(sh)==0: 132 t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" 133 tname="def test_%s_%s_rank%s"%(case,args,len(sh)) 134 t_prog+=" %s(self):\n"%tname 135 if args in ["float","array" ]: 136 a=makeArray(sh,[-1,1]) 137 r=makeResult2(a,case) 138 if len(sh)==0: 139 t_prog+=" arg=%s\n"%a 140 else: 141 t_prog+=" arg=numarray.array(%s)\n"%a.tolist() 142 t_prog+=" ref=%s\n"%r 143 t_prog+=" res=%s(arg)\n"%case 144 t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n" 145 t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n" 146 elif args== "constData": 147 a=makeArray(sh,[-1,1]) 148 r=makeResult2(a,case) 149 if len(sh)==0: 150 t_prog+=" arg=Data(%s,self.functionspace)\n"%(a) 151 else: 152 t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist()) 153 t_prog+=" ref=%s\n"%r 154 t_prog+=" res=%s(arg)\n"%case 155 t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n" 156 t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n" 157 elif args in [ "taggedData","expandedData"]: 158 a=makeArray(sh,[-1,1]) 159 r=makeResult2(a,case) 160 a1=makeArray(sh,[-1,1]) 161 r1=makeResult2(a1,case) 162 if case in ["Lsup","sup"]: 163 r=max(r,r1) 164 else: 165 r=min(r,r1) 166 if len(sh)==0: 167 if args=="expandedData": 168 t_prog+=" arg=Data(%s,self.functionspace,True)\n"%(a) 169 else: 170 t_prog+=" arg=Data(%s,self.functionspace)\n"%(a) 171 t_prog+=" arg.setTaggedValue(1,%s)\n"%a 172 else: 173 if args=="expandedData": 174 t_prog+=" arg=Data(numarray.array(%s),self.functionspace,True)\n"%(a.tolist()) 175 else: 176 t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist()) 177 t_prog+=" arg.setTaggedValue(1,%s)\n"%a1.tolist() 178 t_prog+=" res=%s(arg)\n"%case 179 t_prog+=" ref=%s\n"%r 180 t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n" 181 t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n" 182 183 print t_prog 184 185 1/0 186 187 188 for f in func: 189 u_prog+="def %s(arg):\n"%f[0] 190 u_prog+=" \"\"\"\n" 191 u_prog+=" returns %s of argument arg\n\n"%f[0] 192 u_prog+=" @param arg: argument\n" 193 u_prog+=" @type arg: C{float}, L{escript.Data}, L{escript.Symbol}, L{numarray.NumArray}.\n" 194 u_prog+=" @rtype:C{float}, L{escript.Data}, L{escript.Symbol}, L{numarray.NumArray} depending on the type of arg.\n" 195 u_prog+=" @raises TypeError: if the type of the argument is not expected.\n" 196 u_prog+=" \"\"\"\n" 197 u_prog+=" if isinstance(arg,numarray.NumArray):\n" 198 u_prog+=" return numarray.%s(arg)\n"%f[0] 199 u_prog+=" elif isinstance(arg,escript.Data):\n" 200 u_prog+=" return arg._%s(arg)\n"%f[0] 201 u_prog+=" elif isinstance(arg,float):\n" 202 u_prog+=" return math.%s(arg)\n"%f[0] 203 u_prog+=" elif isinstance(arg,int):\n" 204 u_prog+=" return math.%s(float(arg))\n"%f[0] 205 u_prog+=" elif isinstance(arg,Symbol):\n" 206 u_prog+=" return Symbol_%s(arg)\n"%f[0] 207 u_prog+=" else:\n" 208 u_prog+=" raise TypeError,\"%s: Unknown argument type.\"\n"%f[0] 209 u_prog+="class Symbol_%s(Symbol):\n"%f[0] 210 u_prog+=" \"\"\"\n" 211 u_prog+=" Symbol of the result of the %s function\n"%f[0] 212 u_prog+=" \"\"\"\n" 213 u_prog+=" def __init__(self,arg):\n" 214 u_prog+=" \"\"\"\n" 215 u_prog+=" initialization %s function with argument arg\n"%f[0] 216 u_prog+=" @param arg: argument of function\n" 217 u_prog+=" @type arg: L{Symbol}.\n" 218 u_prog+=" \"\"\"\n" 219 u_prog+=" Symbol.__init__(self,shape=arg.getShape(),dim=arg.getDim(),args=[arg])\n" 220 u_prog+=" def __str__(self):\n" 221 u_prog+=" \"\"\"\n" 222 u_prog+=" string representation of the object\n" 223 u_prog+=" @rtype: C{str}\n" 224 u_prog+=" \"\"\"\n" 225 u_prog+=" return \"%s(%%s)\"%%str(self.getArgument(0))\n"%f[0] 226 u_prog+=" def substitute(self,argvals):\n" 227 u_prog+=" \"\"\"\n" 228 u_prog+=" assigns new values to symbols in the definition of the symbol\n The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.\n\n" 229 u_prog+=" @param argvals: new values assigned to symbols\n" 230 u_prog+=" @type argvals: C{dict} with keywords of type L{Symbol}.\n" 231 u_prog+=" @return: result of the substitution process. Operations are executed as much as possible.\"" 232 u_prog+=" @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution\n" 233 u_prog+=" raise: TypeError: if a value for a L{Symbol} cannot be substituted.\n" 234 u_prog+=" \"\"\"\n" 235 u_prog+=" if argval.has_key(self): \n" 236 u_prog+=" if self.isAppropriateValue(arg):\n" 237 u_prog+=" return argval[self] \n" 238 u_prog+=" else:\n" 239 u_prog+=" raise TypeError,\"%s: new value is not appropriate.\"\n" 240 u_prog+=" else:\n" 241 u_prog+=" arg=self.getSubstitutedArguments(argvals)[0]\n" 242 u_prog+=" return %s(arg)\n"%f[0] 243 u_prog+=" def diff(self,arg):\n" 244 u_prog+=" \"\"\"\n" 245 u_prog+=" returns the derivative of the symbol with respect to L{Symbol} arg\n" 246 247 u_prog+=" @param arg: the derivative is calculated with respect to arg\n" 248 u_prog+=" @type arg: typically L{escript.Symbol} but can also be C{float}, L{escript.Data}, L{numarray.NumArray} depending the involved functions and data.\n" 249 u_prog+=" @raise NotImplementedError: derivative not available.\n" 250 u_prog+=" \"\"\"\n" 251 u_prog+=" if arg==self: \n" 252 u_prog+=" return identity(self.getShape())\n" 253 u_prog+=" else:\n" 254 u_prog+=" darg=self.getDifferentiatedArguments(arg)[0]\n" 255 u_prog+=" raise NotImplementedError,\"%s: derivative is not available.\"\n"%f[0] 256 u_prog+=" # dself=?\n" 257 u_prog+=" return dself.matchShape(darg)*darg\n" 258 259 260 for args in ["float","array","constData","taggedData","expandedData","symbol"]: 261 for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]: 262 if not args=="float" or len(sh)==0: 263 t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" 264 tname="def test_%s_%s_rank%s"%(f[0],args,len(sh)) 265 t_prog+=" %s(self):\n"%tname 266 if args in ["float","array" ]: 267 a=makeArray(sh,f[1]) 268 r=makeResult(a,f) 269 if len(sh)==0: 270 t_prog+=" arg=%s\n"%a 271 t_prog+=" ref=%s\n"%r 272 else: 273 t_prog+=" arg=numarray.array(%s)\n"%a.tolist() 274 t_prog+=" ref=numarray.array(%s)\n"%r.tolist() 275 t_prog+=" res=%s(arg)\n"%f[0] 276 if args=="float": 277 t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n" 278 else: 279 t_prog+=" self.failUnlessEqual(res.shape,%s,\"wrong shape of result.\")\n"%str(sh) 280 t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n" 281 elif args== "constData": 282 a=makeArray(sh,f[1]) 283 r=makeResult(a,f) 284 if len(sh)==0: 285 t_prog+=" arg=Data(%s,self.functionspace)\n"%(a) 286 t_prog+=" ref=%s\n"%r 287 else: 288 t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist()) 289 t_prog+=" ref=numarray.array(%s)\n"%r.tolist() 290 t_prog+=" res=%s(arg)\n"%f[0] 291 t_prog+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh) 292 t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n" 293 elif args in [ "taggedData","expandedData"]: 294 a=makeArray(sh,f[1]) 295 r=makeResult(a,f) 296 a1=makeArray(sh,f[1]) 297 r1=makeResult(a1,f) 298 if len(sh)==0: 299 if args=="expandedData": 300 t_prog+=" arg=Data(%s,self.functionspace,True)\n"%(a) 301 t_prog+=" ref=Data(%s,self.functionspace,True)\n"%(r) 302 else: 303 t_prog+=" arg=Data(%s,self.functionspace)\n"%(a) 304 t_prog+=" ref=Data(%s,self.functionspace)\n"%(r) 305 t_prog+=" arg.setTaggedValue(1,%s)\n"%a 306 t_prog+=" ref.setTaggedValue(1,%s)\n"%r1 307 else: 308 if args=="expandedData": 309 t_prog+=" arg=Data(numarray.array(%s),self.functionspace,True)\n"%(a.tolist()) 310 t_prog+=" ref=Data(numarray.array(%s),self.functionspace,True)\n"%(r.tolist()) 311 else: 312 t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist()) 313 t_prog+=" ref=Data(numarray.array(%s),self.functionspace)\n"%(r.tolist()) 314 t_prog+=" arg.setTaggedValue(1,%s)\n"%a1.tolist() 315 t_prog+=" ref.setTaggedValue(1,%s)\n"%r1.tolist() 316 t_prog+=" res=%s(arg)\n"%f[0] 317 t_prog+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh) 318 t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n" 319 elif args=="symbol": 320 t_prog+=" arg=Symbol(shape=%s)\n"%str(sh) 321 t_prog+=" v=%s(arg)\n"%f[0] 322 t_prog+=" self.failUnlessRaises(ValueError,v.substitute,Symbol(shape=(1,1)),\"illegal shape of substitute not identified.\")\n" 323 a=makeArray(sh,f[1]) 324 r=makeResult(a,f) 325 if len(sh)==0: 326 t_prog+=" res=v.substitute({arg : %s})\n"%a 327 t_prog+=" ref=%s\n"%r 328 t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n" 329 else: 330 t_prog+=" res=v.substitute({arg : numarray.array(%s)})\n"%a.tolist() 331 t_prog+=" ref=numarray.array(%s)\n"%r.tolist() 332 t_prog+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of substitution result.\")\n"%str(sh) 333 t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n" 334 335 if len(sh)==0: 336 t_prog+=" # test derivative with respect to itself:\n" 337 t_prog+=" dvdv=v.diff(v)\n" 338 t_prog+=" self.failUnlessEqual(dvdv,1.,\"derivative with respect to self is not 1.\")\n" 339 elif len(sh)==1: 340 t_prog+=" # test derivative with respect to itself:\n" 341 t_prog+=" dvdv=v.diff(v)\n" 342 t_prog+=" self.failUnlessEqual(dvdv.shape,%s,\"shape of derivative with respect is wrong\")\n"%str(sh+sh) 343 for i0_l in range(sh[0]): 344 for i0_r in range(sh[0]): 345 if i0_l == i0_r: 346 v=1. 347 else: 348 v=0. 349 t_prog+=" self.failUnlessEqual(dvdv[%s,%s],%s,\"derivative with respect to self: [%s,%s] is not %s\")\n"%(i0_l,i0_r,v,i0_l,i0_r,v) 350 elif len(sh)==2: 351 t_prog+=" # test derivative with respect to itself:\n" 352 t_prog+=" dvdv=v.diff(v)\n" 353 t_prog+=" self.failUnlessEqual(dvdv.shape,%s,\"shape of derivative with respect is wrong\")\n"%str(sh+sh) 354 for i0_l in range(sh[0]): 355 for i0_r in range(sh[0]): 356 for i1_l in range(sh[1]): 357 for i1_r in range(sh[1]): 358 if i0_l == i0_r and i1_l == i1_r: 359 v=1. 360 else: 361 v=0. 362 t_prog+=" self.failUnlessEqual(dvdv[%s,%s,%s,%s],%s,\"derivative with respect to self: [%s,%s,%s,%s] is not %s\")\n"%(i0_l,i1_l,i0_r,i1_r,v,i0_l,i1_l,i0_r,i1_r,v) 363 364 for sh_in in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]: 365 if len(sh_in)+len(sh)<=4: 366 367 t_prog+=" # test derivative with shape %s as argument\n"%str(sh_in) 368 trafo=makeArray(sh+sh_in,[0,1.]) 369 a_in=makeArray(sh_in,f[1]) 370 t_prog+=" arg_in=Symbol(shape=%s)\n"%str(sh_in) 371 t_prog+=" arg2=Symbol(shape=%s)\n"%str(sh) 372 373 if len(sh)==0: 374 t_prog+=" arg2=" 375 if len(sh_in)==0: 376 t_prog+="%s*arg_in\n"%trafo 377 elif len(sh_in)==1: 378 for i0 in range(sh_in[0]): 379 if i0>0: t_prog+="+" 380 t_prog+="%s*arg_in[%s]"%(trafo[i0],i0) 381 t_prog+="\n" 382 elif len(sh_in)==2: 383 for i0 in range(sh_in[0]): 384 for i1 in range(sh_in[1]): 385 if i0+i1>0: t_prog+="+" 386 t_prog+="%s*arg_in[%s,%s]"%(trafo[i0,i1],i0,i1) 387 388 elif len(sh_in)==3: 389 for i0 in range(sh_in[0]): 390 for i1 in range(sh_in[1]): 391 for i2 in range(sh_in[2]): 392 if i0+i1+i2>0: t_prog+="+" 393 t_prog+="%s*arg_in[%s,%s,%s]"%(trafo[i0,i1,i2],i0,i1,i2) 394 elif len(sh_in)==4: 395 for i0 in range(sh_in[0]): 396 for i1 in range(sh_in[1]): 397 for i2 in range(sh_in[2]): 398 for i3 in range(sh_in[3]): 399 if i0+i1+i2+i3>0: t_prog+="+" 400 t_prog+="%s*arg_in[%s,%s,%s,%s]"%(trafo[i0,i1,i2,i3],i0,i1,i2,i3) 401 t_prog+="\n" 402 elif len(sh)==1: 403 for j0 in range(sh[0]): 404 t_prog+=" arg2[%s]="%j0 405 if len(sh_in)==0: 406 t_prog+="%s*arg_in"%trafo[j0] 407 elif len(sh_in)==1: 408 for i0 in range(sh_in[0]): 409 if i0>0: t_prog+="+" 410 t_prog+="%s*arg_in[%s]"%(trafo[j0,i0],i0) 411 elif len(sh_in)==2: 412 for i0 in range(sh_in[0]): 413 for i1 in range(sh_in[1]): 414 if i0+i1>0: t_prog+="+" 415 t_prog+="%s*arg_in[%s,%s]"%(trafo[j0,i0,i1],i0,i1) 416 elif len(sh_in)==3: 417 for i0 in range(sh_in[0]): 418 for i1 in range(sh_in[1]): 419 for i2 in range(sh_in[2]): 420 if i0+i1+i2>0: t_prog+="+" 421 t_prog+="%s*arg_in[%s,%s,%s]"%(trafo[j0,i0,i1,i2],i0,i1,i2) 422 t_prog+="\n" 423 elif len(sh)==2: 424 for j0 in range(sh[0]): 425 for j1 in range(sh[1]): 426 t_prog+=" arg2[%s,%s]="%(j0,j1) 427 if len(sh_in)==0: 428 t_prog+="%s*arg_in"%trafo[j0,j1] 429 elif len(sh_in)==1: 430 for i0 in range(sh_in[0]): 431 if i0>0: t_prog+="+" 432 t_prog+="%s*arg_in[%s]"%(trafo[j0,j1,i0],i0) 433 elif len(sh_in)==2: 434 for i0 in range(sh_in[0]): 435 for i1 in range(sh_in[1]): 436 if i0+i1>0: t_prog+="+" 437 t_prog+="%s*arg_in[%s,%s]"%(trafo[j0,j1,i0,i1],i0,i1) 438 t_prog+="\n" 439 elif len(sh)==3: 440 for j0 in range(sh[0]): 441 for j1 in range(sh[1]): 442 for j2 in range(sh[2]): 443 t_prog+=" arg2[%s,%s,%s]="%(j0,j1,j2) 444 if len(sh_in)==0: 445 t_prog+="%s*arg_in"%trafo[j0,j1,j2] 446 elif len(sh_in)==1: 447 for i0 in range(sh_in[0]): 448 if i0>0: t_prog+="+" 449 t_prog+="%s*arg_in[%s]"%(trafo[j0,j1,j2,i0],i0) 450 t_prog+="\n" 451 elif len(sh)==4: 452 for j0 in range(sh[0]): 453 for j1 in range(sh[1]): 454 for j2 in range(sh[2]): 455 for j3 in range(sh[3]): 456 t_prog+=" arg2[%s,%s,%s,%s]="%(j0,j1,j2,j3) 457 if len(sh_in)==0: 458 t_prog+="%s*arg_in"%trafo[j0,j1,j2,j3] 459 t_prog+="\n" 460 t_prog+=" dvdin=v.substitute({arg : arg2}).diff(arg_in)\n" 461 if len(sh_in)==0: 462 t_prog+=" res_in=dvdin.substitute({arg_in : %s})\n"%a_in 463 else: 464 t_prog+=" res_in=dvdin.substitute({arg : numarray.array(%s)})\n"%a_in.tolist() 465 466 if len(sh)==0: 467 if len(sh_in)==0: 468 ref_diff=(makeResult(trafo*a_in+finc,f)-makeResult(trafo*a_in,f))/finc 469 t_prog+=" self.failUnlessAlmostEqual(dvdin,%s,self.places,\"%s-derivative: wrong derivative\")\n"%(ref_diff,str(sh_in)) 470 elif len(sh_in)==1: 471 s=0 472 for k0 in range(sh_in[0]): 473 s+=trafo[k0]*a_in[k0] 474 for i0 in range(sh_in[0]): 475 ref_diff=(makeResult(s+trafo[i0]*finc,f)-makeResult(s,f))/finc 476 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s],%s,self.places,\"%s-derivative: wrong derivative with respect of %s\")\n"%(i0,ref_diff,str(sh_in),str(i0)) 477 elif len(sh_in)==2: 478 s=0 479 for k0 in range(sh_in[0]): 480 for k1 in range(sh_in[1]): 481 s+=trafo[k0,k1]*a_in[k0,k1] 482 for i0 in range(sh_in[0]): 483 for i1 in range(sh_in[1]): 484 ref_diff=(makeResult(s+trafo[i0,i1]*finc,f)-makeResult(s,f))/finc 485 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s],%s,self.places,\"%s-derivative: wrong derivative with respect of %s\")\n"%(i0,i1,ref_diff,str(sh_in),str((i0,i1))) 486 487 elif len(sh_in)==3: 488 s=0 489 for k0 in range(sh_in[0]): 490 for k1 in range(sh_in[1]): 491 for k2 in range(sh_in[2]): 492 s+=trafo[k0,k1,k2]*a_in[k0,k1,k2] 493 for i0 in range(sh_in[0]): 494 for i1 in range(sh_in[1]): 495 for i2 in range(sh_in[2]): 496 ref_diff=(makeResult(s+trafo[i0,i1,i2]*finc,f)-makeResult(s,f))/finc 497 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative with respect of %s\")\n"%(i0,i1,i2,ref_diff,str(sh_in),str((i0,i1,i2))) 498 elif len(sh_in)==4: 499 s=0 500 for k0 in range(sh_in[0]): 501 for k1 in range(sh_in[1]): 502 for k2 in range(sh_in[2]): 503 for k3 in range(sh_in[3]): 504 s+=trafo[k0,k1,k2,k3]*a_in[k0,k1,k2,k3] 505 for i0 in range(sh_in[0]): 506 for i1 in range(sh_in[1]): 507 for i2 in range(sh_in[2]): 508 for i3 in range(sh_in[3]): 509 ref_diff=(makeResult(s+trafo[i0,i1,i2,i3]*finc,f)-makeResult(s,f))/finc 510 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative with respect of %s\")\n"%(i0,i1,i2,i3,ref_diff,str(sh_in),str((i0,i1,i2,i3))) 511 elif len(sh)==1: 512 for j0 in range(sh[0]): 513 if len(sh_in)==0: 514 ref_diff=(makeResult(trafo[j0]*a_in+finc,f)-makeResult(trafo[j0]*a_in,f))/finc 515 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s],%s,self.places,\"%s-derivative: wrong derivative of %s\")\n"%(j0,ref_diff,str(sh_in),j0) 516 elif len(sh_in)==1: 517 s=0 518 for k0 in range(sh_in[0]): 519 s+=trafo[j0,k0]*a_in[k0] 520 for i0 in range(sh_in[0]): 521 ref_diff=(makeResult(s+trafo[j0,i0]*finc,f)-makeResult(s,f))/finc 522 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,i0,ref_diff,str(sh_in),str(j0),str(i0)) 523 elif len(sh_in)==2: 524 s=0 525 for k0 in range(sh_in[0]): 526 for k1 in range(sh_in[1]): 527 s+=trafo[j0,k0,k1]*a_in[k0,k1] 528 for i0 in range(sh_in[0]): 529 for i1 in range(sh_in[1]): 530 ref_diff=(makeResult(s+trafo[j0,i0,i1]*finc,f)-makeResult(s,f))/finc 531 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,i0,i1,ref_diff,str(sh_in),str(j0),str((i0,i1))) 532 533 elif len(sh_in)==3: 534 535 s=0 536 for k0 in range(sh_in[0]): 537 for k1 in range(sh_in[1]): 538 for k2 in range(sh_in[2]): 539 s+=trafo[j0,k0,k1,k2]*a_in[k0,k1,k2] 540 541 for i0 in range(sh_in[0]): 542 for i1 in range(sh_in[1]): 543 for i2 in range(sh_in[2]): 544 ref_diff=(makeResult(s+trafo[j0,i0,i1,i2]*finc,f)-makeResult(s,f))/finc 545 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,i0,i1,i2,ref_diff,str(sh_in),str(j0),str((i0,i1,i2))) 546 elif len(sh)==2: 547 for j0 in range(sh[0]): 548 for j1 in range(sh[1]): 549 if len(sh_in)==0: 550 ref_diff=(makeResult(trafo[j0,j1]*a_in+finc,f)-makeResult(trafo[j0,j1]*a_in,f))/finc 551 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s\")\n"%(j0,j1,ref_diff,str(sh_in),str((j0,j1))) 552 elif len(sh_in)==1: 553 s=0 554 for k0 in range(sh_in[0]): 555 s+=trafo[j0,j1,k0]*a_in[k0] 556 for i0 in range(sh_in[0]): 557 ref_diff=(makeResult(s+trafo[j0,j1,i0]*finc,f)-makeResult(s,f))/finc 558 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,j1,i0,ref_diff,str(sh_in),str((j0,j1)),str(i0)) 559 560 elif len(sh_in)==2: 561 s=0 562 for k0 in range(sh_in[0]): 563 for k1 in range(sh_in[1]): 564 s+=trafo[j0,j1,k0,k1]*a_in[k0,k1] 565 for i0 in range(sh_in[0]): 566 for i1 in range(sh_in[1]): 567 ref_diff=(makeResult(s+trafo[j0,j1,i0,i1]*finc,f)-makeResult(s,f))/finc 568 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,j1,i0,i1,ref_diff,str(sh_in),str((j0,j1)),str((i0,i1))) 569 elif len(sh)==3: 570 for j0 in range(sh[0]): 571 for j1 in range(sh[1]): 572 for j2 in range(sh[2]): 573 if len(sh_in)==0: 574 ref_diff=(makeResult(trafo[j0,j1,j2]*a_in+finc,f)-makeResult(trafo[j0,j1,j2]*a_in,f))/finc 575 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s\")\n"%(j0,j1,j2,ref_diff,str(sh_in),str((j0,j1,j2))) 576 elif len(sh_in)==1: 577 s=0 578 for k0 in range(sh_in[0]): 579 s+=trafo[j0,j1,j2,k0]*a_in[k0] 580 for i0 in range(sh_in[0]): 581 ref_diff=(makeResult(s+trafo[j0,j1,j2,i0]*finc,f)-makeResult(s,f))/finc 582 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,j1,j2,i0,ref_diff,str(sh_in),str((j0,j1,j2)),i0) 583 elif len(sh)==4: 584 for j0 in range(sh[0]): 585 for j1 in range(sh[1]): 586 for j2 in range(sh[2]): 587 for j3 in range(sh[3]): 588 if len(sh_in)==0: 589 ref_diff=(makeResult(trafo[j0,j1,j2,j3]*a_in+finc,f)-makeResult(trafo[j0,j1,j2,j3]*a_in,f))/finc 590 t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s\")\n"%(j0,j1,j2,j3,ref_diff,str(sh_in),str((j0,j1,j2,j3))) 591 592 # print u_prog 593 print t_prog

## Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision