Skip to content

Instantly share code, notes, and snippets.

View PhDP's full-sized avatar
🏠
Working from home

Philippe Desjardins-Proulx PhDP

🏠
Working from home
View GitHub Profile
@PhDP
PhDP / lean3-sandbox.lean
Last active October 11, 2022 18:30
First few problems in the Lean 3 number game.
import data.real.basic
#check ℝ
open nat
-- Links:
-- * Lean 3's big math library: https://github.com/leanprover-community/mathlib
-- * Number game: https://www.ma.imperial.ac.uk/~buzzard/xena/natural_number_game/
-- * Agda's Unimath library: https://github.com/UniMath/agda-unimath
@PhDP
PhDP / leansandbox.lean
Created October 4, 2022 18:54
Random lean code
def hello := "world"
open List
#check Nat.zero
#check Nat.succ
#check Nat.succ 0
#check Nat.add
#check Nat.add 1
#check Nat.add 2 2
@PhDP
PhDP / euler7.rs
Created January 15, 2022 22:34
euler7.rs
fn nth_primes(n: usize) -> usize {
match n {
0 => 1,
1 => 2,
2 => 3,
_ => {
let mut primes = vec![2, 3];
let mut candidate = 5;
loop {
let max = (candidate as f64).sqrt() as usize;
/// You need to add these lines to Cargo.toml's [dependencies]:
/// statrs = "0.15.0"
/// nalgebra = "0.29.0"
use statrs::distribution::{Binomial, Discrete};
use nalgebra::base::DVector;
use nalgebra::dvector;
// Builds a vector of evenly spaced floats within a closed interval [a, b].
pub fn linspace(a: f64, b: f64, n: usize) -> DVector<f64> {
@PhDP
PhDP / rust.md
Last active September 23, 2021 22:09
Installing Rust

Installing Rust

Most editors/IDEs have good Rust support at this point, but I recommend Visual Code with Rust Analyzer.

Linux or OSX

Simply copy and paste the command from the rustup website (here) in a terminal. Rustup will install the rustc compiler and, just as importantly, the cargo package manager.

@PhDP
PhDP / adj.jl
Last active November 26, 2020 19:56
AdjacencySet for Graphs in Julia
# A graph as a vector of sorted vectors. This is very similar to the way sparse matrices
# are represented except each vertex has its own sorted vector (making it much faster to
# add/remove edges). Using BTrees instead of sorted vector would improve performance for
# adding/removing edges.
#WeightedGraph = Vector{Vector{(UInt32, Float64)}}
Graph = Vector{Vector{UInt32}} # Make more generic...
# There has to be a better way (map?):
function make_graph(order)
@PhDP
PhDP / ge.h
Created March 22, 2020 21:36
Draft of grammatical evolution.
/**
* @file grammatical_evolution.h
* @brief Functions to use grammatical evolution to evolve programs, mathematical
* functions, sentences, whatever you want, just use your imagination!
*/
#ifndef RAW_GRAMMATICAL_EVOLUTION_H_
#define RAW_GRAMMATICAL_EVOLUTION_H_
#include "raw/common.h"
#include "raw/grammar.h"
@PhDP
PhDP / gist:17f7a6a726394d307c6f2b6bbea39a3d
Created March 22, 2020 18:48
Grammar for a system of ODEs. I cheat a bit with formula by adding the unecessary <formula> <operator> <variable> and <variable> <operator> <formula>, it helps grammatical evolution algorithms find simpler formulas.
<expr> ::= dS/dt = <formula>\ndI/dt = <formula>\ndR/dt = <formula>
<formula> ::= <unary><formula> | <formula> <operator> <formula> | <formula> <operator> <variable> | <variable> <operator> <formula> | <variable>
<unary> ::= -
<operator> ::= - | *
<variable> ::= a | b | I | S
@PhDP
PhDP / out.txt
Created March 22, 2020 18:45
Grammatical evolution output
Codon size: 100
0: dS/dt = a\ndI/dt = I\ndR/dt = S
1: dS/dt = S - S - b * S - a\ndI/dt = S * I - I - a * S * a * -I * b - I * a * S * a * b * a - I - a\ndR/dt = --a - --I - a * b * -a * ---S - S * a * a - a * a
4: dS/dt = -a - I - b\ndI/dt = S - b - --b - a * -b * a * b - S - S - I\ndR/dt = a * a * S * b - I
7: dS/dt = a\ndI/dt = S * a\ndR/dt = b - I
9: dS/dt = b * S * S\ndI/dt = I * I - b * S * a\ndR/dt = -b - I - b * S - S * S * a * b
12: dS/dt = I * b * S\ndI/dt = -a - I - a * a - S\ndR/dt = S * S * b * -S - S - -S - a - -b - -a - b - a - b - S - S * I - ---b - a - S * S - I * b * S - -a - I - a * a - a * I - b * S * b * -S - S - -S - a - -b - -a - b - a - b - S
15: dS/dt = -I - S\ndI/dt = --a - -b - b\ndR/dt = -a - b - S * b - a - S * -S * -b - S
16: dS/dt = a * b\ndI/dt = -b * b\ndR/dt = b - I - a * b - a * I
17: dS/dt = I * I * a - I\ndI/dt = a * a - -S - ----S - -a * --b - b - S * I - a * b - a\ndR/dt = S - a
@PhDP
PhDP / mm.cc
Created March 8, 2020 00:53
Matrix multiplication with a matrix of type Integer -> Integer -> T.
// C++17
#include <iostream>
#include <array>
#include <initializer_list>
// Column-major matrix based on std::array.
template<size_t ROW, size_t COL, typename T>
class matrix {
public: