relentless.math.KeyedArray#
- class relentless.math.KeyedArray(keys, default=None)#
Numerical array with fixed keys.
Can be used to perform arithmetic operations between two arrays (element-wise) or between an array and a scalar, as well as vector algebraic operations (norm, dot product).
- Parameters:
keys (array_like) – List of keys to be fixed.
default (scalar) – Initial value to fill in the dictionary, defaults to
None.
Examples
Create a keyed array:
k1 = KeyedArray(keys=('A','B')) k2 = KeyedArray(keys=('A','B'))
Set values through update:
k1.update({'A':2.0, 'B':3.0}) k2.update({'A':3.0, 'B':4.0})
Perform array-array arithmetic operations:
>>> print(k1 + k2) {'A':5.0, 'B':7.0} >>> print(k1 - k2) {'A':-1.0, 'B':-1.0} >>> print(k1*k2) {'A':6.0, 'B':12.0} >>> print(k1/k2) {'A':0.6666666666666666, 'B':0.75} >>> print(k1**k2) {'A':8.0, 'B':81.0}
Perform array-scalar arithmetic operations:
>>> print(k1 + 3) {'A':5.0, 'B':6.0} >>> print(3 - k1) {'A':1.0, 'B':0.0} >>> print(3*k1) {'A':6.0, 'B':9.0} >>> print(k1/10) {'A':0.2, 'B':0.3} >>> print(k1**2) {'A':4.0, 'B':9.0} >>> print(-k1) {'A':-2.0, 'B':-3.0}
Compute vector dot product:
>>> print(k1.dot(k2)) 18.0
Compute vector norm:
>>> print(k2.norm()) 5.0
Methods
clear()Clear entries in the dictionary, resetting to default.
dot(val)Vector dot product.
get(k[,d])items()keys()norm()Vector \(\ell^2\)-norm.
pop(k[,d])If key is not found, d is returned if given, otherwise KeyError is raised.
popitem()as a 2-tuple; but raise KeyError if D is empty.
setdefault(k[,d])update([E, ]**F)If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
values()- clear()#
Clear entries in the dictionary, resetting to default.
- dot(val)#
Vector dot product.
For two vectors \(\mathbf{x}=\left[x_1,\ldots,x_n\right]\) and \(\mathbf{y}=\left[y_1,\ldots,y_n\right]\), the vector dot product \(\mathbf{x}\cdot\mathbf{y}\) is computed as:
\[\mathbf{x}\cdot\mathbf{y} = \sum_{k=1}^{n} {x_k y_k}\]- Parameters:
val (
KeyedArray) – One of the arrays used to compute the dot product.- Returns:
The vector dot product.
- Return type:
floats
- get(k[, d]) D[k] if k in D, else d. d defaults to None.#
- items() a set-like object providing a view on D's items#
- keys() a set-like object providing a view on D's keys#
- norm()#
Vector \(\ell^2\)-norm.
For a vector \(\mathbf{x}=\left[x_1,\ldots,x_n\right]\), the Euclidean 2-norm \(\lVert\mathbf{x}\rVert\) is computed as:
\[\lVert\mathbf{x}\rVert = \sqrt{\sum_{k=1}^{n} {x_k}^2}\]- Returns:
The vector norm.
- Return type:
- pop(k[, d]) v, remove specified key and return the corresponding value.#
If key is not found, d is returned if given, otherwise KeyError is raised.
- popitem() (k, v), remove and return some (key, value) pair#
as a 2-tuple; but raise KeyError if D is empty.
- setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D#
- update([E, ]**F) None. Update D from mapping/iterable E and F.#
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() an object providing a view on D's values#