Skip to content

Instantly share code, notes, and snippets.

View briochemc's full-sized avatar

Benoît Pasquier briochemc

View GitHub Profile
@briochemc
briochemc / Benchmark_backslash.jl
Created February 15, 2019 05:51
Benchmarking backslash in Julia with Float64, ComplexF64, and Dual128 (via DualMatrixTools.jl)
using LinearAlgebra, SparseArrays, SuiteSparse
using DualNumbers, DualMatrixTools
using BenchmarkTools
n = 1000 ;
y = rand(n) ;
A = sprand(n, n, 20/n) ;
i, j, v = findnz(A) ;
# Error check
using LinearAlgebra, SparseArrays, SuiteSparse, BenchmarkTools, MAT
M = matread("test_matrix.mat")["M"] ;
x = matread("test_matrix.mat")["x"] ;
Mf = factorize(M) ;
sol1 = Mf \ x ;
sol2 = M \ x ;
norm(x)
norm(x - M * sol1) / norm(x)
norm(x - M * sol1)
@briochemc
briochemc / playing_with_constants.jl
Last active February 27, 2019 01:18
Just a little snippet of code to remind me of the behaviour of constants in Julia
const x = 1.0 ;
function foo(y)
println("x = ", x)
end
foo("a string") # should print "x = 1.0"
# Invoking foo just JIT-compiled foo to use x = 1.0 if the argument is a string.
x = 2.0 ; # modify constant
using DualNumbers, LinearAlgebra, SparseArrays, SuiteSparse
n = 10
A = randn(n, n) # a real-valued matrix
B = randn(n, n) # another real-valued matrix
# Create a dual-valued matrix
M = A + ε * B
# And a sparse version of it
mutable struct ThreeVec <: AbstractVector{Float64}
x::Float64
y::Float64
z::Float64
end
Base.size(V::ThreeVec) = (3,)
Base.IndexStyle(::Type{<:ThreeVec}) = IndexLinear()
using Match
Base.getindex(V::ThreeVec, i::Int) = @match i begin
using VegaLite, DataFrames
df = DataFrame(x = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4], y = 1:10, style = ["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
list_styles = ["B", "A"]
mycolors = ["#000000", "#ee00ff"]
p = df |>
@vlplot(
mark={
# 1. Load packages
using DualMatrixTools, DualNumbers, SparseArrays
# 2. Create a random dual-valued vector x
n = 10
a, b = rand(n), rand(n) # real and dual parts
x = a + ε * b # dual-valued x
# 3. Create a sparse random dual-valued matrix M
using Makie
using AbstractPlotting
using FileIO, Colors
# Load World Ocean Atlas 2D fields for PO₄, Si(OH)₄, and NO₄
using WorldOceanAtlasTools
lat, lon, PO₄_2D = WorldOceanAtlasTools.WOA13_surface_map("p", 0, "1°")
lat, lon, NO₄_2D = WorldOceanAtlasTools.WOA13_surface_map("n", 0, "1°")
lat, lon, SiOH₄_2D = WorldOceanAtlasTools.WOA13_surface_map("i", 0, "1°")
# Add the image layer to each plot
using Makie
using GeometryTypes
wet3d = falses(2, 2, 2)
wet3d[[1, 2, 3, 5, 6]] .= true
shoebox3d = permutedims(wet3d, [2, 1, 3])
mycolor(x) = x ? RGBAf0(0,0,1,0.4) : RGBAf0(0.5,0.5,0,1)
Ind = CartesianIndices(size(shoebox3d))
@briochemc
briochemc / resample.jl
Last active May 28, 2019 07:50
For prafter resampling script in Julia
length(ARGS) == 1 && (sheet = "Sheet1")
# Use the installed packages
using ExcelFiles, DataFrames, Interpolations
# Replace by your file with just the data!
df = DataFrame(load(abspath(ARGS[1]), sheet))
println("Resampling $(ARGS[1]) - $sheet")