Skip to content

FAQ: How to print values of a subexpression during optimization? (printme)

Joris Gillis edited this page Sep 8, 2023 · 4 revisions

When is it useful:

It allows you to print out a number during the function evaluation of the graph.

See an example here:

The syntax expr = expr.printme(identifier) will cause any Function or solver that contains expr to print out numerical values during each evaluation. 'identifier' is an integer of choice that you may use to disambiguate between several printme statements. This operation works both in SX and MX, and prints out each nonzero of a sparse matrix expression individually.

There is a related syntax expr=expr.monitor(label) that can be used in MX to print out numerical values as a proper sparse matrix.

http://casadi.sourceforge.net/api/internal/d3/d2b/misc_2printme_8py-example.html

Example printout that you might get:

printout(1) |> 1 : 7.6901368569791070e-04 |> 1 : 7.2262039560504322e-06 |> 1 : -1.8436781636897780e-06 |> 1 : -1.6018687907609327e-07 |> 1 : -3.3309475419490247e-06 |> 1 : 0.0000000000000000e+00 |> 1 : 7.2262039560504390e-06 |> 1 : 6.0602823037207492e-04 |> 1 : 
-1.7075753255744311e-04 |> 1 : -1.3026914974007571e-05 |> 1 : -2.6936258908461164e-04 |> 1 : 0.0000000000000000e+00 |> 1 : 
-1.8436781636897805e-06 |> 1 : -1.7075753255744314e-04 |> 1 : 9.8675625748731260e-04 |> 1 : -2.4419292872406771e-05 |> 1 : 
-5.0493775673056136e-04 |> 1 : 0.0000000000000000e+00 |> 1 : -1.6018687907609319e-07 |> 1 : -1.3026914974007571e-05 |> 1 : 
-2.4419292872406761e-05 |> 1 : 1.1741992049602121e-02 |> 1 : 1.8947002317916553e-05 |> 1 : 0.0000000000000000e+00 |> 1 : 
-3.3309475419490294e-06 |> 1 : -2.6936258908461170e-04 |> 1 : -5.0493775673056136e-04 |> 1 : 1.8947002317916556e-05 |> 1 : 
9.0124286103891935e-03 |> 1 : 0.0000000000000000e+00 |> 1 : 0.0000000000000000e+00 |> 1 : 0.0000000000000000e+00 |> 1 : 
0.0000000000000000e+00 |> 1 : 0.0000000000000000e+00 |> 1 : 0.0000000000000000e+00 |> 1 : 2.4271232262337402e-02

If the thing I'm printing out is a matrix, then in what order will the elements be?

Matrix elements are in this order:

image

Clone this wiki locally