Skip to content

Dense

Dense vector operations on sequences.

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

Two families of functions:

  • vec... — accept any iterable, return a new sequence (default tuple, configurable via factory).
  • veci... — accept a MutableSequence, mutate it in-place and return it.

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: _VecZero = _VecZero()

Zero vector.

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

An empty tuple that is also callable as a factory shorthand: veczero == (), veczero() returns (), veczero(factory=list) returns [].

vecbasis(i: int, c: T = 1, zero: T = 0, factory: Callable[[Iterable[T]], V] = tuple) -> V

Return a basis vector.

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

Returns a tuple with i many zeros followed by c.

See also
Source code in vector\dense\creation.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def vecbasis(i:int, c:T=1, zero:T=0, factory:Callable[[Iterable[T]],V]=tuple) -> V:
    r"""Return a basis vector.

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

    Returns a tuple with `i` many `zero`s followed by `c`.

    See also
    --------
    - for all basis vectors: [`vecbases`][vector.dense.creation.vecbases]
    """
    return factory(veclbasis(i, c, zero))

vecbases(start: int = 0, c: T = 1, zero: T = 0, factory: Callable[[Iterable[T]], V] = tuple) -> Generator[V]

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\dense\creation.py
51
52
53
54
55
56
57
58
59
60
61
62
63
def vecbases(start:int=0, c:T=1, zero:T=0, factory:Callable[[Iterable[T]],V]=tuple) -> Generator[V]:
    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: [`vecbasis`][vector.dense.creation.vecbasis]
    """
    for v in veclbases(start, c, zero):
        yield factory(v)

vecrand(n: int, factory: Callable[[Iterable[float]], V] = tuple) -> V

Return a random vector of uniformly 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\dense\creation.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def vecrand(n:int, factory:Callable[[Iterable[float]],V]=tuple) -> V:
    r"""Return a random vector of uniformly 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).
    """
    return factory(veclrand(n))

vecrandn(n: int, normed: bool = True, mu: float = 0.0, sigma: float = 1.0, weights: Iterable[float] | None = None, factory: Callable[[Iterable[float]], V] = tuple) -> V

Return a random vector of normally sampled 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 numpy.random, because seems more concise (not random & gauss as in the stdlib).

Source code in vector\dense\creation.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def vecrandn(n:int, normed:bool=True, mu:float=0.0, sigma:float=1.0, weights:Iterable[float]|None=None, factory:Callable[[Iterable[float]],V]=tuple) -> V:
    r"""Return a random vector of normally sampled `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 [`numpy.random`](https://numpy.org/doc/stable/reference/random/legacy.html),
    because seems more concise (not `random` & `gauss` as in the stdlib).
    """
    result = veclrandn(n, mu, sigma)
    if not normed:
        return factory(result)
    else:
        v:tuple[float,...] = tuple(result)
        return vectruediv(v, vecabs(v, weights), factory=factory)

utility

veclen(v: Iterable) -> int

Return the length (number of set coefficients).

Doesn't handle trailing zeros, use vectrim if needed.

Notes

For generators as they have no length, altough the vector is gone then.

Source code in vector\dense\utility.py
21
22
23
24
25
26
27
28
29
30
31
32
33
def veclen(v:Iterable) -> int:
    """Return the length (number of set coefficients).

    Doesn't handle trailing zeros, use [`vectrim`][vector.dense.utility.vectrim]
    if needed.

    Notes
    -----
    For generators as they have no `len`gth, altough the vector is gone then.
    """
    if hasattr(v, '__len__'):
        return len(v)
    return sum(1 for _ in v)

veceq(v: Iterable, w: Iterable, factory: Callable[[Iterable[bool]], V] = all) -> V

Return whether 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\dense\utility.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def veceq(v:Iterable, w:Iterable, factory:Callable[[Iterable[bool]],V]=all) -> V:
    r"""Return whether 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`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(vecleq(v, w))

vectrim(v: Iterable[T], tol: T | None = None, factory: Callable[[Iterable[T]], V] | None = None) -> V

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

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

