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 / fol.pegjs
Created October 1, 2016 03:04
PEGJs Logic + Math parser
{
function getTerms(t, ts) {
if (t) {
var arr = [];
for (var i = 0; i < ts.length; ++i) {
arr.push(ts[i][2]);
}
return [t].concat(arr);
}
return [];
@PhDP
PhDP / rh_hash_table.hpp
Created April 27, 2017 20:04 — forked from ssylvan/rh_hash_table.hpp
Quick'n'dirty Robin Hood hash table implementation. Note, I have implemented this algorithm before, with tons of tests etc. But *this* code was written specifically for the blog post at http://sebastiansylvan.com/post/robin-hood-hashing-should-be-your-default-hash-table-implementation/, it has not been extensively tested so there may be bugs (an…
#define USE_ROBIN_HOOD_HASH 1
#define USE_SEPARATE_HASH_ARRAY 1
template<class Key, class Value>
class hash_table
{
static const int INITIAL_SIZE = 256;
static const int LOAD_FACTOR_PERCENT = 90;
struct elem
#include <iostream>
#include <map>
#include <unordered_map>
template<typename Key, typename Value>
using stdmap = std::map<Key, Value>;
template<typename Key, typename Value>
using stdumap = std::unordered_map<Key, Value>;
@PhDP
PhDP / quantum_demux_oo.cc
Last active January 27, 2018 18:37
C++17 object-oriented version of a quantum circuit model for a 2-bit demultiplexer.
// With gcc 7+ or clang 5+, compiles with -std=c++17 flag
#include <iostream>
#include <memory>
#include <vector>
// Base class for gates:
struct gate {
virtual void apply(std::vector<bool>& bits) const = 0;
virtual std::ostream& print(std::ostream&) const = 0;
@PhDP
PhDP / quantum_demux_va.cc
Last active January 27, 2018 18:37
C++17 sum-types version of a quantum circuit model for a 2-bit demultiplexer. It's like the object-oriented version, but it doesn't suck.
// With gcc 7+ or clang 5+, compiles with -std=c++17 flag
#include <iostream>
#include <variant>
#include <vector>
struct toffoli_gate {
uint32_t c0, c1, x;
toffoli_gate(uint32_t c0_, uint32_t c1_, uint32_t x_) noexcept
: c0(c0_), c1(c1_), x(x_) {
@PhDP
PhDP / fol.js
Created March 1, 2018 19:57
fol.js
// tslint:disable
/*
* Generated by PEG.js 0.10.0.
*
* http://pegjs.org/
*/
"use strict";
function peg$subclass(child, parent) {
@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:
@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 / 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 / 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"