Fundamental question about MX objects

116 views
Skip to first unread message

Behnon Ormoro Whodid

unread,
Aug 18, 2019, 6:42:06 AM8/18/19
to CasADi
Hi,

I do not understand this point from the documentation, and do not know if Casadi fulfills my use case. My area is control engineering, and I often meet large and complex matrices with many symbolic variables that I want to keep symbolic until the end of a loop - where I'll insert newly measured values for my symbolical variables and arrive at a purely numerical matrix. Is this even possible? 


However, I'm having issues with extracting float / double elements from a MX object. Is there really no way to jump between symbolic and numerical matrices? What I would like to do is: 
Create MX objects                  ->     Perform symbolical operations on the MX objects         -> Evaluate the final MX object by inserting the numerical values of the symbolics -> Extract individual scalars from the matrix 
      [a, 0,   0]            [x_1]                           [a*x_1]                                                                                                                                    [1]
A =[0, b,   0]      x =  [x_2]       =>     A*x =  [b*x_2]                                                                =>         a = 1 & b = 2      :    x_dot = A*x =  [2]                                                                       =>     x1_dot = x_dot[0] = 1 (type: int/float)
      [0, 0, a^2]           [x_3]                           [a^2*x_3]                                                                  x_1 = x_2 = x_3 = 1  :                           [1] 

Bruno M L

unread,
Aug 19, 2019, 10:16:27 AM8/19/19
to CasADi
Hi!

  1. First create the input variables with MX (x and y). 
  2. Then, build the symbolic expression (sym_ex).
  3. Create a Function object e.g. sym_fun =  Function('name of the function', [x, y], [sym_fun])
  4. Call the function with numeric values: result = sym_fun(num_x, num_y)
  5. The result will be an DM object. Transform it to numpy arrays with result.full().
Regards,
Bruno

Sahib Singh

unread,
Feb 3, 2020, 8:30:13 PM2/3/20
to CasADi
Hi, 

Can you please elaborate/present an example!
Not able to implement what you wrote, however seems like this is what I want for my problem solving.

*fairly new to CASADI*

Thanks
Sahib

Bruno M L

unread,
Feb 4, 2020, 7:40:07 AM2/4/20
to CasADi
Hi!

I have created a simple example in python. Also, please take a look at the docs page.

from casadi import *

x = SX.sym('x',1)

sym_expression = jacobian(sin(x),x)

print(sym_expression)

fun = Function('fun_name', [x], [sym_expression], ['in_name'], ['out_name'])

result = fun([0])
print(type(result))     # the type is casadi.casadi.DM

result_array = result.full()
print(type(result_array))   # the type is numpy.ndarray
Reply all
Reply to author
Forward
0 new messages