# Diff of /trunk/escript/py_src/generateutil

revision 784 by gross, Fri Mar 10 04:45:04 2006 UTC revision 785 by gross, Tue Jul 25 03:48:10 2006 UTC
# Line 153  def getResultCaseForBin(case0,case1): Line 153  def getResultCaseForBin(case0,case1):
153  def makeArray(shape,rng):  def makeArray(shape,rng):
154     l=rng[1]-rng[0]     l=rng[1]-rng[0]
155     out=numarray.zeros(shape,numarray.Float64)     out=numarray.zeros(shape,numarray.Float64)
156     if len(shape)==0:     if isinstance(l,int):
157         out=l*random.random()+rng[0]       if len(shape)==0:
158     elif len(shape)==1:           out=int(l*random.random()+rng[0])*1.
159         for i0 in range(shape[0]):       elif len(shape)==1:
160                     out[i0]=l*random.random()+rng[0]           for i0 in range(shape[0]):
161     elif len(shape)==2:                       out[i0]=int(l*random.random()+rng[0])
162         for i0 in range(shape[0]):       elif len(shape)==2:
163            for i1 in range(shape[1]):           for i0 in range(shape[0]):
164                     out[i0,i1]=l*random.random()+rng[0]              for i1 in range(shape[1]):
165     elif len(shape)==3:                       out[i0,i1]=int(l*random.random()+rng[0])
166         for i0 in range(shape[0]):       elif len(shape)==3:
167            for i1 in range(shape[1]):           for i0 in range(shape[0]):
168               for i2 in range(shape[2]):              for i1 in range(shape[1]):
169                     out[i0,i1,i2]=l*random.random()+rng[0]                 for i2 in range(shape[2]):
170     elif len(shape)==4:                       out[i0,i1,i2]=int(l*random.random()+rng[0])
171         for i0 in range(shape[0]):       elif len(shape)==4:
172            for i1 in range(shape[1]):           for i0 in range(shape[0]):
173               for i2 in range(shape[2]):              for i1 in range(shape[1]):
174                  for i3 in range(shape[3]):                 for i2 in range(shape[2]):
175                     out[i0,i1,i2,i3]=l*random.random()+rng[0]                    for i3 in range(shape[3]):
176     elif len(shape)==5:                       out[i0,i1,i2,i3]=int(l*random.random()+rng[0])
177         for i0 in range(shape[0]):       elif len(shape)==5:
178            for i1 in range(shape[1]):           for i0 in range(shape[0]):
179               for i2 in range(shape[2]):              for i1 in range(shape[1]):
180                  for i3 in range(shape[3]):                 for i2 in range(shape[2]):
181                    for i4 in range(shape[4]):                    for i3 in range(shape[3]):
182                     out[i0,i1,i2,i3,i4]=l*ranm.random()+rng[0]                      for i4 in range(shape[4]):
183                         out[i0,i1,i2,i3,i4]=int(l*ranm.random()+rng[0])
184         else:
185             raise SystemError,"rank is restricted to 5"
186     else:     else:
187         raise SystemError,"rank is restricted to 5"       if len(shape)==0:
188             out=l*random.random()+rng[0]
189         elif len(shape)==1:
190             for i0 in range(shape[0]):
191                         out[i0]=l*random.random()+rng[0]
192         elif len(shape)==2:
193             for i0 in range(shape[0]):
194                for i1 in range(shape[1]):
195                         out[i0,i1]=l*random.random()+rng[0]
196         elif len(shape)==3:
197             for i0 in range(shape[0]):
198                for i1 in range(shape[1]):
199                   for i2 in range(shape[2]):
200                         out[i0,i1,i2]=l*random.random()+rng[0]
201         elif len(shape)==4:
202             for i0 in range(shape[0]):
203                for i1 in range(shape[1]):
204                   for i2 in range(shape[2]):
205                      for i3 in range(shape[3]):
206                         out[i0,i1,i2,i3]=l*random.random()+rng[0]
207         elif len(shape)==5:
208             for i0 in range(shape[0]):
209                for i1 in range(shape[1]):
210                   for i2 in range(shape[2]):
211                      for i3 in range(shape[3]):
212                        for i4 in range(shape[4]):
213                         out[i0,i1,i2,i3,i4]=l*ranm.random()+rng[0]
214         else:
215             raise SystemError,"rank is restricted to 5"
216     return out             return out
217
218  def makeNumberedArray(shape,s=1.):  def makeNumberedArray(shape,s=1.):
# Line 505  def mkText(case,name,a,a1=None,use_taggi Line 536  def mkText(case,name,a,a1=None,use_taggi
536                   t_out+="      %s.setTaggedValue(1,numarray.array(%s))\n"%(name,a1.tolist())                   t_out+="      %s.setTaggedValue(1,numarray.array(%s))\n"%(name,a1.tolist())
537                t_out+="      %s.expand()\n"%name                          t_out+="      %s.expand()\n"%name
538             else:             else:
539                t_out+="      msk_%s=whereZero(self.functionspace.getX()[0],1.e-8)\n"%name                t_out+="      msk_%s=1-whereZero(self.functionspace.getX()[0],1.e-8)\n"%name
540                if isinstance(a,float):                if isinstance(a,float):
541                     t_out+="      %s=(1.-msk_%s)*(%s)+msk_%s*(%s)\n"%(name,name,a,name,a1)                     t_out+="      %s=msk_%s*(%s)+(1-msk_%s)*(%s)\n"%(name,name,a,name,a1)
542                elif a.rank==0:                elif a.rank==0:
543                     t_out+="      %s=msk_%s*numarray.array(%s)+(1.-msk_%s)*numarray.array(%s)\n"%(name,name,a,name,a1)                     t_out+="      %s=msk_%s*numarray.array(%s)+(1.-msk_%s)*numarray.array(%s)\n"%(name,name,a,name,a1)
544                else:                else:
# Line 549  def mkCode(txt,args=[],intend=""): Line 580  def mkCode(txt,args=[],intend=""):
580      for r in args:      for r in args:
581        out=out.replace("%%a%s%%"%c,r)        out=out.replace("%%a%s%%"%c,r)
582      return out        return out
583    #=================================================
584    def testMatrixMult(arg0,arg1,sh_s):
585         return numarray.matrixmultiply(arg0,arg1)
586
587    def shapeTestMatrixMult(sh0,sh1,sh_s):
588     if len(sh_s)==1 and len(sh0+sh_s)==2 and (len(sh_s+sh1)==1 or len(sh_s+sh1)==2):
589            return sh0+sh_s,sh_s+sh1
590
591    def testTensorMult(arg0,arg1,sh_s):
592         if arg0.rank==2:
593            return numarray.matrixmultiply(arg0,arg1)
594         else:
595            if arg1.rank==4:
596              out=numarray.zeros((arg0.shape[0],arg0.shape[1],arg1.shape[2],arg1.shape[3]),numarray.Float)
597              for i0 in range(arg0.shape[0]):
598               for i1 in range(arg0.shape[1]):
599                for i2 in range(arg0.shape[2]):
600                 for i3 in range(arg0.shape[3]):
601                  for j2 in range(arg1.shape[2]):
602                   for j3 in range(arg1.shape[3]):
603                         out[i0,i1,j2,j3]+=arg0[i0,i1,i2,i3]*arg1[i2,i3,j2,j3]
604            elif arg1.rank==3:
605              out=numarray.zeros((arg0.shape[0],arg0.shape[1],arg1.shape[2]),numarray.Float)
606              for i0 in range(arg0.shape[0]):
607               for i1 in range(arg0.shape[1]):
608                for i2 in range(arg0.shape[2]):
609                 for i3 in range(arg0.shape[3]):
610                  for j2 in range(arg1.shape[2]):
611                         out[i0,i1,j2]+=arg0[i0,i1,i2,i3]*arg1[i2,i3,j2]
612            elif arg1.rank==2:
613              out=numarray.zeros((arg0.shape[0],arg0.shape[1]),numarray.Float)
614              for i0 in range(arg0.shape[0]):
615               for i1 in range(arg0.shape[1]):
616                for i2 in range(arg0.shape[2]):
617                 for i3 in range(arg0.shape[3]):
618                         out[i0,i1]+=arg0[i0,i1,i2,i3]*arg1[i2,i3]
619            return out
620
621    def shapeTestTensorMult(sh0,sh1,sh_s):
622     if ( len(sh_s)==1 and len(sh0+sh_s)==2 and ( len(sh1+sh_s)==2 or len(sh1+sh_s)==1 )) or (len(sh_s)==2 and len(sh0+sh_s)==4 and (len(sh1+sh_s)==2 or len(sh1+sh_s)==3 or len(sh1+sh_s)==4)):
623            return sh0+sh_s,sh_s+sh1
624
625    def tensorProductTest(arg0,arg1,sh_s):
626        if isinstance(arg0,float):
627           out=numarray.array(arg0*arg1)
628        elif isinstance(arg1,float):
629           out=numarray.array(arg0*arg1)
630        elif len(sh_s)==0:
631           out=numarray.outerproduct(arg0,arg1).resize(arg0.shape+arg1.shape)
632        else:
633           l=len(sh_s)
634           sh0=arg0.shape[:arg0.rank-l]
635           sh1=arg1.shape[l:]
636           ls,l0,l1=1,1,1
637           for i in sh_s: ls*=i
638           for i in sh0: l0*=i
639           for i in sh1: l1*=i
640           out1=numarray.outerproduct(arg0,arg1).resize((l0,ls,ls,l1))
641           out2=numarray.zeros((l0,l1),numarray.Float)
642           for i0 in range(l0):
643              for i1 in range(l1):
644                  for i in range(ls): out2[i0,i1]+=out1[i0,i,i,i1]
645           out=out2.resize(sh0+sh1)
646        return out
647    def tensorProductShape(sh0,sh1,sh_s):
648            return sh0+sh_s,sh_s+sh1
649
650    def transpose(r,offset):
651        if isinstance(r,float): return r
652        s=r.shape
653        s1=1
654        for i in s[:offset]: s1*=i
655        s2=1
656        for i in s[offset:]: s2*=i
657        out=numarray.reshape(r,(s1,s2))
658        out.transpose()
659        return numarray.resize(out,s[offset:]+s[:offset])
660
661    def shapeTestTMatrixMult(sh0,sh1,sh_s):
662     if len(sh_s)==1 and len(sh0+sh_s)==2 and (len(sh_s+sh1)==1 or len(sh_s+sh1)==2):
663            return sh_s+sh0,sh_s+sh1
664
665    def testTMatrixMult(arg0,arg1,sh_s):
666         return numarray.matrixmultiply(transpose(arg0,1),arg1)
667
668    def shapeTestTTensorMult(sh0,sh1,sh_s):
669     if ( len(sh_s)==1 and len(sh0+sh_s)==2 and ( len(sh1+sh_s)==2 or len(sh1+sh_s)==1 )) or (len(sh_s)==2 and len(sh0+sh_s)==4 and (len(sh1+sh_s)==2 or len(sh1+sh_s)==3 or len(sh1+sh_s)==4)):
670            return sh_s+sh0,sh_s+sh1
671
672    def testTTensorMult(arg0,arg1,sh_s):
673         if arg0.rank==2:
674            return numarray.matrixmultiply(transpose(arg0,1),arg1)
675         else:
676            if arg1.rank==4:
677              out=numarray.zeros((arg0.shape[2],arg0.shape[3],arg1.shape[2],arg1.shape[3]),numarray.Float)
678              for i0 in range(arg0.shape[2]):
679               for i1 in range(arg0.shape[3]):
680                for i2 in range(arg0.shape[0]):
681                 for i3 in range(arg0.shape[1]):
682                  for j2 in range(arg1.shape[2]):
683                   for j3 in range(arg1.shape[3]):
684                         out[i0,i1,j2,j3]+=arg0[i2,i3,i0,i1]*arg1[i2,i3,j2,j3]
685            elif arg1.rank==3:
686              out=numarray.zeros((arg0.shape[2],arg0.shape[3],arg1.shape[2]),numarray.Float)
687              for i0 in range(arg0.shape[2]):
688               for i1 in range(arg0.shape[3]):
689                for i2 in range(arg0.shape[0]):
690                 for i3 in range(arg0.shape[1]):
691                  for j2 in range(arg1.shape[2]):
692                         out[i0,i1,j2]+=arg0[i2,i3,i0,i1]*arg1[i2,i3,j2]
693            elif arg1.rank==2:
694              out=numarray.zeros((arg0.shape[2],arg0.shape[3]),numarray.Float)
695              for i0 in range(arg0.shape[2]):
696               for i1 in range(arg0.shape[3]):
697                for i2 in range(arg0.shape[0]):
698                 for i3 in range(arg0.shape[1]):
699                         out[i0,i1]+=arg0[i2,i3,i0,i1]*arg1[i2,i3]
700            return out
701
702    def tensorTProductShape(sh0,sh1,sh_s):
703            return sh_s+sh0,sh_s+sh1
704
705    def tensorTProductTest(arg0,arg1,sh_s):
706        if isinstance(arg0,float):
707           out=numarray.array(arg0*arg1)
708        elif isinstance(arg1,float):
709           out=numarray.array(arg0*arg1)
710        elif len(sh_s)==0:
711           out=numarray.outerproduct(arg0,arg1).resize(arg0.shape+arg1.shape)
712        else:
713           l=len(sh_s)
714           sh0=arg0.shape[l:]
715           sh1=arg1.shape[l:]
716           ls,l0,l1=1,1,1
717           for i in sh_s: ls*=i
718           for i in sh0: l0*=i
719           for i in sh1: l1*=i
720           out1=numarray.outerproduct(arg0,arg1).resize((ls,l0,ls,l1))
721           out2=numarray.zeros((l0,l1),numarray.Float)
722           for i0 in range(l0):
723              for i1 in range(l1):
724                  for i in range(ls): out2[i0,i1]+=out1[i,i0,i,i1]
725           out=out2.resize(sh0+sh1)
726        return out
727
728    def shapeTestMatrixTMult(sh0,sh1,sh_s):
729     if len(sh_s)==1 and len(sh0+sh_s)==2 and len(sh_s+sh1)==2:
730         return sh0+sh_s,sh1+sh_s
731
732    def testMatrixTMult(arg0,arg1,sh_s):
733         return numarray.matrixmultiply(arg0,transpose(arg1,1))
734
735    def shapeTestTensorTMult(sh0,sh1,sh_s):
736     if ( len(sh_s)==1 and len(sh0+sh_s)==2 and ( len(sh1+sh_s)==2 or len(sh1+sh_s)==1 )) or (len(sh_s)==2 and len(sh0+sh_s)==4 and (len(sh1+sh_s)==2 or len(sh1+sh_s)==3 or len(sh1+sh_s)==4)):
737            return sh0+sh_s,sh1+sh_s
738
739    def testTensorTMult(arg0,arg1,sh_s):
740         if arg0.rank==2:
741            return numarray.matrixmultiply(arg0,transpose(arg1,1))
742         else:
743            if arg1.rank==4:
744              out=numarray.zeros((arg0.shape[0],arg0.shape[1],arg1.shape[0],arg1.shape[1]),numarray.Float)
745              for i0 in range(arg0.shape[0]):
746               for i1 in range(arg0.shape[1]):
747                for i2 in range(arg0.shape[2]):
748                 for i3 in range(arg0.shape[3]):
749                  for j0 in range(arg1.shape[0]):
750                   for j1 in range(arg1.shape[1]):
751                         out[i0,i1,j0,j1]+=arg0[i0,i1,i2,i3]*arg1[j0,j1,i2,i3]
752            elif arg1.rank==3:
753              out=numarray.zeros((arg0.shape[0],arg0.shape[1],arg1.shape[0]),numarray.Float)
754              for i0 in range(arg0.shape[0]):
755               for i1 in range(arg0.shape[1]):
756                for i2 in range(arg0.shape[2]):
757                 for i3 in range(arg0.shape[3]):
758                  for j0 in range(arg1.shape[0]):
759                         out[i0,i1,j0]+=arg0[i0,i1,i2,i3]*arg1[j0,i2,i3]
760            elif arg1.rank==2:
761              out=numarray.zeros((arg0.shape[0],arg0.shape[1]),numarray.Float)
762              for i0 in range(arg0.shape[0]):
763               for i1 in range(arg0.shape[1]):
764                for i2 in range(arg0.shape[2]):
765                 for i3 in range(arg0.shape[3]):
766                         out[i0,i1]+=arg0[i0,i1,i2,i3]*arg1[i2,i3]
767            return out
768
769    def tensorProductTTest(arg0,arg1,sh_s):
770        if isinstance(arg0,float):
771           out=numarray.array(arg0*arg1)
772        elif isinstance(arg1,float):
773           out=numarray.array(arg0*arg1)
774        elif len(sh_s)==0:
775           out=numarray.outerproduct(arg0,arg1).resize(arg0.shape+arg1.shape)
776        else:
777           l=len(sh_s)
778           sh0=arg0.shape[:arg0.rank-l]
779           sh1=arg1.shape[:arg1.rank-l]
780           ls,l0,l1=1,1,1
781           for i in sh_s: ls*=i
782           for i in sh0: l0*=i
783           for i in sh1: l1*=i
784           out1=numarray.outerproduct(arg0,arg1).resize((l0,ls,l1,ls))
785           out2=numarray.zeros((l0,l1),numarray.Float)
786           for i0 in range(l0):
787              for i1 in range(l1):
788                  for i in range(ls): out2[i0,i1]+=out1[i0,i,i1,i]
789           out=out2.resize(sh0+sh1)
790        return out
791
792    def tensorProductTShape(sh0,sh1,sh_s):
793            return sh0+sh_s,sh1+sh_s
794
795    #=======================================================================================================
796    # tensor multiply
797    #=======================================================================================================
798    # oper=
799    # oper=
800    for oper in [ ["tensor_mult",testTensorMult,shapeTestTensorMult], \
801                  ["generalTensorProduct",tensorProductTest,tensorProductShape], \
802                  ["matrix_mult",testMatrixMult,shapeTestMatrixMult], \
803                  ["transposed_matrix_mult",testTMatrixMult,shapeTestTMatrixMult], \
804                  ["transposed_tensor_mult",testTTensorMult,shapeTestTTensorMult], \
805                  ["generalTransposedTensorProduct",tensorTProductTest,tensorTProductShape], \
806                  ["matrix_transposed_mult",testMatrixTMult,shapeTestMatrixTMult], \
807                  ["tensor_transposed_mult",testTensorTMult,shapeTestTensorTMult], \
808                  ["generalTensorTransposedProduct",tensorProductTTest,tensorProductTShape] ]:
809
810     for case0 in ["float","array","Symbol","constData","taggedData","expandedData"]:
811      for sh0 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
812       for case1 in ["float","array","Symbol","constData","taggedData","expandedData"]:
813         for sh1 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
814           for sh_s in [ (),(3,), (2,3), (2,4,3),(4,2,3,2)]:
815              if (len(sh0+sh_s)==0 or not case0=="float") and (len(sh1+sh_s)==0 or not case1=="float") \
816                   and len(sh0+sh1)<5 and len(sh0+sh_s)<5 and len(sh1+sh_s)<5:
817                arg_shape=oper[2](sh0,sh1,sh_s)
818                if not arg_shape==None:
819                  case=getResultCaseForBin(case0,case1)
820                  use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
821                  text="   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
822                  if oper[0] in [ "generalTensorProduct", "generalTransposedTensorProduct", "generalTensorTransposedProduct"]:
823                    tname="test_%s_%s_rank%s_%s_rank%s_offset%s"%(oper[0],case0,len(sh0+sh_s),case1,len(sh_s+sh1),len(sh_s))
824
825                    res_text="      res=%s(arg0,arg1,axis_offset=%s)\n"%(oper[0],len(sh_s))
826                  else:
827                    tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0+sh_s),case1,len(sh_s+sh1))
828
829                    res_text="      res=%s(arg0,arg1)\n"%oper[0]
830
831                  text+="   def %s(self):\n"%tname
832                  a_0=makeArray(arg_shape[0],[-8,8])
833                  if case0 in ["taggedData", "expandedData"]:
834                      a1_0=makeArray(arg_shape[0],[-8,8])
835                  else:
836                      a1_0=a_0
837
838                  a_1=makeArray(arg_shape[1],[-8,8])
839                  if case1 in ["taggedData", "expandedData"]:
840                      a1_1=makeArray(arg_shape[1],[-8,8])
841                  else:
842                      a1_1=a_1
843                  r=oper[1](a_0,a_1,sh_s)
844                  r1=oper[1](a1_0,a1_1,sh_s)
845                  text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
846                  text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
847                  text+=res_text
848                  if case=="Symbol":
849                     c0_res,c1_res=case0,case1
850                     subs="{"
851                     if case0=="Symbol":
852                        text+=mkText("array","s0",a_0,a1_0)
853                        subs+="arg0:s0"
854                        c0_res="array"
855                     if case1=="Symbol":
856                        text+=mkText("array","s1",a_1,a1_1)
857                        if not subs.endswith("{"): subs+=","
858                        subs+="arg1:s1"
859                        c1_res="array"
860                     subs+="}"
861                     text+="      sub=res.substitute(%s)\n"%subs
862                     res="sub"
863                     text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
864                  else:
865                     res="res"
866                     text+=mkText(case,"ref",r,r1)
867                  text+=mkTypeAndShapeTest(case,sh0+sh1,"res")
868                  text+="      self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
869                  if case0 == "taggedData" or case1 == "taggedData":
870                      t_prog_with_tags+=text
871                  else:
872                      t_prog+=text