Skip to content

Instantly share code, notes, and snippets.

@truncs
Created September 28, 2022 06:01
Show Gist options
  • Save truncs/f4954b2c9fc89cdccfdd9ef96aae0ee3 to your computer and use it in GitHub Desktop.
Save truncs/f4954b2c9fc89cdccfdd9ef96aae0ee3 to your computer and use it in GitHub Desktop.
Casadi Jacobian
class Fun(Callback):
# sin(x+3*y)
def __init__(self):
Callback.__init__(self)
self.construct("Fun", {})
def get_n_in(self): return 2
def get_n_out(self): return 1
def eval(self,arg):
x = arg[0]
y = arg[1]
z0 = 3*y
z1 = x+z0
z2 = sin(z1)
return [z2]
def has_forward(self,nfwd): return False
def has_reverse(self,nadj): return False
def has_jacobian(self): return True
def get_jacobian(self, name, inames, onames, opts):
x = SX.sym("x")
y = SX.sym("y")
out_g = SX.sym('out_g', Sparsity(1,1))
J = Function(name, [x,y,out_g],[horzcat(cos(x+3*y),3*cos(x+3*y))], inames, onames, opts)
return J
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment