Skip to content

Instantly share code, notes, and snippets.

View MasonProtter's full-sized avatar

Mason Protter MasonProtter

View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
using InteractiveUtils, Pkg
for pk in ["Transducers", "LoopVectorization"]
if pk ∉ keys(Pkg.project().dependencies)
Pkg.add(pk)
end
end
using Transducers, LoopVectorization
function picalc_kernel(r, a, b)
using BenchmarkTools
function _pi(x::Vector, y::Vector)
acc = 0
@inbounds @simd for i ∈ eachindex(x)
acc += (x[i]*x[i] + y[i]*y[i]) < 1.0
end
4acc/length(x)
end
using BenchmarkTools, StaticArrays, Transducers
function _pi_kernel_static()
xs = @SVector rand(50)
ys = @SVector rand(50)
sum(((x, y),) -> x^2 + y^2 < 1.0, zip(xs, ys))
end
function _pi_kernel(r)
xs = rand(r)
ys = rand(r)
module Syms
export sym, Sym
#--------------------------------------------------------------------------------
# Set up some symbolic types
#--------------------------------------------------------------------------------
abstract type Symbolic{T} end # Symbolic{T} will act like it is <: T
struct Sym{T} <: Symbolic{T}
name::Symbol
#+BEGIN_SRC jupyter-julia
using Interpolations
xs = range(-10, 10, length=50) # 50 evenly spaced points from -5 to 5
# model function, some Gaussian
f(x; A=1, σ=2, b=0) = A * exp(-x^2/2σ) + b
ys = f.(xs)
method = BSpline(Cubic(Line(OnGrid()))) #interpolation method
f_itp = scale(interpolate(ys, method), xs) # interpolation function
#+BEGIN_SRC julia
const ketpat = r"\|.*?\>"
ketrep(str) = "Ket("*(match(r"(?<=\|).*?(?=>)", str).match)*")"
const brapat = r"\<.*?\|"
brarep(str) = "Bra("*(match(r"(?<=<).*?(?=\|)", str).match)*")"
function rep_braket(str)
replace(replace(str, brapat => brarep), ketpat => ketrep)
end
#+BEGIN_SRC jupyter-julia
using CUDA
struct StaticString{N} <: AbstractString
chars::NTuple{N, Char}
end
macro s_str(s)
chars = tuple(collect(s)...)
N = length(chars)
esc(:(StaticString{$N}($chars)))
end