arrayop.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. import itertools
  2. from collections.abc import Iterable
  3. from sympy.core._print_helpers import Printable
  4. from sympy.core.containers import Tuple
  5. from sympy.core.function import diff
  6. from sympy.core.singleton import S
  7. from sympy.core.sympify import _sympify
  8. from sympy.tensor.array.ndim_array import NDimArray
  9. from sympy.tensor.array.dense_ndim_array import DenseNDimArray, ImmutableDenseNDimArray
  10. from sympy.tensor.array.sparse_ndim_array import SparseNDimArray
  11. def _arrayfy(a):
  12. from sympy.matrices import MatrixBase
  13. if isinstance(a, NDimArray):
  14. return a
  15. if isinstance(a, (MatrixBase, list, tuple, Tuple)):
  16. return ImmutableDenseNDimArray(a)
  17. return a
  18. def tensorproduct(*args):
  19. """
  20. Tensor product among scalars or array-like objects.
  21. The equivalent operator for array expressions is ``ArrayTensorProduct``,
  22. which can be used to keep the expression unevaluated.
  23. Examples
  24. ========
  25. >>> from sympy.tensor.array import tensorproduct, Array
  26. >>> from sympy.abc import x, y, z, t
  27. >>> A = Array([[1, 2], [3, 4]])
  28. >>> B = Array([x, y])
  29. >>> tensorproduct(A, B)
  30. [[[x, y], [2*x, 2*y]], [[3*x, 3*y], [4*x, 4*y]]]
  31. >>> tensorproduct(A, x)
  32. [[x, 2*x], [3*x, 4*x]]
  33. >>> tensorproduct(A, B, B)
  34. [[[[x**2, x*y], [x*y, y**2]], [[2*x**2, 2*x*y], [2*x*y, 2*y**2]]], [[[3*x**2, 3*x*y], [3*x*y, 3*y**2]], [[4*x**2, 4*x*y], [4*x*y, 4*y**2]]]]
  35. Applying this function on two matrices will result in a rank 4 array.
  36. >>> from sympy import Matrix, eye
  37. >>> m = Matrix([[x, y], [z, t]])
  38. >>> p = tensorproduct(eye(3), m)
  39. >>> p
  40. [[[[x, y], [z, t]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[0, 0], [0, 0]], [[x, y], [z, t]], [[0, 0], [0, 0]]], [[[0, 0], [0, 0]], [[0, 0], [0, 0]], [[x, y], [z, t]]]]
  41. See Also
  42. ========
  43. sympy.tensor.array.expressions.array_expressions.ArrayTensorProduct
  44. """
  45. from sympy.tensor.array import SparseNDimArray, ImmutableSparseNDimArray
  46. if len(args) == 0:
  47. return S.One
  48. if len(args) == 1:
  49. return _arrayfy(args[0])
  50. from sympy.tensor.array.expressions.array_expressions import _CodegenArrayAbstract
  51. from sympy.tensor.array.expressions.array_expressions import ArrayTensorProduct
  52. from sympy.tensor.array.expressions.array_expressions import _ArrayExpr
  53. from sympy.matrices.expressions.matexpr import MatrixSymbol
  54. if any(isinstance(arg, (_ArrayExpr, _CodegenArrayAbstract, MatrixSymbol)) for arg in args):
  55. return ArrayTensorProduct(*args)
  56. if len(args) > 2:
  57. return tensorproduct(tensorproduct(args[0], args[1]), *args[2:])
  58. # length of args is 2:
  59. a, b = map(_arrayfy, args)
  60. if not isinstance(a, NDimArray) or not isinstance(b, NDimArray):
  61. return a*b
  62. if isinstance(a, SparseNDimArray) and isinstance(b, SparseNDimArray):
  63. lp = len(b)
  64. new_array = {k1*lp + k2: v1*v2 for k1, v1 in a._sparse_array.items() for k2, v2 in b._sparse_array.items()}
  65. return ImmutableSparseNDimArray(new_array, a.shape + b.shape)
  66. product_list = [i*j for i in Flatten(a) for j in Flatten(b)]
  67. return ImmutableDenseNDimArray(product_list, a.shape + b.shape)
  68. def _util_contraction_diagonal(array, *contraction_or_diagonal_axes):
  69. array = _arrayfy(array)
  70. # Verify contraction_axes:
  71. taken_dims = set()
  72. for axes_group in contraction_or_diagonal_axes:
  73. if not isinstance(axes_group, Iterable):
  74. raise ValueError("collections of contraction/diagonal axes expected")
  75. dim = array.shape[axes_group[0]]
  76. for d in axes_group:
  77. if d in taken_dims:
  78. raise ValueError("dimension specified more than once")
  79. if dim != array.shape[d]:
  80. raise ValueError("cannot contract or diagonalize between axes of different dimension")
  81. taken_dims.add(d)
  82. rank = array.rank()
  83. remaining_shape = [dim for i, dim in enumerate(array.shape) if i not in taken_dims]
  84. cum_shape = [0]*rank
  85. _cumul = 1
  86. for i in range(rank):
  87. cum_shape[rank - i - 1] = _cumul
  88. _cumul *= int(array.shape[rank - i - 1])
  89. # DEFINITION: by absolute position it is meant the position along the one
  90. # dimensional array containing all the tensor components.
  91. # Possible future work on this module: move computation of absolute
  92. # positions to a class method.
  93. # Determine absolute positions of the uncontracted indices:
  94. remaining_indices = [[cum_shape[i]*j for j in range(array.shape[i])]
  95. for i in range(rank) if i not in taken_dims]
  96. # Determine absolute positions of the contracted indices:
  97. summed_deltas = []
  98. for axes_group in contraction_or_diagonal_axes:
  99. lidx = []
  100. for js in range(array.shape[axes_group[0]]):
  101. lidx.append(sum([cum_shape[ig] * js for ig in axes_group]))
  102. summed_deltas.append(lidx)
  103. return array, remaining_indices, remaining_shape, summed_deltas
  104. def tensorcontraction(array, *contraction_axes):
  105. """
  106. Contraction of an array-like object on the specified axes.
  107. The equivalent operator for array expressions is ``ArrayContraction``,
  108. which can be used to keep the expression unevaluated.
  109. Examples
  110. ========
  111. >>> from sympy import Array, tensorcontraction
  112. >>> from sympy import Matrix, eye
  113. >>> tensorcontraction(eye(3), (0, 1))
  114. 3
  115. >>> A = Array(range(18), (3, 2, 3))
  116. >>> A
  117. [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]], [[12, 13, 14], [15, 16, 17]]]
  118. >>> tensorcontraction(A, (0, 2))
  119. [21, 30]
  120. Matrix multiplication may be emulated with a proper combination of
  121. ``tensorcontraction`` and ``tensorproduct``
  122. >>> from sympy import tensorproduct
  123. >>> from sympy.abc import a,b,c,d,e,f,g,h
  124. >>> m1 = Matrix([[a, b], [c, d]])
  125. >>> m2 = Matrix([[e, f], [g, h]])
  126. >>> p = tensorproduct(m1, m2)
  127. >>> p
  128. [[[[a*e, a*f], [a*g, a*h]], [[b*e, b*f], [b*g, b*h]]], [[[c*e, c*f], [c*g, c*h]], [[d*e, d*f], [d*g, d*h]]]]
  129. >>> tensorcontraction(p, (1, 2))
  130. [[a*e + b*g, a*f + b*h], [c*e + d*g, c*f + d*h]]
  131. >>> m1*m2
  132. Matrix([
  133. [a*e + b*g, a*f + b*h],
  134. [c*e + d*g, c*f + d*h]])
  135. See Also
  136. ========
  137. sympy.tensor.array.expressions.array_expressions.ArrayContraction
  138. """
  139. from sympy.tensor.array.expressions.array_expressions import _array_contraction
  140. from sympy.tensor.array.expressions.array_expressions import _CodegenArrayAbstract
  141. from sympy.tensor.array.expressions.array_expressions import _ArrayExpr
  142. from sympy.matrices.expressions.matexpr import MatrixSymbol
  143. if isinstance(array, (_ArrayExpr, _CodegenArrayAbstract, MatrixSymbol)):
  144. return _array_contraction(array, *contraction_axes)
  145. array, remaining_indices, remaining_shape, summed_deltas = _util_contraction_diagonal(array, *contraction_axes)
  146. # Compute the contracted array:
  147. #
  148. # 1. external for loops on all uncontracted indices.
  149. # Uncontracted indices are determined by the combinatorial product of
  150. # the absolute positions of the remaining indices.
  151. # 2. internal loop on all contracted indices.
  152. # It sums the values of the absolute contracted index and the absolute
  153. # uncontracted index for the external loop.
  154. contracted_array = []
  155. for icontrib in itertools.product(*remaining_indices):
  156. index_base_position = sum(icontrib)
  157. isum = S.Zero
  158. for sum_to_index in itertools.product(*summed_deltas):
  159. idx = array._get_tuple_index(index_base_position + sum(sum_to_index))
  160. isum += array[idx]
  161. contracted_array.append(isum)
  162. if len(remaining_indices) == 0:
  163. assert len(contracted_array) == 1
  164. return contracted_array[0]
  165. return type(array)(contracted_array, remaining_shape)
  166. def tensordiagonal(array, *diagonal_axes):
  167. """
  168. Diagonalization of an array-like object on the specified axes.
  169. This is equivalent to multiplying the expression by Kronecker deltas
  170. uniting the axes.
  171. The diagonal indices are put at the end of the axes.
  172. The equivalent operator for array expressions is ``ArrayDiagonal``, which
  173. can be used to keep the expression unevaluated.
  174. Examples
  175. ========
  176. ``tensordiagonal`` acting on a 2-dimensional array by axes 0 and 1 is
  177. equivalent to the diagonal of the matrix:
  178. >>> from sympy import Array, tensordiagonal
  179. >>> from sympy import Matrix, eye
  180. >>> tensordiagonal(eye(3), (0, 1))
  181. [1, 1, 1]
  182. >>> from sympy.abc import a,b,c,d
  183. >>> m1 = Matrix([[a, b], [c, d]])
  184. >>> tensordiagonal(m1, [0, 1])
  185. [a, d]
  186. In case of higher dimensional arrays, the diagonalized out dimensions
  187. are appended removed and appended as a single dimension at the end:
  188. >>> A = Array(range(18), (3, 2, 3))
  189. >>> A
  190. [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]], [[12, 13, 14], [15, 16, 17]]]
  191. >>> tensordiagonal(A, (0, 2))
  192. [[0, 7, 14], [3, 10, 17]]
  193. >>> from sympy import permutedims
  194. >>> tensordiagonal(A, (0, 2)) == permutedims(Array([A[0, :, 0], A[1, :, 1], A[2, :, 2]]), [1, 0])
  195. True
  196. See Also
  197. ========
  198. sympy.tensor.array.expressions.array_expressions.ArrayDiagonal
  199. """
  200. if any(len(i) <= 1 for i in diagonal_axes):
  201. raise ValueError("need at least two axes to diagonalize")
  202. from sympy.tensor.array.expressions.array_expressions import _ArrayExpr
  203. from sympy.tensor.array.expressions.array_expressions import _CodegenArrayAbstract
  204. from sympy.tensor.array.expressions.array_expressions import ArrayDiagonal, _array_diagonal
  205. from sympy.matrices.expressions.matexpr import MatrixSymbol
  206. if isinstance(array, (_ArrayExpr, _CodegenArrayAbstract, MatrixSymbol)):
  207. return _array_diagonal(array, *diagonal_axes)
  208. ArrayDiagonal._validate(array, *diagonal_axes)
  209. array, remaining_indices, remaining_shape, diagonal_deltas = _util_contraction_diagonal(array, *diagonal_axes)
  210. # Compute the diagonalized array:
  211. #
  212. # 1. external for loops on all undiagonalized indices.
  213. # Undiagonalized indices are determined by the combinatorial product of
  214. # the absolute positions of the remaining indices.
  215. # 2. internal loop on all diagonal indices.
  216. # It appends the values of the absolute diagonalized index and the absolute
  217. # undiagonalized index for the external loop.
  218. diagonalized_array = []
  219. diagonal_shape = [len(i) for i in diagonal_deltas]
  220. for icontrib in itertools.product(*remaining_indices):
  221. index_base_position = sum(icontrib)
  222. isum = []
  223. for sum_to_index in itertools.product(*diagonal_deltas):
  224. idx = array._get_tuple_index(index_base_position + sum(sum_to_index))
  225. isum.append(array[idx])
  226. isum = type(array)(isum).reshape(*diagonal_shape)
  227. diagonalized_array.append(isum)
  228. return type(array)(diagonalized_array, remaining_shape + diagonal_shape)
  229. def derive_by_array(expr, dx):
  230. r"""
  231. Derivative by arrays. Supports both arrays and scalars.
  232. The equivalent operator for array expressions is ``array_derive``.
  233. Explanation
  234. ===========
  235. Given the array `A_{i_1, \ldots, i_N}` and the array `X_{j_1, \ldots, j_M}`
  236. this function will return a new array `B` defined by
  237. `B_{j_1,\ldots,j_M,i_1,\ldots,i_N} := \frac{\partial A_{i_1,\ldots,i_N}}{\partial X_{j_1,\ldots,j_M}}`
  238. Examples
  239. ========
  240. >>> from sympy import derive_by_array
  241. >>> from sympy.abc import x, y, z, t
  242. >>> from sympy import cos
  243. >>> derive_by_array(cos(x*t), x)
  244. -t*sin(t*x)
  245. >>> derive_by_array(cos(x*t), [x, y, z, t])
  246. [-t*sin(t*x), 0, 0, -x*sin(t*x)]
  247. >>> derive_by_array([x, y**2*z], [[x, y], [z, t]])
  248. [[[1, 0], [0, 2*y*z]], [[0, y**2], [0, 0]]]
  249. """
  250. from sympy.matrices import MatrixBase
  251. from sympy.tensor.array import SparseNDimArray
  252. array_types = (Iterable, MatrixBase, NDimArray)
  253. if isinstance(dx, array_types):
  254. dx = ImmutableDenseNDimArray(dx)
  255. for i in dx:
  256. if not i._diff_wrt:
  257. raise ValueError("cannot derive by this array")
  258. if isinstance(expr, array_types):
  259. if isinstance(expr, NDimArray):
  260. expr = expr.as_immutable()
  261. else:
  262. expr = ImmutableDenseNDimArray(expr)
  263. if isinstance(dx, array_types):
  264. if isinstance(expr, SparseNDimArray):
  265. lp = len(expr)
  266. new_array = {k + i*lp: v
  267. for i, x in enumerate(Flatten(dx))
  268. for k, v in expr.diff(x)._sparse_array.items()}
  269. else:
  270. new_array = [[y.diff(x) for y in Flatten(expr)] for x in Flatten(dx)]
  271. return type(expr)(new_array, dx.shape + expr.shape)
  272. else:
  273. return expr.diff(dx)
  274. else:
  275. expr = _sympify(expr)
  276. if isinstance(dx, array_types):
  277. return ImmutableDenseNDimArray([expr.diff(i) for i in Flatten(dx)], dx.shape)
  278. else:
  279. dx = _sympify(dx)
  280. return diff(expr, dx)
  281. def permutedims(expr, perm=None, index_order_old=None, index_order_new=None):
  282. """
  283. Permutes the indices of an array.
  284. Parameter specifies the permutation of the indices.
  285. The equivalent operator for array expressions is ``PermuteDims``, which can
  286. be used to keep the expression unevaluated.
  287. Examples
  288. ========
  289. >>> from sympy.abc import x, y, z, t
  290. >>> from sympy import sin
  291. >>> from sympy import Array, permutedims
  292. >>> a = Array([[x, y, z], [t, sin(x), 0]])
  293. >>> a
  294. [[x, y, z], [t, sin(x), 0]]
  295. >>> permutedims(a, (1, 0))
  296. [[x, t], [y, sin(x)], [z, 0]]
  297. If the array is of second order, ``transpose`` can be used:
  298. >>> from sympy import transpose
  299. >>> transpose(a)
  300. [[x, t], [y, sin(x)], [z, 0]]
  301. Examples on higher dimensions:
  302. >>> b = Array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
  303. >>> permutedims(b, (2, 1, 0))
  304. [[[1, 5], [3, 7]], [[2, 6], [4, 8]]]
  305. >>> permutedims(b, (1, 2, 0))
  306. [[[1, 5], [2, 6]], [[3, 7], [4, 8]]]
  307. An alternative way to specify the same permutations as in the previous
  308. lines involves passing the *old* and *new* indices, either as a list or as
  309. a string:
  310. >>> permutedims(b, index_order_old="cba", index_order_new="abc")
  311. [[[1, 5], [3, 7]], [[2, 6], [4, 8]]]
  312. >>> permutedims(b, index_order_old="cab", index_order_new="abc")
  313. [[[1, 5], [2, 6]], [[3, 7], [4, 8]]]
  314. ``Permutation`` objects are also allowed:
  315. >>> from sympy.combinatorics import Permutation
  316. >>> permutedims(b, Permutation([1, 2, 0]))
  317. [[[1, 5], [2, 6]], [[3, 7], [4, 8]]]
  318. See Also
  319. ========
  320. sympy.tensor.array.expressions.array_expressions.PermuteDims
  321. """
  322. from sympy.tensor.array import SparseNDimArray
  323. from sympy.tensor.array.expressions.array_expressions import _ArrayExpr
  324. from sympy.tensor.array.expressions.array_expressions import _CodegenArrayAbstract
  325. from sympy.tensor.array.expressions.array_expressions import _permute_dims
  326. from sympy.matrices.expressions.matexpr import MatrixSymbol
  327. from sympy.tensor.array.expressions import PermuteDims
  328. from sympy.tensor.array.expressions.array_expressions import get_rank
  329. perm = PermuteDims._get_permutation_from_arguments(perm, index_order_old, index_order_new, get_rank(expr))
  330. if isinstance(expr, (_ArrayExpr, _CodegenArrayAbstract, MatrixSymbol)):
  331. return _permute_dims(expr, perm)
  332. if not isinstance(expr, NDimArray):
  333. expr = ImmutableDenseNDimArray(expr)
  334. from sympy.combinatorics import Permutation
  335. if not isinstance(perm, Permutation):
  336. perm = Permutation(list(perm))
  337. if perm.size != expr.rank():
  338. raise ValueError("wrong permutation size")
  339. # Get the inverse permutation:
  340. iperm = ~perm
  341. new_shape = perm(expr.shape)
  342. if isinstance(expr, SparseNDimArray):
  343. return type(expr)({tuple(perm(expr._get_tuple_index(k))): v
  344. for k, v in expr._sparse_array.items()}, new_shape)
  345. indices_span = perm([range(i) for i in expr.shape])
  346. new_array = [None]*len(expr)
  347. for i, idx in enumerate(itertools.product(*indices_span)):
  348. t = iperm(idx)
  349. new_array[i] = expr[t]
  350. return type(expr)(new_array, new_shape)
  351. class Flatten(Printable):
  352. """
  353. Flatten an iterable object to a list in a lazy-evaluation way.
  354. Notes
  355. =====
  356. This class is an iterator with which the memory cost can be economised.
  357. Optimisation has been considered to ameliorate the performance for some
  358. specific data types like DenseNDimArray and SparseNDimArray.
  359. Examples
  360. ========
  361. >>> from sympy.tensor.array.arrayop import Flatten
  362. >>> from sympy.tensor.array import Array
  363. >>> A = Array(range(6)).reshape(2, 3)
  364. >>> Flatten(A)
  365. Flatten([[0, 1, 2], [3, 4, 5]])
  366. >>> [i for i in Flatten(A)]
  367. [0, 1, 2, 3, 4, 5]
  368. """
  369. def __init__(self, iterable):
  370. from sympy.matrices.matrices import MatrixBase
  371. from sympy.tensor.array import NDimArray
  372. if not isinstance(iterable, (Iterable, MatrixBase)):
  373. raise NotImplementedError("Data type not yet supported")
  374. if isinstance(iterable, list):
  375. iterable = NDimArray(iterable)
  376. self._iter = iterable
  377. self._idx = 0
  378. def __iter__(self):
  379. return self
  380. def __next__(self):
  381. from sympy.matrices.matrices import MatrixBase
  382. if len(self._iter) > self._idx:
  383. if isinstance(self._iter, DenseNDimArray):
  384. result = self._iter._array[self._idx]
  385. elif isinstance(self._iter, SparseNDimArray):
  386. if self._idx in self._iter._sparse_array:
  387. result = self._iter._sparse_array[self._idx]
  388. else:
  389. result = 0
  390. elif isinstance(self._iter, MatrixBase):
  391. result = self._iter[self._idx]
  392. elif hasattr(self._iter, '__next__'):
  393. result = next(self._iter)
  394. else:
  395. result = self._iter[self._idx]
  396. else:
  397. raise StopIteration
  398. self._idx += 1
  399. return result
  400. def next(self):
  401. return self.__next__()
  402. def _sympystr(self, printer):
  403. return type(self).__name__ + '(' + printer._print(self._iter) + ')'