Skip to content

Instantly share code, notes, and snippets.

View JeffreySarnoff's full-sized avatar

Jeffrey Sarnoff JeffreySarnoff

View GitHub Profile
@JeffreySarnoff
JeffreySarnoff / benchmarktools_data.jl
Created February 22, 2020 12:22
Show how to get data from BenchmarkTools
using BenchmarkTools, Statistics
const BParams = BenchmarkTools.DEFAULT_PARAMETERS
BParams.evals = 1; # generally good to set as 1
BParams.samples = 2; # usually set to e.g. 3000, here it is 2 so result is viewable
avec = collect(1:8);
trial = @benchmark $avec .+ $avec;
#=
julia> trial = @benchmark $avec .+ $avec
@JeffreySarnoff
JeffreySarnoff / faster_reinterpreted_vectors.jl
Last active September 27, 2019 12:41
A more performant `reinterpret(::Type{Target}, x::Vector{Source}) where {T_arget, S_ource} `isbitstype(T) && isbitstype(S) && sizeof(T) === sizeof(S)`
#=
# follows code by Jacob Quinn in response to this
t = zeros(Int64, 100000);
@btime sum(t)
12.084 μs (0 allocations: 0 bytes)
0
t2 = reinterpret(Int64, zeros(UInt64, 100000));
@JeffreySarnoff
JeffreySarnoff / typestable.jl
Last active September 28, 2019 06:45
Find out if a function returns a single concrete type or returns one of a few bitstypes, so Julia can resolve quick dispatching over the type returned.
using Base: uniontypes
"""
is_type_stable(fn, argtypes)
`fn` returns values of a stable type
if `is_type_stable(fn, argtypes)`
then `y = fn(args)` supports fast dispatch on `y`
"""
@JeffreySarnoff
JeffreySarnoff / TurboColors.jl
Created August 26, 2019 04:00
Google's Turbo Color Palette
using Colors, FixedPointNumbers
const TurboColors = RGB{Normed{UInt8,8}}[
RGB{N0f8}(0.192,0.075,0.243),
RGB{N0f8}(0.196,0.082,0.259),
RGB{N0f8}(0.196,0.09,0.278),
RGB{N0f8}(0.2,0.094,0.294),
RGB{N0f8}(0.204,0.102,0.314),
RGB{N0f8}(0.204,0.11,0.329),
RGB{N0f8}(0.208,0.118,0.349),
@JeffreySarnoff
JeffreySarnoff / compactify.jl
Last active July 9, 2019 22:17
compactify a rational
#=
Can we make `compactify` run more quickly than it already does?
=#
#=
The algorithm for `compactify` is from Hiroshi Murakami's paper
Calculation of rational numbers in an interval whose denominator is the smallest
ACM Communications in Computer Algebra, Vol 48, No. 3, Issue 189, September 2014
Hiroshi Murakami, Department of Mathematics and Information Sciences,
@JeffreySarnoff
JeffreySarnoff / expmatrix.jl
Last active April 8, 2019 07:35
ArbNumerics supports `exp(matrix)`
#=
These examples use ArbNumerics v0.4.4, no earlier version supports `exp(matrix)`.
So, either first do `pkg> up` or do `pkg> rm ArbNumerics` then `pkg> add ArbNumerics`.
Sometimes, rebuilding with `pkg> build ArbNumerics` cleans up after itself.
All the matrix handling is new code, the examples here are working as of now.
If you find anything unclear or something awry, post an issue and
please use slack's direct message to draw my attention.
=#
#=
# within Base/mpfr.jl
function _duplicate(x::BigFloat)
z = BigFloat(; precision = precision(x))
ccall((:mpfr_set, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32), z, x, 0)
return z
end
function nextfloat!(x::BigFloat)
ccall((:mpfr_nextabove, :libmpfr), Int32, (Ref{BigFloat},), x) != 0
# https://gist.github.com/pfitzseb/240744ce79a9d7561c1277de7dbfa43e
module TraceCalls
using Cassette
mutable struct Trace
level::Int
cutoff::Int
end
@JeffreySarnoff
JeffreySarnoff / curriedcomparisons.md
Created January 31, 2019 01:05
curried comparisons

add in Base/operators.jl

"""
    !=(x)

Create a function that compares its argument to `x` using [`!=`](@ref), i.e.
a function equivalent to `y -> y != x`.
The returned function is of type `Base.Fix2{typeof(!=)}`, which can be
used to implement specialized methods.
@JeffreySarnoff
JeffreySarnoff / DayCountConventions.jl
Created January 22, 2019 23:33
another approach to encoding financial day counts
using Dates
const DTM = Union{Date, DateTime}
value(dtm::DTM) = dtm.instant.periods.value
abstract type AbstractDayCountConvention end
struct ActualActualConvention <: AbstractDayCountConvention end