Skip to content

Instantly share code, notes, and snippets.

View hugmanrique's full-sized avatar

Hugo Manrique hugmanrique

View GitHub Profile
@hugmanrique
hugmanrique / index.html
Last active June 12, 2019 19:51
Plot of a BivariateBeta(alpha_X, beta_X, alpha_Y, beta_Y) distribution (assumes X and Y are independent events)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Plot</title>
</head>
<body>
<div id="plot"></div>
<script src="https://unpkg.com/@stdlib/stdlib@0.0.32/dist/stdlib-flat.min.js"></script>
@hugmanrique
hugmanrique / birthday.r
Created July 13, 2019 21:04
Birthday Paradox in R
d = 365 # days
n = 100 # people
prob = function(i) { 1 - ((d - 1) / d)^(i * (i - 1) / 2)}
X <- seq(1, n, 1)
plot(prob(X), type="l")
prob(72)
# = 0.9990993
@hugmanrique
hugmanrique / PackageInjector.java
Created July 29, 2019 17:06
Byte Buddy PackageInjector
import com.google.common.reflect.ClassPath;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.loading.ClassInjector;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.nio.file.Files;
@hugmanrique
hugmanrique / optimizeFont.sh
Last active April 4, 2020 14:06
Font optimization
#!/bin/bash
# Required:
# fonttools, zopfli, installable via pip install.
set -e
ORIGINAL=original.otf
UNICODES_FILE=unicodes.txt
FORMATS=(woff woff2)
@hugmanrique
hugmanrique / svd.m
Created April 28, 2020 12:23
Toy program to compress greyscale images using a singular value decomposition
clear;
RGB = imread('escher.jpg');
I = double(rgb2gray(RGB));
[m, n] = size(I);
[V, S, W] = svd(I);
% Achieves a 2.04 compression ratio while still being visible
slimit = mean(mean(S)) + 4 * std(std(S));
@hugmanrique
hugmanrique / regionfile-pr.md
Last active October 26, 2020 05:34
Paper PR about RegionFiles

Currently, the RegionFile (line 52) class extends the file if it's size is not a multiple of 4KB, but does it in a inefficient way:

for(int i = 0; i < (this.file.length() & 0xfff); i++) {
        this.file.write(0);
}

That code could potentially cause up to 4095 file extensions, when we could just do them at once with the following code:

@hugmanrique
hugmanrique / adapsimpson2.m
Last active November 17, 2020 11:34
Adaptive Simpson's rule for a bivariate function
clear;
T = 7;
f = @(x, y) exp(-T * (x.^2 - y.^2)) - exp(-T * (x.^2 + y.^2));
xa = 0;
xb = 2;
ya = 0;
yb = 3;
S = adapsimpson2(f, xa, xb, ya, yb, 1.e18)
@hugmanrique
hugmanrique / jekyll-transclude.rb
Last active November 17, 2020 11:41
Jekyll Liquid block that allows includes with content
# frozen_string_literal: true
# Based on https://github.com/jekyll/jekyll/blob/master/lib/jekyll/tags/include.rb,
# provides a transclude block that maintains Jekyll include semantics. Files can
# pass parameters to transcludes via the `include` object, with the addition of
# {{ include.content }} which includes the block content.
#
# Transcludes must be located in Jekyll's _includes dir.
#
# Example:
@hugmanrique
hugmanrique / MemoryAccessBenchmark.java
Last active January 1, 2021 23:34
Panama (3-b385) MemoryAccess vs Unsafe vs ByteBuffer on the heap
package me.hugmanrique.mab;
import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import jdk.incubator.foreign.MemoryAccess;
import jdk.incubator.foreign.MemorySegment;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
@hugmanrique
hugmanrique / isPerfect.js
Last active January 30, 2021 21:03
Checks if a number is perfect
// Every perfect number is represented as p one bits followed by (p - 1) zero bits,
// where p is a Mersenne prime.
// Let x be the largest `MAX_BITS`-bit perfect number. Then ceil(log2(x)) = p + (p - 1).
const MAX_BITS = 32; // Bitwise operators operate on 32-bit integers
const MAX_ONES = Math.floor((MAX_BITS + 1) / 2); // upper bound on p
// Precomputed list of all prime m <= p s.t. 2^m - 1 is prime.
// TODO Compute automatically.
const MERSENNE_EXPS = new Set([2, 3, 5, 7, 13]);