Vectors map arguments to zero or a specified rational value
This class derives from Function, so its methods apply here as well.
What has changed:
This class’s members are also hashable, which means they can be used as keys (in Set and Mapping, and their built-in variants set and dict).
>>> {Function({})}
Traceback (most recent call last):
...
TypeError: unhashable type: 'Function'
>>> assert {Vector({})} == {Vector({})}
Unspecified values are assumed to be zero.
>>> f = Vector({'a': 1.1, 'b': '-1/2','c': 0})
>>> assert f == Vector({'a': '11/10', 'c': 0, 'b': '-1/2'})
>>> f['d']
Fraction(0, 1)
The union of domains is used under pointwise operations.
>>> f = Vector({'a': 1.1, 'b': '-1/2','c': 0})
>>> g = Vector({'b': '.6', 'c': -2, 'd': 0.0})
>>> assert (
... 1 + (.3 * f - g) / 2 ==
... Vector({'a': '233/200', 'c': 2, 'b': '5/8', 'd': 1})
... )
A vector’s domain can be restricted/extended to a specified Set.
>>> f = Vector({'a': 1.1, 'b': '-1/2','c': 0})
>>> assert f | {'a','b'} == Vector({'a': '11/10', 'b': '-1/2'})
>>> assert f | {'a','d'} == Vector({'a': '11/10', 'd': 0})
Sum of the values of the vector
returns: the sum of all values of the vector rtype: Fraction
>>> Vector({'a': 1, 'b': '-1/2','c': 0}).mass()
Fraction(1, 2)
‘Sum-of-values’-normalized version of the vector
returns: the gamble, but with its values divided by the sum of the vector’s values rtype: Vector
>>> assert (
... Vector({'a': 1, 'b': '-1/2','c': 0}).sum_normalized() ==
... Vector({'a': 2, 'c': 0, 'b': -1})
... )
>>> assert Vector({'a': 1, 'b': -1,'c': 0}).sum_normalized() == None
Note
None is returned in case the the sum of the vector’s values is zero.
A linear transformation between vector spaces
Features:
The transformation can be applied to Vector (and Set thereof) using the << operator:
>>> T = Trafo()
>>> T['a'] = {('a', 'c'): 1, ('a', 'd'): 1}
>>> T['b'] = {('b', 'c'): 1, ('b', 'd'): 1}
>>> assert (
... T << Vector({'a': 1, 'b': 2}) ==
... Vector({('b', 'c'): 2, ('a', 'd'): 1,
... ('a', 'c'): 1, ('b', 'd'): 2})
... )
>>> P = Polytope({Vector({'a': -2})})
>>> assert T << P == Polytope({Vector({('a', 'd'): -2, ('a', 'c'): -2})})