Skip to content

FAQ: Is 3 (or more) dimensional data supported in CasADi data structures (e.g. DM)?

András Retzler edited this page Oct 30, 2022 · 1 revision

According to the documentation,

DM is mainly used for storing matrices in CasADi and as inputs and outputs of functions.

As such, casadi.DM always has two dimensions. If you try to cast a 3D tensor to DM, you will get the following error:

>>> casadi.DM([[[1,2],[3,4]],[[5,6],[7,8]]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pcfl/.local/lib/python3.8/site-packages/casadi/casadi.py", line 7437, in __init__
    this = _casadi.new_DM(*args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'new_DM'.
  Possible prototypes are:
    DM()
    DM(Sparsity)
    DM(float)
    DM([[float]])
    DM(DM)
    DM([SXElem])
    DM(SX)
    DM(int,int)
    DM(Sparsity,DM)
  You have: '([[[int]]])'

As a note, if you convert an 1D array to DM, it will still have two dimensions.

>>> A
DM([5, 7, 6, 8])
>>> A.shape
(4, 1)
Clone this wiki locally