Solve the tensor equation ``a x = b`` for x. It is assumed that all indices of `x` are summed over in the product, together with the rightmost indices of `a`, as is done in, for example, ``tensordot(a, x, axes=x.ndim)``. Parameters ---------- a : array_like Coe
(a, b, axes=None)
| 292 | |
| 293 | @array_function_dispatch(_tensorsolve_dispatcher) |
| 294 | def tensorsolve(a, b, axes=None): |
| 295 | """ |
| 296 | Solve the tensor equation ``a x = b`` for x. |
| 297 | |
| 298 | It is assumed that all indices of `x` are summed over in the product, |
| 299 | together with the rightmost indices of `a`, as is done in, for example, |
| 300 | ``tensordot(a, x, axes=x.ndim)``. |
| 301 | |
| 302 | Parameters |
| 303 | ---------- |
| 304 | a : array_like |
| 305 | Coefficient tensor, of shape ``b.shape + Q``. `Q`, a tuple, equals |
| 306 | the shape of that sub-tensor of `a` consisting of the appropriate |
| 307 | number of its rightmost indices, and must be such that |
| 308 | ``prod(Q) == prod(b.shape)`` (in which sense `a` is said to be |
| 309 | 'square'). |
| 310 | b : array_like |
| 311 | Right-hand tensor, which can be of any shape. |
| 312 | axes : tuple of ints, optional |
| 313 | Axes in `a` to reorder to the right, before inversion. |
| 314 | If None (default), no reordering is done. |
| 315 | |
| 316 | Returns |
| 317 | ------- |
| 318 | x : ndarray, shape Q |
| 319 | |
| 320 | Raises |
| 321 | ------ |
| 322 | LinAlgError |
| 323 | If `a` is singular or not 'square' (in the above sense). |
| 324 | |
| 325 | See Also |
| 326 | -------- |
| 327 | numpy.tensordot, tensorinv, numpy.einsum |
| 328 | |
| 329 | Examples |
| 330 | -------- |
| 331 | >>> import numpy as np |
| 332 | >>> a = np.eye(2*3*4).reshape((2*3, 4, 2, 3, 4)) |
| 333 | >>> rng = np.random.default_rng() |
| 334 | >>> b = rng.normal(size=(2*3, 4)) |
| 335 | >>> x = np.linalg.tensorsolve(a, b) |
| 336 | >>> x.shape |
| 337 | (2, 3, 4) |
| 338 | >>> np.allclose(np.tensordot(a, x, axes=3), b) |
| 339 | True |
| 340 | |
| 341 | """ |
| 342 | a, wrap = _makearray(a) |
| 343 | b = asarray(b) |
| 344 | an = a.ndim |
| 345 | |
| 346 | if axes is not None: |
| 347 | allaxes = list(range(an)) |
| 348 | for k in axes: |
| 349 | allaxes.remove(k) |
| 350 | allaxes.insert(an, k) |
| 351 | a = a.transpose(allaxes) |
nothing calls this directly
no test coverage detected
searching dependent graphs…