This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flatten(arr) { | |
return arr.reduce(function (flat, toFlatten) { | |
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten); | |
}, []); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reshape(oldNdarr, newShape) { | |
/* | |
Here oldNdarray is a ndarray, newShape is an array spcifying the newer one's shape. | |
*/ | |
return ndarray(oldNdarr.data, newShape); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fib[n_] := Which[n == 0, 0, n == 1, 1, n > 1, fib[n - 1] + fib[n - 2]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/>`_. |