Array expressions and symbolic gradients in SymPy
2022-08-31 , HS 118

SymPy is an open source computer algebra system (CAS) written in Python.

The recent addition of the array expression module provides an alternative to the matrix expression module, with generalized support to higher dimensions (matrices are constrained to 2 dimensions).

Given the importance of multidimensional arrays in machine learning and mathematical optimization problems, this talk will illustrate examples of tensorial expressions in mathematics and how they can be manipulated using either module or in the index-explicit way.

Conversion tools have been provided to SymPy to allow users to switch an expression between the array form and either the matrix or index-explicit form. In particular, the conversion from array to matrix form attempts to represent contractions, diagonalizations and axis-permutations with operations commonly used in matrix algebra, such as matrix multiplication, transposition, trace, Hadamard and Kronecker products.

A gradient algorithm for array expressions has been implemented, returning a closed-form array expression equivalent to the derivative of arrays by arrays. The derivative algorithm for matrix expressions now uses this algorithm, attempting to convert the array back to matrix form if trivial dimensions can be dropped.


SymPy supports various ways to represent multidimensional mathematical expressions. For example, the matrix product M N where both M and N are matrix symbols, may be represented as:

  • M*N ==> matrix expression
  • ArrayContraction(ArrayTensorProduct(M, N), (1, 2)) ==> array expression equivalent to matrix multiplication
  • Sum(M[i, j]*N[j, k], (j, ...)) ==> index-explicit expression with summation equivalent to matrix multiplication

Conversion from the array expression form is implemented in SymPy. Conversions generally work, except for some higher-dimensional array expression forms that cannot be converted to matrix expressions.

The derivative of an array expressions by another array symbol returns an array expression. In the index-explicit form it is performed by using Kronecker delta, while the matrix form currently relies on the conversions to the array form.


Public link to supporting material

https://docs.sympy.org/latest/modules/tensor/array_expressions.html#module-sympy.tensor.array.expressions

Expected audience expertise: Domain

expert

Expected audience expertise: Python

some

Abstract as a tweet

Expressions for multidimensional arrays and tensors in SymPy: manipulations, conversions and derivatives

Project Homepage / Git

https://github.com/sympy/sympy

Domains

Open Source Library, Vector and array manipulation

MSc. in physics from the University of Milano, Italy (2012).
Software engineer in the industry (2012-2015).
PhD researcher at the Max Planck Institute of Colloids and Interfaces, Potsdam, Germany (2015-2018).
Data scientist (2018-now).