CasADi tutorial

/home/travis/build/casadi/binaries/casadi/docs/tutorials/python/src/tools/temp.py
June 30, 2015
CasADi tutorial
1
52
f = S X F u n c t i o n ( ’ f ’, [ s t a t e s . c a t ] , [ x * y * z ] )
53
f = S X F u n c t i o n ( ’ f ’, [ s t a t e s ] , [ x * y * z ] )
In many cases, states will be auto-cast to SX:
0
1
2
3
4
5
6
#
#
#
#
#
#
#
Expanded structure syntax and ordering
The structure defnition above can also be written in expanded syntax:
This tutorial file explains the use of structures Structures are a python-only feature from the tools library:
14
15
from c a s a d i . t o o l s i m p o r t *
The struct tools offer a way to structure your symbols and data It allows you to make abstraction of ordering and
indices.
Put simply, the goal is to eleminate code with ’magic numbers’ numbers such as:
f = MXFunction(’f’, [V],[ V[214] ]) # time ... x_opt = solver.getOutput()[::5] # Obtain
all optimized x’s
and replace it with f = MXFunction(’f’, [V],[ V[“T”] ]) ... shooting(solver.getOutput())[“x”,:]
Introduction
63
64
65
66
67
More information can be attached to the entries shape argument : specify sparsity/shape
print
74
print
[ [ y_0 , y_2 ] ,
[ y_1 , y_3 ] ]
75
states [ "y" ]
x , y , z = s t a t e s [ ... ]
The cat attribute will return the concatenated version of the struct. This will always be a column vector
print
print
states.cat
[ x, y, z ]
repeat argument : specify nested lists
84
85
86
87
88
89
s t a t e s = struct_symSX([
e n t r y ("w", r e p e a t = 2),
e n t r y (" v ", r e p e a t = [ 2,3 ] ),
])
print
This structure is of size:
47
print
s t a t e s [ "w" ]
[ SX( w_0 ), SX( w_1 ) ]
s t a t e s . s i z e , " = ", s t a t e s . c a t . s h a p e
3 = (3, 1)
50
51
states.cat
[ x_0 , x_1 , x_2 , y_0 , y_1 , y_2 , y_3 , z_0 , z_1 , z_2 ]
To obtain aliases, use the Ellipsis index:
44
states [ "z" ]
Note that the cat version of this structure does only contain the nonzeros
78
y
41
print
[ [ z_0 , 00 ] ,
[ z_1 , z_2 ] ]
Superficially, states behaves like a dictionary
print
states [ "y" ]
states
s y m b o l i c SX w i t h f o l l o w i n g s t r u c t u r e :
S t r u c t u r e w i t h t o t a l s i z e 3.
Structure holding 3 entries.
O r d e r : [ ’ x ’, ’ y ’, ’ z ’ ]
y = 1- by -1 ( d e n s e )
x = 1- by -1 ( d e n s e )
z = 1- by -1 ( d e n s e )
38
states [ "x" ]
[ x_0 , x_1 , x_2 ]
s t a t e s = s t r u c t _ s y m S X ( [ " x "," y "," z " ] )
print
s t a t e s = struct_symSX([
e n t r y (" x ", s h a p e = 3),
e n t r y (" y ", s h a p e = (2,2)),
e n t r y (" z ", s h a p e = S p a r s i t y . l o w e r (2))
])
71
72
73
74
75
76
77
Create a structured SX.sym
33
34
35
s i m p l e s t a t e s = struct_symSX([
e n t r y (" x "),
e n t r y (" y "),
e n t r y (" z ")
])
87
print
states [ "v" ]
[ [ SX( v _ 0 _ 0 ), SX( v _ 0 _ 1 ), SX( v _ 0 _ 2 ) ] , [ SX( v _ 1 _ 0 ), SX( v _ 1 _ 1 ), SX( v _ 1 _ 2 ) ] ]
Notice that all v variables come before the v entries:
/home/travis/build/casadi/binaries/casadi/docs/tutorials/python/src/tools/temp.py
91
92
June 30, 2015
f o r i , s i n e n u m e r a t e ( s t a t e s . l a b e l s ()) :
print i , s
0 [ w,0,0 ]
1 [ w,1,0 ]
2 [ v ,0,0,0 ]
3 [ v ,0,1,0 ]
4 [ v ,0,2,0 ]
5 [ v ,1,0,0 ]
6 [ v ,1,1,0 ]
7 [ v ,1,2,0 ]
We can influency this order by introducing a grouping bracket:
101
102
103
104
105
106
107
108
If we use more exoctic indices, we call this a powerIndex
124
" a ",
( e n t r y ("w", r e p e a t = 2),
e n t r y (" v ", r e p e a t = [ 2,3 ] )
),
"b"
])
128
129
130
Nesting, Values and PowerIndex
print
s t a t e s = s t r u c t _ s y m S X ( [ " x "," y ", e n t r y (" q ", s h a p e = 4) ] )
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
132
133
i n i t [ "X",0,-1," y " ] = 12
135
print
The corresponding numerical value has changed now:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
The entry that changed is in fact this one:
138
print
139
print
print
s h o o t i n g [ "X",0,0," x " ]
X_0_0_x
i n i t . f [ "X",0,-1," y " ]
[ I M a t r i x (13) ]
i n i t . c a t [ 13 ]
12
One can lookup the meaning of the 13th entry in the cat version as such: Note that the canonicalIndex does not
contain negative numbers
143
print
s h o o t i n g . g e t C a n o n i c a l I n d e x (13)
( ’X ’, 0, 2, ’ y ’, 0)
145
146
print
s h o o t i n g . l a b e l s () [ 13 ]
[ X,0,2, y ,0 ]
Other datatypes
shooting.size
94
The canonicalIndex is the combination of strings and numbers that uniquely defines the entries of a structure:
i n i t .cat
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
shooting = struct_symSX([
e n t r y ("X", r e p e a t = [ 5,3 ] , s t r u c t = s t a t e s ),
e n t r y ("U", r e p e a t = 4, s h a p e = 1),
])
print
i n i t .cat
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Structures can be nested. For example consider a statespace of two cartesian coordinates and a quaternion
121
i n i t = s h o o t i n g (0)
f o r i , s i n e n u m e r a t e ( s t a t e s . l a b e l s ()) :
print i , s
[ a ,0 ]
[ w,0,0 ]
[ v ,0,0,0 ]
[ v ,0,1,0 ]
[ v ,0,2,0 ]
[ w,1,0 ]
[ v ,1,0,0 ]
[ v ,1,1,0 ]
[ v ,1,2,0 ]
[ b ,0 ]
s h o o t i n g [ "X", : ,0," x " ]
Having structured symbolics is one thing. The numeric structures can be derived: The following line allocates a
DMatrix of correct size, initialised with zeros
s t a t e s = struct_symSX([
0
1
2
3
4
5
6
7
8
9
111
112
113
114
115
116
117
118
print
[ SX( X_0_0_x ), SX( X_1_0_x ), SX( X_2_0_x ), SX( X_3_0_x ), SX( X_4_0_x ) ]
Notice how the w and v variables are now interleaved:
105
106
2
A symbolic structure is immutable
155
156
157
158
159
try :
s t a t e s [ " x " ] = s t a t e s [ " x " ] **2
e x c e p t E x c e p t i o n as e :
p r i n t " Oops : ", e
/home/travis/build/casadi/binaries/casadi/docs/tutorials/python/src/tools/temp.py
June 30, 2015
’ s s y m S t r u c t ’ o b j e c t does n o t s u p p o r t i t e m a s s i g n m e n t
If you want to have a mutable variant, for example to contian the right hand side of an ode, use struct_SX:
By default SX.sym structure constructor will create new SX.syms. To recycle one that is already available, use
the ’sym’ argument:
Oops :
158
159
160
161
162
163
164
rhs = struct_SX(states)
r h s [ " x " ] = s t a t e s [ " x " ] **2
rhs [ "y" ] = states [ "y" ]*states [ "x" ]
rhs [ "q" ] = -states [ "q" ]
197
198
199
The ’sym’ feature is not available for struct_MX, since it will construct one parent MX.
print
rhs.cat
More powerIndex
Alternatively, you can supply the expressions at defintion time:
x , y , q = s t a t e s [ ... ]
rhs = struct_SX([
e n t r y (" x ", e x p r = x **2),
e n t r y (" y ", e x p r = x * y ),
e n t r y (" q ", e x p r = - q )
])
print
One can also construct symbolic MX structures
210
211
212
print
i n i t [ "X", : , : ," x " ]
Callables/functions can be thrown in in the powerIndex at any location. They operate on subresults obtain from
resolving the remainder of the powerIndex
217
218
print
i n i t [ "X", : , h o r z c a t , : ," x " ]
[ D M a t r i x ( [ [ 0, 1, 2 ] ] ), D M a t r i x ( [ [ 0, 1, 2 ] ] ), D M a t r i x ( [ [ 0, 1, 2 ] ] ), D M a t r i x
( [ [ 0, 1, 2 ] ] ), D M a t r i x ( [ [ 0, 1, 2 ] ] ) ]
218
print
i n i t [ "X", v e r t c a t , : , h o r z c a t , : ," x " ]
p r i n t V. c a t
[ [ 0,
[ 0,
[ 0,
[ 0,
[ 0,
p r i n t V. s h a p e
(94, 1)
185
i n i t [ "X", : , : ," x " ] = r e p e a t e d ( r a n g e (3))
[ [ D M a t r i x (0), D M a t r i x (1), D M a t r i x (2) ] , [ D M a t r i x (0), D M a t r i x (1), D M a t r i x (2)
] , [ D M a t r i x (0), D M a t r i x (1), D M a t r i x (2) ] , [ D M a t r i x (0), D M a t r i x (1),
D M a t r i x (2) ] , [ D M a t r i x (0), D M a t r i x (1), D M a t r i x (2) ] ]
V
184
i n i t [ "X", : , : ," x " ]
The repeated method duplicates its argument a number of times such that it matches the length that is needed at
the lhs
print V
The catted version is one single MX from which all entries are derived:
print
[ [ D M a t r i x (0), D M a t r i x (0), D M a t r i x (0) ] , [ D M a t r i x (0), D M a t r i x (0), D M a t r i x (0)
] , [ D M a t r i x (0), D M a t r i x (0), D M a t r i x (0) ] , [ D M a t r i x (0), D M a t r i x (0),
D M a t r i x (0) ] , [ D M a t r i x (0), D M a t r i x (0), D M a t r i x (0) ] ]
V = struct_symMX( s h o o t i n g )
MX. sym w i t h f o l l o w i n g s t r u c t u r e :
S t r u c t u r e w i t h t o t a l s i z e 94.
Structure holding 2 entries.
O r d e r : [ ’X ’, ’U ’ ]
X = r e p e a t e d ( [ 5, 3 ] ) : { y : 1- by -1 ( d e n s e ), x : 1- by -1 ( d e n s e ), q : 4- by -1 (
d e n s e )}
U = r e p e a t e d ( [ 4 ] ) : 1- by -1 ( d e n s e )
183
As illustrated before, powerIndex allows slicing
207
rhs.cat
[ sq ( x ), ( x * y ), (- q_0 ), (- q_1 ), (- q_2 ), (- q_3 ) ]
178
179
180
qsym = SX. sym (" q u a t e r n i o n ",4)
s t a t e s = s t r u c t _ s y m S X ( [ " x "," y ", e n t r y (" q ", sym=qsym ) ] )
print states.cat
[ x, y, q u a t e r n i o n _ 0 , q u a t e r n i o n _ 1 , q u a t e r n i o n _ 2 , q u a t e r n i o n _ 3 ]
[ sq ( x ), ( y * x ), (- q_0 ), (- q_1 ), (- q_2 ), (- q_3 ) ]
167
168
169
170
171
172
173
174
3
p r i n t V [ "X",0,-1," y " ]
219
print
1,
1,
1,
1,
1,
2],
2],
2],
2],
2] ]
i n i t [ "X", b l o c k c a t , : , : ," x " ]
v e r t s p l i t ( v e r t s p l i t (V){2}){1}
Similar to struct_SX, we have struct_MX:
193
194
195
196
197
198
[ [ 0,
[ 0,
[ 0,
[ 0,
[ 0,
1, 2 ] ,
1, 2 ] ,
1, 2 ] ,
1, 2 ] ,
1, 2 ] ]
Set all quaternions to 1,0,0,0
V = struct_MX([
(
e n t r y ("X", e x p r = [ [ MX. sym (" x ",6)**2 f o r
j i n r a n g e (3) ] f o r i i n r a n g e (5) ] )
,
e n t r y ("U", e x p r = [ -MX. sym (" u ") f o r i i n r a n g e (4) ] )
)
])
222
i n i t [ "X", : , : ," q " ] = r e p e a t e d ( r e p e a t e d ( D M a t r i x ( [ 1,0,0,0 ] )))
{} can be used in the powerIndex to expand into a dictionary once
/home/travis/build/casadi/binaries/casadi/docs/tutorials/python/src/tools/temp.py
225
226
227
June 30, 2015
[ 1, 1, 1 ] ,
[ 1, 1, 1 ] ,
[ 1, 1, 1 ] ]
i n i t [ "X", : ,0,{} ] = r e p e a t e d ({" y " : 9})
print
i n i t [ "X", : ,0,{} ]
[ { ’ y ’ : D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1,
D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1, 0,
D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1, 0,
D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1, 0,
D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1, 0,
0,
0,
0,
0,
0,
0, 0 ] )}, { ’ y ’ :
0 ] )}, { ’ y ’ :
0 ] )}, { ’ y ’ :
0 ] )}, { ’ y ’ :
0 ] )} ]
245
246
print
print
[ [ 1,
[ 1,
[ 1,
[ 1,
[ 1,
lists can be used in powerIndex in both list context or dict context:
230
4
s h o o t i n g [ "X", [ 0,1 ] , [ 0,1 ] ," x " ]
i n i t [ "X", b l o c k c a t , : , : ," q ",0,0 ]
1,
1,
1,
1,
1,
1],
1],
1],
1],
1] ]
[ [ SX( X_0_0_x ), SX( X_0_1_x ) ] , [ SX( X_1_0_x ), SX( X_1_1_x ) ] ]
231
print
s h o o t i n g [ "X", [ 0,1 ] ,0, [ " x "," y " ] ]
shapeStruct and delegated indexing
[ [ SX( X_0_0_x ), SX( X_0_0_y ) ] , [ SX( X_1_0_x ), SX( X_1_0_y ) ] ]
nesteddict can be used to expand into a dictionary recursively
234
print
i n i t [ nesteddict ]
{ ’X ’ : [ [ { ’ y ’ : D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’
y ’ : D M a t r i x (0), ’ x ’ : D M a t r i x (1), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’ y ’ :
D M a t r i x (12), ’ x ’ : D M a t r i x (2), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )} ] , [ { ’ y ’ :
D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’ y ’ :
D M a t r i x (0), ’ x ’ : D M a t r i x (1), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’ y ’ :
D M a t r i x (0), ’ x ’ : D M a t r i x (2), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )} ] , [ { ’ y ’ :
D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’ y ’ :
D M a t r i x (0), ’ x ’ : D M a t r i x (1), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’ y ’ :
D M a t r i x (0), ’ x ’ : D M a t r i x (2), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )} ] , [ { ’ y ’ :
D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’ y ’ :
D M a t r i x (0), ’ x ’ : D M a t r i x (1), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’ y ’ :
D M a t r i x (0), ’ x ’ : D M a t r i x (2), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )} ] , [ { ’ y ’ :
D M a t r i x (9), ’ x ’ : D M a t r i x (0), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’ y ’ :
D M a t r i x (0), ’ x ’ : D M a t r i x (1), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )}, { ’ y ’ :
D M a t r i x (0), ’ x ’ : D M a t r i x (2), ’ q ’ : D M a t r i x ( [ 1, 0, 0, 0 ] )} ] ] , ’U ’ : [
D M a t r i x (0), D M a t r i x (0), D M a t r i x (0), D M a t r i x (0) ] }
... will expand entries as an ordered list
237
print
If the powerIndex ends at the boundary of a structure, it’s catted version is returned:
print
s t a t e s = s t r u c t ( [ " x "," y ", e n t r y (" q ", r e p e a t = 2) ] )
V = struct_symSX([
e n t r y ("X", r e p e a t = 5, s t r u c t = s t a t e s ),
e n t r y (" P ", r e p e a t = 5, s h a p e s t r u c t = ( s t a t e s , s t a t e s ))
])
259
p r i n t V [ " P ",0 ]
P has a 4x4 shape
[ [ P_0_0 ,
[ P_0_1 ,
[ P_0_2 ,
[ P_0_3 ,
p r i n t V [ " P ",0, [ " x "," y " ] , [ " x "," y " ] ]
There is a problem when we wich to use the full potential of powerIndex in these extraIndices: The following is
in fact invalid python syntax:
We resolve this by using delegater objects index/indexf:
269
270
p r i n t V [ " P ",0, i n d e x f [ " q ", : ] , i n d e x f [ " q ", : ] ]
i n i t [ "X",0,0 ]
[ [ P_0_10 , P_0_14 ] ,
[ P_0_11 , P_0_15 ] ]
If the powerIndex is longer than what could be resolved as structure, the remainder, extraIndex, is passed onto
the resulting Casadi-matrix-type
print
P_0_8 , P_0_12 ] ,
P_0_9 , P_0_13 ] ,
P_0_10 , P_0_14 ] ,
P_0_11 , P_0_15 ] ]
[ [ P_0_0 , P_0_4 ] ,
[ P_0_1 , P_0_5 ] ]
[ 0, 9, 1, 0, 0, 0 ]
243
P_0_4 ,
P_0_5 ,
P_0_6 ,
P_0_7 ,
Now we can use powerIndex-style in the extraIndex:
262
i n i t [ "X", : ,0,... ]
[ [ D M a t r i x (0), D M a t r i x (9), D M a t r i x ( [ 1, 0, 0, 0 ] ) ] , [ D M a t r i x (0), D M a t r i x (9),
D M a t r i x ( [ 1, 0, 0, 0 ] ) ] , [ D M a t r i x (0), D M a t r i x (9), D M a t r i x ( [ 1, 0, 0, 0 ]
) ] , [ D M a t r i x (0), D M a t r i x (9), D M a t r i x ( [ 1, 0, 0, 0 ] ) ] , [ D M a t r i x (0),
D M a t r i x (9), D M a t r i x ( [ 1, 0, 0, 0 ] ) ] ]
240
When working with covariance matrices, both the rows and columns relate to states
252
253
254
255
256
257
i n i t [ "X", b l o c k c a t , : , : ," q ",0 ]
[ [ 1, 1, 1 ] ,
[ 1, 1, 1 ] ,
Of course, in this basic example, also the following would be allowed
272
p r i n t V [ " P ",0," q "," q " ]
[ [ P_0_10 , P_0_14 ] ,
[ P_0_11 , P_0_15 ] ]
/home/travis/build/casadi/binaries/casadi/docs/tutorials/python/src/tools/temp.py
June 30, 2015
Prefixing
The prefix attribute allows you to create shorthands for long powerIndices
279
280
281
282
283
284
285
s t a t e s = s t r u c t ( [ " x "," y "," z " ] )
V = struct_symSX([
e n t r y ("X", r e p e a t = [ 4,5 ] , s t r u c t = s t a t e s )
])
We can conveniently access it as follows:
330
331
332
333
334
337
initial
You can access its concents with a call:
[ [ 0, 2, 0 ] ,
[ 3, 0, 0 ] ,
[ 0, 0, 0 ] ]
i n i t i a l [ "x" ] = 1
i n i t i a l [ "y" ] = 2
i n i t i a l [ "z" ] = 3
If you work with Simulator, ControlSimulator, you typically end up with wanting to index a DMatrix that is n x N
with n the size of a statespace and N an arbitrary integer
But often, it will behave like a DMatrix transparantly:
343
P0 . s e t (P)
Next we represent the ’squared_repeated’ helper construct Imagine we somehow obtain a matrix that represents
a horizontal concatenation of covariance
347
P0 = h o r z c a t ( [ D M a t r i x .zeros( s t a t e s . s i z e , s t a t e s . s i z e ), D M a t r i x .ones( s t a t e s . s i z e
,states.size)])
350
351
352
353
354
P = s t a t e s . s q u a r e d _ r e p e a t e d ( P0 )
P [ 0," x "," y " ] = 2
P [ : ," y "," x " ] = 3
We can conveniently access it as follows:
s t a t e s = s t r u c t ( [ " x "," y "," z " ] )
We artificially construct here a DMatrix that could be a Simulator output.
313
o u t p u t = D M a t r i x .zeros( s t a t e s . s i z e ,8)
316
outputs = states.repeated(output)
319
320
321
322
o u t p u t s [ -1 ] = D M a t r i x ( [ 1,2,3 ] )
o u t p u t s [ : ," x " ] = r a n g e (8)
The helper construct is ’repeated’ here. Instead of “states(output)”, we have
Now we have an object that supports powerIndexing:
p r i n t P0
[ [ 0, 2, 0, 1, 1, 1 ] ,
[ 3, 0, 0, 3, 1, 1 ] ,
[ 0, 0, 0, 1, 1, 1 ] ]
Finally, we present the ’product’ helper construct
print
output
[ [ 0, 1, 2, 3, 4, 5, 6, 7 ] ,
[ 0, 0, 0, 0, 0, 0, 0, 2 ] ,
[ 0, 0, 0, 0, 0, 0, 0, 3 ] ]
print
o u t p u t s [ 5,{} ]
{ ’ y ’ : D M a t r i x (0), ’ x ’ : D M a t r i x (5), ’ z ’ : D M a t r i x (0)}
Next we represent the ’squared’ helper construct Imagine we somehow obtain a matrix that represents covariance
327
p r i n t P()
= num. p r e f i x [ "X",0,0 ]
Helper constructors
323
print P
p r e f i x ( ( ’ t ’,), M u t a b l e D M a t r i x ({ t : 3- by -3 ( d e n s e )}))
340
This is equivalent to the longer statements above
310
311
p r i n t P0
P itself is a rather queer object
num [ "X",0,0," x " ] = 1
num [ "X",0,0," y " ] = 2
num [ "X",0,0," z " ] = 3
Note the common part [“X”,0,0]. We can pull this apart with prefix:
295
296
297
298
299
300
P = s t a t e s . s q u a r e d ( P0 )
P [ " x "," y " ] = 2
P [ " y "," x " ] = 3
[ [ 0, 2, 0 ] ,
[ 3, 0, 0 ] ,
[ 0, 0, 0 ] ]
num = V()
Consider the following statements:
288
289
290
291
5
P0 = D M a t r i x .zeros( s t a t e s . s i z e , s t a t e s . s i z e )
357
358
359
360
361
362
363
364
365
366
c o n t r o l s = s t r u c t ( [ " u "," v " ] )
J0 = D M a t r i x .zeros( s t a t e s . s i z e , c o n t r o l s . s i z e )
J = s t a t e s . p r o d u c t ( c o n t r o l s , J0 )
J [ : ," u " ] = 3
J [ [ " x "," z " ] , : ] = 2
p r i n t J ()
/home/travis/build/casadi/binaries/casadi/docs/tutorials/python/src/tools/temp.py
June 30, 2015
[ [ 2, 2 ] ,
[ 3, 0 ] ,
[ 2, 2 ] ]
Saving and loading
It is possible to save and load some types of structures. Supported types are pure structures (the ones created with
’struct’) and numeric structures.
Saving: mystructure.save(“myfilename”)
Loading:
struct_load(“myfilename”)
or pickle.load(file(’myfilename’,’r’))
6