SX -> MX graph conversion and AD

926 views
Skip to first unread message

Milan Vukov

unread,
Sep 1, 2014, 7:09:00 AM9/1/14
to casadi...@googlegroups.com
Hi, 

I am trying to do some SX to MX conversion and then do some AD. I am using CasADi 1.7 (long story). Here is the code which fails:

import casadi as C

a
=C.ssym("a")
b
=C.ssym("b")

sxf
=C.SXFunction([C.veccat([a, b])], [a + b])
sxf
.init()

mxIn
=C.msym("mxIn", 2, 1)
mxOut
=sxf.call([mxIn])

mxJ
=C.jacobian(mxOut[ 0 ], mxIn[ 0 ])

The last line fails:

In [17]: mxJ = C.jacobian(mxOut[ 0 ], mxIn[ 0 ])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-17-04a3fcf3ec0c> in <module>()
----> 1 mxJ = C.jacobian(mxOut[ 0 ], mxIn[ 0 ])

/Users/madwolf/Work/GIT/casadi/build/python/casadi/casadi.pyc in jacobian(*args)
  18591
  18592     """
> 18593   return _casadi.jacobian(*args)
  18594
  18595 def gradient(*args):

RuntimeError:  on line 127 of file "/Users/madwolf/Work/GIT/casadi/symbolic/fx/x_function_internal.hpp"
XFunctionInternal::XFunctionInternal: Xfunction input arguments must be purely symbolic.
Argument #0 is not symbolic.

How can I make this working?

Thanx, Milan

Joel Andersson

unread,
Sep 1, 2014, 7:26:52 AM9/1/14
to casadi...@googlegroups.com
Hi!

With the data structures defined like that, I think what you want to do is:
mxJ=C.jacobian(mxOut[ 0 ], mxIn)

Regards,
Joel

Milan Vukov

unread,
Sep 1, 2014, 1:04:44 PM9/1/14
to casadi...@googlegroups.com
Hi, 

Hi, 

Thanx for the answers. Joel, I want to do exactly what was in my question: to get jacobian of "a + b" wrt "a", but from MX graph. How can I get this? If doing


mxIn=C.msym("mxIn", 2, 1)
mxOut
=sxf.call([mxIn])


is wrong, how should I do it?

My original problem is much more complex, here I posted a simplified one.

Cheers,
Milan

Greg Horn

unread,
Sep 1, 2014, 1:29:57 PM9/1/14
to casadi...@googlegroups.com
import casadi as C

a
=C.ssym("a")
b
=C.ssym("b")


sxf
=C.SXFunction([a, b], [+ b])
sxf
.init()

mxIn1=C.msym("mxIn1", 1, 1)
mxIn2=C.msym("mxIn2", 1, 1)
[mxOut]
=sxf.call([mxIn1,mxIn2])

mxf = MXFunction([mxIn1, mxIn2], [mxOut])
mxfunjac = mxf.jacobian(0,1) # i forget the indexes, it's which input by which output is the jacobian

Greg Horn

unread,
Sep 1, 2014, 1:35:47 PM9/1/14
to casadi...@googlegroups.com
That approach above has jacobian indexes switched, sorry. Also another approach is the same to what i just posted, but instead of forming an MXFunction and using MXFunction.jacobian, you could just do:

mxf = C.jacobian(mxOut, mxIn0)

Milan Vukov

unread,
Sep 1, 2014, 1:40:32 PM9/1/14
to casadi...@googlegroups.com
Thanx :)

Let's upgrade the problem. I have expression of SX's:

sxIn: n_in x1
sxOut n_out x1

I would like to embed this in an MX graph for later processing. So, once embedded I would like to do something like this (pseudo-code):

C.jacobian(mxOut, mxIn[0: n_foo])

i.e. get Jacobian of the embedded expression wrt first n_foo inputs.

Is this possible?

Greg Horn

unread,
Sep 1, 2014, 1:55:31 PM9/1/14
to casadi...@googlegroups.com
That is possible. You should make 2 or more mxinputs, but the first mxinput represents all functions you want derivative with respect to.

if you have 100 inputs, and want the derivative w.r.t first 40
import casadi as C

n = 100

x=C.SX.sym("a",n,1)
sxf=C.SXFunction([x], [x*x])
sxf.init()

nfoo = 40
mxIn1=C.MX.sym("mxIn1", nfoo, 1)
mxIn2=C.MX.sym("mxIn2", n-nfoo, 1)
[mxOut]=sxf.call([C.veccat([mxIn1,mxIn2])])

print C.jacobian(mxOut,mxIn1)

Reply all
Reply to author
Forward
0 new messages