00001 __copyright__ = """
00002 Copyright 2008 Sean Ross-Ross
00003 """
00004 __license__ = """
00005 This file is part of SLIMpy .
00006
00007 SLIMpy is free software: you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation, either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 SLIMpy is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00019 """
00020
00021 from slimpy_base.Core.User.Structures.VectorType import VectorType
00022 from slimpy_base.Core.User.Structures.serial_vector import Vector
00023 from slimpy_base.User.AumentedMatrix.AugmentedBase import AugmentBase
00024 from slimpy_base.api.functions.scalar_functions import scalar_max,scalar_mean,scalar_min
00025 from numpy import average, sum as npsum
00026 from numpy import inner,outer
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class AugVector( AugmentBase ):
00038 """
00039 class containing vectors
00040 used in an augmented system of equations
00041 \see slimpy_base.Core.User.Structures.serial_vector.Vector
00042 """
00043
00044 _contained_type = Vector
00045 __metaclass__ = VectorType
00046
00047 __transpose_flag = False
00048
00049 def _get_meta(self):
00050 if not hasattr(self, '_meta_space' ):
00051 self._meta_space = None
00052 return self._meta_space
00053
00054 def _set_meta(self,val):
00055 self._meta_space = val
00056
00057 meta = property(_get_meta, _set_meta )
00058
00059 def _get_meta_vec(self):
00060 if hasattr(self, "_meta_vector" ):
00061 return self._meta_vector
00062 else:
00063 return None
00064
00065 def _set_meta_vec( self, val ):
00066
00067 self._meta_vector = val
00068
00069 meta_vector = property( _get_meta_vec, _set_meta_vec )
00070
00071 def _get_params(self):
00072 return self.space
00073
00074 params = property( _get_params )
00075
00076 def _get_transp_flag(self):
00077 return self.__transpose_flag
00078 def _set_transp_flag(self,val):
00079 self.__transpose_flag = bool(val)
00080
00081
00082 def _conj_transp(self):
00083 pkw_obj = self.__pk_expannder__( )
00084 aug_vec = self.__attr_func__( '_conj_transp' ,pkw_obj )
00085 aug_vec.is_transp = not self.is_transp
00086 return aug_vec
00087
00088 H = property( _conj_transp )
00089
00090
00091 def __mul__( self, other ):
00092 isvec = (type(type(other) ) == VectorType)
00093 if isvec and (self.is_transp ^ other.is_transp):
00094 if self.is_transp:
00095 return inner( self.flat, other.flat )
00096 else:
00097 return outer( self, other )
00098 else:
00099 return AugmentBase.__mul__(self, other)
00100
00101 is_transp = property( _get_transp_flag, _set_transp_flag )
00102
00103 def thr( self, obj, mode='soft' ):
00104 pkw_obj = self.__pk_expannder__( obj, mode=mode )
00105 return self.__attr_func__( 'thr', pkw_obj )
00106
00107 def flush( self ):
00108 pkw_obj = self.__pk_expannder__( )
00109 self.__attr_func__('flush', pkw_obj)
00110
00111 def setName(self,name):
00112 pkw_obj = self.__pk_expannder__( name )
00113 self.__attr_func__('setName', pkw_obj)
00114
00115 def noise(self,mean=0):
00116 pkw_obj = self.__pk_expannder__( mean=mean )
00117 return self.__attr_func__('noise', pkw_obj)
00118
00119 def __real__(self):
00120 pkw_obj = self.__pk_expannder__( )
00121 return self.__attr_func__('real', pkw_obj)
00122
00123 def __imag__(self):
00124 pkw_obj = self.__pk_expannder__( )
00125 return self.__attr_func__('imag', pkw_obj)
00126
00127 real = __real__
00128 imag = __imag__
00129
00130 def getSpace(self):
00131 "getSpace"
00132 from slimpy_base.User.AumentedMatrix.MetaSpace import MetaSpace
00133
00134 pkw_obj = self.__pk_expannder__( )
00135 new = self.__attr_func__('getSpace', pkw_obj)
00136
00137 new_meta = new.view( MetaSpace )
00138 new_meta._contained_type = MetaSpace._contained_type
00139
00140 new_meta = new_meta.ravel( )
00141 new_meta.meta = self.meta
00142 return new_meta
00143
00144 def orderstat(self , ind):
00145 """
00146 gets the ind-th element of the sorted self
00147
00148 returns an average of the ind-th element in all vectors
00149 """
00150 glo = self.space.get_size( )
00151 loc = self.space.get_local_sizes( )
00152
00153 perc = loc * float(ind) / glo
00154
00155
00156 pkw_obj = self.__pk_expannder__( perc.astype(int) )
00157 ostat = self.__attr_func__( 'orderstat', pkw_obj )
00158
00159 return average( ostat.reshape(-1) )
00160
00161 def rms( self ):
00162 """ Returns the root mean square"""
00163 raise NotImplementedError
00164
00165 def max( self ):
00166 "Returns the maximum value contained in the vector"
00167 pkw_obj = self.__pk_expannder__( )
00168 mmax = self.__attr_func__('max', pkw_obj)
00169 mmax = mmax.reshape(-1).tolist()
00170 return scalar_max( mmax )
00171
00172 def min( self ):
00173 """ Returns the minimum value"""
00174 pkw_obj = self.__pk_expannder__( )
00175 mmin = self.__attr_func__('min', pkw_obj)
00176 mmin = mmin.reshape(-1).tolist()
00177 return scalar_min( mmin )
00178
00179 def mean( self ):
00180 "Returns the mean value"
00181 pkw_obj = self.__pk_expannder__( )
00182 means = self.__attr_func__( 'mean', pkw_obj )
00183 means = means.reshape(-1).tolist()
00184
00185 return scalar_mean( means )
00186
00187
00188 def var( self ):
00189 "Returns the variance"
00190 raise NotImplementedError
00191 def sdt( self ):
00192 "Returns the std deviation"
00193 raise NotImplementedError
00194
00195 def norm( self, lval=2 ):
00196 "Returns the lval norm of the vector"
00197 pkw_obj = self.__pk_expannder__( lval=lval )
00198 mnorm = self.__attr_func__( 'norm', pkw_obj )
00199
00200 if lval == 0:
00201 root = 0
00202 else:
00203 root = 1./lval
00204
00205 vec = mnorm ** lval
00206 nrm = npsum( vec ) ** root
00207
00208 return nrm
00209
00210 space = property( getSpace )