Skip to content

Lazy

Vector operations as lazy generators.

>>> from vector import veclbasis
>>> veclbasis(3)
<generator object veclbasis at 0x0123456789ABCDEF>
>>> tuple(veclbasis(3))
(0, 0, 0, 1)

Prefixed by vecl... (vector - lazy).

Functions are generators.

Lazy generator versions of dense.

Different behaviour:

Not implemented lazily as these are consumers:

creation

veclzero()

Zero vector.

\[ \vec{0} \qquad \mathbb{K}^0 \]

An empty generator.

Source code in vector\lazy\creation.py
10
11
12
13
14
15
16
17
18
19
def veclzero():
    r"""Zero vector.

    $$
        \vec{0} \qquad \mathbb{K}^0
    $$

    An empty generator.
    """
    yield from ()

veclbasis(i, c=1, zero=0)

Return a basis vector.

\[ c\vec{e}_i \qquad \mathbb{K}^{i+1} \]

Yields i zeros followed by c.

See also
Source code in vector\lazy\creation.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def veclbasis(i, c=1, zero=0):
    r"""Return a basis vector.

    $$
        c\vec{e}_i \qquad \mathbb{K}^{i+1}
    $$

    Yields `i` zeros followed by `c`.

    See also
    --------
    - for all basis vectors: [`veclbases`][vector.lazy.creation.veclbases]
    """
    yield from chain(repeat(zero, i), (c,))

veclbases(start=0, c=1, zero=0)

Yield all basis vectors.

\[ \left(\vec{e}_n\right)_{n\in\mathbb{N}_{\geq\text{start}}} = \left(\vec{e}_\text{start}, \vec{e}_{\text{start}+1}, \dots \right) \]
See also
Source code in vector\lazy\creation.py
36
37
38
39
40
41
42
43
44
45
46
47
48
def veclbases(start=0, c=1, zero=0):
    r"""Yield all basis vectors.

    $$
        \left(\vec{e}_n\right)_{n\in\mathbb{N}_{\geq\text{start}}} = \left(\vec{e}_\text{start}, \vec{e}_{\text{start}+1}, \dots \right)
    $$

    See also
    --------
    - for single basis vector: [`veclbasis`][vector.lazy.creation.veclbasis]
    """
    for i in count(start=start):
        yield veclbasis(i, c=c, zero=zero)

veclrand(n)

Return a random vector of uniform sampled float coefficients.

