Package teamwork :: Package math :: Module KeyedVector :: Class KeyedVector
[hide private]
[frames] | no frames]

Class KeyedVector

source code

Known Subclasses:

A dictionary-based representation of a one-dimensional vector

Instance Methods [hide private]
 
__init__(self, args={}) source code
Key[]
keys(self)
Returns: a consistently ordered list of keys
source code
 
setArray(self)
Now deprecated because of irrelevance.
source code
 
_updateString(self)
Updates the string representation of this vector, as needed to enforce consistent ordering (e.g., for hashing)
source code
 
addColumns(self, keys, values=None)
Adds new slots to this vector
source code
array
getArray(self)
Returns: the numeric array representation of this vector
source code
 
__getitem__(self, key) source code
 
__setitem__(self, key, value) source code
 
normalize(self)
Scales this vector so that the highest absolute weight is 1
source code
 
__len__(self) source code
 
items(self) source code
 
__eq__(self, other) source code
 
__delitem__(self, key) source code
 
has_key(self, key) source code
 
fill(self, keys, value=None)
Fills in any missing slots with a default value (it's really just a call to addColumns now)
source code
KeyedVector
instantiate(self, table)
Substitutes values for any abstract references, using the given substitution table
source code
 
instantiateKeys(self, table)
Substitutes values for any abstract references, using the given substitution table
source code
a new KeyedVector instance
compose(self, other, op)
Composes the two vectors together using the given operator
source code
 
freeze(self)
Locks in the dimensions and keys of this vector.
source code
bool
unfreeze(self)
Unlocks the dimensions and keys of this vector
source code
 
__add__(self, other) source code
 
__sub__(self, other) source code
 
__neg__(self) source code
 
__mul__(self, other)
If other is a KeyedVector, then the result is the dot product
source code
 
__rmul__(self, other) source code
 
__str__(self) source code
 
simpleText(self, numbers=True, all=False) source code
 
__hash__(self) source code
 
__xml__(self) source code
 
parse(self, element, changeInPlace=False)
Extracts the distribution from the given XML element
source code
 
__copy__(self) source code
 
__deepcopy__(self, memo) source code
Instance Variables [hide private]
array _array
The numeric representation of this vector
boolean _fresh
flag indicating whether the current array needs an update
boolean _frozen
flag indicating whether the dimensions of this vector are subject to change
str _string
the string representation of this vector
Method Details [hide private]

keys(self)

source code 
Returns: Key[]
a consistently ordered list of keys

setArray(self)

source code 

Now deprecated because of irrelevance. Used to update the internal numeric representation based on the current dictionary contents

addColumns(self, keys, values=None)

source code 

Adds new slots to this vector

Parameters:
  • keys (Key[]) - the (sorted) keys for the new slots to insert
  • values (float, or dict:Key→float) - the values to insert for each key (defaults to 0.)

Warning: Assumes that keys are sorted!

getArray(self)

source code 
Returns: array
the numeric array representation of this vector

__setitem__(self, key, value)
(Index assignment operator)

source code 
Parameters:
  • value (float)
  • key (Key instance)

normalize(self)

source code 

Scales this vector so that the highest absolute weight is 1

Warning: throws exception if the vector is all 0s

fill(self, keys, value=None)

source code 

Fills in any missing slots with a default value (it's really just a call to addColumns now)

Parameters:
  • keys (list of Key instances) - the slots that should be filled
  • value - the default value (defaults to 0)

Note: does not overwrite existing values

instantiate(self, table)

source code 

Substitutes values for any abstract references, using the given substitution table

Parameters:
  • table (dictionary) - dictionary of key-value pairs, where the value will be substituted for any appearance of the given key in a field of this Key object
Returns: KeyedVector

instantiateKeys(self, table)

source code 

Substitutes values for any abstract references, using the given substitution table

Parameters:
  • table (dictionary) - dictionary of key-value pairs, where the value will be substituted for any appearance of the given key in a field of this Key object

compose(self, other, op)

source code 

Composes the two vectors together using the given operator

Parameters:
  • other (KeyedVector instance) - the other vector to compose with
  • op (lambda x,y:f(x,y) where x and y are array instances) - the operator used to generate the new array values
Returns: a new KeyedVector instance

freeze(self)

source code 

Locks in the dimensions and keys of this vector. A frozen vector leads to faster math.

unfreeze(self)

source code 

Unlocks the dimensions and keys of this vector

Returns: bool
True iff the vector was originally frozen

__add__(self, other)
(Addition operator)

source code 

Warning: assumes that your vectors are aligned

parse(self, element, changeInPlace=False)

source code 

Extracts the distribution from the given XML element

Parameters:
  • element (Element) - The XML Element object specifying the vector
  • changeInPlace (boolean) - flag, if True, then modify this vector itself; otherwise, return a new vector
Returns:
the KeyedVector instance