# Vectors & Polytopes¶

## Vectors¶

class murasyp.vectors.Vector(mapping={})[source]

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})
```
mass()[source]

Sum of the values of the vector

returns: the sum of all values of the vector Fraction
```>>> Vector({'a': 1, 'b': '-1/2','c': 0}).mass()
Fraction(1, 2)
```
sum_normalized()[source]

‘Sum-of-values’-normalized version of the vector

returns: the gamble, but with its values divided by the sum of the vector’s values 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.

is_nonnegative()[source]

Checks whether all values are nonnegative

returns: the truth value of the statement bool
```>>> Vector({'a': 1.6, 'b': -.6}).is_nonnegative()
False
>>> Vector({'a': .4, 'b': .6}).is_nonnegative()
True
```

## Polytopes¶

class murasyp.vectors.Polytope(data=[])[source]

A frozenset of vectors

type data: a non-Mapping Iterable Container of arguments accepted by the Vector constructor.
```>>> assert (
...     Polytope([{'a': 2, 'b': 3}, {'b': 1, 'c': 4}]) ==
...     Polytope({Vector({'a': 2, 'b': 3}), Vector({'c': 4, 'b': 1})})
... )
```

This class derives from frozenset, so its methods apply here as well.

Todo

test all set methods and fix, or elegantly deal with, broken ones

domain()[source]

The union of the domains of the element vectors

returns: the union of the domains of the vectors it contains frozenset
```>>> r = Vector({'a': .03, 'b': -.07})
>>> s = Vector({'a': .07, 'c': -.03})
>>> assert Polytope({r, s}).domain() == frozenset({'a', 'c', 'b'})
```

## Transformations¶

class murasyp.vectors.Trafo(mapping={})[source]

A linear transformation between vector spaces

type mapping: a Mapping (such as a dict) of arguments accepted by the Vector constructor.

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})})
```