Skip to content

Instantly share code, notes, and snippets.

View jgillis's full-sized avatar

Joris Gillis jgillis

View GitHub Profile
import casadi.*
% In this example, we fit a nonlinear model to measurements
%
% This example uses more advanced constructs than the vdp* examples:
% Since the number of control intervals is potentially very large here,
% we use memory-efficient Map and MapAccum, in combination with
% codegeneration.
%
% We will be working with a 2-norm objective:
@jgillis
jgillis / simplecacher
Created October 27, 2015 07:25
This file helps you to cache jitted code compilation ( https://github.com/casadi/casadi/issues/1627 )
#!/usr/bin/python
import sys
import hashlib
import subprocess
import shutil
import os
args = sys.argv[1:]
cachedir = ".cache"
casadi2matlabfun =@(F,castfun) @(varargin) subsref(cellfun(castfun,F(varargin),'UniformOutput',false),struct('type','{}','subs',{{':'}}));
Hf = casadi2matlabfun(H,@full)
@jgillis
jgillis / casadi_struct2vec.m
Last active February 6, 2016 14:58
casadistructs in matlab
function [out] = casadi_struct2vec(s)
flat = {};
if isstruct(s)
for f=fieldnames(s)'
flat = {flat{:} casadi_struct2vec(s.(f{1}))};
end
out = vertcat(flat{:});
else if iscell(s)
for i=1:length(s)
flat = {flat{:} casadi_struct2vec(s{i})};
@jgillis
jgillis / PSDchecker.m
Last active March 8, 2016 08:27
enforcing P>0
classdef PSDchecker < casadi.Callback2
properties
n
fwd
adj
end
methods
function self = PSDchecker(n)
self.n = n;
self.fwd = mydergen(n,true);
from functools import partial
class Infix(object):
def __init__(self, func):
self.func = func
def __or__(self, other):
return self.func(other)
def __ror__(self, other):
return Infix(partial(self.func, other))
def __call__(self, v1, v2):
# Pendulum collocation
# w position of cart
# w1,y1 position of pendulum
import numpy as NP
from casadi import *
from casadi.tools import *
import matplotlib.pyplot as plt
function ret = classify_linear(e,v)
%
% Takes vector expression e, and symbolic primitives v
% Returns classification vector
% For each element in e, determines if:
% - element is nonlinear in v (2)
% - element is linear in v (1)
% - element does not depend on v at all (0)
%
% This method can be sped up a lot with JacSparsityTraits::sp
from casadi import *
H = DM([[1,-1],[-1,2]])
G = DM([-2,-6])
A = DM([[1, 1],[-1, 2],[2, 1]])
LBA = DM([-inf]*3)
UBA = DM([2, 2, 3])
LBX = DM([0]*2)
classdef Function < casadi.SharedObject
%FUNCTION General function.
%
%
%
%A general function $f$ in casadi can be multi-input, multi-output. Number of
%inputs: nin n_in() Number of outputs: nout n_out() We can view this
%function as a being composed of a ( nin, nout) grid of single-input, single-
%output primitive functions. Each such primitive function $f_ {i, j}
%\\forall i \\in [0, nin-1], j \\in [0, nout-1]$ can map as $\\mathbf