45 |
|
|
46 |
# def maximum(arg0,arg1): |
# def maximum(arg0,arg1): |
47 |
# def minimum(arg0,arg1): |
# def minimum(arg0,arg1): |
48 |
# def minval(arg0): |
|
|
# def maxval(arg0): |
|
49 |
# def transpose(arg,axis=None): |
# def transpose(arg,axis=None): |
50 |
# def trace(arg,axis0=0,axis1=1): |
# def trace(arg,axis0=0,axis1=1): |
|
# def length(arg): |
|
51 |
# def reorderComponents(arg,index): |
# def reorderComponents(arg,index): |
52 |
|
|
53 |
# def integrate(arg,where=None): |
# def integrate(arg,where=None): |
2677 |
val=matchShape(sign(myarg),self.getDifferentiatedArguments(arg)[0]) |
val=matchShape(sign(myarg),self.getDifferentiatedArguments(arg)[0]) |
2678 |
return val[0]*val[1] |
return val[0]*val[1] |
2679 |
|
|
2680 |
|
def minval(arg): |
2681 |
|
""" |
2682 |
|
returns minimum value over all components of arg at each data point |
2683 |
|
|
2684 |
|
@param arg: argument |
2685 |
|
@type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}. |
2686 |
|
@rtype:C{float}, L{escript.Data}, L{Symbol} depending on the type of arg. |
2687 |
|
@raises TypeError: if the type of the argument is not expected. |
2688 |
|
""" |
2689 |
|
if isinstance(arg,numarray.NumArray): |
2690 |
|
return arg.min() |
2691 |
|
elif isinstance(arg,escript.Data): |
2692 |
|
return arg._minval() |
2693 |
|
elif isinstance(arg,float): |
2694 |
|
return arg |
2695 |
|
elif isinstance(arg,int): |
2696 |
|
return float(arg) |
2697 |
|
elif isinstance(arg,Symbol): |
2698 |
|
return Minval_Symbol(arg) |
2699 |
|
else: |
2700 |
|
raise TypeError,"minval: Unknown argument type." |
2701 |
|
|
2702 |
|
class Minval_Symbol(DependendSymbol): |
2703 |
|
""" |
2704 |
|
L{Symbol} representing the result of the minimum value function |
2705 |
|
""" |
2706 |
|
def __init__(self,arg): |
2707 |
|
""" |
2708 |
|
initialization of minimum value L{Symbol} with argument arg |
2709 |
|
@param arg: argument of function |
2710 |
|
@type arg: typically L{Symbol}. |
2711 |
|
""" |
2712 |
|
DependendSymbol.__init__(self,args=[arg],shape=(),dim=arg.getDim()) |
2713 |
|
|
2714 |
|
def getMyCode(self,argstrs,format="escript"): |
2715 |
|
""" |
2716 |
|
returns a program code that can be used to evaluate the symbol. |
2717 |
|
|
2718 |
|
@param argstrs: gives for each argument a string representing the argument for the evaluation. |
2719 |
|
@type argstrs: C{str} or a C{list} of length 1 of C{str}. |
2720 |
|
@param format: specifies the format to be used. At the moment only "escript" ,"text" and "str" are supported. |
2721 |
|
@type format: C{str} |
2722 |
|
@return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available. |
2723 |
|
@rtype: C{str} |
2724 |
|
@raise: NotImplementedError: if the requested format is not available |
2725 |
|
""" |
2726 |
|
if isinstance(argstrs,list): |
2727 |
|
argstrs=argstrs[0] |
2728 |
|
if format=="escript" or format=="str" or format=="text": |
2729 |
|
return "minval(%s)"%argstrs |
2730 |
|
else: |
2731 |
|
raise NotImplementedError,"Minval_Symbol does not provide program code for format %s."%format |
2732 |
|
|
2733 |
|
def substitute(self,argvals): |
2734 |
|
""" |
2735 |
|
assigns new values to symbols in the definition of the symbol. |
2736 |
|
The method replaces the L{Symbol} u by argvals[u] in the expression defining this object. |
2737 |
|
|
2738 |
|
@param argvals: new values assigned to symbols |
2739 |
|
@type argvals: C{dict} with keywords of type L{Symbol}. |
2740 |
|
@return: result of the substitution process. Operations are executed as much as possible. |
2741 |
|
@rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution |
2742 |
|
@raise TypeError: if a value for a L{Symbol} cannot be substituted. |
2743 |
|
""" |
2744 |
|
if argvals.has_key(self): |
2745 |
|
arg=argvals[self] |
2746 |
|
if self.isAppropriateValue(arg): |
2747 |
|
return arg |
2748 |
|
else: |
2749 |
|
raise TypeError,"%s: new value is not appropriate."%str(self) |
2750 |
|
else: |
2751 |
|
arg=self.getSubstitutedArguments(argvals)[0] |
2752 |
|
return minval(arg) |
2753 |
|
|
2754 |
|
def maxval(arg): |
2755 |
|
""" |
2756 |
|
returns maximum value over all components of arg at each data point |
2757 |
|
|
2758 |
|
@param arg: argument |
2759 |
|
@type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}. |
2760 |
|
@rtype:C{float}, L{escript.Data}, L{Symbol} depending on the type of arg. |
2761 |
|
@raises TypeError: if the type of the argument is not expected. |
2762 |
|
""" |
2763 |
|
if isinstance(arg,numarray.NumArray): |
2764 |
|
return arg.max() |
2765 |
|
elif isinstance(arg,escript.Data): |
2766 |
|
return arg._maxval() |
2767 |
|
elif isinstance(arg,float): |
2768 |
|
return arg |
2769 |
|
elif isinstance(arg,int): |
2770 |
|
return float(arg) |
2771 |
|
elif isinstance(arg,Symbol): |
2772 |
|
return Maxval_Symbol(arg) |
2773 |
|
else: |
2774 |
|
raise TypeError,"maxval: Unknown argument type." |
2775 |
|
|
2776 |
|
class Maxval_Symbol(DependendSymbol): |
2777 |
|
""" |
2778 |
|
L{Symbol} representing the result of the maximum value function |
2779 |
|
""" |
2780 |
|
def __init__(self,arg): |
2781 |
|
""" |
2782 |
|
initialization of maximum value L{Symbol} with argument arg |
2783 |
|
@param arg: argument of function |
2784 |
|
@type arg: typically L{Symbol}. |
2785 |
|
""" |
2786 |
|
DependendSymbol.__init__(self,args=[arg],shape=(),dim=arg.getDim()) |
2787 |
|
|
2788 |
|
def getMyCode(self,argstrs,format="escript"): |
2789 |
|
""" |
2790 |
|
returns a program code that can be used to evaluate the symbol. |
2791 |
|
|
2792 |
|
@param argstrs: gives for each argument a string representing the argument for the evaluation. |
2793 |
|
@type argstrs: C{str} or a C{list} of length 1 of C{str}. |
2794 |
|
@param format: specifies the format to be used. At the moment only "escript" ,"text" and "str" are supported. |
2795 |
|
@type format: C{str} |
2796 |
|
@return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available. |
2797 |
|
@rtype: C{str} |
2798 |
|
@raise: NotImplementedError: if the requested format is not available |
2799 |
|
""" |
2800 |
|
if isinstance(argstrs,list): |
2801 |
|
argstrs=argstrs[0] |
2802 |
|
if format=="escript" or format=="str" or format=="text": |
2803 |
|
return "maxval(%s)"%argstrs |
2804 |
|
else: |
2805 |
|
raise NotImplementedError,"Maxval_Symbol does not provide program code for format %s."%format |
2806 |
|
|
2807 |
|
def substitute(self,argvals): |
2808 |
|
""" |
2809 |
|
assigns new values to symbols in the definition of the symbol. |
2810 |
|
The method replaces the L{Symbol} u by argvals[u] in the expression defining this object. |
2811 |
|
|
2812 |
|
@param argvals: new values assigned to symbols |
2813 |
|
@type argvals: C{dict} with keywords of type L{Symbol}. |
2814 |
|
@return: result of the substitution process. Operations are executed as much as possible. |
2815 |
|
@rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution |
2816 |
|
@raise TypeError: if a value for a L{Symbol} cannot be substituted. |
2817 |
|
""" |
2818 |
|
if argvals.has_key(self): |
2819 |
|
arg=argvals[self] |
2820 |
|
if self.isAppropriateValue(arg): |
2821 |
|
return arg |
2822 |
|
else: |
2823 |
|
raise TypeError,"%s: new value is not appropriate."%str(self) |
2824 |
|
else: |
2825 |
|
arg=self.getSubstitutedArguments(argvals)[0] |
2826 |
|
return maxval(arg) |
2827 |
|
|
2828 |
|
def length(arg): |
2829 |
|
""" |
2830 |
|
returns length/Euclidean norm of argument arg at each data point |
2831 |
|
|
2832 |
|
@param arg: argument |
2833 |
|
@type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}. |
2834 |
|
@rtype:C{float}, L{escript.Data}, L{Symbol} depending on the type of arg. |
2835 |
|
""" |
2836 |
|
return sqrt(inner(arg,arg)) |
2837 |
|
|
2838 |
#======================================================= |
#======================================================= |
2839 |
# Binary operations: |
# Binary operations: |
3248 |
dargs=self.getDifferentiatedArguments(arg) |
dargs=self.getDifferentiatedArguments(arg) |
3249 |
return mult(self,add(mult(log(myargs[0]),dargs[1]),mult(quotient(myargs[1],myargs[0]),dargs[0]))) |
return mult(self,add(mult(log(myargs[0]),dargs[1]),mult(quotient(myargs[1],myargs[0]),dargs[0]))) |
3250 |
|
|
3251 |
def maximum(arg0,arg1): |
def maximum(*args): |
3252 |
""" |
""" |
3253 |
the maximum of the two arguments |
the maximum over arguments args |
3254 |
|
|
3255 |
@param arg0: first argument |
@param args: arguments |
3256 |
@type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} |
@type args: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} |
3257 |
@param arg1: second argument |
@return: is on object which gives at each entry the maximum of the coresponding values all args |
|
@type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} |
|
|
@return: is on object which has the shape of arg0+arg1 and gives at each entry the maximum of the coresponding values of arg0 and arg1. |
|
3258 |
@rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} depending on the input |
@rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} depending on the input |
3259 |
""" |
""" |
3260 |
m=whereNegative(arg0-arg1) |
out=None |
3261 |
return m*arg1+(1.-m)*arg0 |
for a in args: |
3262 |
|
if out==None: |
3263 |
|
out=a |
3264 |
|
else: |
3265 |
|
m=whereNegative(out-a) |
3266 |
|
out=m*a+(1.-m)*out |
3267 |
|
return out |
3268 |
|
|
3269 |
def minimum(arg0,arg1): |
def minimum(*arg): |
3270 |
""" |
""" |
3271 |
the minumum of the two arguments |
the minimum over arguments args |
3272 |
|
|
3273 |
@param arg0: first argument |
@param args: arguments |
3274 |
@type arg0: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} |
@type args: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} |
3275 |
@param arg1: second argument |
@return: is on object which gives at each entry the minimum of the coresponding values all args |
|
@type arg1: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} |
|
|
@return: is on object which has the shape of arg0+arg1 and gives at each entry the minimum of the coresponding values of arg0 and arg1. |
|
3276 |
@rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} depending on the input |
@rtype: L{numarray.NumArray}, L{escript.Data}, L{Symbol}, C{int} or C{float} depending on the input |
3277 |
""" |
""" |
3278 |
m=whereNegative(arg0-arg1) |
out=None |
3279 |
return m*arg0+(1.-m)*arg1 |
for a in args: |
3280 |
|
if out==None: |
3281 |
|
out=a |
3282 |
|
else: |
3283 |
|
m=whereNegative(out-a) |
3284 |
|
out=m*out+(1.-m)*a |
3285 |
|
return out |
3286 |
|
|
3287 |
def inner(arg0,arg1): |
def inner(arg0,arg1): |
3288 |
""" |
""" |
3429 |
else: |
else: |
3430 |
if not arg0.shape[arg0.rank-offset:]==arg1.shape[:offset]: |
if not arg0.shape[arg0.rank-offset:]==arg1.shape[:offset]: |
3431 |
raise ValueError,"generalTensorProduct: dimensions of last %s components in left argument don't match the first %s components in the right argument."%(offset,offset) |
raise ValueError,"generalTensorProduct: dimensions of last %s components in left argument don't match the first %s components in the right argument."%(offset,offset) |
3432 |
|
arg0_c=arg0.copy() |
3433 |
|
arg1_c=arg1.copy() |
3434 |
sh0,sh1=arg0.shape,arg1.shape |
sh0,sh1=arg0.shape,arg1.shape |
3435 |
d0,d1,d01=1,1,1 |
d0,d1,d01=1,1,1 |
3436 |
for i in sh0[:arg0.rank-offset]: d0*=i |
for i in sh0[:arg0.rank-offset]: d0*=i |
3437 |
for i in sh1[offset:]: d1*=i |
for i in sh1[offset:]: d1*=i |
3438 |
for i in sh1[:offset]: d01*=i |
for i in sh1[:offset]: d01*=i |
3439 |
arg0.resize((d0,d01)) |
arg0_c.resize((d0,d01)) |
3440 |
arg1.resize((d01,d1)) |
arg1_c.resize((d01,d1)) |
3441 |
out=numarray.zeros((d0,d1),numarray.Float) |
out=numarray.zeros((d0,d1),numarray.Float) |
3442 |
for i0 in range(d0): |
for i0 in range(d0): |
3443 |
for i1 in range(d1): |
for i1 in range(d1): |
3444 |
out[i0,i1]=numarray.sum(arg0[i0,:]*arg1[:,i1]) |
out[i0,i1]=numarray.sum(arg0_c[i0,:]*arg1_c[:,i1]) |
|
arg0.resize(sh0) |
|
|
arg1.resize(sh1) |
|
3445 |
out.resize(sh0[:arg0.rank-offset]+sh1[offset:]) |
out.resize(sh0[:arg0.rank-offset]+sh1[offset:]) |
3446 |
return out |
return out |
3447 |
elif isinstance(arg0,escript.Data): |
elif isinstance(arg0,escript.Data): |
3516 |
return generalTensorProduct(args[0],args[1],args[2]) |
return generalTensorProduct(args[0],args[1],args[2]) |
3517 |
|
|
3518 |
def escript_generalTensorProduct(arg0,arg1,offset): # this should be escript._generalTensorProduct |
def escript_generalTensorProduct(arg0,arg1,offset): # this should be escript._generalTensorProduct |
3519 |
"arg0 and arg1 are both Data objects but not neccesrily on the same function space!!!" |
"arg0 and arg1 are both Data objects but not neccesrily on the same function space. they could be identical!!!" |
3520 |
# calculate the return shape: |
# calculate the return shape: |
3521 |
shape0=arg0.getShape()[:arg0.getRank()-offset] |
shape0=arg0.getShape()[:arg0.getRank()-offset] |
3522 |
shape01=arg0.getShape()[arg0.getRank()-offset:] |
shape01=arg0.getShape()[arg0.getRank()-offset:] |
3656 |
|
|
3657 |
# functions involving the underlying Domain: |
# functions involving the underlying Domain: |
3658 |
|
|
|
|
|
|
# functions returning Data objects: |
|
|
|
|
|
def minval(arg): |
|
|
return arg._minval() |
|
|
|
|
|
def maxval(arg): |
|
|
return arg._maxval() |
|
|
|
|
3659 |
def transpose(arg,axis=None): |
def transpose(arg,axis=None): |
3660 |
""" |
""" |
3661 |
Returns the transpose of the Data object arg. |
Returns the transpose of the Data object arg. |
3707 |
else: |
else: |
3708 |
return numarray.trace(arg,axis0=axis0,axis1=axis1) |
return numarray.trace(arg,axis0=axis0,axis1=axis1) |
3709 |
|
|
|
def length(arg): |
|
|
""" |
|
|
|
|
|
@param arg: |
|
|
""" |
|
|
if isinstance(arg,escript.Data): |
|
|
if arg.isEmpty(): return escript.Data() |
|
|
if arg.getRank()==0: |
|
|
return abs(arg) |
|
|
elif arg.getRank()==1: |
|
|
out=escript.Scalar(0,arg.getFunctionSpace()) |
|
|
for i in range(arg.getShape()[0]): |
|
|
out+=arg[i]**2 |
|
|
return sqrt(out) |
|
|
elif arg.getRank()==2: |
|
|
out=escript.Scalar(0,arg.getFunctionSpace()) |
|
|
for i in range(arg.getShape()[0]): |
|
|
for j in range(arg.getShape()[1]): |
|
|
out+=arg[i,j]**2 |
|
|
return sqrt(out) |
|
|
elif arg.getRank()==3: |
|
|
out=escript.Scalar(0,arg.getFunctionSpace()) |
|
|
for i in range(arg.getShape()[0]): |
|
|
for j in range(arg.getShape()[1]): |
|
|
for k in range(arg.getShape()[2]): |
|
|
out+=arg[i,j,k]**2 |
|
|
return sqrt(out) |
|
|
elif arg.getRank()==4: |
|
|
out=escript.Scalar(0,arg.getFunctionSpace()) |
|
|
for i in range(arg.getShape()[0]): |
|
|
for j in range(arg.getShape()[1]): |
|
|
for k in range(arg.getShape()[2]): |
|
|
for l in range(arg.getShape()[3]): |
|
|
out+=arg[i,j,k,l]**2 |
|
|
return sqrt(out) |
|
|
else: |
|
|
raise SystemError,"length is not been fully implemented yet" |
|
|
# return arg.length() |
|
|
elif isinstance(arg,float): |
|
|
return abs(arg) |
|
|
else: |
|
|
return sqrt((arg**2).sum()) |
|
3710 |
|
|
3711 |
def reorderComponents(arg,index): |
def reorderComponents(arg,index): |
3712 |
""" |
""" |