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 / showall.jl
Last active January 25, 2019 11:42
Show all elements of a `Vector` #Julia #IO
# Referenced from
# https://stackoverflow.com/questions/49304329/how-to-show-all-elements-of-vectors-and-matrices-in-julia
function showall(io, x, limit = true)
println(io, summary(x), ":")
Base.print_matrix(IOContext(io, :limit => limit), x)
end
@singularitti
singularitti / remove duplicate pdf pages.py
Created March 6, 2019 08:39
Remove duplicate PDF pages #Python #PDF
# function: remove pause frames of a beamer PDF
from pdfrw import PdfReader, PdfWriter
def run_stage(src, out):
i = PdfReader(src)
o = PdfWriter()
sum_i = len(i.pages)
num_i = i.Root.PageLabels.Nums
for r in range(1, len(num_i) // 2):
@singularitti
singularitti / check_vcrelax_fini.sh
Last active April 8, 2019 02:33
Check whether vc-relax of QE is finished #Shell #QE #tool
#!/usr/bin/env bash
# This script checks whether the vc-relax output gives correct result,
# by examining if there is "End final coordinates" in the output.
# Helpful guides: https://github.com/koalaman/shellcheck/wiki/SC2045,
# http://jafrog.com/2013/11/23/colors-in-terminal.html,
# and https://github.com/koalaman/shellcheck/wiki/SC2068
folders=()
for folder in "$@"; do
@singularitti
singularitti / grep_final_cell.sh
Last active April 8, 2019 02:32
Grep final optimized cell from vc-relax #Shell #QE
#!/usr/bin/env bash
# Helpful guides: https://github.com/koalaman/shellcheck/wiki/SC1065
# and: https://superuser.com/questions/748724/pass-a-large-string-to-grep-instead-of-a-file-name
folders=()
if [ -f finalcell ]; then
rm finalcell
else
touch finalcell
fi
@singularitti
singularitti / deepflatten.jl
Last active April 1, 2019 00:31
Unfold a nested Julia array #Julia #array
# For a detailed explanation, please see here:
# https://discourse.julialang.org/t/how-do-you-unfold-a-nested-julia-array/2243/7?u=singularitti
function deepflatten(arr::Vector{<: Vector})
dim = [1]
function recursiveflatten(arr, dim)
if arr isa Vector{<: Vector}
recursiveflatten(collect(Iterators.flatten(arr)),
pushfirst!(dim, length(arr) / prod(dim)))
else
@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 / crossProduct.js
Created April 7, 2019 05:16
Compute cross product of 2 3D arrays #JavaScript #math #array
function crossProduct(arr1, arr2) {
/*
Here arr1, arr2 are both 1D arrays.
*/
let u1 = arr1[0];
let u2 = arr1[1];
let u3 = arr1[2];
let v1 = arr2[0];
let v2 = arr2[1];
let v3 = arr2[2];
@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 / 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 / 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;