Skip to content

Instantly share code, notes, and snippets.

@akabe
akabe / dka.ml
Last active August 29, 2015 14:04
Durand-Kerner-Aberth method (well-known algorithm to compute all roots of a polynominal)
open Complex
(** [roots_initvals ?r [c(n); ...; c(2); c(1); c(0)]] computes
initial values for [roots] by using Aberth's method.
*)
let roots_initvals ?(r=1000.0) coeff_list =
match coeff_list with
| a::b::_ ->
let n = List.length coeff_list - 1 in
let s = 3.14159265358979 /. (float n) in
@akabe
akabe / levinson.ml
Created July 20, 2014 13:58
Levinson-Durvin method (estimation of AR coefficients)
(** [levinson r] computes AR coefficients by using Levinson-Durvin method.
@param r values of autocorrelation function: [[r(0); r(1); r(2); ...; r(M)]]
@return [([ar(1); ar(2); ...; ar(M)], sigma2)] where [ar(i)] is the [i]-th
coefficient of AR([M]) and [sigma2] is variance of errors.
*)
let levinson r =
let rec loop r ar sigma2 = function
| [] -> (List.rev ar, sigma2)
| rm :: rest ->
let rev_ar = List.rev ar in
@akabe
akabe / recursiveNeuralNetwork.ml
Created January 7, 2015 11:54
Recursive Neural Networks and Online Backpropagation Through Structure (BPTS)
(* recursiveNeuralNetwork.ml --- Recursive Neural Networks and
Online Backpropagation Through Structure (BPTS)
[MIT License] Copyright (C) 2015 Akinori ABE
Compilation:
$ ocamlfind ocamlopt -linkpkg -package slap recursiveNeuralNetwork.ml
This program requires Sized Linear Algebra Library (SLAP), a linear algebra
library for OCaml with static size checking for matrix operations (see
@akabe
akabe / unfoldingRAE.ml
Created January 20, 2015 05:17
Unfolding Recursive Autoencoder and Online Backpropagation Through Structure (BPTS)
(* unfoldingRAE.ml --- Unfolding Recursive Autoencoder and
Online Backpropagation Through Structure (BPTS)
[MIT License] Copyright (C) 2015 Akinori ABE
Compilation:
$ ocamlfind ocamlopt -linkpkg -package slap unfoldingRAE.ml
This program requires Sized Linear Algebra Package (SLAP), a linear algebra
library for OCaml with static size checking for matrix operations (see
@akabe
akabe / maze_solver.ml
Last active October 13, 2019 16:34
A maze solver by A* algorithm on OCaml
(* ========================================================================== *
* General implementation of A-star algorithm
* ========================================================================== *)
module Astar :
sig
type 'a t =
{
cost : 'a -> 'a -> int;
goal : 'a;
@akabe
akabe / type-level-list.cpp
Last active November 23, 2022 04:35
Type-level list and compile-time sort in C++
template <int n>
struct Int { static const int val = n; }; // Wrap an integer
template <class fst, class snd>
struct pair {
typedef fst first;
typedef snd second;
};
struct nil {
@akabe
akabe / generative_phantom_type_functor.ml
Created October 29, 2015 06:50
Static size checking for lists (loaded from files) by generative phantom types (functor version)
(* http://akabe.github.io/2015/10/GenerativePhantomTypes *)
#load "str.cma";;
(** Loads a list of integers from a file of a given path (delimiters = spaces,
tabs, or line feeds). *)
let load_list fname =
let re = Str.regexp "[ \t]+" in
let oc = open_in fname in
let rec aux acc = try aux (input_line oc :: acc) with End_of_file -> acc in
@akabe
akabe / generative_phantom_type_first_class_module.ml
Created October 29, 2015 06:58
Static size checking for lists (loaded from files) by generative phantom types (first-class-module version)
(* http://akabe.github.io/2015/10/GenerativePhantomTypes *)
#load "str.cma";;
(** Loads a list of integers from a file of a given path (delimiters = spaces,
tabs, or line feeds). *)
let load_list fname =
let re = Str.regexp "[ \t]+" in
let oc = open_in fname in
let rec aux acc = try aux (input_line oc :: acc) with End_of_file -> acc in
@akabe
akabe / generative_phantom_type_GADT.ml
Created October 29, 2015 07:06
Static size checking for lists (loaded from files) by generative phantom types (GADT version)
(* http://akabe.github.io/2015/10/GenerativePhantomTypes *)
#load "str.cma";;
(** Loads a list of integers from a file of a given path (delimiters = spaces,
tabs, or line feeds). *)
let load_list fname =
let re = Str.regexp "[ \t]+" in
let oc = open_in fname in
let rec aux acc = try aux (input_line oc :: acc) with End_of_file -> acc in
@akabe
akabe / generative_phantom_type_first_class_polymorphism.ml
Created October 29, 2015 07:19
Static size checking for lists (loaded from files) by generative phantom types (first-class-polymorphism version)
(* http://akabe.github.io/2015/10/GenerativePhantomTypes *)
#load "str.cma";;
(** Loads a list of integers from a file of a given path (delimiters = spaces,
tabs, or line feeds). *)
let load_list fname =
let re = Str.regexp "[ \t]+" in
let oc = open_in fname in
let rec aux acc = try aux (input_line oc :: acc) with End_of_file -> acc in