Skip to content

FAQ: what is the difference between SX and MX?

jgillis edited this page Aug 3, 2021 · 1 revision

Definitions

An SX is really a container type; a sparse matrix of scalar entries. Those entries are scalar expressions, composed of scalar symbols and unary/binary operations. x=SX.sym('x',3,3) is represented by a container with 9 unrelated symbols in it. det(x) gives a scalar expression graph with a lot of + and * operations in it. An MX is not a container, but rather an undivisable entity that behaves as a sparse matrix. x=MX.sym('x',3,3) is represented by a single symbolic node. det(x) gives a matrix expression graph with a single determinant node using a single symbolic node as input.

Why choose one over the other?

  • SX is restrictive (not all operations supported) but optimized for evaluation speed.
  • MX supports a lot, and is optimized for memory usage and not evaluation speed.

Rule of thumb:

  • Use SX until you can't anymore (you run out of memory or encounter an operation that is not expandable)
  • More advanced: always use MX, but apply expand on critical sections of your code.

Compatibility

  • Any Function defined using SX symbols can be called with MX symbols/expressions. The result is an MX node that represents a call to that Function.
  • Many Functionss defined using MX symbols can be called using SX symbols/expressions. This process is known as 'expanding'. See also eval_sx error
  • Converting a Function of MX expressions is trivial to convert into a Function of SX expressions by using fun.expand().
  • Converting an MX expression to SX expression is not supported; you can go via a Function called with symbols, though.
Clone this wiki locally