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.Interface.PSpace import PSpace
00022 from slimpy_base.Core.Interface.Structure import Structure
00023 from slimpy_base.Core.User.Structures.serial_vector import Vector
00024 from slimpy_base.User.AumentedMatrix.MetaSpace import MetaSpace
00025
00026
00027 class VectorSpace( PSpace, Structure ):
00028 """
00029 PSpace type that knows about vectors
00030 Class to track metadata of @ref slimpy_base.Core.User.Structures.serial_vector.Vector "Vector" objects
00031 @ingroup userclasses
00032 """
00033 def testCommand( self, cmd, *args, **kargs ):
00034 """
00035 returns the resulting space from applying the command
00036 but does not add the command to the graph
00037 @return new VectorSpace
00038 """
00039 new_space = Structure.testCommand( self, cmd, *args, **kargs )
00040 assert new_space is not None
00041
00042 if isinstance(new_space, MetaSpace):
00043 return new_space.copy()
00044 else:
00045 return self.__class__( new_space )
00046
00047 def create( self, out=0 ,istmp=None ):
00048 """
00049 space.create( self, out=0 ,istmp=None ) -> vector
00050 Create a vector within this vectorspace
00051 where the following evaluates to True:
00052 @code
00053 >>> vector in space
00054 @endcode
00055
00056 @return Vector
00057 """
00058 param = self.copy( )
00059 param.pop( 'out', None )
00060
00061 command = self.generate_command( "create", out=out, **self.params )
00062
00063
00064 data_container = self.makeContaner( command=command ,tmp=istmp)
00065
00066 converter = data_container.get_converter( command )
00067
00068 commpack = converter.convert( None, command )
00069
00070 commpack.target = data_container
00071 commpack.source = None
00072
00073 self.env['graphmgr'].graphAppend( commpack )
00074
00075 return Vector( data_container )
00076
00077 def noise( self, mean=0, seed=None, var=None ):
00078 """
00079 Create a vector within this vector space
00080 @param mean mean value of the noise
00081 @param seed random seed
00082 @param var variance
00083 """
00084 param = self.copy( )
00085 param.pop( 'out', None )
00086
00087 param.update(mean=mean)
00088 if seed is not None:
00089 param.update(seed=seed)
00090 if var is not None:
00091 param.update(var=var)
00092
00093 command = self.generate_command( "create_noise", **param.params )
00094
00095
00096 data_container = self.makeContaner( command=command )
00097
00098 converter = data_container.get_converter( command )
00099
00100 commpack = converter.convert( None, command )
00101
00102 commpack.target = data_container
00103 commpack.source = None
00104
00105 self.env['graphmgr'].graphAppend( commpack )
00106
00107 return Vector( data_container )
00108
00109 def zeros( self ):
00110 """
00111 Create a vector of zeros within this vector space
00112
00113 space.zeros( ) -> vector
00114 """
00115 return self.create()
00116
00117 def spike(self,**kw):
00118 """
00119 create a vector with spikes
00120 """
00121 space = self.copy( )
00122 space.pop( 'out', None )
00123 istmp = kw.pop( 'istmp', True )
00124
00125 params = self.params.copy()
00126 params.update( kw)
00127
00128 command = self.generate_command( "spike", **params )
00129
00130
00131 data_container = self.makeContaner( command=command ,tmp=istmp)
00132
00133 converter = data_container.get_converter( command )
00134
00135 commpack = converter.convert( None, command )
00136
00137 commpack.target = data_container
00138 commpack.source = None
00139
00140 self.env['graphmgr'].graphAppend( commpack )
00141
00142 return Vector( data_container )
00143
00144 def ones( self ):
00145 """
00146 Create a vector of ones within this vector space
00147 """
00148 return self.create( out=1 )
00149
00150 def isReal( self ):
00151 'test is the space contains real values'
00152 return self['data_type'] == 'float' or self['data_type'] == float
00153
00154
00155 def isComplex( self ):
00156 return self['data_type'] == 'complex' or self['data_type'] == complex
00157
00158 def isInt( self ):
00159 "test is the space contains integer values"
00160 return self['data_type'] == 'int' or self['data_type'] == int
00161
00162
00163 def VectorAddition(self, *other_spaces ):
00164 """
00165 VectorSpace.VectorAddition( *spaces ) -> New space
00166 """
00167 plugin = self.plugin
00168 addition = self.generate_command('add')
00169 newspace = self
00170
00171 for space in other_spaces:
00172 if isinstance( space, PSpace ):
00173 converter = plugin.get_converter( addition )
00174 converter.constr( addition, newspace, space )
00175 newspace = converter.trans( addition, newspace, space )
00176 else:
00177 pass
00178
00179 return newspace.copy( )
00180
00181 def VectorAddition( stuff , *default ):
00182 """
00183 space that would result from vector additions
00184 @ingroup userclasses
00185 @param stuff a list of vector spaces
00186 @param default object to return if no space instances are found in param stuff
00187 @exception TypeError if no spaces are in param stuff and default is not given
00188 @return VectorSpace
00189 @relatesalso slimpy_base.Core.User.Structures.VectorSpace.VectorSpace
00190 """
00191 if len(default) > 1 :
00192 raise TypeError( "too many arguments need at most two" )
00193
00194
00195 item = stuff.pop()
00196 while not isinstance(item, PSpace ):
00197 if not stuff:
00198 if not default:
00199 raise TypeError( "no Spaces found to add and no default specified" )
00200 else:
00201 return default[0]
00202
00203 item = stuff.pop()
00204
00205
00206 space = VectorSpace( item )
00207
00208 return space.VectorAddition( *stuff )
00209