Skip to content

Functional

Vector operation functions.

>>> from vector import vecadd
>>> a = (5, 6, 7)
>>> b = [2]
>>> c = range(4)
>>> vecadd(a, b, c)
(7, 7, 9, 3)

Prefixed by vec... (vector).

All functions accept vectors as single exhaustible iterables.

They return vectors as tuples.

The functions are type-independent. However, the coefficients used must support necessary scalar operations. For instance, for vector addition, coefficients must be addable.

For complete type safety a zero argument is available. Default is int(0).

Docstring conventions

Summary

Math notation (vector notation if possible, index notation, domain & codomain)

More information ("More efficient than ...").

Complexity

For a vector of length \(n\) there will be - \(x\) scalar additions (add), ...

Notes

Design choices

See also

Similar functions

References

Wikipedia, numpy, ...

creation

veczero = ()

Zero vector.

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

An empty tuple: ().

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

Return the i-th basis vector times c.

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

Returns a tuple with i many zeros followed by c.

Source code in vector\functional\creation.py
23
24
25
26
27
28
29
30
31
32
def vecbasis(i, c=1, zero=0):
    r"""Return the `i`-th basis vector times `c`.

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

    Returns a tuple with `i` many `zero`s followed by `c`.
    """
    return (zero,)*i + (c,)

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

Yield all basis vectors.

\[ \left(\vec{e}_n\right)_\mathbb{n\in\mathbb{N_0}} = \left(\vec{e}_0, \vec{e}_1, \vec{e}_2, \dots \right) \]
See also
Source code in vector\functional\creation.py
34
35
36
37
38
39
40
41
42
43
44
45
46
def vecbases(start=0, c=1, zero=0):
    r"""Yield all basis vectors.

    $$
        \left(\vec{e}_n\right)_\mathbb{n\in\mathbb{N_0}} = \left(\vec{e}_0, \vec{e}_1, \vec{e}_2, \dots \right)
    $$

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

vecrand(n)

Return a random vector of n uniform float coefficients in [0, 1[.

\[ \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 in numpy.random, because seems more concise (not random & gauss as in the stdlib).

Source code in vector\functional\creation.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def vecrand(n):
    r"""Return a random vector of `n` uniform `float` coefficients in `[0, 1[`.

    $$
        \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 in `numpy.random`, because seems more concise
    (not `random` & `gauss` as in the stdlib).
    """
    return tuple(veclrand(n))

vecrandn(n, normed=True, mu=0, sigma=1, weights=None)

Return a random vector of n normal distributed float coefficients.

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

The coefficients are sampled from a normal distribution.

Notes

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

Source code in vector\functional\creation.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def vecrandn(n, normed=True, mu=0, sigma=1, weights=None):
    r"""Return a random vector of `n` normal distributed `float` coefficients.

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

    The coefficients are sampled from a normal distribution.

    Notes
    -----
    Naming like in `numpy.random`, because seems more concise
    (not `random` & `gauss` as in the stdlib).
    """
    v = tuple(veclrandn(n, mu, sigma))
    return vectruediv(v, vecabs(v, weights)) if normed else v

utility

veclen(v)

Return the length (number of set coefficients) of the vector.

Also works for single exhaustible iterables where len(v) wouldn't work, altough the vector is gone then.

Source code in vector\functional\utility.py
 9
10
11
12
13
14
15
def veclen(v):
    """Return the length (number of set coefficients) of the vector.

    Also works for single exhaustible iterables where `len(v)` wouldn't work,
    altough the vector is gone then.
    """
    return sum(1 for _ in v)

veceq(v, w)

Return if two vectors are equal.

\[ \vec{v}\overset{?}{=}\vec{w} \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{B} \]
Complexity

For two vectors of lengths \(n\) & \(m\) there will be at most

  • \(\min\{n, m\}\) scalar comparisons (eq) &
  • \(|n-m|\) scalar boolean evaluations (bool).
Source code in vector\functional\utility.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def veceq(v, w):
    r"""Return if two vectors are equal.

    $$
        \vec{v}\overset{?}{=}\vec{w} \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{B}
    $$

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be at most

    - $\min\{n, m\}$ scalar comparisons (`eq`) &
    - $|n-m|$ scalar boolean evaluations (`bool`).
    """
    return all(vecleq(v, w))

vectrim(v, tol=1e-09)

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|>\text{tol}\,\}\cup\{-1\} \qquad \mathbb{K}^n\to\mathbb{K}^{\leq n} \]
Complexity

For a vector of length \(n\) there will be

  • \(n\) scalar absolute evaluations (abs) &
  • \(n\) scalar comparisons (gt).
Notes
  • Cutting of elements that are abs(vi)<=tol instead of abs(vi)<tol to allow cutting of elements that are exactly zero by trim(v, 0) instead of trim(v, sys.float_info.min).
  • tol=1e-9 like in PEP 485.
Source code in vector\functional\utility.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def vectrim(v, tol=1e-9):
    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|>\text{tol}\,\}\cup\{-1\} \qquad \mathbb{K}^n\to\mathbb{K}^{\leq n}
    $$

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar absolute evaluations (`abs`) &
    - $n$ scalar comparisons (`gt`).

    Notes
    -----
    - Cutting of elements that are `abs(vi)<=tol` instead of `abs(vi)<tol` to
    allow cutting of elements that are exactly zero by `trim(v, 0)` instead
    of `trim(v, sys.float_info.min)`.
    - `tol=1e-9` like in [PEP 485](https://peps.python.org/pep-0485/#defaults).
    """
    r, t = [], []
    for x in v:
        t.append(x)
        if abs(x)>tol:
            r.extend(t)
            t.clear()
    return tuple(r)

