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 (defaulttuple, configurable viafactory).veci...— accept aMutableSequence, 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.
An empty tuple that is also callable as a factory shorthand:
veczero == (), veczero() returns (), veczero(factory=list) returns [].
vecbasis(i: int, c: Any = 1, zero: Any = 0, factory: Callable[[Iterable], S] = tuple) -> S
Return a basis vector.
Returns a tuple with i many zeros followed by c.
See also
- for all basis vectors:
vecbases
Source code in vector\dense\creation.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
vecbases(start: int = 0, c: Any = 1, zero: Any = 0, factory: Callable[[Iterable], S] = tuple) -> Generator[S]
Yield all basis vectors.
See also
- for single basis vector:
vecbasis
Source code in vector\dense\creation.py
51 52 53 54 55 56 57 58 59 60 61 62 63 | |
vecrand(n: int, factory: Callable[[Iterable], S] = tuple) -> S
Return a random vector of uniformly sampled float coefficients.
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 | |
vecrandn(n: int, normed: bool = True, mu: float = 0, sigma: float = 1, weights: Iterable | None = None, factory: Callable[[Iterable], S] = tuple) -> S
Return a random vector of normally sampled float coefficients.
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 | |
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
20 21 22 23 24 25 26 27 28 29 30 | |
veceq(v: Iterable, w: Iterable) -> bool
Return whether two vectors are equal.
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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
vectrim(v: Iterable, tol: Any | None = None, factory: Callable[[Iterable], S] | None = None) -> S
Remove all trailing near zero (abs(v_i)<=tol) coefficients.
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)<=tolinstead ofabs(v_i)<tolto allow cutting of elements that are exactly zero bytrim(v, 0)instead oftrim(v, sys.float_info.min).
Source code in vector\dense\utility.py
50 51 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 | |
vecitrim(v: M, tol: Any | None = None) -> M
Remove all trailing near zero (abs(v_i)<=tol) coefficients.
tol may also be None,
then all coefficients that evaluate to False are trimmed.
Notes
- Cutting of elements that are
abs(v_i)<=tolinstead ofabs(v_i)<tolto allow cutting of elements that are exactly zero bytrim(v, 0)instead oftrim(v, sys.float_info.min).
Source code in vector\dense\utility.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
vecrshift(v: Iterable, n: int, zero: Any = 0, factory: Callable[[Iterable], S] | None = None) -> S
Shift coefficients up.
Source code in vector\dense\utility.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
vecirshift(v: M, n: int, zero: Any = 0) -> M
Shift coefficients up.
Source code in vector\dense\utility.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
veclshift(v: Iterable, n: int, factory: Callable[[Iterable], S] | None = None) -> S
Shift coefficients down.
Source code in vector\dense\utility.py
145 146 147 148 149 150 151 152 153 154 155 156 157 | |
vecilshift(v: M, n: int) -> M
Shift coefficients down.
Source code in vector\dense\utility.py
159 160 161 162 163 164 165 166 167 168 169 170 171 | |
hilbertspace
vecconj(v: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the complex conjugate.
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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
veciconj(v: M) -> M
Complex conjugate.
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
42 43 44 45 46 47 48 49 50 51 52 53 54 | |
vecabs(v: Iterable, weights: Iterable | None = None, conjugate: bool = False, zero: Any = 0) -> Any
Return the Euclidean/\(\ell_{\mathbb{N}_0}^2\)-norm.
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.5call.
See also
- squared version without square root:
vecabsq
Source code in vector\dense\hilbertspace.py
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 | |
vecabsq(v: Iterable, weights: Iterable | None = None, conjugate: bool = False, zero: Any = 0) -> Any
Return the sum of absolute squares.
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
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 109 110 111 112 113 114 115 116 117 | |
vecdot(v: Iterable, w: Iterable, weights: Iterable | None = None, conjugate: bool = False, zero: Any = 0) -> Any
Return the inner product.
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 (
mulwithout/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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
vectorspace
vecpos(v: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the identity.
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 | |
vecipos(v: M) -> M[Any, ...]
Apply the unary plus operator.
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 | |
vecneg(v: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the negation.
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 | |
vecineg(v: M) -> M[Any, ...]
Negate.
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 | |
vecadd(*vs: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the sum.
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 | |
veciadd(v: M, *ws: Iterable) -> M
Add.
See also
- for sum on a single coefficient:
veciaddc
Source code in vector\dense\vectorspace.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
vecaddc(v: Iterable, c: Any, i: int = 0, zero: Any = 0, factory: Callable[[Iterable], S] | None = None) -> S
Return the sum with a basis vector.
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
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
veciaddc(v: M, c: Any, i: int = 0, zero: Any = 0) -> M
Add a basis vector.
More efficient than veciaddc(v, vecibasis(i, c)).
See also
- for sum on more coefficients:
veciadd
Source code in vector\dense\vectorspace.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
vecsub(v: Iterable, w: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the difference.
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
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
vecisub(v: M, w: Iterable) -> M
Subtract.
See also
- for difference on a single coefficient:
vecisubc
Source code in vector\dense\vectorspace.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
vecsubc(v: Iterable, c: Any, i: int = 0, zero: Any = 0, factory: Callable[[Iterable], S] | None = None) -> S
Return the difference with a basis vector.
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
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
vecisubc(v: M, c: Any, i: int = 0, zero: Any = 0) -> M
Subtract a basis vector.
More efficient than vecisub(v, vecibasis(i, c)).
See also
- for difference on more coefficients:
vecisub
Source code in vector\dense\vectorspace.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
vecmul(v: Iterable, a: Any, factory: Callable[[Iterable], S] | None = None) -> S
Return the product.
Complexity
For a vector of length \(n\) there will be
- \(n\) scalar multiplications (
rmul).
Source code in vector\dense\vectorspace.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
vecrmul(a: Any, v: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the product.
Complexity
For a vector of length \(n\) there will be
- \(n\) scalar multiplications (
rmul).
Source code in vector\dense\vectorspace.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
vecimul(v: M, a: Any) -> M
Multiply.
Source code in vector\dense\vectorspace.py
294 295 296 297 298 299 300 301 302 303 | |
vectruediv(v: Iterable, a: Any, factory: Callable[[Iterable], S] | None = None) -> S
Return the true quotient.
Complexity
For a vector of length \(n\) there will be
- \(n\) scalar true divisions (
truediv).
Notes
Why called truediv instead of div?
divwould 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,truedivandfloordivoperations have to be provided, and none should be privileged over the other by getting the universaldivname.truediv/floordivis unambiguous, like Pythonoperators.
Source code in vector\dense\vectorspace.py
306 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 | |
vecitruediv(v: M, a: Any) -> M
True divide.
Notes
Why called truediv instead of div?
divwould 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,truedivandfloordivoperations have to be provided, and none should be privileged over the other by getting the universaldivname.truediv/floordivis unambiguous, like Pythonoperators.
Source code in vector\dense\vectorspace.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
vecfloordiv(v: Iterable, a: Any, factory: Callable[[Iterable], S] | None = None) -> S
Return the floor quotient.
Complexity
For a vector of length \(n\) there will be
- \(n\) scalar floor divisions (
floordiv).
Source code in vector\dense\vectorspace.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
vecifloordiv(v: M, a: Any) -> M
Floor divide.
Source code in vector\dense\vectorspace.py
372 373 374 375 376 377 378 379 380 381 | |
vecmod(v: Iterable, a: Any, factory: Callable[[Iterable], S] | None = None) -> S
Return the remainder.
Complexity
For a vector of length \(n\) there will be
- \(n\) scalar modulos (
mod).
Source code in vector\dense\vectorspace.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
vecimod(v: M, a: Any) -> M
Mod.
Source code in vector\dense\vectorspace.py
400 401 402 403 404 405 406 407 408 409 | |
vecdivmod(v: Iterable, a: Any, factory: Callable[[Iterable], S] | None = None) -> tuple[S, S]
Return the floor quotient and remainder.
Complexity
For a vector of length \(n\) there will be
- \(n\) scalar divmods (
divmod).
Source code in vector\dense\vectorspace.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
elementwise
vechadamard(*vs: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the elementwise product.
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 | |
vecihadamard(v: M, *ws: Sequence) -> M
Return the elementwise product.
Source code in vector\dense\elementwise.py
39 40 41 42 43 44 45 46 47 48 49 50 51 | |
vechadamardtruediv(v: Iterable, w: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the elementwise true quotient.
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 | |
vecihadamardtruediv(v: M, w: Iterable) -> M
Return the elementwise true quotient.
Source code in vector\dense\elementwise.py
70 71 72 73 74 75 76 77 78 79 | |
vechadamardfloordiv(v: Iterable, w: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the elementwise floor quotient.
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 | |
vecihadamardfloordiv(v: M, w: Iterable) -> M
Return the elementwise floor quotient.
Source code in vector\dense\elementwise.py
98 99 100 101 102 103 104 105 106 107 | |
vechadamardmod(v: Iterable, w: Iterable, factory: Callable[[Iterable], S] | None = None) -> S
Return the elementwise remainder.
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 | |
vecihadamardmod(v: M, w: Iterable) -> M
Return the elementwise remainder.
Source code in vector\dense\elementwise.py
126 127 128 129 130 131 132 133 134 135 | |
vechadamarddivmod(v: Iterable, w: Iterable, factory: Callable[[Iterable], S] | None = None) -> tuple[S, S]
Return the elementwise floor quotient and remainder.
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 | |
vechadamardmin(*vs: Iterable, key: Callable[[Any], Any] | None = None, factory: Callable[[Iterable], S] | None = None) -> S
Return the elementwise minimum.
Complexity
For two vectors of lengths \(n\) & \(m\) there will be
- \(\min\{n, m\}\) comparisons (
lt).
Source code in vector\dense\elementwise.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
vechadamardmax(*vs: Iterable, key: Callable[[Any], Any] | None = None, factory: Callable[[Iterable], S] | None = None) -> S
Return the elementwise maximum.
Complexity
For two vectors of lengths \(n\) & \(m\) there will be
- \(\min\{n, m\}\) comparisons (
gt).
Source code in vector\dense\elementwise.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |