Skip to content

Instantly share code, notes, and snippets.

@denis-bz
denis-bz / less-or-grep.sh
Created May 27, 2014 11:16
Make files "live", e.g. ~/bin/todo ~/bin/books ..., by sourcing this tiny script
#!/bin/sh
# less-or-grep.sh 2014-05-25 may denis
case $1 in -h* | --h* ) # help
exec cat <<!
To make files "live", e.g. ~/bin/todo ~/bin/books ...
add this line at the top:
source less-or-grep.sh # file -> less file, file 'pattern' -> egrep
and
@denis-bz
denis-bz / cg.py
Last active August 29, 2015 14:04
cg.py for students, from Jon F. Claerbout's nice, clear [Conjugate Gradient Tutorial](http://sepwww.stanford.edu/theses/sep48/48_25.pdf) 1986, 7p: "present the geometrical conceps and a one-page program."
#!/usr/bin/env python
""" conjugate gradient cg( A, x, Res )
in: A: an array, numpy dense or scipy.sparse
or sparse LinearOperator with .matvec .rmatvec
x: initial guess, 0 or ...
Res = Y - A x before calling cg()
iter: x += step
Res -= S *inplace*
from Jon F. Claerbout's nice, clear
@denis-bz
denis-bz / Nd-testfuncs-python.md
Last active March 4, 2024 06:23
N-dimensional test functions for optimization, in Python
@denis-bz
denis-bz / Leastsq_bounds.py
Last active September 9, 2020 12:48
leastsq_bounds, a wrapper for scipy.optimize.leastsq with box bounds
# leastsq_bounds.py
# see also test_leastsq_bounds.py on gist.github.com/denis-bz
from __future__ import division
import numpy as np
from scipy.optimize import leastsq
__version__ = "2015-01-10 jan denis" # orig 2012
@denis-bz
denis-bz / Iterative-learning-control.py
Last active February 28, 2024 03:15
A simple example of ILC, Iterative learning control, for "learning" a control curve
""" a simple example of ILC
from Moore, Iterative learning control: a tutorial and big picture 2006 6p
"""
# https://en.wikipedia.org/wiki/Iterative_learning_control
# https://en.wikipedia.org/wiki/LTI_system_theory
from __future__ import division
import sys
import numpy as np
@denis-bz
denis-bz / Mixed-order-interpolation.md
Last active August 29, 2015 14:17
Interpolation between order 1 (bilinear, trilinear ...) and order 0 (nearest corner) on a box grid

Mixed order interpolation

Interpolating e.g. wind or water temperature on a box grid in d dimensions usually looks at (order+1)^d neighbors of each point. Order 1, bilinear trilinear ... looks at all 2^d corners of the box around each data point; 2^d grows pretty fast. Order 0, though, looks at only 1 the nearest corner, so is blocky, discontinuous at box edges.

What's in between ? A simple method is

@denis-bz
denis-bz / Fitline.md
Last active June 8, 2016 15:10
Fitline: fit a line to data, print plot trim the biggest residuals, in Python

Fitline: fit a line to data, print plot trim the biggest residuals, in Python

Example:

from fitline import Fitline  # a class with functions fit(), line(), plot(), trim(), trimfit()

F = Fitline( level=.95, nbig=5, verbose=1, axis=None, xline=None )  # the defaults
F.fit( x, y )  # -> f .a .b .residuals ...

a, b = F.a, F.b # y ~= a + b * x

@denis-bz
denis-bz / nanutil.py
Created July 22, 2015 11:10
nanutil.py: corr, cosine similarity, linterpol skipping missing data
#!/usr/bin/env python2
""" nanutil.py: corr, cosine similarity, linterpol ignoring missing data (NaNs)
A "NaN" is a marker for missing data, aka NA, Not Available, in numpy / pandas data arrays.
(Technically, it's a special, "sticky" floating point value, with e.g.
NaN + anything = NaN; see https://en.wikipedia.org/wiki/NaN .)
Numpy has a handful of functions that ignore NaNs, for example
`np.nanmean([ 1, np.NaN, 3 ]) == 2`.
Here are a few more, short and straightforward:
@denis-bz
denis-bz / Cookies.txt
Created September 14, 2015 10:15
Several dozen cookies: e.g. `random-paragraph cookies` in your `.login`
source less-or-grep.sh # this file -> less, file 'pattern' -> egrep
Alice: "Would you tell me, please, which way I ought to go from here ?"
"That depends a good deal on where you want to get to," said the Cat.
"I don't know where ...," said Alice.
"Then it doesn't matter which way you go," said the Cat.
On the planet Earth, man had always assumed he was more intelligent than dolphins
because he had achieved so much -- the wheel, New York, wars and so on --
while all the dolphins had ever done was muck about in the water having a good time.
@denis-bz
denis-bz / Between-trapezoid-Simpson-frequency-response.md
Created October 5, 2015 09:21
Between-trapezoid-Simpson-frequency-response

Between trapezoid and Simpson integration: frequency response

Hamming, Digital Filters pages 66-69, shows that "the frequency point of view ... sheds a new light on classical numerical integration formulas."
The plot and code below show the frequency response of T (trapezoid), S (Simpson), and some combinations like .2 T + .8 S which give a good improvement, easily.