/[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 536 by gross, Fri Feb 17 03:20:53 2006 UTC revision 550 by gross, Wed Feb 22 02:14:38 2006 UTC
# Line 3286  def symmetric(arg): Line 3286  def symmetric(arg):
3286      @return: symmetric part of arg      @return: symmetric part of arg
3287      @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
3288      """      """
3289      return (arg+transpose(arg))/2      if isinstance(arg,numarray.NumArray):
3290          if arg.rank==2:
3291            if not (arg.shape[0]==arg.shape[1]):
3292               raise ValueError,"symmetric: argument must be square."
3293          elif arg.rank==4:
3294            if not (arg.shape[0]==arg.shape[2] and arg.shape[1]==arg.shape[3]):
3295               raise ValueError,"symmetric: argument must be square."
3296          else:
3297            raise ValueError,"symmetric: rank 2 or 4 is required."
3298          return (arg+transpose(arg))/2
3299        elif isinstance(arg,escript.Data):
3300          return escript_symmetric(arg)
3301        elif isinstance(arg,float):
3302          return arg
3303        elif isinstance(arg,int):
3304          return float(arg)
3305        elif isinstance(arg,Symbol):
3306          if arg.getRank()==2:
3307            if not (arg.getShape()[0]==arg.getShape()[1]):
3308               raise ValueError,"symmetric: argument must be square."
3309          elif arg.getRank()==4:
3310            if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):
3311               raise ValueError,"symmetric: argument must be square."
3312          else:
3313            raise ValueError,"symmetric: rank 2 or 4 is required."
3314          return (arg+transpose(arg))/2
3315        else:
3316          raise TypeError,"symmetric: Unknown argument type."
3317    
3318    def escript_symmetric(arg): # this should be implemented in c++
3319          if arg.getRank()==2:
3320            if not (arg.getShape()[0]==arg.getShape()[1]):
3321               raise ValueError,"escript_symmetric: argument must be square."
3322            out=escript.Data(0.,arg.getShape(),arg.getFunctionSpace())
3323            for i0 in range(arg.getShape()[0]):
3324               for i1 in range(arg.getShape()[1]):
3325                  out[i0,i1]=(arg[i0,i1]+arg[i1,i0])/2.
3326          elif arg.getRank()==4:
3327            if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):
3328               raise ValueError,"escript_symmetric: argument must be square."
3329            out=escript.Data(0.,arg.getShape(),arg.getFunctionSpace())
3330            for i0 in range(arg.getShape()[0]):
3331               for i1 in range(arg.getShape()[1]):
3332                  for i2 in range(arg.getShape()[2]):
3333                     for i3 in range(arg.getShape()[3]):
3334                         out[i0,i1,i2,i3]=(arg[i0,i1,i2,i3]+arg[i2,i3,i0,i1])/2.
3335          else:
3336            raise ValueError,"escript_symmetric: rank 2 or 4 is required."
3337          return out
3338    
3339  def nonsymmetric(arg):  def nonsymmetric(arg):
3340      """      """
# Line 3294  def nonsymmetric(arg): Line 3342  def nonsymmetric(arg):
3342    
3343      @param arg: square matrix. Must have rank 2 or 4 and be square.      @param arg: square matrix. Must have rank 2 or 4 and be square.
3344      @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}      @type arg: L{numarray.NumArray}, L{escript.Data}, L{Symbol}
3345      @return: symmetric part of arg      @return: nonsymmetric part of arg
3346      @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
3347      """      """
3348      return (arg-transpose(arg))/2      if isinstance(arg,numarray.NumArray):
3349          if arg.rank==2:
3350            if not (arg.shape[0]==arg.shape[1]):
3351               raise ValueError,"nonsymmetric: argument must be square."
3352          elif arg.rank==4:
3353            if not (arg.shape[0]==arg.shape[2] and arg.shape[1]==arg.shape[3]):
3354               raise ValueError,"nonsymmetric: argument must be square."
3355          else:
3356            raise ValueError,"nonsymmetric: rank 2 or 4 is required."
3357          return (arg-transpose(arg))/2
3358        elif isinstance(arg,escript.Data):
3359          return escript_nonsymmetric(arg)
3360        elif isinstance(arg,float):
3361          return arg
3362        elif isinstance(arg,int):
3363          return float(arg)
3364        elif isinstance(arg,Symbol):
3365          if arg.getRank()==2:
3366            if not (arg.getShape()[0]==arg.getShape()[1]):
3367               raise ValueError,"nonsymmetric: argument must be square."
3368          elif arg.getRank()==4:
3369            if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):
3370               raise ValueError,"nonsymmetric: argument must be square."
3371          else:
3372            raise ValueError,"nonsymmetric: rank 2 or 4 is required."
3373          return (arg-transpose(arg))/2
3374        else:
3375          raise TypeError,"nonsymmetric: Unknown argument type."
3376    
3377    def escript_nonsymmetric(arg): # this should be implemented in c++
3378          if arg.getRank()==2:
3379            if not (arg.getShape()[0]==arg.getShape()[1]):
3380               raise ValueError,"escript_nonsymmetric: argument must be square."
3381            out=escript.Data(0.,arg.getShape(),arg.getFunctionSpace())
3382            for i0 in range(arg.getShape()[0]):
3383               for i1 in range(arg.getShape()[1]):
3384                  out[i0,i1]=(arg[i0,i1]-arg[i1,i0])/2.
3385          elif arg.getRank()==4:
3386            if not (arg.getShape()[0]==arg.getShape()[2] and arg.getShape()[1]==arg.getShape()[3]):
3387               raise ValueError,"escript_nonsymmetric: argument must be square."
3388            out=escript.Data(0.,arg.getShape(),arg.getFunctionSpace())
3389            for i0 in range(arg.getShape()[0]):
3390               for i1 in range(arg.getShape()[1]):
3391                  for i2 in range(arg.getShape()[2]):
3392                     for i3 in range(arg.getShape()[3]):
3393                         out[i0,i1,i2,i3]=(arg[i0,i1,i2,i3]-arg[i2,i3,i0,i1])/2.
3394          else:
3395            raise ValueError,"escript_nonsymmetric: rank 2 or 4 is required."
3396          return out
3397    
3398    
3399  def inverse(arg):  def inverse(arg):
3400      """      """

Legend:
Removed from v.536  
changed lines
  Added in v.550

  ViewVC Help
Powered by ViewVC 1.1.26