Skip to content

Instantly share code, notes, and snippets.

View singularitti's full-sized avatar
:octocat:
WFH

Qi Zhang singularitti

:octocat:
WFH
View GitHub Profile
@singularitti
singularitti / euler_angle.wl
Last active April 7, 2019 05:19
Rotation by Euler angle #Mathematica #mechanics
Clear["Global`*"]
Rx[\[Phi]_] := MatrixExp[-I \[Phi] {{0, 0, 0}, {0, 0, -I}, {0, I, 0}}]
Ry[\[Phi]_] := MatrixExp[-I \[Phi] {{0, 0, I}, {0, 0, 0}, {-I, 0, 0}}]
Rz[\[Phi]_] := MatrixExp[-I \[Phi] {{0, -I, 0}, {I, 0, 0}, {0, 0, 0}}]
@singularitti
singularitti / gitlg.sh
Last active April 7, 2019 05:25
Set Git log formatting #Git #log #formatting
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
@singularitti
singularitti / meshgrid3D.js
Created April 7, 2019 05:21
Meshgrid on 3D #JavaScript #array
function meshgrid(xArray, yArray, zArray) {
/*
Here xArray, yArray, zArray should all be 1d arrays.
Then it returns 3 fortran-style 3D arrays, that is,
they are all column-major order:
http://www.wikiwand.com/en/Row-_and_column-major_order.
The returned xMesh, yMesh, and zMesh are all of shape
[zNum, xNum, yNum].
*/
let xNum = xArray.size;
@singularitti
singularitti / flattenArray.js
Last active April 7, 2019 05:27
Flatten multidimensional array #JavaScript #array
function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
@singularitti
singularitti / reshapeArray.js
Last active April 7, 2019 05:27
Reshape array #JavaScript #array
function reshape(oldNdarr, newShape) {
/*
Here oldNdarray is a ndarray, newShape is an array spcifying the newer one's shape.
*/
return ndarray(oldNdarr.data, newShape);
}
@singularitti
singularitti / meshgrid2D.js
Created April 7, 2019 05:21
Meshgrid on 2D #JavaScript #array
function meshgrid(xArray, yArray) {
/*
Here xArray, yArray should all be 1d arrays.
Then it returns 2 fortran-style 2D arrays, that is,
they are all column-major order:
http://www.wikiwand.com/en/Row-_and_column-major_order.
The returned xMesh and yMesh are all of shape [xNum, yNum].
*/
let xNum = xArray.size;
let yNum = yArray.size;
@singularitti
singularitti / Slice.String.hs
Created April 7, 2019 05:34
Slice a string in Haskell #Haskell #string
-- from [StackOverflow](https://stackoverflow.com/questions/4597820/does-haskell-have-list-slices-i-e-python)
takeStep :: Int -> [a] -> [a]
takeStep _ [] = []
takeStep n (x:xs) = x : takeStep n (drop (n-1) xs)
slice :: Int -> Int -> Int -> [a] -> [a]
slice start stop step = takeStep step . take (stop - start) . drop start
@singularitti
singularitti / linspace.js
Created April 7, 2019 05:30
Make an array with linear spacing #JavaScript #array
function linspace(shape, start, end, options) {
/*
If we use ndarray-linspace package,
it returns a ndarray with dtype='array',
but this dtype cannot be used by ndarray-tile package, it needs 'float'.
So we need to transform dtype manually.
*/
let tmp = linspace(ndarray([], shape), start, end, options);
return reshape(ndarray(new Float64Array(tmp.data)), shape);
}
@singularitti
singularitti / fib.wl
Created April 7, 2019 05:51
Fibonacci sequence #Mathematica #math #recursive
fib[n_] := Which[n == 0, 0, n == 1, 1, n > 1, fib[n - 1] + fib[n - 2]]
@singularitti
singularitti / iter_islast.py
Created April 7, 2019 22:45
Generates pairs where the first element is an item from the iterable source and the second element is a boolean flag indicating if it is the last item in the sequence. #Python #iterate
def iter_islast(iterable):
"""
iter_islast(iterable) -> generates (item, islast) pairs
Generates pairs where the first element is an item from the iterable
source and the second element is a boolean flag indicating if it is
the last item in the sequence.
Referenced from
`here <http://code.activestate.com/recipes/392015-finding-the-last-item-in-a-loop/>`_.