Complexity

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

  • \(n\) scalar boolean evaluations (bool).
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\dense\utility.py
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
def vectrim(v:Iterable[T], tol:T|None=None, factory:Callable[[Iterable[T]],V]|None=None) -> V:
    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}
    $$

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

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

    - $n$ scalar boolean evaluations (`bool`).

    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)`.
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(vecltrim(v, tol))

vecitrim(v: M, tol: Any | None = None) -> M

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

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\dense\utility.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def vecitrim(v:M, tol:Any|None=None) -> M:
    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}
    $$

    `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)`.
    """
    p = (lambda x: bool(x)) if tol is None else (lambda x: abs(x)>tol)
    idx = next((i for i in reversed(range(len(v))) if p(v[i])), -1)
    del v[idx+1:]
    return v

vecrshift(v: Iterable, n: int, zero: Any = 0, factory: Callable[[Iterable], V] | None = None) -> V

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\dense\utility.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def vecrshift(v:Iterable, n:int, zero:Any=0, factory:Callable[[Iterable],V]|None=None) -> V:
    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}
    $$
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclrshift(v, n, zero))

vecirshift(v: M, n: int, zero: Any = 0) -> M

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\dense\utility.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def vecirshift(v:M, n:int, zero:Any=0) -> M:
    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}
    $$
    """
    v[:0] = (zero,) * n
    return v

veclshift(v: Iterable, n: int, factory: Callable[[Iterable], V] | None = None) -> V

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\dense\utility.py
144
145
146
147
148
149
150
151
152
153
154
155
156
def veclshift(v:Iterable, n:int, factory:Callable[[Iterable],V]|None=None) -> V:
    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\}}
    $$
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(vecllshift(v, n))

vecilshift(v: M, n: int) -> M

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\dense\utility.py
158
159
160
161
162
163
164
165
166
167
168
169
170
def vecilshift(v:M, n:int) -> M:
    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\}}
    $$
    """
    del v[:n]
    return v

hilbertspace

vecconj(v: Iterable, factory: Callable[[Iterable], V] | None = None) -> 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.

Complexity

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

  • \(n\) scalar conjugations (conjugate).
Source code in vector\dense\hilbertspace.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def vecconj(v:Iterable, factory:Callable[[Iterable],V]|None=None) -> 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.

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

    - $n$ scalar conjugations (`conjugate`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclconj(v))

veciconj(v: M) -> M

Complex conjugate.

\[ \vec{v} = \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\dense\hilbertspace.py
40
41
42
43
44
45
46
47
48
49
50
51
52
def veciconj(v:M) -> M:
    r"""Complex conjugate.

    $$
        \vec{v} = \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.
    """
    for i, vi in enumerate(v):
        v[i] = try_conjugate(vi)
    return v

vecabs(v: Iterable, weights: Iterable | None = None, conjugate: bool = False, zero: Any = 0) -> Any

Return the Euclidean/\(\ell_{\mathbb{N}_0}^2\)-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 ^0.5 call.
See also
  • squared version without square root: vecabsq
Source code in vector\dense\hilbertspace.py
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
def vecabs(v:Iterable, weights:Iterable|None=None, conjugate:bool=False, zero:Any=0) -> Any:
    r"""Return the Euclidean/$\ell_{\mathbb{N}_0}^2$-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.dense.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 `^0.5` call.

    See also
    --------
    - squared version without square root: [`vecabsq`][vector.dense.hilbertspace.vecabsq]
    """
    #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: Iterable, weights: Iterable | None = None, conjugate: bool = False, zero: Any = 0) -> Any

Return the sum of absolute squares.

\[ ||\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\dense\hilbertspace.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def vecabsq(v:Iterable, weights:Iterable|None=None, conjugate:bool=False, zero:Any=0) -> Any:
    r"""Return the sum of absolute squares.

    $$
        ||\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`
    """
    return vecdot(*tee(v, 2), weights, conjugate, zero)

vecdot(v: Iterable, w: Iterable, weights: Iterable | None = None, conjugate: bool = False, zero: Any = 0) -> Any

Return the inner product.

\[ \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\dense\hilbertspace.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def vecdot(v:Iterable, w:Iterable, weights:Iterable|None=None, conjugate:bool=False, zero:Any=0) -> Any:
    r"""Return the inner product.

    $$
        \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 = vecconj(v, iter)
    #don't sum(vecldot) but rather use sumprod explicitly for improved float precision
    if weights is None:
        return sumprod_default(v, w, default=zero)
    else:
        return sumprod_default(map(mul, v, w), weights, default=zero)

vectorspace

vecpos(v: Iterable, factory: Callable[[Iterable], V] | None = None) -> V

