Multiaxis
Prefixed by ten...
(tensor).
Handle multiaxis vectors, that for example represent multivariate polynomials.
Tensors are returned as numpy.ndarray
s.
Broadcasting happens similar to numpy
s broadcasting,
but the axes are matched in ascending order instead of descending order, and
the arrays don't get stretched but rather padded with zeros.
Creation
tenzero = np.zeros((), dtype=object)
Zero tensor.
\[
0
\]
Notes
Why shape (0,)
(=one dimensional, zero length) instead of ()
(zero dimensional)?
Shape ()
would be size one (empty product) and a scalar that could have any nonzero value.
Dimensionality of one isn't perfect, but at least its size is then zero and it couln't be any arbitrary value.
See also
veczero
tenbasis(i, c=1)
Return the i
-th basis tensor times c
.
\[
ce_i
\]
Returns a numpy.ndarray
with i+1
zeros in each direction and a c
in
the outer corner.
See also
vecbasis
Source code in vector\multiaxis.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 | def tenbasis(i, c=1):
"""Return the `i`-th basis tensor times `c`.
$$
ce_i
$$
Returns a `numpy.ndarray` with `i+1` zeros in each direction and a `c` in
the outer corner.
See also
--------
[`vecbasis`][vector.functional.vecbasis]
"""
t = np.zeros(np.add(i, 1), dtype=np.result_type(c))
t[i] = c #dont unpack i, it might be a scalar
return t
|
tenrand(*d)
Return a random tensor of d
uniform coefficients in [0, 1[
.
\[
t \sim \mathcal{U}^d([0, 1[)
\]
d
may be multiple dimensions.
See also
numpy.random.rand
,
vecrand
Source code in vector\multiaxis.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 | def tenrand(*d):
r"""Return a random tensor of `d` uniform coefficients in `[0, 1[`.
$$
t \sim \mathcal{U}^d([0, 1[)
$$
`d` may be multiple dimensions.
See also
--------
[`numpy.random.rand`](https://numpy.org/doc/stable/reference/generated/numpy.random.rand.html),
[`vecrand`][vector.functional.vecrand]
"""
return np.random.rand(*d)
|
tenrandn(*d)
Return a random tensor of d
normal distributed coefficients.
\[
t \sim \mathcal{N}^d
\]
d
may be multiple dimensions.
See also
numpy.random.randn
,
vecrandn
Source code in vector\multiaxis.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 | def tenrandn(*d):
r"""Return a random tensor of `d` normal distributed coefficients.
$$
t \sim \mathcal{N}^d
$$
`d` may be multiple dimensions.
See also
--------
[`numpy.random.randn`](https://numpy.org/doc/stable/reference/generated/numpy.random.randn.html),
[`vecrandn`][vector.functional.vecrandn]
"""
return np.random.randn(*d)
|
Utility
tenrank(t)
Return the rank of a tensor.
\[
\text{rank}\,t
\]
See also
numpy.ndarray.ndim
Source code in vector\multiaxis.py
93
94
95
96
97
98
99
100
101
102
103
104 | def tenrank(t):
r"""Return the rank of a tensor.
$$
\text{rank}\,t
$$
See also
--------
[`numpy.ndarray.ndim`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.ndim.html)
"""
return np.asarray(t).ndim
|
tendim(t)
Return the dimensionalities of a tensor.
\[
\dim t
\]
See also
numpy.ndarray.shape
Source code in vector\multiaxis.py
106
107
108
109
110
111
112
113
114
115
116
117 | def tendim(t):
r"""Return the dimensionalities of a tensor.
$$
\dim t
$$
See also
--------
[`numpy.ndarray.shape`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html)
"""
return np.asarray(t).shape
|
tentrim(t, tol=1e-09)
Remove all trailing near zero (abs(v_i)<=tol
) coefficients.
See also
vectrim
Source code in vector\multiaxis.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 | def tentrim(t, tol=1e-9):
"""Remove all trailing near zero (`abs(v_i)<=tol`) coefficients.
See also
--------
[`vectrim`][vector.functional.vectrim]
"""
t = np.asarray(t)
for d in range(t.ndim): #reduce dimension
i = (slice(None, None, None),)*d + (-1,) + (...,)
while t.shape[d]>0 and np.all(np.abs(t[*i])<=tol):
t = t[(slice(None, None, None),)*d + (slice(0, -1),) + (...,)]
while t.shape and t.shape[-1] == 1: #reduce rank
t = t[..., 0]
return t
|
tenround(t, ndigits=0)
Round all coefficients to the given precision.
\[
(\text{round}_\text{ndigits}(v_i))_i
\]
See also
numpy.round
,
vecround
Source code in vector\multiaxis.py
135
136
137
138
139
140
141
142
143
144
145
146
147 | def tenround(t, ndigits=0):
r"""Round all coefficients to the given precision.
$$
(\text{round}_\text{ndigits}(v_i))_i
$$
See also
--------
[`numpy.round`](https://numpy.org/doc/stable/reference/generated/numpy.round.html),
[`vecround`][vector.functional.vecround]
"""
return np.round(t, ndigits)
|
Vector space
tenpos(t)
Return the tensor with the unary positive operator applied.
\[
+t
\]
See also
numpy.ndarray.__pos__
,
vecpos
Source code in vector\multiaxis.py
151
152
153
154
155
156
157
158
159
160
161
162
163 | def tenpos(t):
"""Return the tensor with the unary positive operator applied.
$$
+t
$$
See also
--------
[`numpy.ndarray.__pos__`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.__pos__.html),
[`vecpos`][vector.functional.vecpos]
"""
return +np.asarray(t)
|
tenneg(t)
Return the tensor with the unary negative operator applied.
\[
-t
\]
See also
numpy.ndarray.__neg__
,
vecneg
Source code in vector\multiaxis.py
165
166
167
168
169
170
171
172
173
174
175
176
177 | def tenneg(t):
"""Return the tensor with the unary negative operator applied.
$$
-t
$$
See also
--------
[`numpy.ndarray.__neg__`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.__neg__.html),
[`vecneg`][vector.functional.vecneg]
"""
return -np.asarray(t)
|
tenaddc(t, c, i=(0,))
Return t
with c
added to the i
-th coefficient.
More efficient than tenadd(t, tenbasis(i, c))
.
See also
vecaddc
Source code in vector\multiaxis.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 | def tenaddc(t, c, i=(0,)):
r"""Return `t` with `c` added to the `i`-th coefficient.
More efficient than `tenadd(t, tenbasis(i, c))`.
See also
--------
[`vecaddc`][vector.functional.vecaddc]
"""
t = np.asarray(t)
while t.ndim < len(i):
t = np.expand_dims(t, axis=-1)
t = np.pad(t, tuple((0, max(ii-s+1, 0)) for s, ii in zip(t.shape, i)))
t[i + (0,)*(len(i)-t.ndim)] += c
return t
|
tenadd(*ts)
Return the sum of tensors.
\[
t_0 + t_1 + \cdots
\]
See also
vecadd
Source code in vector\multiaxis.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 | def tenadd(*ts):
r"""Return the sum of tensors.
$$
t_0 + t_1 + \cdots
$$
See also
--------
[`vecadd`][vector.functional.vecadd]
"""
ts = tuple(map(np.asarray, ts))
shape = vechadamardmax(*(t.shape for t in ts))
r = np.zeros(shape, dtype=np.result_type(*ts) if ts else object)
for t in ts:
r[tuple(map(slice, t.shape)) + (0,)*(r.ndim-t.ndim)] += t
return r
|
tensub(s, t)
Return the difference of two tensors.
\[
s - t
\]
See also
vecsub
Source code in vector\multiaxis.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 | def tensub(s, t):
"""Return the difference of two tensors.
$$
s - t
$$
See also
--------
[`vecsub`][vector.functional.vecsub]
"""
s, t = np.asarray(s), np.asarray(t)
shape = vechadamardmax(s.shape, t.shape)
r = np.zeros(shape, dtype=np.result_type(s, t))
r[tuple(map(slice, s.shape)) + (0,)*(r.ndim-s.ndim)] = s
r[tuple(map(slice, t.shape)) + (0,)*(r.ndim-t.ndim)] -= t
return r
|
tenmul(a, t)
Return the product of a scalar and a tensor.
\[
a t
\]
See also
numpy.ndarray.__mul__
,
vecmul
Source code in vector\multiaxis.py
231
232
233
234
235
236
237
238
239
240
241
242
243 | def tenmul(a, t):
"""Return the product of a scalar and a tensor.
$$
a t
$$
See also
--------
[`numpy.ndarray.__mul__`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.__mul__.html),
[`vecmul`][vector.functional.vecmul]
"""
return a * np.asarray(t)
|
tentruediv(t, a)
Return the true division of a tensor and a scalar.
\[
\frac{t}{a}
\]
See also
numpy.ndarray.__truediv__
,
vectruediv
Source code in vector\multiaxis.py
245
246
247
248
249
250
251
252
253
254
255
256
257 | def tentruediv(t, a):
r"""Return the true division of a tensor and a scalar.
$$
\frac{t}{a}
$$
See also
--------
[`numpy.ndarray.__truediv__`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.__truediv__.html),
[`vectruediv`][vector.functional.vectruediv]
"""
return np.asarray(t) / a
|
tenfloordiv(t, a)
Return the floor division of a tensor and a scalar.
\[
\left(\left\lfloor\frac{t_i}{a}\right\rfloor\right)_i
\]
See also
numpy.ndarray.__floordiv__
,
vecfloordiv
Source code in vector\multiaxis.py
259
260
261
262
263
264
265
266
267
268
269
270
271 | def tenfloordiv(t, a):
r"""Return the floor division of a tensor and a scalar.
$$
\left(\left\lfloor\frac{t_i}{a}\right\rfloor\right)_i
$$
See also
--------
[`numpy.ndarray.__floordiv__`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.__floordiv__.html),
[`vecfloordiv`][vector.functional.vecfloordiv]
"""
return np.asarray(t) // a
|
tenmod(t, a)
Return the elementwise mod of a tensor and a scalar.
\[
\left(t_i \mod a\right)_i
\]
See also
numpy.ndarray.__mod__
,
vecmod
Source code in vector\multiaxis.py
273
274
275
276
277
278
279
280
281
282
283
284
285 | def tenmod(t, a):
r"""Return the elementwise mod of a tensor and a scalar.
$$
\left(t_i \mod a\right)_i
$$
See also
--------
[`numpy.ndarray.__mod__`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.__mod__.html),
[`vecmod`][vector.functional.vecmod]
"""
return np.asarray(t) % a
|
tendivmod(t, a)
Return the elementwise divmod of a tensor and a scalar.
\[
\left(\left\lfloor\frac{t_i}{a}\right\rfloor\right)_i, \ \left(t_i \mod a\right)_i
\]
See also
numpy.ndarray.__divmod__
,
vecdivmod
Source code in vector\multiaxis.py
287
288
289
290
291
292
293
294
295
296
297
298
299 | def tendivmod(t, a):
r"""Return the elementwise divmod of a tensor and a scalar.
$$
\left(\left\lfloor\frac{t_i}{a}\right\rfloor\right)_i, \ \left(t_i \mod a\right)_i
$$
See also
--------
[`numpy.ndarray.__divmod__`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.__divmod__.html),
[`vecdivmod`][vector.functional.vecdivmod]
"""
return divmod(np.asarray(t), a)
|
Elementwise
tenhadamard(*ts)
Return the elementwise product of tensors.
\[
\left((t_0)_i \cdot (t_1)_i \cdot \cdots\right)_i
\]
See also
vechadamard
Source code in vector\multiaxis.py
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 | def tenhadamard(*ts):
r"""Return the elementwise product of tensors.
$$
\left((t_0)_i \cdot (t_1)_i \cdot \cdots\right)_i
$$
See also
--------
[`vechadamard`][vector.functional.vechadamard]
"""
ts = tuple(map(np.asarray, ts))
shape = tuple(map(min, zip(*(t.shape for t in ts))))
r = np.zeros(shape, dtype=np.result_type(*ts) if ts else object)
if ts:
r = ts[0][tuple(map(slice, shape)), ...]
for t in ts[1:]:
r *= t[tuple(map(slice, shape)), ...]
return r
|
tenhadamardtruediv(s, t)
Return the elementwise true division of two tensors.
\[
\left(\frac{s_i}{t_i}\right)_i
\]
See also
vechadamardtruediv
Source code in vector\multiaxis.py
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 | def tenhadamardtruediv(s, t):
r"""Return the elementwise true division of two tensors.
$$
\left(\frac{s_i}{t_i}\right)_i
$$
See also
--------
[`vechadamardtruediv`][vector.functional.vechadamardtruediv]
"""
s, t = np.asarray(s), np.asarray(t)
r = np.zeros(s.shape, dtype=np.result_type(s, t))
r = s[tuple(map(slice, r.shape)), ...]
r /= t[tuple(map(slice, r.shape)), ...]
return r
|
tenhadamardfloordiv(s, t)
Return the elementwise floor division of two tensors.
\[
\left(\left\lfloor\frac{s_i}{t_i}\right\rfloor\right)_i
\]
See also
vechadamardfloordiv
Source code in vector\multiaxis.py
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355 | def tenhadamardfloordiv(s, t):
r"""Return the elementwise floor division of two tensors.
$$
\left(\left\lfloor\frac{s_i}{t_i}\right\rfloor\right)_i
$$
See also
--------
[`vechadamardfloordiv`][vector.functional.vechadamardfloordiv]
"""
s, t = np.asarray(s), np.asarray(t)
r = np.zeros(s.shape, dtype=np.result_type(s, t))
r = s[tuple(map(slice, r.shape)), ...]
r //= t[tuple(map(slice, r.shape)), ...]
return r
|
tenhadamardmod(s, t)
Return the elementwise mod of two tensors.
\[
\left(s_i \mod t_i\right)_i
\]
See also
vechadamardmod
Source code in vector\multiaxis.py
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372 | def tenhadamardmod(s, t):
r"""Return the elementwise mod of two tensors.
$$
\left(s_i \mod t_i\right)_i
$$
See also
--------
[`vechadamardmod`][vector.functional.vechadamardmod]
"""
s, t = np.asarray(s), np.asarray(t)
r = np.zeros(s.shape, dtype=np.result_type(s, t))
r = s[tuple(map(slice, r.shape)), ...]
r %= t[tuple(map(slice, r.shape)), ...]
return r
|