Skip to content

Instantly share code, notes, and snippets.

@rikusalminen
rikusalminen / gist:8866479
Created February 7, 2014 16:40
Ideas for a toy programming language project
fib(n) =
if n <= 1
then n // TODO: is "then" keyword required? not strictly but improves readability?
else
left = fib(n-2)
right = fib(n-1)
left + right
fib_iter(n) =
iter(a, b, n) =
@rikusalminen
rikusalminen / kepler.c
Last active August 29, 2015 14:02
Kepler problem in C
struct kepler_elements {
double semi_latus_rectum;
double eccentricity;
double mean_motion;
double inclination;
double longitude_of_ascending_node;
double argument_of_periapsis;
double periapsis_time;
};
@rikusalminen
rikusalminen / numtest.c
Created June 24, 2014 14:32
Special purpose test framework for numerical code
#define ZEROF(x) ((x)*(x) < 1.0e-15)
#define EQF(a, b) ((ZEROF(a) && ZEROF(b)) || ZEROF(((a)-(b))*((a)-(b))/((a)*(a) + ((b)*(b)))))
#define LTF(a, b) ((a) < (b) || EQF((a), (b)))
#define ASSERT(cond, msg, ...) { numtest_assert(cond, test_ctx, __FILE__, __LINE__, __FUNCTION__, msg, ##__VA_ARGS__); }
#define ASSERT_EQF(a, b, msg, ...) ASSERT(EQF((a), (b)), msg, ##__VA_ARGS__)
#define ASSERT_LTF(a, b, msg, ...) ASSERT(LTF((a), (b)), msg, ##__VA_ARGS__)
#define ASSERT_RANGEF(x, min, max, msg, ...) ASSERT(LTF((min), (x)) && LTF((x), (max)), msg, ##__VA_ARGS__)
struct numtest_ctx;
@rikusalminen
rikusalminen / universal_var.c
Created June 30, 2014 15:35
Universal variable solution to the Kepler problem
#include <stdbool.h>
#include <float.h>
#include <math.h>
double dot(const double *a, const double *b) {
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}
double mag(const double *a) {
return sqrt(dot(a, a));
@rikusalminen
rikusalminen / orbits.gnuplot
Created October 1, 2014 20:48
Plotting satellite orbits with Gnuplot
r(p,e,f) = p/(1.0 + e * cos(f))
orbit_x(p,e,i,an,argp,f) = \
r(p,e,f) * (\
cos(f) * ((cos(argp) * cos(an)) - (sin(argp) * sin(an) * cos(i))) + \
sin(f) * (-(cos(argp) * sin(an) * cos(i)) - (sin(argp) * cos(an))))
orbit_y(p,e,i,an,argp,f) = \
r(p,e,f) * (\
cos(f) * ((sin(argp) * cos(an) * cos(i)) + (cos(argp) * sin(an))) + \
sin(f) * ((cos(argp) * cos(an) * cos(i)) - (sin(argp) * sin(an))))
@rikusalminen
rikusalminen / celestialcheatsheet.tex
Last active August 29, 2015 14:10
Celestial mechanics cheat sheet
% vim:nolist lbr tw=78 expandtab autoindent nocindent
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[landscape,top=1cm,bottom=1cm,left=1cm,right=1cm]{geometry}
%\usepackage{amsmath}
\pagestyle{empty}
\setcounter{secnumdepth}{0}
@rikusalminen
rikusalminen / hsv2rgb.c
Created September 13, 2015 09:36
HSV to RGB
#include <math.h>
void hsv2rgb(float h, float s, float v, float *rgb) {
float c = v * s;
float hh = h * 6.0;
float x = c * (1.0 - fabsf(fmodf(hh, 2) - 1.));
float m = v - c;
int i = (int)hh % 6;
rgb[(1 + 2*i)%3] = x + m;
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()
set nocompatible
colorscheme desert
tab all
set mouse=a
.code64
.section .text
.global thread_context_switch
# from thread: rdi = interrupt_stack_frame*, rsi = registers_t*
# to thread: rdx = const interrupt_stack_frame*, rcx = const registers_t*
thread_context_switch:
# store general purpose registers to *rdi
mov %r15, 0(%rdi)
mov %r14, 8(%rdi)
mov %r13, 16(%rdi)
@rikusalminen
rikusalminen / gist:1012156
Created June 7, 2011 12:42
quickfix keybindings
noremap <silent> <leader>qw :botright cwindow<cr>
noremap <silent> <leader>qo :botright copen<cr>
noremap <silent> <leader>qc :cclose<cr>
noremap <silent> <leader>qq :cc!<cr>
noremap <silent> <leader>qn :cnext<cr>
noremap <silent> <leader>qp :cprev<cr>
noremap <silent> <leader>lw :botright lwindow<cr>
noremap <silent> <leader>lo :botright lopen<cr>
noremap <silent> <leader>lc :lclose<cr>