Skip to content

Instantly share code, notes, and snippets.

View alphaville's full-sized avatar
:octocat:
(De)coding

Pantelis Sopasakis alphaville

:octocat:
(De)coding
View GitHub Profile
@alphaville
alphaville / unit_test.c
Created January 25, 2013 03:30
A prototype for Unit Tests in C. Works on Netbeans.
/*
* File: dataloader_test.c
* Author: imt
*
* Created on Jan 23, 2013, 11:12:09 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@alphaville
alphaville / dvsm_quad.c
Last active December 11, 2015 19:28
Multiply a dense vector with a sparse matrix.
#ifndef csi
#define csi ptrdiff_t
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
typedef struct cs_sparse {
@alphaville
alphaville / gist:4649140
Created January 27, 2013 16:34
Matrix-vector multiplication using BLAS.
void doStuff(void) {
// Les data:
float A[4][4] = // the matrix to be multiplied
{
{1.0f, 1.0f, 1.0f, 1.0f},
{0.0f, 1.0f, 1.0f, 4.0f},
{1.0f, 1.0f, 1.0f, 4.0f},
{6.0f, 2.0f, 1.0f, 1.0f}
};
float a = 0.5f;
@alphaville
alphaville / gist:4649329
Created January 27, 2013 17:32
float[][] as float*
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main(int argc, char** argv) {
float A[4][4] ={
{1.0f, 2.0f, 3.0f, 4.0f},
{4.0f, 5.0f, 6.0f, 7.0f},
{8.0f, 9.0f, 10.0f, 11.0f},
@alphaville
alphaville / gist:4649485
Last active December 11, 2015 19:38
Quadratic cost of MPC given the sequence of states, inputs and the weight matrices Q, R and Pf.
zreal primal_cost(const cs *Q, const cs *R, const cs *Pf, const ptrdiff_t N, const zmatr* X, const zmatr* U) {
zreal J = 0;
ptrdiff_t k;
zvect* xk = malloc(sizeof(zvect));
zvect* uk = malloc(sizeof(zvect));
for (k = 0; k < N; k++) {
*xk = X->values + k;
*uk = U->values + k;
J += dvsm_quad(Q, *xk);
J += dvsm_quad(R, *uk);
@alphaville
alphaville / smdv_quad.m
Created January 28, 2013 22:56
SMDV_QUAD returns x'*Q*x where Q is a sparse matrix and x a vector.
function [y, time] = smdv_quad(Q,x)%#codegen
%SMDV_QUAD returns x'*Q*x where Q is a sparse matrix and x a vector.
coder.extrinsic('tic');
coder.extrinsic('toc');
tic;
y=0;
for k=1:size(Q,1)
i = Q(k,1);
y = y + Q(k,3) * x(i) * x(Q(k,2));
@alphaville
alphaville / compile.m
Last active December 11, 2015 21:48
x'Qx in C - Implementation for MATLAB - MEX file and compilation instructions
% SMDV_QUAD.m
disp('Compiling : SMDV_QUAD');
mex CFLAGS='$CFLAGS -Wall' -O -output smdv_quad -largeArrayDims mx_smdv_quad.c smdv_quad.c
% PCOST.m
disp('Compiling : PCOST');
mex CFLAGS='$CFLAGS -Wall' -O -output pcost -largeArrayDims pcost.c smdv_quad.c /usr/lib/libcblas.dylib
mex CFLAGS='$CFLAGS -Wall' -O -output dgrad -largeArrayDims dgrad.c smdv_quad.c vec_util.c /usr/lib/libcblas.dylib
function output = val2worspace(varargin)
% All variables of this function will be exported
% to the workspace
WHO = who;
for i = 1:length(WHO)
assignin('base', WHO{i}, eval(WHO{i}));
end
/*
author: jbenet
os x, compile with: gcc -o testo test.c
linux, compile with: gcc -o testo test.c -lrt
*/
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
@alphaville
alphaville / execution_time.m
Created February 1, 2013 15:15
Measures the execution time of a MATLAB function (on average). Uses a fixed test time.
time_test = 4; % Time each test may last (in seconds)
tic;
t1=0;
ntrials=0;
while (t1<time_test)
% Do things to be benchmarked
ntrials=ntrials+1;
t1=toc;
end