Skip to content

Instantly share code, notes, and snippets.

@matthieubulte
matthieubulte / main.jl
Last active January 23, 2021 17:20
answer to geophf concerns about the rk43 method
# Answer to: https://twitter.com/1HaskellADay/status/875063542784958465
#
# So the Initial Value Problem (IVP) you want to solve seems to be the following
#
# dy/dx = 3*exp(-x) - 0.4*y
# y(x_0) = y_0 with x_0 = 0 and y_0 = 5
#
# You then ask: what is the value of y when x = 3?
#
# This can be computed using the Runge Kutta method by feeding it
function quad42(f,a,b,tol; α=0.9, Θ=3.0)
x, val, h = a, 0, (b-a)/10
xx, f₀ = [x], f(x)
err, evals = 0.0, 1
while x < b
fₘ, f₁ = f(x+h/2), f(x+h) # effektive Stufenzahl 2 (2 f-Auswertungen pro Schritt)
evals += 2
Q2 = h*(f₀+f₁)/2 # Trapezregel (Ordnung 2)
#include <iostream>
#include <unistd.h>
#include <sys/mman.h>
#include <cstring>
#include <cstdarg>
#include <ctime>
class code_emiter {
public:
code_emiter(char* base) : base(base) {}
function sort_inverse(p)
out = [ i for i in 1:length(p) ]
for i = 1:length(p)
out[p[i]] = i
end
out
end
# TODO: limit allocations to improve performance
# Reverse Cuthill McKee
function fast_solve_sylvester(A, B, C) # 6 m^3
n = size(A, 1)
R,U = schur(complex(A)) # m^3
S,V = schur(complex(B)) # m^3
M = U'*C*V
Y = zeros(n, n)
m = zeros(n)
for i = 1:n # 2 m^3
(* TODO: use Random.State *)
open Random;;
open List;;
(* =========================================== General Utils = *)
let sign x = if x < 0 then -1 else 1;;
let truncate x l = sign x * min (abs x) l;;
(* Good morning everyone, I'm currently learning ocaml for one of my CS class and needed to implement
an avl tree using ocaml. I thought that it would be interesting to go a step further and try
to verify the balance property of the avl tree using the type system. Here's the resulting code
annotated for people new to the ideas of type level programming :)
*)
(* the property we are going to try to verify is that at each node of our tree, the height difference between
the left and the right sub-trees is at most of 1. *)
-- The Nat type class is to be seen as a port of the Nat type to the type level.
--
-- At value level, the usual Nat type only posseses two constructors by being defined as followed:
--
-- data Nat = Z | S Nat
--
-- Being then usable by pattern matching on the constructor as following:
--
-- foo :: Nat -> ...
-- foo n = case n of
@matthieubulte
matthieubulte / Leibniz.purs
Last active August 30, 2016 11:41
Foundations for proving stuff in PureScript
{-
A bunch of useful primitives when using PureScript to prove *things*.
Idea of using Leibniz equality comes from http://code.slipthrough.net/2016/08/10/approximating-gadts-in-purescript/
-}
module Leibniz where
import Prelude ((<<<), ($))
import Unsafe.Coerce (unsafeCoerce)
@matthieubulte
matthieubulte / ClassSerializer.java
Created April 6, 2016 14:37
java to typescript
package com.codegen;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class ClassSerializer {
private String indentation;
private String newLine;