Skip to content

Instantly share code, notes, and snippets.

View nlw0's full-sized avatar

Nicolau Leal Werneck nlw0

View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nlw0
nlw0 / MergeOverlappingIntervals.scala
Last active December 9, 2017 13:56
merge overlapping intervals in C++ with std::accumulate
object MergeOverlappingIntervals extends App {
val intervals = Vector(
(0, 4),
(1, 3),
(3, 6),
(7, 9),
(9, 10),
(12, 14),
(13, 15),
@nlw0
nlw0 / test_list_locality.cpp
Last active October 28, 2018 16:28
Testing the memory locality of a linked list in C++
#import<iostream>
#import<list>
struct Node {
int value;
Node* next;
Node(int v, Node* n=NULL) : value(v), next(n) {}
};
int main(int, char**) {
package conann
import breeze.linalg._
import breeze.plot._
import breeze.stats.distributions._
import breeze.stats._
import scala.math._
import java.awt.Color
@nlw0
nlw0 / BranchAndBound.scala
Last active November 4, 2018 10:20
Code fragments of a branch-and-bound solver applied to two different problems
case class BranchAndBound[S <: BnBSolution[S], F <: Formula[F]]() {
def solve(initial_state: S): Option[S] = {
Logger.println(s"[BnB] starting new problem")
_solve(List(initial_state), None)
}
@tailrec
private final def _solve(state_stack: Seq[S], best_state: Option[S]): Option[S] =
if (state_stack.isEmpty) best_state
@nlw0
nlw0 / quickdemo.ipynb
Last active November 15, 2018 21:50
Julia quick demo
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nlw0
nlw0 / Julia_rosenbrock.ipynb
Created November 18, 2018 00:04
Tiny Julia demo: plotting the Rosenbrock "banana" function
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nlw0
nlw0 / myexp.c
Created November 18, 2018 00:18
Exponential function benchmark in C
#include<stdio.h>
#include<math.h>
// thanks Joe from stackoverflow
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
@nlw0
nlw0 / myexp.jl
Last active November 20, 2018 21:07
Exponential function benchmark in Julia
@inline @fastmath function myexpm1(x :: Float64)
ln2_hi = 6.93147180369123816490e-01
ln2_lo = 1.90821492927058770002e-10
one = 1.0
Q1 = -3.33333333333331316428e-02
Q2 = 1.58730158725481460165e-03
Q3 = -7.93650757867487942473e-05
Q4 = 4.00821782732936239552e-06
Q5 = -2.01099218183624371326e-07
hi = x - ln2_hi
@nlw0
nlw0 / myexp.py
Created November 18, 2018 00:28
Exponential function benchmark in Python
import timeit
import numpy as np
def myexpm1(x):
ln2_hi = 6.93147180369123816490e-01
ln2_lo = 1.90821492927058770002e-10
one = 1.0
Q = np.array([1.0,