Return the identity.

\[ +\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\dense\vectorspace.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def vecpos(v:Iterable, factory:Callable[[Iterable],V]|None=None) -> V:
    r"""Return the identity.

    $$
        +\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`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclpos(v))

vecipos(v: M) -> M

Apply the unary plus operator.

\[ +\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\dense\vectorspace.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def vecipos(v:M) -> M:
    r"""Apply the unary plus operator.

    $$
        +\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`).
    """
    for i, vi in enumerate(v):
        v[i] = +vi
    return v

vecneg(v: Iterable, factory: Callable[[Iterable], V] | None = None) -> V

Return the negation.

\[ -\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\dense\vectorspace.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def vecneg(v:Iterable, factory:Callable[[Iterable],V]|None=None) -> V:
    r"""Return the negation.

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

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

    - $n$ scalar negations (`neg`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclneg(v))

vecineg(v: M) -> M

Negate.

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

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

  • \(n\) scalar unary negations (neg).
Source code in vector\dense\vectorspace.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def vecineg(v:M) -> M:
    r"""Negate.

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

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

    - $n$ scalar unary negations (`neg`).
    """
    for i, vi in enumerate(v):
        v[i] = -vi
    return v

vecadd(*vs: Iterable, factory: Callable[[Iterable], V] | None = None) -> V

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

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

  • \(\min\{n, m\}\) scalar additions (add).
See also
  • for sum on a single coefficient: vecaddc
Source code in vector\dense\vectorspace.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def vecadd(*vs:Iterable, factory:Callable[[Iterable],V]|None=None) -> V:
    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}
    $$

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

    - $\min\{n, m\}$ scalar additions (`add`).

    See also
    --------
    - for sum on a single coefficient: [`vecaddc`][vector.dense.vectorspace.vecaddc]
    """
    result = vecladd(*vs)
    if factory is None:
        factory = (iter if isinstance(vs[0], Iterator) else type(vs[0])) if vs else tuple
    return factory(result)

veciadd(v: M, *ws: Iterable) -> M

Add.

\[ \vec{v} += \vec{w}_0+\vec{w}_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: veciaddc
Source code in vector\dense\vectorspace.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def veciadd(v:M, *ws:Iterable) -> M:
    r"""Add.

    $$
        \vec{v} += \vec{w}_0+\vec{w}_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: [`veciaddc`][vector.dense.vectorspace.veciaddc]
    """
    it = map(sum_default, group_ordinal(*ws))
    for i, wi in enumerate(islice(it, len(v))):
        v[i] += wi
    v.extend(+wi for wi in it)
    return v

vecaddc(v: Iterable, c: Any, i: int = 0, zero: Any = 0, factory: Callable[[Iterable], V] | None = None) -> V

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 vecadd(v, vecbasis(i, c)).

Complexity

There will be

  • one scalar addition (add) if \(i\le n\) or
  • one unary plus operations (pos) otherwise.
See also
  • for sum on more coefficients: vecadd
Source code in vector\dense\vectorspace.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def vecaddc(v:Iterable, c:Any, i:int=0, zero:Any=0, factory:Callable[[Iterable],V]|None=None) -> V:
    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 `vecadd(v, vecbasis(i, c))`.

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

    - one scalar addition (`add`) if $i\le n$ or
    - one unary plus operations (`pos`) otherwise.

    See also
    --------
    - for sum on more coefficients: [`vecadd`][vector.dense.vectorspace.vecadd]
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(vecladdc(v, c, i, zero))

veciaddc(v: M, c: Any, i: int = 0, zero: Any = 0) -> M

Add a basis vector.

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

More efficient than veciaddc(v, vecibasis(i, c)).

See also
  • for sum on more coefficients: veciadd
Source code in vector\dense\vectorspace.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def veciaddc(v:M, c:Any, i:int=0, zero:Any=0) -> M:
    r"""Add a basis vector.

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

    More efficient than `veciaddc(v, vecibasis(i, c))`.

    See also
    --------
    - for sum on more coefficients: [`veciadd`][vector.dense.vectorspace.veciadd]
    """
    if i < len(v):
        v[i] += c
    else:
        v.extend(repeat(zero, i-len(v)))
        v.append(+c)
    return v

vecsub(v: Iterable, w: Iterable, factory: Callable[[Iterable], V] | None = None) -> V

Return the difference.

\[ \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).
See also
  • for difference on a single coefficient: vecsubc
Source code in vector\dense\vectorspace.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def vecsub(v:Iterable, w:Iterable, factory:Callable[[Iterable],V]|None=None) -> V:
    r"""Return the difference.

    $$
        \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`).

    See also
    --------
    - for difference on a single coefficient: [`vecsubc`][vector.dense.vectorspace.vecsubc]
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclsub(v, w))

vecisub(v: M, w: Iterable) -> M

Subtract.

\[ \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: vecisubc
Source code in vector\dense\vectorspace.py
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
def vecisub(v:M, w:Iterable) -> M:
    r"""Subtract.

    $$
        \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: [`vecisubc`][vector.dense.vectorspace.vecisubc]
    """
    it = iter(w)
    for i, wi in enumerate(islice(it, len(v))):
        v[i] -= wi
    v.extend(-wi for wi in it)
    return v

vecsubc(v: Iterable, c: Any, i: int = 0, zero: Any = 0, factory: Callable[[Iterable], V] | None = None) -> V

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 vecsub(v, vecbasis(i, c)).

Complexity

There will be

  • one scalar subtraction (sub) if \(i\le n\) or
  • one scalar negation (neg) otherwise.
See also
  • for difference on more coefficients: vecsub
Source code in vector\dense\vectorspace.py
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
def vecsubc(v:Iterable, c:Any, i:int=0, zero:Any=0, factory:Callable[[Iterable],V]|None=None) -> V:
    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 `vecsub(v, vecbasis(i, c))`.

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

    - one scalar subtraction (`sub`) if $i\le n$ or
    - one scalar negation (`neg`) otherwise.

    See also
    --------
    - for difference on more coefficients: [`vecsub`][vector.dense.vectorspace.vecsub]
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclsubc(v, c, i, zero))

vecisubc(v: M, c: Any, i: int = 0, zero: Any = 0) -> M

Subtract a basis vector.

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

More efficient than vecisub(v, vecibasis(i, c)).

See also
  • for difference on more coefficients: vecisub
Source code in vector\dense\vectorspace.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def vecisubc(v:M, c:Any, i:int=0, zero:Any=0) -> M:
    r"""Subtract a basis vector.

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

    More efficient than `vecisub(v, vecibasis(i, c))`.

    See also
    --------
    - for difference on more coefficients: [`vecisub`][vector.dense.vectorspace.vecisub]
    """
    if i < len(v):
        v[i] -= c
    else:
        v.extend(repeat(zero, i-len(v)))
        v.append(-c)
    return v

vecmul(v: Iterable, a: Any, factory: Callable[[Iterable], V] | None = None) -> V

Return the product.

\[ \vec{v}a \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\dense\vectorspace.py
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
def vecmul(v:Iterable, a:Any, factory:Callable[[Iterable],V]|None=None) -> V:
    r"""Return the product.

    $$
        \vec{v}a \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`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclmul(v, a))

vecrmul(a: Any, v: Iterable, factory: Callable[[Iterable], V] | None = None) -> V

Return the product.

\[ 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\dense\vectorspace.py
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
def vecrmul(a:Any, v:Iterable, factory:Callable[[Iterable],V]|None=None) -> V:
    r"""Return the product.

    $$
        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`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclrmul(a, v))

vecimul(v: M, a: Any) -> M

Multiply.

\[ \vec{v} \cdot= a \qquad \mathbb{K}\times\mathbb{K}^n\to\mathbb{K}^n \]
Source code in vector\dense\vectorspace.py
295
296
297
298
299
300
301
302
303
304
def vecimul(v:M, a:Any) -> M:
    r"""Multiply.

    $$
        \vec{v} \cdot= a \qquad \mathbb{K}\times\mathbb{K}^n\to\mathbb{K}^n
    $$
    """
    for i in range(len(v)):
        v[i] *= a
    return v

vectruediv(v: Iterable, a: Any, factory: Callable[[Iterable], V] | None = None) -> V

Return the true quotient.

\[ \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\dense\vectorspace.py
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
def vectruediv(v:Iterable, a:Any, factory:Callable[[Iterable],V]|None=None) -> V:
    r"""Return the true quotient.

    $$
        \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.
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(vecltruediv(v, a))

vecitruediv(v: M, a: Any) -> M

True divide.

\[ \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\dense\vectorspace.py
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
def vecitruediv(v:M, a:Any) -> M:
    r"""True divide.

    $$
        \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.
    """
    for i in range(len(v)):
        v[i] /= a
    return v

vecfloordiv(v: Iterable, a: Any, factory: Callable[[Iterable], V] | None = None) -> V

Return the floor quotient.

\[ \left\lfloor\frac{\vec{v}}{a}\right\rfloor \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\dense\vectorspace.py
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
def vecfloordiv(v:Iterable, a:Any, factory:Callable[[Iterable],V]|None=None) -> V:
    r"""Return the floor quotient.

    $$
        \left\lfloor\frac{\vec{v}}{a}\right\rfloor \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`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclfloordiv(v, a))

