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 / 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 / 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 / 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 / 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 / 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.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 / fizzbuzz.hs
Created June 28, 2013 17:50
fizzbuzz
fizzbuzz a b x = [fb i | i <- [1..x]]
where
ab = a * b
fb n
| mod n ab == 0 = "FizzBuzz"
| mod n a == 0 = "Fizz"
| mod n b == 0 = "Buzz"
| otherwise = show n
main = mapM_ print $ fizzbuzz 5 7 1000
@PhDP
PhDP / yesod-ubuntu13.04.md
Last active December 19, 2015 02:59
Making Yesod work on Ubuntu 13.04

For a project named XXX with a postgreSQL database:

  1. $ sudo apt-get install libedit-dev libbsd-dev libgmp3-dev zlib1g-dev freeglut3-dev postgresql pgadmin3 libghc-persistent-postgresql-dev cabal-install
  2. Install all packages for 'yesod'.
  3. $ cabal update
  4. $ cd XXX && cabal install && yesod devel
@PhDP
PhDP / scriptoria-mockup.html
Last active December 19, 2015 03:49
A first (failed) design for Scriptoria based on a Bootstrap example. I like the Orange/Red theme (revolution!!!!), and I like the idea of having each article in a small touch-friendly orange box, but it's very hard to read... UPDATE: I now use a less obnoxious color theme.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>Scriptoria -- a database of scientific documents written with distributed revision control systems.</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
@PhDP
PhDP / lux
Last active December 20, 2015 06:29
Ruby script to set the brightness on my Dell XPS (the normal way fails after Ubuntu 13.04).
#!/usr/bin/env ruby
if ARGV.length != 1 then
puts 'Provide a single argument between 0.1 (nearly black) and 1.0 (max brigthness).'
exit(42)
end
b = ARGV[0].to_f
b = (if b < 0.1 then 0.1 elsif b > 1.0 then 1.0 else b end).to_s