Skip to content

Instantly share code, notes, and snippets.

View singularitti's full-sized avatar
🎯
Focusing

Qi Zhang singularitti

🎯
Focusing
View GitHub Profile
@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 / 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 / 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 / plotGrid.wl
Last active January 26, 2024 19:28
Grid plotting #Mathematica #plotting
Options[plotGrid] = {ImagePadding -> 40};
plotGrid[l_List, w_, h_, opts : OptionsPattern[]] :=
Module[{nx, ny, sidePadding = OptionValue[plotGrid, ImagePadding],
topPadding = 0, widths, heights, dimensions, positions, frameOptions
= FilterRules[{opts}, FilterRules[Options[Graphics], Except[{ImagePadding,
Frame, FrameTicks}]]]},
{ny, nx} = Dimensions[l];
widths = (w - 2 sidePadding) / nx Table[1, {nx}];
widths[[1]] = widths[[1]] + sidePadding;
@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 / soundVelocityForCubic.wl
Last active September 13, 2019 20:13
Calculate sound velocity from phonon dispersion for a cubic crystal #Mathematica #materials-science #solid-state #physics
Clear["Global`*"]
GreenChristoffelEquations[{ qx_, qy_, qz_ }] := {{
c11 qx ^ 2 + c44(qy ^ 2 + qz ^ 2), (c12 + c44) qx qy, (c12 + c44) qx qz
}, {
(c12 + c44) qx qy, c11 qy ^ 2 + c44(qz ^ 2 + qx ^ 2), (c12 + c44) qy qz
}, {
(c12 + c44) qx qz, (c12 + c44) qy qz, c11 qz ^ 2 + c44(qx ^ 2 + qy ^ 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/>`_.
@singularitti
singularitti / listdir_nohidden.py
Last active April 8, 2019 19:21
List everything except hidden files/folders under a certain path #Python #iterate #OS
def listdir_nohidden(path):
for f in os.listdir(path):
if not f.startswith('.'):
yield f
@singularitti
singularitti / plot_dos_pdos.py
Created April 9, 2019 04:27
Plot DOS and PDOS of Fe read from QE's output #QE #tool #plotting #Python
#!/usr/bin/env python -u -i
import os
import re
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.ticker import ScalarFormatter
@singularitti
singularitti / jacobian_matrix.wl
Created May 5, 2019 07:37
Jacobian matrix #math #Mathematica
(* Taken from http://mathworld.wolfram.com/Jacobian.html *)
JacobianMatrix[f_List?VectorQ, x_List] := Outer[D, f, x] /; Equal @@ (Dimensions /@ {f, x})
JacobianDeterminant[f_List?VectorQ, x_List] := Det[JacobianMatrix[f, x]] /; Equal @@ (Dimensions /@ {f, x})