Support for basic python functionalities

783 views
Skip to first unread message

Mario Zanon

unread,
May 17, 2017, 7:59:41 AM5/17/17
to CasADi
Hi guys!

I have noticed that in the latest casadi versions, the use of casadi types in python has become really difficult.
Basic functions like abs, min, max, sum and plotting do not accept casadi DM anymore. 
This is often adding one layer of complication which was not there in previous casadi versions.
Is there any plan to bring these functionalities back?

Joel Andersson

unread,
May 17, 2017, 10:44:45 AM5/17/17
to CasADi
abs, min, max, sum are built-in operations in Python that don't lend themselves to overloading very easily. Python's built-in implementation of "sum" or "abs" are not consistent with the CasADi datatypes and Python doesn't provide a good way of overloading them except just shadowing them, which is what we did before. That can cause problems in user code however, which is why we decided to rename them instead and leave the built-in Python operations alone. So the syntax is "fabs", "fmin", "fmax", and "sum1"/"sum2" now.

Joel

Mario Zanon

unread,
May 17, 2017, 10:51:45 AM5/17/17
to CasADi
Sure, I understand that this can be difficult to get to work properly and it's often not even a casadi-related difficulty.

I still find it a bit weird that e.g. the following code throws an error at the last line. Basically numpy can digest casadi types but python cannot.
from casadi import *
import numpy as np

a
= DM([1,2,3])
b
= np.array(a)
print a,b, list(b)
print list(a)

I wonder if / how do other softwares get around these issues. It somehow seems weird that there is no fix...

Joel Andersson

unread,
May 17, 2017, 11:29:12 AM5/17/17
to CasADi
It's not clear what "list(DM)" actually means. It used to mean: get the nonzero elements of the DM matrix. But it could also mean, get a list with [1,2,3]. But DM is a matrix type, so maybe you want a list of lists: [[1],[2],[3]]? In CasADi 3, we decided to avoid the confusion by requiring the user to specify what kind of conversion he/she wants. So:

print a.nonzeros() # if you want the nonzeros, equivalent to list(a) before
print a.full() # if you want a dense numpy matrix
print a.sparse() # if you want a sparse scipy matrix

Joel

Reply all
Reply to author
Forward
0 new messages