{{ message }}

Instantly share code, notes, and snippets.

# Nicolas P. Rougier rougier

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 }
Created Feb 28, 2016
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))
Created Nov 3, 2016
Numpy find view
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
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
Created Nov 27, 2016
Matplotlib / individual rotation markers using a single path
View marker-rotation.py
 # Matplotlib / individual rotation markers using a single path import numpy as np import matplotlib.pyplot as plt from matplotlib.path import Path from matplotlib.animation import FuncAnimation from matplotlib.collections import PathCollection triangle = [[(-0.25, -0.5), (+0.0, +0.5), (+0.25, -0.5), (+0.0, +0.0)],
Created Feb 17, 2017 — forked from jakevdp/convolution_matrix.py
Convolution Matrix
View convolution_matrix.py
 # Author: Jake VanderPlas # LICENSE: MIT from __future__ import division import numpy as np def convolution_matrix(x, N=None, mode='full'): """Compute the Convolution Matrix
Last active Feb 24, 2017
Cellular automata 1D
View cellular-automata-1d.py
 # Copyright (2017) Nicolas P. Rougier - BSD license # Twitter version (140 characters) # -------------------------------- _='0' R,C="{:08b}".format(30),_*9+'1'+_*9 for k in range(9): print(C) C = _+''.join([R[7-eval('0b'+C[i:i+3])] for i in range(len(C)-2)])+_
Created Sep 16, 2017 — forked from MorganBorman/main.py
A short example of how to use vertex array objects in PyOpenGL
View main.py
 import OpenGL.GL as GL import OpenGL.GL.shaders import ctypes import pygame import numpy vertex_shader = """ #version 330 in vec4 position;
Created Jul 7, 2018 — forked from stucchio/beta_bandit.py
The beta-distribution based bayesian bandit algorith,.
View beta_bandit.py
 from numpy import * from scipy.stats import beta class BetaBandit(object): def __init__(self, num_options=2, prior=(1.0,1.0)): self.trials = zeros(shape=(num_options,), dtype=int) self.successes = zeros(shape=(num_options,), dtype=int) self.num_options = num_options self.prior = prior