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 / bisection.py
Created June 24, 2013 18:19
Bisection algorithm in Python (to compare with the Haskell version).
# bisection(lambda x: x**2 - 2.0, 0, 100, 1e-15)
def bisection(f, a, b, err):
if err < 1e-15 or abs(b - a) / 2 < err:
return None
if a > b:
a, b = b, a
m = (b + a) / 2.0
while (b - a) / 2.0 > err and f(m) != 0.0:
if f(a) * f(m) < 0.0:
b = m
@PhDP
PhDP / bisection.hs
Last active December 18, 2015 21:48
Safe bisection algorithm in Haskell.
bisection :: (Double -> Double) -> Double -> Double -> Double -> Maybe Double
bisection f a b err
| err < 1e-15 = Nothing
| abs (b - a) / 2 < err = Nothing
| a < b = bis a b
| otherwise = bis b a
where
bis a b
| d < err || f m == 0.0 = Just m
| f a * f m < 0.0 = bis a m
@PhDP
PhDP / bisection.hs
Created June 24, 2013 12:42
The bisection algorithm in Haskell.
bisection f a b e = if a < b then bis a b else bis b a
where err = if e < 1e-15 then 1e-15 else e
bis a b = let d = (b - a) / 2; m = (b + a) / 2 in
if d < err then
m
else if f a * f m < 0.0 then
bis a m
else
bis m b
@PhDP
PhDP / gist:5691541
Created June 1, 2013 19:55
Results for the updated "Redskins Rules" (http://en.wikipedia.org/wiki/Redskins_Rule) with the C4.5 decision tree algorithm.
=== Run information ===
Scheme:weka.classifiers.trees.J48 -C 0.25 -M 2
Relation: Dataset0 - Sheet1-weka.filters.unsupervised.attribute.Remove-R1-weka.filters.supervised.attribute.NominalToBinary-weka.filters.supervised.attribute.NominalToBinary-weka.filters.supervised.attribute.NominalToBinary-weka.filters.unsupervised.attribute.NumericToNominal-Rfirst-last
Instances: 21
Attributes: 3
@PhDP
PhDP / networks.R
Last active December 16, 2015 08:59
Functions to generate geometric networks: random geometric graphs, connected random geometric graphs, random geometric trees.
# Functions to generate networks ("graphs") for spatial ecology.
#
# Stores the graph in a rather inefficient (but easy-to-use) adjacency matrix
# of boolean values.
#
# Key functions:
# - geograph returns a random geometric graph.
# - cgeograph returns a connected random geometric graph.
# - geotree returns a random geometric tree.
#
@PhDP
PhDP / main.cc
Last active October 10, 2023 03:46
A simple tutorial on C++11's random number generators.
// Compile with:
// $ clang++ -O3 -std=C++11 main.cc -o main
#include <iostream>
#include <random> // The header for the generators.
#include <ctime> // To seed the generator.
using namespace std;
int main() {
@PhDP
PhDP / main.cc
Created January 28, 2013 04:21
Random program generator for optimists
// Compile with g++ -std=c++11 -O2 main.c -o main
#include <iostream>
#include <fstream>
#include <string>
#include <random>
#include <ctime>
#include <cstdlib>
using namespace std;
getPrimes = f [2, 3] 5
f ps n max = if n >= max then
ps
else if pr n then
f (ps ++ [n]) (n + 2) max
else
f ps (n + 2) max
where pr y = foldl (&&) True (map (\x -> mod y x > 0) ps)
@PhDP
PhDP / primes.rb
Created November 13, 2012 15:32
primes
# Print a sequence of primes. It's not written to be fast, it's just to demonstrate
# than an infinite sequence can be compressed in a small program and thus have low
# (Kolmogorov) complexity.
def printprimes(max)
primes = []
for i in 2.. max do
primes << i if primes.inject(true) {|res, elt|
res and i % elt != 0
}
end
@PhDP
PhDP / abstract.txt
Created October 5, 2012 16:08
abstract?
Public preprint servers such as arXiv.org have become central to the scientific
process in fields such as physics, mathematics, and economics. These preprint
servers allow researchers to make their research rapidly available to the
broader community prior to peer review, which facilitates discussion, review,
and rapid communication of scientific results. Preprints are increasingly seen
as an important component of open science, because the research can be discussed
by the scientific community as soon as it is finished, instead of being
virtually hidden until officially published. However, in contrast to other
disciplines, the field of biology has effectively no preprint culture, with the
exception of small pockets of primarily highly quantitative research (e.g.,