vecround(v, ndigits=None)

Round all coefficients to the given precision.

\[ (\text{round}_\text{ndigits}(v_i))_i \qquad \mathbb{K}^n\to\mathbb{K}^n \]
Complexity

For a vector of length \(n\) there will be

  • \(n\) scalar roundings (round).
Source code in vector\functional\utility.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def vecround(v, ndigits=None):
    r"""Round all coefficients to the given precision.

    $$
        (\text{round}_\text{ndigits}(v_i))_i \qquad \mathbb{K}^n\to\mathbb{K}^n
    $$

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar roundings (`round`).
    """
    return tuple(veclround(v, ndigits=ndigits))

vecrshift(v, n, zero=0)

Pad n many zeros to the beginning of the vector.

\[ (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\functional\utility.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def vecrshift(v, n, zero=0):
    r"""Pad `n` many `zero`s to the beginning of the vector.

    $$
        (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}
    $$
    """
    return tuple(veclrshift(v, n, zero=zero))

veclshift(v, n)

Remove n many coefficients at the beginning of the vector.

\[ (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\functional\utility.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
def veclshift(v, n):
    r"""Remove `n` many coefficients at the beginning of the vector.

    $$
        (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\}}
    $$
    """
    return tuple(vecllshift(v, n))

hilbert_space

vecconj(v)

Return the elementwise complex conjugate.

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

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

Complexity

For a vector of length \(n\) there will be

  • \(n\) scalar conjugations (conjugate).
Source code in vector\functional\hilbert_space.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def vecconj(v):
    r"""Return the elementwise complex conjugate.

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

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

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar conjugations (`conjugate`).
    """
    return tuple(veclconj(v))

vecabs(v, weights=None, conjugate=False, zero=0)

Return the Euclidean/L2-norm.

\[ ||\vec{v}||_{\ell_{\mathbb{N}_0}^2}=\sqrt{\sum_iv_i^{(*)}v_i\omega_i} \qquad \mathbb{K}^n\to\mathbb{K}_0^+ \]

Returns the square root of vecabsq.

Complexity

For a vector of length \(n\) there will be

  • \(n\) scalar conjugations (conjugate) (if selected),
  • \(n\)/\(2n\) scalar multiplications (mul) without/with weights,
  • \(\begin{cases}n-1&n\ge1\\0&n\le1\end{cases}\) scalar additions (add) &
  • one sqrt call.
Source code in vector\functional\hilbert_space.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def vecabs(v, weights=None, conjugate=False, zero=0):
    r"""Return the Euclidean/L2-norm.

    $$
        ||\vec{v}||_{\ell_{\mathbb{N}_0}^2}=\sqrt{\sum_iv_i^{(*)}v_i\omega_i} \qquad \mathbb{K}^n\to\mathbb{K}_0^+
    $$

    Returns the square root of [`vecabsq`][vector.functional.vecabsq].

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar conjugations (`conjugate`) (if selected),
    - $n$/$2n$ scalar multiplications (`mul`) without/with weights,
    - $\begin{cases}n-1&n\ge1\\0&n\le1\end{cases}$ scalar additions (`add`) &
    - one `sqrt` call.
    """
    #hypot(*v) doesn't work for complex
    #math.sqrt doesn't work for complex and cmath.sqrt always returns complex
    return vecabsq(v, weights=weights, conjugate=conjugate, zero=zero)**0.5

vecabsq(v, weights=None, conjugate=False, zero=0)

Return the sum of absolute squares of the coefficients.

\[ ||\vec{v}||_{\ell_{\mathbb{N}_0}^2}^2=\sum_iv_i^{(*)}v_i\omega_i \qquad \mathbb{K}^n\to\mathbb{K}_0^+ \]
Complexity

For a vector of length \(n\) there will be

  • \(n\) scalar conjugations (conjugate) (if selected),
  • \(\begin{cases}n-1&n\ge1\\0&n\le1\end{cases}\) scalar additions (add) &
  • \(n\)/\(2n\) scalar multiplications (mul) without/with weights.
Notes

Reasons why it exists:

  • Occurs in math.
  • Most importantly: type independent because it doesn't use sqrt.
References
Source code in vector\functional\hilbert_space.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def vecabsq(v, weights=None, conjugate=False, zero=0):
    r"""Return the sum of absolute squares of the coefficients.

    $$
        ||\vec{v}||_{\ell_{\mathbb{N}_0}^2}^2=\sum_iv_i^{(*)}v_i\omega_i \qquad \mathbb{K}^n\to\mathbb{K}_0^+
    $$

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar conjugations (`conjugate`) (if selected),
    - $\begin{cases}n-1&n\ge1\\0&n\le1\end{cases}$ scalar additions (`add`) &
    - $n$/$2n$ scalar multiplications (`mul`) without/with weights.

    Notes
    -----
    Reasons why it exists:

    - Occurs in math.
    - Most importantly: type independent because it doesn't use `sqrt`.

    References
    ----------
    - <https://docs.python.org/3/library/itertools.html#itertools-recipes>: `sum_of_squares`
    """
    vc, v = tee(v, 2)
    if conjugate:
        vc = veclconj(vc)

    if weights is None:
        return sumprod_default(vc, v, default=zero)
    else:
        return sumprod_default(map(mul, vc, v), weights, default=zero)

vecdot(v, w, weights=None, conjugate=False, zero=0)

Return the inner product of two vectors.

\[ \left<\vec{v}\mid\vec{w}\right>_{\ell_{\mathbb{N}_0}^2}=\sum_iv_i^{(*)}w_i\omega_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K} \]
Complexity

For two vectors of lengths \(n\) & \(m\) there will be

  • \(\min\{n, m\}\) scalar conjugations (conjugate) (if selected),
  • \(\min\{n, m\}\)/\(2\min\{n, m\}\) scalar multiplications (mul without/with weights) &
  • \(\begin{cases}\min\{n, m\}-1&n\ge1\land m\ge1\\0&n\le1\lor m\le1\end{cases}\) scalar additions (add).
Source code in vector\functional\hilbert_space.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def vecdot(v, w, weights=None, conjugate=False, zero=0):
    r"""Return the inner product of two vectors.

    $$
        \left<\vec{v}\mid\vec{w}\right>_{\ell_{\mathbb{N}_0}^2}=\sum_iv_i^{(*)}w_i\omega_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}
    $$

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be

    - $\min\{n, m\}$ scalar conjugations (`conjugate`) (if selected),
    - $\min\{n, m\}$/$2\min\{n, m\}$ scalar multiplications (`mul` without/with weights) &
    - $\begin{cases}\min\{n, m\}-1&n\ge1\land m\ge1\\0&n\le1\lor m\le1\end{cases}$ scalar additions (`add`).
    """
    if conjugate:
        v = veclconj(v)

    if weights is None:
        return sumprod_default(v, w, default=zero)
    else:
        return sumprod_default(map(mul, v, w), weights, default=zero)

vecparallel(v, w, weights=None, conjugate=False, zero=0)

Return if two vectors are parallel.

\[ \vec{v}\parallel\vec{w} \qquad ||\vec{v}||\,||\vec{w}|| \overset{?}{=} |\vec{v}\vec{w}|^2 \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{B} \]
Source code in vector\functional\hilbert_space.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def vecparallel(v, w, weights=None, conjugate=False, zero=0):
    r"""Return if two vectors are parallel.

    $$
        \vec{v}\parallel\vec{w} \qquad ||\vec{v}||\,||\vec{w}|| \overset{?}{=} |\vec{v}\vec{w}|^2 \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{B}
    $$
    """
    #doesn't work for exhaustible iterables
    #return vecabsq(v)*vecabsq(w) == abs(vecdot(v, w))**2
    v2, w2, vw = zero, zero, zero
    if weights is None:
        for vi, wi in zip_longest(v, w, fillvalue=zero):
            vic, wic = (try_conjugate(vi), try_conjugate(wi)) if conjugate else (vi, wi)
            v2 += vic * vi
            w2 += wic * wi
            vw += vic * wi
    else:
        for vi, wi, o in zip_longest(v, w, weights, fillvalue=zero):
            vic, wic = (try_conjugate(vi), try_conjugate(wi)) if conjugate else (vi, wi)
            v2 += vic * vi * o
            w2 += wic * wi * o
            vw += vic * wi * o
    return v2 * w2 == (try_conjugate(vw) if conjugate else vw) * vw

vector_space

vecpos(v)

Return the vector with the unary positive operator applied.

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

For a vector of length \(n\) there will be

  • \(n\) scalar unary plus operations (pos).
Source code in vector\functional\vector_space.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def vecpos(v):
    r"""Return the vector with the unary positive operator applied.

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

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar unary plus operations (`pos`).
    """
    return tuple(veclpos(v))

vecneg(v)

Return the vector with the unary negative operator applied.

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

For a vector of length \(n\) there will be

  • \(n\) scalar negations (neg).
Source code in vector\functional\vector_space.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def vecneg(v):
    r"""Return the vector with the unary negative operator applied.

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

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar negations (`neg`).
    """
    return tuple(veclneg(v))

vecadd(*vs)

Return the sum of vectors.

\[ \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} \]
Complexity

For two vectors of lengths \(n\) & \(m\) there will be

  • \(\min\{n, m\}\) scalar additions (add).
Source code in vector\functional\vector_space.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def vecadd(*vs):
    r"""Return the sum of vectors.

    $$
        \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}
    $$

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be

    - $\min\{n, m\}$ scalar additions (`add`).
    """
    return tuple(vecladd(*vs))

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

Return v with c added to the i-th coefficient.

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

More efficient than vecadd(v, vecbasis(i, c)).

Complexity

There will be

  • one scalar addition (add) if \(i\le n\) or
  • one unary plus operations (pos) otherwise.
Source code in vector\functional\vector_space.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def vecaddc(v, c, i=0, zero=0):
    r"""Return `v` with `c` added to the `i`-th coefficient.

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

    More efficient than `vecadd(v, vecbasis(i, c))`.

    Complexity
    ----------
    There will be

    - one scalar addition (`add`) if $i\le n$ or
    - one unary plus operations (`pos`) otherwise.
    """
    return tuple(vecladdc(v, c, i=i, zero=zero))

vecsub(v, w)

Return the difference of two vectors.

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

For two vectors of lengths \(n\) & \(m\) there will be

  • \(\min\{n, m\}\) scalar subtractions (sub) &
  • \(\begin{cases}m-n&m\ge n\\0&m\le n\end{cases}\) negations (neg).
Source code in vector\functional\vector_space.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def vecsub(v, w):
    r"""Return the difference of two vectors.

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

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be

    - $\min\{n, m\}$ scalar subtractions (`sub`) &
    - $\begin{cases}m-n&m\ge n\\0&m\le n\end{cases}$ negations (`neg`).
    """
    return tuple(veclsub(v, w))

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

Return v with c added to the i-th coefficient.

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

More efficient than vecsub(v, vecbasis(i, c)).

Complexity

There will be

  • one scalar subtraction (sub) if \(i\le n\) or
  • one scalar negation (neg) otherwise.
Source code in vector\functional\vector_space.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def vecsubc(v, c, i=0, zero=0):
    r"""Return `v` with `c` added to the `i`-th coefficient.

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

    More efficient than `vecsub(v, vecbasis(i, c))`.

    Complexity
    ----------
    There will be

    - one scalar subtraction (`sub`) if $i\le n$ or
    - one scalar negation (`neg`) otherwise.
    """
    return tuple(veclsubc(v, c, i=i, zero=zero))

vecmul(a, v)

Return the product of a scalar and a vector.

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

For a vector of length \(n\) there will be

  • \(n\) scalar multiplications (rmul).
Source code in vector\functional\vector_space.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def vecmul(a, v):
    r"""Return the product of a scalar and a vector.

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

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar multiplications (`rmul`).
    """
    return tuple(veclmul(a, v))

vectruediv(v, a)

Return the true division of a vector and a scalar.

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

For a vector of length \(n\) there will be

  • \(n\) scalar true divisions (truediv).
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\functional\vector_space.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def vectruediv(v, a):
    r"""Return the true division of a vector and a scalar.

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

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar true divisions (`truediv`).

    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.
    """
    return tuple(vecltruediv(v, a))

vecfloordiv(v, a)

Return the floor division of a vector and a scalar.

\[ \left(\left\lfloor\frac{v_i}{a}\right\rfloor\right)_i \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n \]
Complexity

For a vector of length \(n\) there will be

  • \(n\) scalar floor divisions (floordiv).
Source code in vector\functional\vector_space.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def vecfloordiv(v, a):
    r"""Return the floor division of a vector and a scalar.

    $$
        \left(\left\lfloor\frac{v_i}{a}\right\rfloor\right)_i \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n
    $$

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar floor divisions (`floordiv`).
    """
    return tuple(veclfloordiv(v, a))

vecmod(v, a)

Return the elementwise mod of a vector and a scalar.

\[ \left(v_i \mod a\right)_i \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n \]
Complexity

For a vector of length \(n\) there will be

  • \(n\) scalar modulos (mod).
Source code in vector\functional\vector_space.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def vecmod(v, a):
    r"""Return the elementwise mod of a vector and a scalar.

    $$
        \left(v_i \mod a\right)_i \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n
    $$

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar modulos (`mod`).
    """
    return tuple(veclmod(v, a))

vecdivmod(v, a)

Return the elementwise divmod of a vector and a scalar.

\[ \left(\left\lfloor\frac{v_i}{a}\right\rfloor\right)_i, \ \left(v_i \mod a\right)_i \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n\times\mathbb{K}^n \]
Complexity

For a vector of length \(n\) there will be

  • \(n\) scalar divmods (divmod).
Source code in vector\functional\vector_space.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
def vecdivmod(v, a):
    r"""Return the elementwise divmod of a vector and a scalar.

    $$
        \left(\left\lfloor\frac{v_i}{a}\right\rfloor\right)_i, \ \left(v_i \mod a\right)_i \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n\times\mathbb{K}^n
    $$

    Complexity
    ----------
    For a vector of length $n$ there will be

    - $n$ scalar divmods (`divmod`).
    """
    q, r = [], []
    for vi in v:
        qi, ri = divmod(vi, a)
        q.append(qi)
        r.append(ri)
    return tuple(q), tuple(r)

elementwise

vechadamard(*vs)

Return the elementwise product of vectors.

\[ \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} \]
Complexity

For vectors of lengths \(n_1, n_2, \dots, n_N\) there will be

  • \(\begin{cases}(N-1)\min_in_i&N\ge1\land\min_in_i\ge1\\0&N\le1\lor\min_in_i=0\end{cases}\) scalar multiplications (mul).
Source code in vector\functional\elementwise.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def vechadamard(*vs):
    r"""Return the elementwise product of vectors.

    $$
        \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}
    $$

    Complexity
    ----------
    For vectors of lengths $n_1, n_2, \dots, n_N$ there will be

    - $\begin{cases}(N-1)\min_in_i&N\ge1\land\min_in_i\ge1\\0&N\le1\lor\min_in_i=0\end{cases}$ scalar multiplications (`mul`).
    """
    return tuple(veclhadamard(*vs))

vechadamardtruediv(v, w)

Return the elementwise true division of two vectors.

\[ \left(\frac{v_i}{w_i}\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m \]
Complexity

For two vectors of lengths \(n\) & \(m\) there will be

  • \(n\) scalar true divisions (truediv).
Source code in vector\functional\elementwise.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def vechadamardtruediv(v, w):
    r"""Return the elementwise true division of two vectors.

    $$
        \left(\frac{v_i}{w_i}\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m
    $$

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be

    - $n$ scalar true divisions (`truediv`).
    """
    return tuple(veclhadamardtruediv(v, w))

vechadamardfloordiv(v, w)

Return the elementwise floor division of two vectors.

\[ \left(\left\lfloor\frac{v_i}{w_i}\right\rfloor\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m \]
Complexity

For two vectors of lengths \(n\) & \(m\) there will be

  • \(n\) scalar floor divisions (floordiv).
Source code in vector\functional\elementwise.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def vechadamardfloordiv(v, w):
    r"""Return the elementwise floor division of two vectors.

    $$
        \left(\left\lfloor\frac{v_i}{w_i}\right\rfloor\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m
    $$

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be

    - $n$ scalar floor divisions (`floordiv`).
    """
    return tuple(veclhadamardfloordiv(v, w))

vechadamardmod(v, w)

Return the elementwise mod of two vectors.

\[ \left(v_i \mod w_i\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m \]
Complexity

For two vectors of lengths \(n\) & \(m\) there will be

  • \(n\) scalar modulos (mod).
Source code in vector\functional\elementwise.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def vechadamardmod(v, w):
    r"""Return the elementwise mod of two vectors.

    $$
        \left(v_i \mod w_i\right)_i \qquad \mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^m
    $$

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be

    - $n$ scalar modulos (`mod`).
    """
    return tuple(veclhadamardmod(v, w))

vechadamarddivmod(v, w)

Return the elementwise divmod of two vectors.

\[ \left(\left\lfloor\frac{v_i}{w_i}\right\rfloor\right)_i, \ \left(v_i \mod w_i\right)_i \qquad \mathbb{K}^n\times\mathbb{K}^m\to\mathbb{K}^n\times\mathbb{K}^n \]
Complexity

For two vectors of lengths \(n\) & \(m\) there will be

  • \(n\) scalar divmods (divmod).
Source code in vector\functional\elementwise.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def vechadamarddivmod(v, w):
    r"""Return the elementwise divmod of two vectors.

    $$
        \left(\left\lfloor\frac{v_i}{w_i}\right\rfloor\right)_i, \ \left(v_i \mod w_i\right)_i \qquad \mathbb{K}^n\times\mathbb{K}^m\to\mathbb{K}^n\times\mathbb{K}^n
    $$

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be

    - $n$ scalar divmods (`divmod`).
    """
    q, r = [], []
    for qi, ri in veclhadamarddivmod(v, w):
        q.append(qi)
        r.append(ri)
    return tuple(q), tuple(r)

vechadamardmin(*vs, key=None)

Return the elementwise minimum of vectors.

\[ \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} \]
Complexity

For two vectors of lengths \(n\) & \(m\) there will be

  • \(\min\{n, m\}\) comparisons (lt).
Source code in vector\functional\elementwise.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def vechadamardmin(*vs, key=None):
    r"""Return the elementwise minimum of vectors.

    $$
        \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}
    $$

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be

    - $\min\{n, m\}$ comparisons (`lt`).
    """
    return tuple(veclhadamardmin(*vs, key=key))

vechadamardmax(*vs, key=None)

Return the elementwise maximum of vectors.

\[ \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} \]
Complexity

For two vectors of lengths \(n\) & \(m\) there will be

  • \(\min\{n, m\}\) comparisons (gt).
Source code in vector\functional\elementwise.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def vechadamardmax(*vs, key=None):
    r"""Return the elementwise maximum of vectors.

    $$
        \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}
    $$

    Complexity
    ----------
    For two vectors of lengths $n$ & $m$ there will be

    - $\min\{n, m\}$ comparisons (`gt`).
    """
    return tuple(veclhadamardmax(*vs, key=key))