\[ \vec{v}\sim\mathcal{U}^n([0, 1[) \qquad \mathbb{K}^n \]

The coefficients are sampled from a uniform distribution in [0, 1[.

Notes

Naming like numpy.random, because seems more concise (not random & gauss as in the stdlib).

Source code in vector\lazy\creation.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def veclrand(n):
    r"""Return a random vector of uniform sampled `float` coefficients.

    $$
        \vec{v}\sim\mathcal{U}^n([0, 1[) \qquad \mathbb{K}^n
    $$

    The coefficients are sampled from a uniform distribution in `[0, 1[`.

    Notes
    -----
    Naming like [`numpy.random`](https://numpy.org/doc/stable/reference/random/legacy.html),
    because seems more concise (not `random` & `gauss` as in the stdlib).
    """
    yield from (random() for _ in range(n))

veclrandn(n, mu=0, sigma=1)

Return a random vector of normal sampled float coefficients.

\[ \vec{v}\sim\mathcal{N}^n(\mu, \sigma) \qquad \mathbb{K}^n \]

The coefficients are sampled from a normal distribution.

Difference to vecrandn: The vector can't be normalised as it isn't materialised.

Notes

Naming like numpy.random, because seems more concise (not random & gauss as in the stdlib).

Source code in vector\lazy\creation.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def veclrandn(n, mu=0, sigma=1):
    r"""Return a random vector of normal sampled `float` coefficients.

    $$
        \vec{v}\sim\mathcal{N}^n(\mu, \sigma) \qquad \mathbb{K}^n
    $$

    The coefficients are sampled from a normal distribution.

    Difference to [`vecrandn`][vector.dense.vecrandn]:
    The vector can't be normalised as it isn't materialised.

    Notes
    -----
    Naming like [`numpy.random`](https://numpy.org/doc/stable/reference/random/legacy.html),
    because seems more concise (not `random` & `gauss` as in the stdlib).
    """
    yield from (gauss(mu, sigma) for _ in range(n))

utility

vecleq(v, w)

Return if two vectors are equal.

\[ \vec{v}=\vec{w} \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{B} \]
Source code in vector\lazy\utility.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def vecleq(v, w):
    r"""Return if two vectors are equal.

    $$
        \vec{v}=\vec{w} \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{B}
    $$
    """
    sentinel = object()
    for vi, wi in zip_longest(v, w, fillvalue=sentinel):
        if wi is sentinel:
            yield not bool(vi)
        elif vi is sentinel:
            yield not bool(wi)
        else:
            yield vi == wi

vecltrim(v, tol=None)

Remove all trailing near zero (abs(v_i)<=tol) coefficients.

\[ \begin{pmatrix} v_0 \\ \vdots \\ v_m \end{pmatrix} \ \text{where} \ m=\max\{\, j\mid |v_{j-1}|>\text{tol}\,\}\cup\{-1\} \qquad \mathbb{K}^n\to\mathbb{K}^{\leq n} \]

tol may also be None, then all coefficients that evaluate to False are trimmed.

Notes
  • Cutting of elements that are abs(v_i)<=tol instead of abs(v_i)<tol to allow cutting of elements that are exactly zero by trim(v, 0) instead of trim(v, sys.float_info.min).
Source code in vector\lazy\utility.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def vecltrim(v, tol=None):
    r"""Remove all trailing near zero (`abs(v_i)<=tol`) coefficients.

    $$
        \begin{pmatrix}
            v_0 \\
            \vdots \\
            v_m
        \end{pmatrix} \ \text{where} \ m=\max\{\, j\mid |v_{j-1}|>\text{tol}\,\}\cup\{-1\} \qquad \mathbb{K}^n\to\mathbb{K}^{\leq n}
    $$

    `tol` may also be `None`,
    then all coefficients that evaluate to `False` are trimmed.

    Notes
    -----
    - Cutting of elements that are `abs(v_i)<=tol` instead of `abs(v_i)<tol` to
    allow cutting of elements that are exactly zero by `trim(v, 0)` instead
    of `trim(v, sys.float_info.min)`.
    """
    t = []
    for x in v:
        t.append(x)
        if (x if tol is None else abs(x)>tol):
            yield from t
            t.clear()

veclrshift(v, n, zero=0)

Shift coefficients up.

\[ (v_{i-n})_i \qquad \begin{pmatrix} 0 \\ \vdots \\ 0 \\ v_0 \\ v_1 \\ \vdots \end{pmatrix} \qquad \mathbb{K}^m\to\mathbb{K}^{m+n} \]
Source code in vector\lazy\utility.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def veclrshift(v, n, zero=0):
    r"""Shift coefficients up.

    $$
        (v_{i-n})_i \qquad \begin{pmatrix}
            0 \\
            \vdots \\
            0 \\
            v_0 \\
            v_1 \\
            \vdots
        \end{pmatrix} \qquad \mathbb{K}^m\to\mathbb{K}^{m+n}
    $$
    """
    yield from chain(repeat(zero, n), v)

vecllshift(v, n)

Shift coefficients down.

\[ (v_{i+n})_i \qquad \begin{pmatrix} v_n \\ v_{n+1} \\ \vdots \end{pmatrix} \qquad \mathbb{K}^m\to\mathbb{K}^{\max\{m-n, 0\}} \]
Source code in vector\lazy\utility.py
68
69
70
71
72
73
74
75
76
77
78
79
def vecllshift(v, n):
    r"""Shift coefficients down.

    $$
        (v_{i+n})_i \qquad \begin{pmatrix}
            v_n \\
            v_{n+1} \\
            \vdots
        \end{pmatrix} \qquad \mathbb{K}^m\to\mathbb{K}^{\max\{m-n, 0\}}
    $$
    """
    yield from islice(v, n, None)

hilbert_space

veclconj(v)

Return the complex conjugate.

\[ \vec{v}^* \qquad \mathbb{K}^n\to\mathbb{K}^n \]

Tries to call a method conjugate on each element. If not found, simply keeps the element as is.

Source code in vector\lazy\hilbert_space.py
 9
10
11
12
13
14
15
16
17
18
19
def veclconj(v):
    r"""Return the complex conjugate.

    $$
        \vec{v}^* \qquad \mathbb{K}^n\to\mathbb{K}^n
    $$

    Tries to call a method `conjugate` on each element.
    If not found, simply keeps the element as is.
    """
    yield from map(try_conjugate, v)

vector_space

veclpos(v)

Return the identity.

\[ +\vec{v} \qquad \mathbb{K}^n\to\mathbb{K}^n \]
Source code in vector\lazy\vector_space.py
13
14
15
16
17
18
19
20
def veclpos(v):
    r"""Return the identity.

    $$
        +\vec{v} \qquad \mathbb{K}^n\to\mathbb{K}^n
    $$
    """
    yield from map(pos, v)

veclneg(v)

Return the negation.

\[ -\vec{v} \qquad \mathbb{K}^n\to\mathbb{K}^n \]
Source code in vector\lazy\vector_space.py
22
23
24
25
26
27
28
29
def veclneg(v):
    r"""Return the negation.

    $$
        -\vec{v} \qquad \mathbb{K}^n\to\mathbb{K}^n
    $$
    """
    yield from map(neg, v)

vecladd(*vs)

Return the sum.

\[ \vec{v}_0+\vec{v}_1+\cdots \qquad \mathbb{K}^{n_0}\times\mathbb{K}^{n_1}\times\cdots\to\mathbb{K}^{\max_i n_i} \]
See also
  • for sum on a single coefficient: vecladdc
Source code in vector\lazy\vector_space.py
31
32
33
34
35
36
37
38
39
40
41
42
def vecladd(*vs):
    r"""Return the sum.

    $$
        \vec{v}_0+\vec{v}_1+\cdots \qquad \mathbb{K}^{n_0}\times\mathbb{K}^{n_1}\times\cdots\to\mathbb{K}^{\max_i n_i}
    $$

    See also
    --------
    - for sum on a single coefficient: [`vecladdc`][vector.lazy.vector_space.vecladdc]
    """
    yield from map(partial(sum_default, default=MISSING), group_ordinal(*vs))

vecladdc(v, c, i=0, zero=0)

Return the sum with a basis vector.

\[ \vec{v}+c\vec{e}_i \qquad \mathbb{K}^n\to\mathbb{K}^{\max\{n, i+1\}} \]

More efficient than vecladd(v, veclbasis(i, c)).

See also
  • for sum on more coefficients: vecladd
Source code in vector\lazy\vector_space.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def vecladdc(v, c, i=0, zero=0):
    r"""Return the sum with a basis vector.

    $$
        \vec{v}+c\vec{e}_i \qquad \mathbb{K}^n\to\mathbb{K}^{\max\{n, i+1\}}
    $$

    More efficient than `vecladd(v, veclbasis(i, c))`.

    See also
    --------
    - for sum on more coefficients: [`vecladd`][vector.lazy.vector_space.vecladd]
    """
    v = iter(v)
    yield from islice(chain(v, repeat(zero)), i)
    try:
        yield next(v) + c
    except StopIteration:
        yield +c
    yield from v

veclsub(v, w)

Return the difference.

\[ \vec{v}-\vec{w} \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^{\max\{m, n\}} \]
See also
  • for difference on a single coefficient: veclsubc
Source code in vector\lazy\vector_space.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def veclsub(v, w):
    r"""Return the difference.

    $$
        \vec{v}-\vec{w} \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^{\max\{m, n\}}
    $$

    See also
    --------
    - for difference on a single coefficient: [`veclsubc`][vector.lazy.vector_space.veclsubc]
    """
    sentinel = object()
    for vi, wi in zip_longest(v, w, fillvalue=sentinel):
        if wi is sentinel:
            yield vi
        elif vi is sentinel:
            yield -wi
        else:
            yield vi - wi

veclsubc(v, c, i=0, zero=0)

Return the difference with a basis vector.

\[ \vec{v}-c\vec{e}_i \qquad \mathbb{K}^n\to\mathbb{K}^{\max\{n, i+1\}} \]

More efficient than veclsub(v, veclbasis(i, c)).

See also
  • for difference on more coefficients: veclsub
Source code in vector\lazy\vector_space.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def veclsubc(v, c, i=0, zero=0):
    r"""Return the difference with a basis vector.

    $$
        \vec{v}-c\vec{e}_i \qquad \mathbb{K}^n\to\mathbb{K}^{\max\{n, i+1\}}
    $$

    More efficient than `veclsub(v, veclbasis(i, c))`.

    See also
    --------
    - for difference on more coefficients: [`veclsub`][vector.lazy.vector_space.veclsub]
    """
    v = iter(v)
    yield from islice(chain(v, repeat(zero)), i)
    try:
        yield next(v) - c
    except StopIteration:
        yield -c
    yield from v

veclmul(v, a)

Return the product.

\[ \vec{v}a \qquad \mathbb{K}\times\mathbb{K}^n\to\mathbb{K}^n \]
Source code in vector\lazy\vector_space.py
106
107
108
109
110
111
112
113
def veclmul(v, a):
    r"""Return the product.

    $$
        \vec{v}a \qquad \mathbb{K}\times\mathbb{K}^n\to\mathbb{K}^n
    $$
    """
    yield from map(mul, v, repeat(a))

veclrmul(a, v)

Return the product.

\[ a\vec{v} \qquad \mathbb{K}\times\mathbb{K}^n\to\mathbb{K}^n \]
Source code in vector\lazy\vector_space.py
115
116
117
118
119
120
121
122
def veclrmul(a, v):
    r"""Return the product.

    $$
        a\vec{v} \qquad \mathbb{K}\times\mathbb{K}^n\to\mathbb{K}^n
    $$
    """
    yield from map(mul, repeat(a), v)

vecltruediv(v, a)

Return the true quotient.

\[ \frac{\vec{v}}{a} \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n \]
Notes

Why called truediv instead of div?

  • div would be more appropriate for an absolute clean mathematical implementation, that doesn't care about the language used. But the package might be used for pure integers/integer arithmetic, so both, truediv and floordiv operations have to be provided, and none should be privileged over the other by getting the universal div name.
  • truediv/floordiv is unambiguous, like Python operators.
Source code in vector\lazy\vector_space.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def vecltruediv(v, a):
    r"""Return the true quotient.

    $$
        \frac{\vec{v}}{a} \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n
    $$

    Notes
    -----
    Why called `truediv` instead of `div`?

    - `div` would be more appropriate for an absolute clean mathematical
    implementation, that doesn't care about the language used. But the package
    might be used for pure integers/integer arithmetic, so both, `truediv`
    and `floordiv` operations have to be provided, and none should be
    privileged over the other by getting the universal `div` name.
    - `truediv`/`floordiv` is unambiguous, like Python `operator`s.
    """
    yield from map(truediv, v, repeat(a))

veclfloordiv(v, a)

Return the floor quotient.

\[ \left\lfloor\frac{\vec{v}}{a}\right\rfloor \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n \]
Source code in vector\lazy\vector_space.py
144
145
146
147
148
149
150
151
def veclfloordiv(v, a):
    r"""Return the floor quotient.

    $$
        \left\lfloor\frac{\vec{v}}{a}\right\rfloor \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n
    $$
    """
    yield from map(floordiv, v, repeat(a))

veclmod(v, a)

Return the remainder.

\[ \vec{v} \bmod a \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n \]
Source code in vector\lazy\vector_space.py
153
154
155
156
157
158
159
160
def veclmod(v, a):
    r"""Return the remainder.

    $$
        \vec{v} \bmod a \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n
    $$
    """
    yield from map(mod, v, repeat(a))

vecldivmod(v, a)

Return the floor quotient and remainder.

\[ \left\lfloor\frac{\vec{v}}{a}\right\rfloor, \ \left(\vec{v} \bmod a\right) \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n\times\mathbb{K}^n \]
Source code in vector\lazy\vector_space.py
162
163
164
165
166
167
168
169
170
def vecldivmod(v, a):
    r"""Return the floor quotient and remainder.

    $$
        \left\lfloor\frac{\vec{v}}{a}\right\rfloor, \ \left(\vec{v} \bmod a\right) \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n\times\mathbb{K}^n
    $$
    """
    for vi in v:
        yield divmod(vi, a)

elementwise

veclhadamard(*vs)

Return the elementwise product.

\[ \left((\vec{v}_0)_i\cdot(\vec{v}_1)_i\cdot\cdots\right)_i \qquad \mathbb{K}^{n_0}\times\mathbb{K}^{n_1}\times\cdots\to\mathbb{K}^{\min_i n_i} \]
Source code in vector\lazy\elementwise.py
14
15
16
17
18
19
20
21
def veclhadamard(*vs):
    r"""Return the elementwise product.

    $$
        \left((\vec{v}_0)_i\cdot(\vec{v}_1)_i\cdot\cdots\right)_i \qquad \mathbb{K}^{n_0}\times\mathbb{K}^{n_1}\times\cdots\to\mathbb{K}^{\min_i n_i}
    $$
    """
    yield from map(partial(prod_default, default=MISSING), zip(*vs))

veclhadamardtruediv(v, w)

Return the elementwise true quotient.

\[ \left(\frac{v_i}{w_i}\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m \]
Source code in vector\lazy\elementwise.py
23
24
25
26
27
28
29
30
def veclhadamardtruediv(v, w):
    r"""Return the elementwise true quotient.

    $$
        \left(\frac{v_i}{w_i}\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m
    $$
    """
    yield from map(truediv, v, chain(w, exception_generator(ZeroDivisionError)))

veclhadamardfloordiv(v, w)

Return the elementwise floor quotient.

\[ \left(\left\lfloor\frac{v_i}{w_i}\right\rfloor\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m \]
Source code in vector\lazy\elementwise.py
32
33
34
35
36
37
38
39
def veclhadamardfloordiv(v, w):
    r"""Return the elementwise floor quotient.

    $$
        \left(\left\lfloor\frac{v_i}{w_i}\right\rfloor\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m
    $$
    """
    yield from map(floordiv, v, chain(w, exception_generator(ZeroDivisionError)))

veclhadamardmod(v, w)

Return the elementwise remainder.

\[ \left(v_i \bmod w_i\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m \]
Source code in vector\lazy\elementwise.py
41
42
43
44
45
46
47
48
def veclhadamardmod(v, w):
    r"""Return the elementwise remainder.

    $$
        \left(v_i \bmod w_i\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m
    $$
    """
    yield from map(mod, v, chain(w, exception_generator(ZeroDivisionError)))

veclhadamarddivmod(v, w)

Return the elementwise floor quotient and remainder.

\[ \left(\left\lfloor\frac{v_i}{w_i}\right\rfloor\right)_i, \ \left(v_i \bmod w_i\right)_i \qquad \mathbb{K}^n\times\mathbb{K}^m\to\mathbb{K}^n\times\mathbb{K}^n \]
Source code in vector\lazy\elementwise.py
50
51
52
53
54
55
56
57
def veclhadamarddivmod(v, w):
    r"""Return the elementwise floor quotient and remainder.

    $$
        \left(\left\lfloor\frac{v_i}{w_i}\right\rfloor\right)_i, \ \left(v_i \bmod w_i\right)_i \qquad \mathbb{K}^n\times\mathbb{K}^m\to\mathbb{K}^n\times\mathbb{K}^n
    $$
    """
    yield from map(divmod, v, chain(w, exception_generator(ZeroDivisionError)))

veclhadamardmin(*vs, key=None)

Return the elementwise minimum.

\[ \left(\min((\vec{v}_0)_i,(\vec{v}_1)_i,\cdots)\right)_i \qquad \mathbb{K}^{n_0}\times\mathbb{K}^{n_1}\times\cdots\to\mathbb{K}^{\max_i n_i} \]
Source code in vector\lazy\elementwise.py
59
60
61
62
63
64
65
66
def veclhadamardmin(*vs, key=None):
    r"""Return the elementwise minimum.

    $$
        \left(\min((\vec{v}_0)_i,(\vec{v}_1)_i,\cdots)\right)_i \qquad \mathbb{K}^{n_0}\times\mathbb{K}^{n_1}\times\cdots\to\mathbb{K}^{\max_i n_i}
    $$
    """
    yield from map(partial(min, key=key), group_ordinal(*vs))

veclhadamardmax(*vs, key=None)

Return the elementwise maximum.

\[ \left(\max((\vec{v}_0)_i,(\vec{v}_1)_i,\cdots)\right)_i \qquad \mathbb{K}^{n_0}\times\mathbb{K}^{n_1}\times\cdots\to\mathbb{K}^{\max_i n_i} \]
Source code in vector\lazy\elementwise.py
68
69
70
71
72
73
74
75
def veclhadamardmax(*vs, key=None):
    r"""Return the elementwise maximum.

    $$
        \left(\max((\vec{v}_0)_i,(\vec{v}_1)_i,\cdots)\right)_i \qquad \mathbb{K}^{n_0}\times\mathbb{K}^{n_1}\times\cdots\to\mathbb{K}^{\max_i n_i}
    $$
    """
    yield from map(partial(max, key=key), group_ordinal(*vs))