Skip to content

Instantly share code, notes, and snippets.

View briochemc's full-sized avatar

Benoît Pasquier briochemc

View GitHub Profile
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={
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 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
@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
# 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 / 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) ;
# Example function to find the root of
f(x) = [(x[1] + x[2] + 1)^3, (x[1] - x[2] - 1)^3]
function f_Jac!(J, x)
J[1, 1] = 3(x[1] + x[2] + 1)^2
J[2, 1] = 3(x[1] - x[2] - 1)^2
J[1, 2] = 3(x[1] + x[2] + 1)^2
J[2, 2] = -3(x[1] - x[2] - 1)^2
return J
end
f1(x) = x
f2(x) = 2x
f3(x) = 3x
list_methods = [
("method1", :f1)
("method2", :f2)
("method3", :f3)
]
load Downloads/CTL.mat
T = output.TR ;
f1 = figure ;
spy(T, 'w') ;
darkBackground(f1, [0 0 0], [1 1 1])
# Here I store more data as I go,
# which I could use to plot figures comparing methods
# the convergence speed of different methods
# I use the Rosenbrock classic example and
# Optim.jl as an iterative process which will
# call f multiple times to find its minimum
# and I plot the convergence vs time
using Optim, Cassette