vecifloordiv(v: M, a: Any) -> M

Floor divide.

\[ \vec{v} //= a \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n \]
Source code in vector\dense\vectorspace.py
373
374
375
376
377
378
379
380
381
382
def vecifloordiv(v:M, a:Any) -> M:
    r"""Floor divide.

    $$
        \vec{v} //= a \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n
    $$
    """
    for i in range(len(v)):
        v[i] //= a
    return v

vecmod(v: Iterable, a: Any, factory: Callable[[Iterable], V] | None = None) -> V

Return the remainder.

\[ \vec{v} \bmod a \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\dense\vectorspace.py
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
def vecmod(v:Iterable, a:Any, factory:Callable[[Iterable],V]|None=None) -> V:
    r"""Return the remainder.

    $$
        \vec{v} \bmod a \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`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclmod(v, a))

vecimod(v: M, a: Any) -> M

Mod.

\[ \vec{v} \%= a \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n \]
Source code in vector\dense\vectorspace.py
401
402
403
404
405
406
407
408
409
410
def vecimod(v:M, a:Any) -> M:
    r"""Mod.

    $$
        \vec{v} \%= a \qquad \mathbb{K}^n\times\mathbb{K}\to\mathbb{K}^n
    $$
    """
    for i in range(len(v)):
        v[i] %= a
    return v

vecdivmod(v: Iterable, a: Any, factory: Callable[[Iterable], V] | None = None) -> Generator[tuple[Any, Any]] | tuple[V, V]

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

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

  • \(n\) scalar divmods (divmod).
Source code in vector\dense\vectorspace.py
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
def vecdivmod(v:Iterable, a:Any, factory:Callable[[Iterable],V]|None=None) -> Generator[tuple[Any,Any]]|tuple[V,V]:
    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
    $$

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

    - $n$ scalar divmods (`divmod`).
    """
    result = vecldivmod(v, a)

    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    if factory is iter:
        return result

    q, r = [], []
    for qi, ri in result:
        q.append(qi)
        r.append(ri)
    return factory(q), factory(r)

elementwise

vechadamard(*vs: Iterable, factory: Callable[[Iterable], V] | None = None) -> V

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} \]
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\dense\elementwise.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def vechadamard(*vs:Iterable, factory:Callable[[Iterable],V]|None=None) -> V:
    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}
    $$

    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`).
    """
    if factory is None:
        factory = (iter if isinstance(vs[0], Iterator) else type(vs[0])) if vs else tuple
    return factory(veclhadamard(*vs))

vecihadamard(v: M, *ws: Sequence) -> M

Return the elementwise product.

\[ \left((\vec{v})_i\cdot(\vec{w}_0)_i\cdot(\vec{w}_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\dense\elementwise.py
39
40
41
42
43
44
45
46
47
48
49
50
51
def vecihadamard(v:M, *ws:Sequence) -> M:
    r"""Return the elementwise product.

    $$
        \left((\vec{v})_i\cdot(\vec{w}_0)_i\cdot(\vec{w}_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}
    $$
    """
    if ws:
        del v[min(len(w) for w in ws):]
        for w in ws:
            for i, wi in enumerate(w[:len(v)]):
                v[i] *= wi
    return v

vechadamardtruediv(v: Iterable, w: Iterable, factory: Callable[[Iterable], V] | None = None) -> V

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

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

  • \(n\) scalar true divisions (truediv).
Source code in vector\dense\elementwise.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def vechadamardtruediv(v:Iterable, w:Iterable, factory:Callable[[Iterable],V]|None=None) -> V:
    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
    $$

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

    - $n$ scalar true divisions (`truediv`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclhadamardtruediv(v, w))

vecihadamardtruediv(v: M, w: Iterable) -> M

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\dense\elementwise.py
70
71
72
73
74
75
76
77
78
79
def vecihadamardtruediv(v:M, w:Iterable) -> M:
    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
    $$
    """
    for i, wi in enumerate(islice(chain(w, repeat(0)), len(v))):
        v[i] /= wi
    return v

vechadamardfloordiv(v: Iterable, w: Iterable, factory: Callable[[Iterable], V] | None = None) -> V

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

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

  • \(n\) scalar floor divisions (floordiv).
Source code in vector\dense\elementwise.py
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def vechadamardfloordiv(v:Iterable, w:Iterable, factory:Callable[[Iterable],V]|None=None) -> V:
    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
    $$

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

    - $n$ scalar floor divisions (`floordiv`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclhadamardfloordiv(v, w))

vecihadamardfloordiv(v: M, w: Iterable) -> M

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\dense\elementwise.py
 98
 99
100
101
102
103
104
105
106
107
def vecihadamardfloordiv(v:M, w:Iterable) -> M:
    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
    $$
    """
    for i, wi in enumerate(islice(chain(w, repeat(0)), len(v))):
        v[i] //= wi
    return v

vechadamardmod(v: Iterable, w: Iterable, factory: Callable[[Iterable], V] | None = None) -> V

Return the elementwise remainder.

\[ \left(v_i \bmod 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\dense\elementwise.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def vechadamardmod(v:Iterable, w:Iterable, factory:Callable[[Iterable],V]|None=None) -> V:
    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
    $$

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

    - $n$ scalar modulos (`mod`).
    """
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    return factory(veclhadamardmod(v, w))

vecihadamardmod(v: M, w: Iterable) -> M

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\dense\elementwise.py
126
127
128
129
130
131
132
133
134
135
def vecihadamardmod(v:M, w:Iterable) -> M:
    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
    $$
    """
    for i, wi in enumerate(islice(chain(w, repeat(0)), len(v))):
        v[i] %= wi
    return v

vechadamarddivmod(v: Iterable, w: Iterable, factory: Callable[[Iterable], V] | None = None) -> Generator[tuple[Any, Any]] | tuple[V, V]

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

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

  • \(n\) scalar divmods (divmod).
Source code in vector\dense\elementwise.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def vechadamarddivmod(v:Iterable, w:Iterable, factory:Callable[[Iterable],V]|None=None) -> Generator[tuple[Any,Any]]|tuple[V,V]:
    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
    $$

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

    - $n$ scalar divmods (`divmod`).
    """
    result = veclhadamarddivmod(v, w)
    factory = factory or (iter if isinstance(v, Iterator) else type(v))
    if factory is iter:
        return result

    q, r = [], []
    for qi, ri in result:
        q.append(qi)
        r.append(ri)
    return factory(q), factory(r)

vechadamardmin(*vs: Iterable, key: Callable[[Any], Any] | None = None, factory: Callable[[Iterable], V] | None = None) -> V

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

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

  • \(\min\{n, m\}\) comparisons (lt).
Source code in vector\dense\elementwise.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
def vechadamardmin(*vs:Iterable, key:Callable[[Any], Any]|None=None, factory:Callable[[Iterable],V]|None=None) -> V:
    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}
    $$

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

    - $\min\{n, m\}$ comparisons (`lt`).
    """
    if factory is None:
        factory = (iter if isinstance(vs[0], Iterator) else type(vs[0])) if vs else tuple
    return factory(veclhadamardmin(*vs, key=key))

vechadamardmax(*vs: Iterable, key: Callable[[Any], Any] | None = None, factory: Callable[[Iterable], V] | None = None) -> V

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

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

  • \(\min\{n, m\}\) comparisons (gt).
Source code in vector\dense\elementwise.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def vechadamardmax(*vs:Iterable, key:Callable[[Any], Any]|None=None, factory:Callable[[Iterable],V]|None=None) -> V:
    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}
    $$

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

    - $\min\{n, m\}$ comparisons (`gt`).
    """
    if factory is None:
        factory = (iter if isinstance(vs[0], Iterator) else type(vs[0])) if vs else tuple
    return factory(veclhadamardmax(*vs, key=key))