Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / julian.c
Last active April 26, 2017 08:01
Convert to/from Julian date numbers
int julian(int y, int m, int d) {
return 367*y -
(7 * (y + (m+9)/12))/4 -
(3 * ((y + (m-9)/7)/100 + 1))/4 +
275*m/9 + d + 1721029;
}
void gregorian(int j, int *year, int *month, int *day) {
int a = j + 68569;
int b = 4*a / 146097;
@rikusalminen
rikusalminen / cube.glslv
Last active January 26, 2022 15:24
Cube vertex shader - usage: glDrawArrays(GL_TRIANGLES, 0, 36), disable all vertex arrays
#version 420
uniform mat4 projection_matrix;
uniform mat4 model_matrix;
void main()
{
int tri = gl_VertexID / 3;
int idx = gl_VertexID % 3;
int face = tri / 2;
@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.hs
Last active April 5, 2020 15:20
Computing Kepler's elements with Haskell
module Main where
import Debug.Trace
import Control.Monad (forM_)
epsilon = 1.0e-10 :: Double
add (x1, y1, z1) (x2, y2, z2) = (x1+x2, y1+y2, z1+z2)
sub (x1, y1, z1) (x2, y2, z2) = (x1-x2, y1-y2, z1-z2)
mul (x1, y1, z1) (x2, y2, z2) = (x1*x2, y1*y2, z1*z2)
@rikusalminen
rikusalminen / looperthread.c
Last active August 27, 2017 08:15
Multi-core game loop. Triple buffer, periodically updated by N threads in parallel, readers-writers locking.
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#ifdef __linux__
#define _GNU_SOURCE
#define __USE_GNU
@rikusalminen
rikusalminen / rkn-steps.c
Last active December 16, 2015 03:29
Magic from the 20th century: Runge-Kutta-Nyström numerical integration in structure of array style.
#include <stdio.h>
static float mad(float a, float b, float c)
{
return a * b + c;
}
static void step1(const float *vel0, const float *a, float *d, float *v, float *delta, float *vel, float dt, size_t n)
{
for(size_t i = 0; i < n; ++i)
@rikusalminen
rikusalminen / gist:5187063
Created March 18, 2013 13:16
Numerical integration is like magic
#include <stdio.h>
void integrate_euler(float t0, float dt, float x0, float v0, float *x, float *v, float(*accelerate)(float,float,float))
{
float a = accelerate(t0, x0, v0);
float dx = v0 * dt + 0.5 * a * dt * dt;
float dv = a * dt;
*x = x0 + dx;
*v = v0 + dv;
@rikusalminen
rikusalminen / kerbalmate.py
Created February 13, 2013 10:53
A python script for mating Kerbal Space Program spacecraft with launch vehicle stages. Work in progress, doesn't do anything yet.
import re
from pprint import PrettyPrinter
pprint = PrettyPrinter().pprint
def load_craft(filename):
stack = [(None, [], [])]
with open(filename) as f:
prev = None
for line in f: