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

Class KeyedMatrix

source code

object --+    
         |    
      dict --+
             |
            KeyedMatrix
Known Subclasses:

A dictionary-based representation of a two-dimensional matrix


Warning: Use the set method to set individual values. Using built-in assignment calls (e.g., M[row][col] = 0.) will lead to inconsistencies. If you can think of a clever way to enforce this in code, please feel free to do so.

Instance Methods [hide private]
new empty dictionary

__init__(self, args={})
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
Key[]
rowKeys(self)
Returns: ordered list of the keys to all rows in this matrix
source code
Key[]
colKeys(self)
Returns: ordered list of the keys to all columns in this matrix
source code
int
colOrder(self, key)
Returns: the position of the given key within the columns of this matrix
source code
 
set(self, row, col, value)
Sets an individual value in this matrix
source code
int
rowPosition(self, key)
Finds the row position where this key should be inserted
source code
 
addRows(self, keys, value=None)
Adds a new row slot to this matrix
source code
boolean
isIdentity(self)
Returns: True iff this matrix is an identity matrix
source code
 
addColumns(self, keys, value=None)
Adds new column slots to this matrix
source code
array
getArray(self)
Returns: the numeric array representation of this matrix
source code
 
__setitem__(self, key, value)
x[i]=y
source code
 
__delitem__(self, key)
del x[y]
source code
 
deleteColumn(self, key)
Removes the specified column from the matrix
source code
 
fill(self, keys, value=None)
Fills in any missing rows and columns with a default value
source code
 
freeze(self)
Locks in the dimensions and keys of this matrix
source code
 
unfreeze(self)
Unlocks the dimensions and keys of this matrix
source code
a new KeyedMatrix instance
compose(self, other, op)
Composes the two matrices together using the given operator
source code
 
__add__(self, other) source code
 
__sub__(self, other) source code
 
__neg__(self) source code
 
__mul__(self, other) source code
 
_multiplyByMatrix(self, other)
Handles multiplication when multiplicand is two-dimensional
source code
 
_multiplyByVector(self, other)
Handles multiplication when multiplicand is one-dimensional
source code
 
_multiplyByOther(self, other)
Handles multiplication when multiplicand is something unknown class (typically, float)
source code
KeyedMatrix
copyFromArray(self, matrix)
Copies all aspects of this array (keys, etc.) except for the numerical value, which is taken fom the given matrix
source code
 
inverse(self) source code
 
__hash__(self)
hash(x)
source code
KeyedMatrix
merge(self, other)
Merges rows from the two matrices (if any row keys are common, then the new row is used)
source code
 
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
 
__xml__(self)
Returns: An XML Document object representing this matrix
source code
 
parse(self, element)
Extracts the distribution from the given XML element
source code
str
simpleText(self, numbers=True, all=False)
Returns: a more user-friendly string representation of this matrix
source code
 
__copy__(self) source code
 
__deepcopy__(self, memo) source code

Inherited from dict: __cmp__, __contains__, __eq__, __ge__, __getattribute__, __getitem__, __gt__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __repr__, clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, update, values

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__, __str__

Class Variables [hide private]
float defaultValue = 0.0
the default value to insert into any missing positions
  dimension = 2
Instance Variables [hide private]
  _array
The numeric representation of this matrix
boolean _fresh
flag indicating whether there have been any changes to the array contents since the last update to the numeric array
boolean _frozen
flag indicating whether the dimensions of this matrix are subject to change
Key[] _rowKeys
an ordered list of the indices of the rows of this matrix
dict(Key:int) _rowOrder
a dictionary containing the row position of each key within the matrix
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, args={})
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Returns:
new empty dictionary

Overrides: object.__init__
(inherited documentation)

rowKeys(self)

source code 
Returns: Key[]
ordered list of the keys to all rows in this matrix

colKeys(self)

source code 
Returns: Key[]
ordered list of the keys to all columns in this matrix

colOrder(self, key)

source code 
Returns: int
the position of the given key within the columns of this matrix

set(self, row, col, value)

source code 

Sets an individual value in this matrix

Parameters:
  • row, col - the keys for this value's position
  • value (float) - the value to insert
  • row,col (Key)

rowPosition(self, key)

source code 

Finds the row position where this key should be inserted

Parameters:
  • key (Key) - the key to insert
Returns: int
the index to insert at

Warning: does not check whether key is already present

addRows(self, keys, value=None)

source code 

Adds a new row slot to this matrix

Parameters:
  • keys (Key[]) - the (sorted) keys for the new slots to insert
  • value (float) - the row to insert (defaults to a row of defaultValue)

Warning: assumes that this row does not already exist

isIdentity(self)

source code 
Returns: boolean
True iff this matrix is an identity matrix

addColumns(self, keys, value=None)

source code 

Adds new column slots to this matrix

Parameters:
  • keys (Key[]) - the (sorted) keys for the new slots to insert
  • value (float) - the column to insert (defaults to a row of defaultValue)

getArray(self)

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

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

source code 

x[i]=y

Parameters:
  • value (KeyedVector)
  • key (Key instance)
Overrides: dict.__setitem__

__delitem__(self, key)
(Index deletion operator)

source code 

del x[y]

Overrides: dict.__delitem__
(inherited documentation)

deleteColumn(self, key)

source code 

Removes the specified column from the matrix

Parameters:
  • key (Key) - the index for the column to remove

fill(self, keys, value=None)

source code 

Fills in any missing rows and columns with a default value

Parameters:
  • keys (list of Key instances) - the new slots that should be filled
  • value - the default value (default is defaultValue)

Note: does not overwrite existing values, but does fill in values for missing existing keys

compose(self, other, op)

source code 

Composes the two matrices together using the given operator

Parameters:
  • other (KeyedMatrix,float) - the other matrix 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 KeyedMatrix instance

_multiplyByMatrix(self, other)

source code 

Handles multiplication when multiplicand is two-dimensional

Parameters:

_multiplyByVector(self, other)

source code 

Handles multiplication when multiplicand is one-dimensional

Parameters:

copyFromArray(self, matrix)

source code 

Copies all aspects of this array (keys, etc.) except for the numerical value, which is taken fom the given matrix

Parameters:
  • matrix (array) - the numerical value to use
Returns: KeyedMatrix

__hash__(self)
(Hashing function)

source code 

hash(x)

Overrides: object.__hash__

Warning: This is not cached, so repeated hashing may be inefficient

merge(self, other)

source code 

Merges rows from the two matrices (if any row keys are common, then the new row is used)

Parameters:
Returns: KeyedMatrix
The new matrix

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

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

__xml__(self)

source code 
Returns:
An XML Document object representing this matrix

parse(self, element)

source code 

Extracts the distribution from the given XML element

Parameters:
  • element (Element) - The XML object specifying the distribution

Warning: modifies object in place; erases any previous contents of this matrix

simpleText(self, numbers=True, all=False)

source code 
Returns: str
a more user-friendly string representation of this matrix