Multilinear Sparse
Sparse tensor arithmetic.
Prefixed by tens... (tensor - sparse).
Handle sparse multiaxis tensors, that for example represent multivariate polynomials.
Sparse tensors are accepted and returned as dicts whos keys are trimmed (no trailing zeros), non-negative int tuples.
creation
tenszero: dict[Never, Never] = {}
Zero tensor.
An empty dictionary: {}.
tensbasis(i: tuple[int, ...], c: Any = 1) -> dict[tuple[int, ...], Any]
Return a basis tensor.
Returns a dictionary with a single element i:c.
Source code in vector\multilinear_sparse\creation.py
22 23 24 25 26 27 28 29 30 31 | |
tensrand(*d: int) -> dict[tuple[int, ...], float]
Return a random tensor 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\multilinear_sparse\creation.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
tensrandn(*d: int, mu: float = 0, sigma: float = 1) -> dict[tuple[int, ...], float]
Return a random tensor 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\multilinear_sparse\creation.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
conversion
tenstod(t: Mapping[tuple[int, ...], Any], zero: Any = 0) -> np.ndarray
Return a sparse tensor (dict) as a dense tensor (numpy.ndarray).
Source code in vector\multilinear_sparse\conversion.py
13 14 15 16 17 18 | |
tendtos(t: np.ndarray) -> dict[tuple[int, ...], Any]
Return a dense tensor (numpy.ndarray) as a sparse tensor (dict).
The resulting dict is not trimmed.
Source code in vector\multilinear_sparse\conversion.py
20 21 22 23 24 25 | |
utility
tensrank(t: Mapping[tuple[int, ...], Any]) -> int
Return the rank.
Source code in vector\multilinear_sparse\utility.py
15 16 17 18 19 20 21 22 | |
tensdim(t: Mapping[tuple[int, ...], Any]) -> tuple[int, ...]
Return the dimensionalities.
Source code in vector\multilinear_sparse\utility.py
24 25 26 27 28 29 30 31 | |
tenseq(s: Mapping[tuple[int, ...], Any], t: Mapping[tuple[int, ...], Any]) -> bool
Return whether two tensors are equal.
Source code in vector\multilinear_sparse\utility.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
tenstrim(t: Mapping[tuple[int, ...], Any], tol: Any | None = None) -> dict[tuple[int, ...], Any]
Remove all near zero (abs(t_i)<=tol) coefficients.
tol may also be None,
then all coefficients that evaluate to False are trimmed.
Notes
- Cutting of elements that are
abs(t_i)<=tolinstead ofabs(t_i)<tolto allow cutting of elements that are exactly zero bytrim(t, 0)instead oftrim(t, sys.float_info.min).
Source code in vector\multilinear_sparse\utility.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
tensitrim(t: MutableMapping[tuple[int, ...], Any], tol: Any | None = None) -> MutableMapping[tuple[int, ...], Any]
Remove all near zero (abs(t_i)<=tol) coefficients.
tol may also be None,
then all coefficients that evaluate to False are trimmed.
Notes
- Cutting of elements that are
abs(t_i)<=tolinstead ofabs(t_i)<tolto allow cutting of elements that are exactly zero bytrim(t, 0)instead oftrim(t, sys.float_info.min).
Source code in vector\multilinear_sparse\utility.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
tensrshift(t: Mapping[tuple[int, ...], Any], n: tuple[int, ...]) -> dict[tuple[int, ...], Any]
Shift coefficients up.
Source code in vector\multilinear_sparse\utility.py
90 91 92 93 94 | |
tenslshift(t: Mapping[tuple[int, ...], Any], n: tuple[int, ...]) -> dict[tuple[int, ...], Any]
Shift coefficients down.
Source code in vector\multilinear_sparse\utility.py
96 97 98 99 100 101 102 103 | |
hilbertspace
tensconj(t: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the complex conjugate.
Tries to call a method conjugate on each element.
If not found, simply keeps the element as is.
Source code in vector\multilinear_sparse\hilbertspace.py
11 12 13 14 15 16 17 18 19 20 21 | |
tensiconj(t: MutableMapping[tuple[int, ...], Any]) -> MutableMapping[tuple[int, ...], Any]
Complex conjugate.
Tries to call a method conjugate on each element.
If not found, simply keeps the element as is.
Source code in vector\multilinear_sparse\hilbertspace.py
23 24 25 26 27 28 29 30 31 32 33 34 35 | |
vectorspace
tenspos(t: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the identity.
Source code in vector\multilinear_sparse\_pyvectorspace.py
20 21 22 23 24 25 26 27 | |
tensipos(t: MutableMapping[tuple[int, ...], Any]) -> MutableMapping[tuple[int, ...], Any]
Apply unary plus.
Source code in vector\multilinear_sparse\_pyvectorspace.py
29 30 31 32 33 34 35 36 37 38 | |
tensneg(t: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the negation.
Source code in vector\multilinear_sparse\_pyvectorspace.py
40 41 42 43 44 45 46 47 | |
tensineg(t: dict[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Negate.
Source code in vector\multilinear_sparse\_pyvectorspace.py
49 50 51 52 53 54 55 56 57 58 | |
tensadd(*ts: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the sum.
See also
- for sum on a single coefficient:
tensaddc
Source code in vector\multilinear_sparse\_pyvectorspace.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
tensiadd(s: MutableMapping[tuple[int, ...], Any], *ts: Mapping[tuple[int, ...], Any]) -> MutableMapping[tuple[int, ...], Any]
Add.
See also
- for sum on a single coefficient:
tensiaddc
Source code in vector\multilinear_sparse\_pyvectorspace.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
tensaddc(t: Mapping[tuple[int, ...], Any], c: Any, i: tuple[int, ...] = ()) -> dict[tuple[int, ...], Any]
Return the sum with a basis tensor.
See also
- for sum on more coefficients:
tensadd
Source code in vector\multilinear_sparse\_pyvectorspace.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
tensiaddc(t: MutableMapping[tuple[int, ...], Any], c: Any, i: tuple[int, ...] = ()) -> MutableMapping[tuple[int, ...], Any]
Add a basis tensor.
See also
- for sum on more coefficients:
tensiadd
Source code in vector\multilinear_sparse\_pyvectorspace.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
tenssub(s: Mapping[tuple[int, ...], Any], t: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the difference.
See also
- for difference on a single coefficient:
tenssubc
Source code in vector\multilinear_sparse\_pyvectorspace.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
tensisub(s: MutableMapping[tuple[int, ...], Any], t: Mapping[tuple[int, ...], Any]) -> MutableMapping[tuple[int, ...], Any]
Subtract.
See also
- for difference on a single coefficient:
tensisubc
Source code in vector\multilinear_sparse\_pyvectorspace.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
tenssubc(t: Mapping[tuple[int, ...], Any], c: Any, i: tuple[int, ...] = ()) -> dict[tuple[int, ...], Any]
Return the difference with a basis tensor.
See also
- for difference on more coefficients:
tenssub
Source code in vector\multilinear_sparse\_pyvectorspace.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
tensisubc(t: MutableMapping[tuple[int, ...], Any], c: Any, i: tuple[int, ...] = ()) -> MutableMapping[tuple[int, ...], Any]
Subtract a basis tensor.
See also
- for difference on more coefficients:
tensisub
Source code in vector\multilinear_sparse\_pyvectorspace.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
tensmul(t: Mapping[tuple[int, ...], Any], a: Any) -> dict[tuple[int, ...], Any]
Return the product.
Source code in vector\multilinear_sparse\_pyvectorspace.py
206 207 208 209 210 211 212 213 | |
tensrmul(a: Any, t: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the product.
Source code in vector\multilinear_sparse\_pyvectorspace.py
215 216 217 218 219 220 221 222 | |
tensimul(t: MutableMapping[tuple[int, ...], Any], a: Any) -> MutableMapping[tuple[int, ...], Any]
Multiply.
Source code in vector\multilinear_sparse\_pyvectorspace.py
224 225 226 227 228 229 230 231 232 233 | |
tenstruediv(t: Mapping[tuple[int, ...], Any], a: Any) -> dict[tuple[int, ...], Any]
Return the true quotient.
Notes
Why called truediv instead of div?
divwould be more appropriate for an absolutely 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\multilinear_sparse\_pyvectorspace.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
tensitruediv(t: MutableMapping[tuple[int, ...], Any], a: Any) -> MutableMapping[tuple[int, ...], Any]
True divide.
Source code in vector\multilinear_sparse\_pyvectorspace.py
255 256 257 258 259 260 261 262 263 264 | |
tensfloordiv(t: Mapping[tuple[int, ...], Any], a: Any) -> dict[tuple[int, ...], Any]
Return the floor quotient.
Source code in vector\multilinear_sparse\_pyvectorspace.py
266 267 268 269 270 271 272 273 | |
tensifloordiv(t: MutableMapping[tuple[int, ...], Any], a: Any) -> MutableMapping[tuple[int, ...], Any]
Floor divide.
Source code in vector\multilinear_sparse\_pyvectorspace.py
275 276 277 278 279 280 281 282 283 284 | |
tensmod(t: Mapping[tuple[int, ...], Any], a: Any) -> dict[tuple[int, ...], Any]
Return the remainder.
Source code in vector\multilinear_sparse\_pyvectorspace.py
286 287 288 289 290 291 292 293 | |
tensimod(t: MutableMapping[tuple[int, ...], Any], a: Any) -> MutableMapping[tuple[int, ...], Any]
Mod.
Source code in vector\multilinear_sparse\_pyvectorspace.py
295 296 297 298 299 300 301 302 303 304 | |
tensdivmod(t: Mapping[tuple[int, ...], Any], a: Any) -> tuple[dict[tuple[int, ...], Any], dict[tuple[int, ...], Any]]
Return the floor quotient and remainder.
Source code in vector\multilinear_sparse\_pyvectorspace.py
306 307 308 309 310 311 312 313 314 315 316 317 | |
elementwise
tenshadamard(*ts: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the elementwise product.
Source code in vector\multilinear_sparse\elementwise.py
13 14 15 16 17 18 19 20 21 22 23 24 25 | |
tenshadamardtruediv(s: Mapping[tuple[int, ...], Any], t: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the elementwise true quotient.
Source code in vector\multilinear_sparse\elementwise.py
27 28 29 30 31 32 33 34 | |
tenshadamardfloordiv(s: Mapping[tuple[int, ...], Any], t: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the elementwise floor quotient.
Source code in vector\multilinear_sparse\elementwise.py
36 37 38 39 40 41 42 43 | |
tenshadamardmod(s: Mapping[tuple[int, ...], Any], t: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the elementwise remainder.
Source code in vector\multilinear_sparse\elementwise.py
45 46 47 48 49 50 51 52 | |
tenshadamarddivmod(s: Mapping[tuple[int, ...], Any], t: Mapping[tuple[int, ...], Any]) -> dict[tuple[int, ...], Any]
Return the elementwise floor quotient and remainder.
Source code in vector\multilinear_sparse\elementwise.py
54 55 56 57 58 59 60 61 62 63 64 65 | |
tenshadamardmin(*ts: Mapping[tuple[int, ...], Any], key: Callable[[Any], Any] | None = None) -> dict[tuple[int, ...], Any]
Return the elementwise minimum.
Source code in vector\multilinear_sparse\elementwise.py
67 68 69 70 71 72 73 74 75 76 77 78 79 | |
tenshadamardmax(*ts: Mapping[tuple[int, ...], Any], key: Callable[[Any], Any] | None = None) -> dict[tuple[int, ...], Any]
Return the elementwise maximum.
Source code in vector\multilinear_sparse\elementwise.py
81 82 83 84 85 86 87 88 89 90 91 92 93 | |