Skip to content

Instantly share code, notes, and snippets.

View jgillis's full-sized avatar

Joris Gillis jgillis

View GitHub Profile
@jgillis
jgillis / mfunctionparser.py
Created March 14, 2013 12:58
Parse m-functions in casadi
import casadi
import re
class MFunctionParser:
def parse(self,filename):
f = open(filename,"r")
signature = f.readline()
m = re.search("function \[?([\w,]+)\]? = \w+\((.*)\)",signature)
if not m:
@jgillis
jgillis / multisens
Created May 24, 2013 07:49
casadi multiple sensitivities
from casadi import *
x = msym("x",2)
y = msym("y",2,2)
f = MXFunction([x,y],[mul(y,x),y])
f.setOption("number_of_fwd_dir",2)
f.init()
f.input(0).set(DMatrix([1.1,1.3]))
@jgillis
jgillis / gist:5683620
Last active December 17, 2015 22:39
Method to print the class inheritance hierarchy
def print_subclasses(myclass, depth=0):
print (" " * depth) + " - " + myclass.__name__
for s in myclass.__subclasses__():
print_subclasses(s,depth=depth+1)
import numpy as NP
import casadi as C
def qpsolve(H,g,lbx,ubx,A=NP.zeros((0,0)),lba=NP.zeros(0),uba=NP.zeros(0)):
# Convert to CasADi types
H = C.DMatrix(H)
g = C.DMatrix(g)
lbx = C.DMatrix(lbx)
ubx = C.DMatrix(ubx)
A = C.DMatrix(A)
A = A.reshape((A.size1(),H.size1())) # Make sure matching dimensions
@jgillis
jgillis / HackyFunction.py
Last active September 6, 2021 13:37
using vertcat(MX,MX) as inputs to Function
from casadi import *
def HackyFunction(name,ins,outs):
# Obtain all symbolic primitives present in the inputs
Vs = symvar(veccat(*ins))
# Construct a helper function with these symbols as inputs
h = Function("helper",Vs, ins)
# Assert dense inputs
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head><script src='http://d3js.org/d3.v3.min.js'></script><script src='http://cpettitt.github.io/project/dagre-d3/v0.1.5/dagre-d3.min.js'></script><style>svg { overflow: hidden;}.node rect { stroke: #333; stroke-width: 1.5px; fill: #fff;}.edgeLabel rect { fill: #fff;}.edgePath { stroke: #333; stroke-width: 1.5px; fill: none;}.outer { width: 1024px; height: 960px; overflow: auto;}.inner { width: 8000px; height: 6000px;}svg { display: block; width: 100%; height: 100%;}
.link {
stroke: #999;
stroke-width: 2px;
}
#!/usr/bin/env python
"""
simple example script for running and testing notebooks.
Usage: `ipnbdoctest.py foo.ipynb [bar.ipynb [...]]`
Each cell is submitted to the kernel, and the outputs are compared with those stored in the notebook.
"""
import os,sys,time
import os
import subprocess
import io
import shutil
dir = '/home/frs/project/casadi/CasADi/'
shutil.rmtree(os.path.join(dir,'branches'))
os.mkdir(os.path.join(dir,'branches'))
import casadi.*
load SilverboxSimulated
u_data = u;
y_data = y;
% simulation horizon
N = size(u_data, 1);
% declare symbols for states and controls
y = MX.sym('y');
import casadi.*
load SilverboxSimulated
u_data = u;
y_data = y;
%%
% simulation horizon
N = size(u_data, 1);