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 / seam_carving.jl
Last active July 8, 2022 05:54 — forked from ricebunnyNL/seam_carving.jl
Optimized seam carving for Julia
function find_min_energy_map2(energy)
energy = transpose(energy)
row, col = size(energy)
dir_arr = [-1, 0, 1]
energy_map = zeros(row, col)
energy_map[:, end] = energy[:, end]
dirs = zeros(Int, row, col)
@inbounds for c = col-1:-1:1
@singularitti
singularitti / compare.jl
Last active May 25, 2021 09:33
Spglib helper
function compare_with_qe(result, qe_result, alat)
result = map(result * alat) do vec
map(v -> round(v; digits = 7), vec)
end
f(result) = sortperm(DataFrame(hcat(result...)', [:x, :y, :z]), [:x, :y, :z])
g(result) = result[sortperm(DataFrame(hcat(result...)', [:x, :y, :z]), [:x, :y, :z])]
# result_df = sortperm(DataFrame(hcat(result...)', [:x, :y, :z]), [:x, :y, :z])
# qe_df = sortperm(DataFrame(hcat(qe_result...)', [:x, :y, :z]), [:x, :y, :z])
# result_df, qe_df
symdiff(result, qe_result)
@singularitti
singularitti / spglib_get_kmesh.py
Last active April 19, 2021 00:00
Spglib get k-mesh #Python #crystallography
import collections
def irrmesh(mapping, grid, shift = [0, 0, 0]):
shift = np.array(list(map(lambda x: 0.5 if x == 1 else 0, shift)))
count = collections.Counter(mapping)
result = []
for ir_gp_id, gp in zip(np.unique(mapping), grid[np.unique(mapping)]):
result.append(np.append((gp.astype(float) + shift) / mesh, count[ir_gp_id]).tolist())
return np.array(result)
@singularitti
singularitti / struct_equal.jl
Created January 15, 2021 22:46
Compare equality of 2 `struct`s #Julia
# See https://github.com/JuliaLang/julia/issues/4648#issuecomment-761030051
# ASSUMTION: `e1` and `e2` have the same run-time type
@generated structEqual(e1, e2) = begin
if fieldcount(e1) == 0
return :(true)
end
mkEq = fldName -> :(e1.$fldName == e2.$fldName)
# generate individual equality checks
eqExprs = map(mkEq, fieldnames(e1))
# construct &&-expression for chaining all checks
@singularitti
singularitti / mercury_trail.jl
Created December 29, 2020 06:20
Mercury trail #Julia #physics
using Plots
# https://www.zhihu.com/question/22477205/answer/21521946
earth(t) = (cos(2pi * t), sin(2pi * t))
mercury(t) = 0.387 .* (cos(26.1t), sin(26.1t)) .- earth(t)
mercury2(t) = (
(0.387cos(26.1t) - cos(2t * pi)) * cos(730t * pi) -
(0.387sin(26.1t) - sin(2t * pi)) * sin(730t * pi),
@singularitti
singularitti / merge_graph.jl
Last active December 7, 2022 19:48
Merge 2 graphs #Julia #graph
function merge(g, b)
a = copy(g)
add_vertices!(a, nv(b))
for e in edges(h)
add_edge!(a, src(e) + nv(g), dst(e) + nv(g))
end
return a
end
@singularitti
singularitti / execute.jl
Created September 5, 2020 05:36
Execute shell commands in Julia wrapper #Julia
# From https://discourse.julialang.org/t/better-support-for-running-external-commands/44933/26
function execute(cmd::Cmd)
out = Pipe()
err = Pipe()
process = run(pipeline(ignorestatus(cmd), stdout = out, stderr = err))
close(out.in)
close(err.in)
stdout = @async String(read(out))
@singularitti
singularitti / bench.jl
Created June 11, 2020 10:11
Parametric types which add or delete fields #Julia #benchmark
# Parametric types which add or delete fields
# from https://groups.google.com/forum/#!msg/julia-users/jUMu9A3QKQQ/qjgVWr7vAwAJ
using BenchmarkTools
struct Container1{T}
val::T
end
inc(::Int) = 1
inc(::Float64) = 2
@singularitti
singularitti / iosevka-ss.jl
Last active July 20, 2020 01:13
Add iosevka-ss family fonts to Homebrew Cask #Julia #font
using Suppressor
const version = v"3.1.1"
map(1:13) do i
ext = lpad(i, 2, "0")
file = cp(
"Casks/font-iosevka-ss14.rb",
"Casks/font-iosevka-ss$ext.rb",
)
@singularitti
singularitti / vscode.json
Last active July 1, 2020 12:21
在 VSCode 的 LaTeX Workshop 插件中使用 LaTeXmk #VSCode #LaTeX
{
"latex-workshop.latex.tools": [
{
"name": "XeLaTeXmk",
"command": "latexmk",
"args": [
"-xelatex",
"-synctex=1",
"-shell-escape",
"-interaction=nonstopmode",