440 |
@type arg0: L{numarray.NumArray},L{escript.Data},C{float}, C{int}, L{Symbol} |
@type arg0: L{numarray.NumArray},L{escript.Data},C{float}, C{int}, L{Symbol} |
441 |
@param arg1: a given object |
@param arg1: a given object |
442 |
@type arg1: L{numarray.NumArray},L{escript.Data},C{float}, C{int}, L{Symbol} |
@type arg1: L{numarray.NumArray},L{escript.Data},C{float}, C{int}, L{Symbol} |
443 |
@return: arg0 and arg1 where copies are returned when the shape has to be changed. |
@return: C{arg0} and C{arg1} where copies are returned when the shape has to be changed. |
444 |
@rtype: C{tuple} |
@rtype: C{tuple} |
445 |
""" |
""" |
446 |
sh=commonShape(arg0,arg1) |
sh=commonShape(arg0,arg1) |
2930 |
|
|
2931 |
@param arg: argument |
@param arg: argument |
2932 |
@type arg: L{escript.Data}, L{Symbol}, L{numarray.NumArray}. |
@type arg: L{escript.Data}, L{Symbol}, L{numarray.NumArray}. |
2933 |
@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 |
2934 |
axis_offset and axis_offset+1 must be equal. |
C{axis_offset} and axis_offset+1 must be equal. |
2935 |
@type axis_offset: C{int} |
@type axis_offset: C{int} |
2936 |
@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. |
2937 |
@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. |
2982 |
initialization of trace L{Symbol} with argument arg |
initialization of trace L{Symbol} with argument arg |
2983 |
@param arg: argument of function |
@param arg: argument of function |
2984 |
@type arg: L{Symbol}. |
@type arg: L{Symbol}. |
2985 |
@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 |
2986 |
axis_offset and axis_offset+1 must be equal. |
C{axis_offset} and axis_offset+1 must be equal. |
2987 |
@type axis_offset: C{int} |
@type axis_offset: C{int} |
2988 |
""" |
""" |
2989 |
if arg.getRank()<2: |
if arg.getRank()<2: |
3049 |
|
|
3050 |
def transpose(arg,axis_offset=None): |
def transpose(arg,axis_offset=None): |
3051 |
""" |
""" |
3052 |
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. |
3053 |
|
|
3054 |
@param arg: argument |
@param arg: argument |
3055 |
@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} |
3056 |
@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. |
3057 |
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. |
3058 |
@type axis_offset: C{int} |
@type axis_offset: C{int} |
3059 |
@return: transpose of arg |
@return: transpose of arg |
3060 |
@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. |
3092 |
|
|
3093 |
@param arg: argument of function |
@param arg: argument of function |
3094 |
@type arg: L{Symbol}. |
@type arg: L{Symbol}. |
3095 |
@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. |
3096 |
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. |
3097 |
@type axis_offset: C{int} |
@type axis_offset: C{int} |
3098 |
""" |
""" |
3099 |
if axis_offset==None: axis_offset=int(arg.getRank()/2) |
if axis_offset==None: axis_offset=int(arg.getRank()/2) |
3100 |
if axis_offset<0 or axis_offset>arg.getRank(): |
if axis_offset<0 or axis_offset>arg.getRank(): |
3101 |
raise ValueError,"axis_offset must be between 0 and %s"%r |
raise ValueError,"axis_offset must be between 0 and %s"%arg.getRank() |
3102 |
s=arg.getShape() |
s=arg.getShape() |
3103 |
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()) |
3104 |
|
|
3153 |
return identity(self.getShape()) |
return identity(self.getShape()) |
3154 |
else: |
else: |
3155 |
return transpose(self.getDifferentiatedArguments(arg)[0],axis_offset=self.getArgument()[1]) |
return transpose(self.getDifferentiatedArguments(arg)[0],axis_offset=self.getArgument()[1]) |
3156 |
|
|
3157 |
|
def swap_axes(arg,axis0=0,axis1=1): |
3158 |
|
""" |
3159 |
|
returns the swap of arg by swaping the components axis0 and axis1 |
3160 |
|
|
3161 |
|
@param arg: argument |
3162 |
|
@type arg: L{escript.Data}, L{Symbol}, L{numarray.NumArray}. |
3163 |
|
@param axis0: axis. C{axis0} must be non-negative and less than the rank of arg. |
3164 |
|
@type axis0: C{int} |
3165 |
|
@param axis1: axis. C{axis1} must be non-negative and less than the rank of arg. |
3166 |
|
@type axis1: C{int} |
3167 |
|
@return: C{arg} with swaped components |
3168 |
|
@rtype: L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg. |
3169 |
|
""" |
3170 |
|
if axis0 > axis1: |
3171 |
|
axis0,axis1=axis1,axis0 |
3172 |
|
if isinstance(arg,numarray.NumArray): |
3173 |
|
return numarray.swapaxes(arg,axis0,axis1) |
3174 |
|
elif isinstance(arg,escript.Data): |
3175 |
|
return arg._swap_axes(axis0,axis1) |
3176 |
|
elif isinstance(arg,float): |
3177 |
|
raise TyepError,"float argument is not supported." |
3178 |
|
elif isinstance(arg,int): |
3179 |
|
raise TyepError,"int argument is not supported." |
3180 |
|
elif isinstance(arg,Symbol): |
3181 |
|
return SwapAxes_Symbol(arg,axis0,axis1) |
3182 |
|
else: |
3183 |
|
raise TypeError,"Unknown argument type." |
3184 |
|
|
3185 |
|
class SwapAxes_Symbol(DependendSymbol): |
3186 |
|
""" |
3187 |
|
L{Symbol} representing the result of the swap function |
3188 |
|
""" |
3189 |
|
def __init__(self,arg,axis0=0,axis1=1): |
3190 |
|
""" |
3191 |
|
initialization of swap L{Symbol} with argument arg |
3192 |
|
|
3193 |
|
@param arg: argument |
3194 |
|
@type arg: L{Symbol}. |
3195 |
|
@param axis0: axis. C{axis0} must be non-negative and less than the rank of arg. |
3196 |
|
@type axis0: C{int} |
3197 |
|
@param axis1: axis. C{axis1} must be non-negative and less than the rank of arg. |
3198 |
|
@type axis1: C{int} |
3199 |
|
""" |
3200 |
|
if arg.getRank()<2: |
3201 |
|
raise ValueError,"argument must have at least rank 2." |
3202 |
|
if axis0<0 or axis0>arg.getRank()-1: |
3203 |
|
raise ValueError,"axis0 must be between 0 and %s"%arg.getRank()-1 |
3204 |
|
if axis1<0 or axis1>arg.getRank()-1: |
3205 |
|
raise ValueError,"axis1 must be between 0 and %s"%arg.getRank()-1 |
3206 |
|
if axis0 == axis1: |
3207 |
|
raise ValueError,"axis indices must be different." |
3208 |
|
if axis0 > axis1: |
3209 |
|
axis0,axis1=axis1,axis0 |
3210 |
|
s=arg.getShape() |
3211 |
|
s_out=[] |
3212 |
|
for i in range(len(s)): |
3213 |
|
if i == axis0: |
3214 |
|
s_out.append(s[axis1]) |
3215 |
|
elif i == axis1: |
3216 |
|
s_out.append(s[axis0]) |
3217 |
|
else: |
3218 |
|
s_out.append(s[i]) |
3219 |
|
super(SwapAxes_Symbol,self).__init__(args=[arg,axis0,axis1],shape=tuple(s_out),dim=arg.getDim()) |
3220 |
|
|
3221 |
|
def getMyCode(self,argstrs,format="escript"): |
3222 |
|
""" |
3223 |
|
returns a program code that can be used to evaluate the symbol. |
3224 |
|
|
3225 |
|
@param argstrs: gives for each argument a string representing the argument for the evaluation. |
3226 |
|
@type argstrs: C{str} or a C{list} of length 1 of C{str}. |
3227 |
|
@param format: specifies the format to be used. At the moment only "escript" ,"text" and "str" are supported. |
3228 |
|
@type format: C{str} |
3229 |
|
@return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available. |
3230 |
|
@rtype: C{str} |
3231 |
|
@raise NotImplementedError: if the requested format is not available |
3232 |
|
""" |
3233 |
|
if format=="escript" or format=="str" or format=="text": |
3234 |
|
return "swap(%s,axis_offset=%s)"%(argstrs[0],argstrs[1]) |
3235 |
|
else: |
3236 |
|
raise NotImplementedError,"SwapAxes_Symbol does not provide program code for format %s."%format |
3237 |
|
|
3238 |
|
def substitute(self,argvals): |
3239 |
|
""" |
3240 |
|
assigns new values to symbols in the definition of the symbol. |
3241 |
|
The method replaces the L{Symbol} u by argvals[u] in the expression defining this object. |
3242 |
|
|
3243 |
|
@param argvals: new values assigned to symbols |
3244 |
|
@type argvals: C{dict} with keywords of type L{Symbol}. |
3245 |
|
@return: result of the substitution process. Operations are executed as much as possible. |
3246 |
|
@rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution |
3247 |
|
@raise TypeError: if a value for a L{Symbol} cannot be substituted. |
3248 |
|
""" |
3249 |
|
if argvals.has_key(self): |
3250 |
|
arg=argvals[self] |
3251 |
|
if self.isAppropriateValue(arg): |
3252 |
|
return arg |
3253 |
|
else: |
3254 |
|
raise TypeError,"%s: new value is not appropriate."%str(self) |
3255 |
|
else: |
3256 |
|
arg=self.getSubstitutedArguments(argvals) |
3257 |
|
return swap_axes(arg[0],axis0=arg[1],axis1=arg[2]) |
3258 |
|
|
3259 |
|
def diff(self,arg): |
3260 |
|
""" |
3261 |
|
differential of this object |
3262 |
|
|
3263 |
|
@param arg: the derivative is calculated with respect to arg |
3264 |
|
@type arg: L{escript.Symbol} |
3265 |
|
@return: derivative with respect to C{arg} |
3266 |
|
@rtype: typically L{Symbol} but other types such as C{float}, L{escript.Data}, L{numarray.NumArray} are possible. |
3267 |
|
""" |
3268 |
|
if arg==self: |
3269 |
|
return identity(self.getShape()) |
3270 |
|
else: |
3271 |
|
return swap_axes(self.getDifferentiatedArguments(arg)[0],axis0=self.getArgument()[1],axis1=self.getArgument()[2]) |
3272 |
|
|
3273 |
def symmetric(arg): |
def symmetric(arg): |
3274 |
""" |
""" |
3275 |
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 |