Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:

Nicolas P. Rougier rougier

:octocat:
View GitHub Profile
View .emacs.el
(defun arrow-right-xpm (color1 color2)
"Return an XPM right arrow string representing."
(format "/* XPM */
static char * arrow_right[] = {
\"12 18 2 1\",
\". c %s\",
\" c %s\",
\". \",
\".. \",
\"... \",
@rougier
rougier / prefix.py
Created Jan 10, 2016
SI and IEC prefix for printing human readable numbers
View prefix.py
# Copyright (c) 2016 Nicolas P. Rougier - BSD License
# SI prefixes as name:value
SI_prefix_name_value = {
"yocto": 10e-24, "zepto": 10e-21, "atto": 10e-18, "femto": 10e-15,
"pico": 10e-12, "nano": 10e-9, "micro": 10e-6, "milli": 10e-3,
"centi": 10e-2, "deci": 10e-1, "deca": 10e1, "hecto": 10e2,
"kilo": 10e3, "mega": 10e6, "giga": 10e9, "tera": 10e12,
"peta": 10e15, "exa": 10e18, "zetta": 10e21, "yotta": 10e24 }
@rougier
rougier / glut-cube.py
Created Jan 20, 2016
Rotating color cube using modern GL, glut and python
View glut-cube.py
# -----------------------------------------------------------------------------
# Copyright (c) 2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import sys
import math
import ctypes
import numpy as np
import OpenGL.GL as gl
import OpenGL.GLUT as glut
@rougier
rougier / progress_bar.py
Created Jan 25, 2016
A progress bar using unicode character for smoother display
View progress_bar.py
# -----------------------------------------------------------------------------
# Copyright (c) 2016, Nicolas P. Rougier
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import sys, math
def progress(value, length=40, title = " ", vmin=0.0, vmax=1.0):
"""
Text progress bar
@rougier
rougier / binomial.py
Created Jan 25, 2016
A fast way to calculate binomial coefficients in python (Andrew Dalke)
View binomial.py
def binomial(n, k):
"""
A fast way to calculate binomial coefficients by Andrew Dalke.
See http://stackoverflow.com/questions/3025162/statistics-combinations-in-python
"""
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in xrange(1, min(k, n - k) + 1):
ntok *= n
@rougier
rougier / admonition.el
Created Feb 28, 2016
Some visible admonitions for emacs
View admonition.el
(make-face 'face-admonition)
(set-face-attribute 'face-admonition nil
:height 120
:weight 'regular
:foreground "white"
:background "light gray"
:box '(:line-width 1 :color "light gray"))
(defvar face-admonition 'face-admonition)
(make-face 'face-admonition-note)
@rougier
rougier / maze.py
Created Apr 20, 2016
Generate a maze with customizable complexity and density
View maze.py
import numpy as np
import matplotlib.pyplot as plt
def maze(shape=(64,64), complexity=.95, density = 1):
# Only odd shapes
shape = ((shape[0]//2)*2+1, (shape[1]//2)*2+1)
# Adjust complexity and density relative to maze size
complexity = int(complexity*(5*(shape[0]+shape[1])))
density = int(density*(shape[0]//2*shape[1]//2))
@rougier
rougier / fractal-dimension.py
Last active Oct 15, 2021
Fractal dimension computing
View fractal-dimension.py
# -----------------------------------------------------------------------------
# From https://en.wikipedia.org/wiki/Minkowski–Bouligand_dimension:
#
# In fractal geometry, the Minkowski–Bouligand dimension, also known as
# Minkowski dimension or box-counting dimension, is a way of determining the
# fractal dimension of a set S in a Euclidean space Rn, or more generally in a
# metric space (X, d).
# -----------------------------------------------------------------------------
import scipy.misc
import numpy as np
View numpy_find_view.py
def find_view(base, view):
"""
Given an array that is a `view` of a `base`, find an index such that
`base[index] is view`
"""
if not isinstance(view, np.ndarray):
return "..."
itemsize = view.itemsize
@rougier
rougier / colorized_voronoi.py
Created Nov 13, 2016 — forked from pv/colorized_voronoi.py
Colorized Voronoi diagram with Scipy, in 2D, including infinite regions
View colorized_voronoi.py
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi
def voronoi_finite_polygons_2d(vor, radius=None):
"""
Reconstruct infinite voronoi regions in a 2D diagram to finite
regions.
Parameters