Skip to content

Instantly share code, notes, and snippets.

@matthieubulte
matthieubulte / gadfly_animate.jl
Last active January 21, 2022 08:43
Animations in Gadfly
using Gadfly
import Cairo, Fontconfig
using Printf
using FFMPEG
#################################### This is pretty much the same code as in https://github.com/JuliaPlots/Plots.jl/blob/master/src/animation.jl
struct Animation
dir::String
frames::Vector{String}
kwargs::Iterators.Pairs
(* Good morning everyone, I'm currently learning ocaml for one of my CS class and needed to implement
an avl tree using ocaml. I thought that it would be interesting to go a step further and try
to verify the balance property of the avl tree using the type system. Here's the resulting code
annotated for people new to the ideas of type level programming :)
*)
(* the property we are going to try to verify is that at each node of our tree, the height difference between
the left and the right sub-trees is at most of 1. *)
### A Pluto.jl notebook ###
# v0.12.21
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
@matthieubulte
matthieubulte / main.jl
Created April 24, 2021 17:31
Symbolic expression from a string
using MacroTools
using Symbolics
function parse(strexpr)
expr = Meta.parse(strexpr)
freevars = Set()
expr = MacroTools.postwalk(x -> begin
if isa(x, Symbol) && !isdefined(Base, x)
push!(freevars, Variable(x))
Expr(:call, Variable, QuoteNode(x))
@matthieubulte
matthieubulte / main.jl
Created April 2, 2021 22:56
Using Symbolics.jl to compile sparse matrix operations
using LinearAlgebra, BenchmarkTools, SparseArrays, SymbolicUtils, Symbolics
## Problem setup
const z = zeros;
wx = Float64[ 0 -1 0;
1 0 0;
0 0 0;];
Ak_1 = [ wx -I zeros(3,12);
@matthieubulte
matthieubulte / main.jl
Created March 26, 2021 22:03
Compiling sparse matrix multiplication
using LinearAlgebra
using BenchmarkTools
using SparseArrays
# To exploit the sparsity of the matrix as much as possible, pre-compile the multiplication operation
# to skip the matrix entries look-up. Since the matrix is sparse this might generate only a few operations
# which are all inlined.
# Note that this only works with small matrices since otherwise the size of the function will just be to
# large.
# Note that this could also be avoided, but this is another topic.
@matthieubulte
matthieubulte / main.jl
Last active January 23, 2021 17:20
answer to geophf concerns about the rk43 method
# Answer to: https://twitter.com/1HaskellADay/status/875063542784958465
#
# So the Initial Value Problem (IVP) you want to solve seems to be the following
#
# dy/dx = 3*exp(-x) - 0.4*y
# y(x_0) = y_0 with x_0 = 0 and y_0 = 5
#
# You then ask: what is the value of y when x = 3?
#
# This can be computed using the Runge Kutta method by feeding it
(* TODO: use Random.State *)
open Random;;
open List;;
(* =========================================== General Utils = *)
let sign x = if x < 0 then -1 else 1;;
let truncate x l = sign x * min (abs x) l;;
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.