Skip to content

Instantly share code, notes, and snippets.

View JeffreySarnoff's full-sized avatar

Jeffrey Sarnoff JeffreySarnoff

View GitHub Profile
@JeffreySarnoff
JeffreySarnoff / SingletonZero.jl
Last active November 29, 2023 00:08
provide a dispatchable singleton Zero
struct ZERO end
const Zero = ZERO()
# to display Zero as Zero
const ShowZero = :Zero
# to display Zero as 0
# const ShowZero = 0
Base.show(io::IO, x::ZERO) = print(io, ShowZero)
Base.zero(::ZERO) = 0
# very small subset of data, for testing
N = 5 # people
T = 3 # times
data = Float32.([
# x y z
1.75 0.0 0.75 # person 1 time 1
2.75 1.75 0.75 # person 1 time 2
1.25 2.75 -0.75 # person 1 time 3
@JeffreySarnoff
JeffreySarnoff / ShowAndTell(c).jl
Last active November 22, 2022 10:39
examples (c, local n most recent observations) from the redesign of RollingFunctions.jl
# see example at line 80
using StatsBase
function StatsBase.median(a::T, b::T, c::T) where {T}
b, c = minmax(b, c)
a, c = minmax(a, c)
max(a, b)
end
@JeffreySarnoff
JeffreySarnoff / ShowAndTell(b).jl
Last active November 22, 2022 08:07
examples (b) from the redesign of RollingFunctions.jl
# see example at line 70
# see accumulator enhancement at line 70
using StatsBase
abstract type Accumulator{T} <: Function end
const AccNum = Float64 # client settable via ENV
StatsBase.nobs(acc::Accumulator) = acc.nobs
@JeffreySarnoff
JeffreySarnoff / ShowAndTell(a).jl
Last active November 22, 2022 04:15
examples (a) from the redesign of RollingFunctions.jl
# see example at line 60
using StatsBase
abstract type Accumulator{T} <: Function end
const AccNum = Float64 # client settable via ENV
StatsBase.nobs(acc::Accumulator) = acc.nobs
@JeffreySarnoff
JeffreySarnoff / compare_shifts.jl
Last active September 19, 2022 12:01
compare shifting up and down
function shiftup(v::AbstractVector{T}, start, finish) where {T}
for i in finish:-1:start
@inbounds v[i+1] = v[i]
end
end
function shiftup4(v::AbstractVector{T}, start, finish) where {T}
n = finish - start
q, r = fldmod(n, 4)
@inbounds for i in finish-3:-4:start+r
@JeffreySarnoff
JeffreySarnoff / arm_log.jl
Last active August 19, 2022 03:40
Arm log function
#=
* Data for log.
*
* Copyright (c) 2018, Arm Limited.
* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
=#
const OneAsUInt64 = 0x3ff0000000000000
const LOG_TABLE_BITS = 7
@JeffreySarnoff
JeffreySarnoff / firstindex_study.jl
Last active February 15, 2021 12:46
benchmarking firstindex
using BenchmarkTools
const BT=BenchmarkTools.DEFAULT_PARAMETERS;
BT.samples=60_000; BT.time_tolerance=1/8192;
#=
IS{Int64} == Union{Int64, Symbol}
IS{Int32} == Union{Int32, Symbol}
=#
const IS = Union{I,Symbol} where {I<:Integer}
# test vars, length(chrs) == 96
@JeffreySarnoff
JeffreySarnoff / timingexample.md
Last active December 30, 2020 01:22
notes on timing an example from twitter

re: https://twitter.com/SimonDeDeo/status/1344031423704006656

You are not timing the same thing in each language. See below for what you were timing in Julia.
And, as has been mentioned, use @time or @btime from BenchmarkTools.jl (time does not do what you expect it to do). When using @time, run the timing once to precompile the test before considering the results valid. The first run may take considerably longer, and that extra time is not computing time. I have omitted those lines below.

@JeffreySarnoff
JeffreySarnoff / nancatcher.jl
Last active December 19, 2020 09:11
Trap NaN where generated
# provide context when a NaN is generated
# IMPORTANT!! define these before overloading the arithmetic functions
function nanfound(fncall)
stk = stacktrace() # stacktrace(catch_backtrace())
stk = relevantframes(stk)
info = []
for frame in stk
push!(info, frameinfo(frame))
end