Skip to content

Instantly share code, notes, and snippets.

View quantumelixir's full-sized avatar

Angel System quantumelixir

View GitHub Profile
@quantumelixir
quantumelixir / gemv.c
Created January 15, 2011 14:26
Matrix-Vector multiplication optimization and benchmarking
/*
* y += A*x
*
* Matrix-Vector multiplication optimization experiment
* TODO: Benchmark against the following:
* eigen2: ourselves, with the default options (SSE2 vectorization enabled).
* eigen2_novec: ourselves but with Eigen's explicit vectorization disabled. However, gcc's auto-vectorization was enabled.
* INTEL_MKL: The Intel Math Kernel Library, which includes a BLAS/LAPACK (11.0). Closed-source.
* ACML: The AMD's core math library, which includes a BLAS/LAPACK (4.2.0). Closed-source.
@quantumelixir
quantumelixir / testblas.c
Created January 15, 2011 14:32
Demonstrate how to use cblas
#include <stdio.h>
#include <cblas.h>
double m[] = {
3, 1, 3,
1, 5, 9,
2, 6, 5
};
double x[] = {
@quantumelixir
quantumelixir / ulamspiral.go
Created January 15, 2011 14:50
Print the ulam spiral
package main
import (
"os"
"fmt"
"flag"
"strconv"
)
@quantumelixir
quantumelixir / markdownlatex
Created February 10, 2011 07:13
Turn Latex code between $..$ and $$..$$ in markdown into images
#!/usr/bin/python
import subprocess
import sys
import os
import re
# output directory for generated png files
global basedir
basedir = '.'
@quantumelixir
quantumelixir / kmp_string_matching.cpp
Created January 25, 2017 16:03
The Knuth Morris Pratt algorithm for string matching
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
// find all occurrences of pattern in the text as a vector of indices
// using the knuth morris pratt algorithm in time O(|text| + |pattern|)
vector<int> kmp_find_all(const string &t, const string &p) {
@quantumelixir
quantumelixir / rotation_matrix_3d.nb
Created June 17, 2017 16:42
Rotation Matrix in 3D
In[79]:= Rz[x_] := ( {
{Cos[x], -Sin[x], 0},
{Sin[x], Cos[x], 0},
{0, 0, 1}
} );
Ry[x_] := ( {
{Cos[x], 0, Sin[x]},
{0, 1, 0},
{-Sin[x], 0, Cos[x]}
} );
@quantumelixir
quantumelixir / reverse.cc
Created November 25, 2017 16:16
Reversing a linked list
// This is just an instructive exercise using pointers. The goal is to
// reverse a linked list, which is not difficult but this solution
// illustrates a particular idea that I first came across in a talk by
// Linus Torvalds.
//
// The idea is simply that holding a pointer to pointer allows
// changing what is being pointed to, and that this can be used in a
// neat way in the context of linked lists. Take the example of
// inserting an item in a list. Here, the role played by the `head`
// pointer is similar to the role played by the `next` pointer of some
@quantumelixir
quantumelixir / temporaries.cc
Created November 26, 2017 17:05
Lifetime of C++ Temporaries
// A little exercise to demonstrate the lifetime of temporaries in
// C++. Inspired by the comment in this video
// https://youtu.be/xu7q8dGvuwk?t=3156 at CppCon2017 by Titus Winters.
//
// Two things to note:
//
// - Temporaries (rvalues) can have their lifetimes extended by
// binding them to a const reference. But the lifetime is extended
// only to the lifetime of the const reference itself, no more (see
// the Split(..) function below).
@quantumelixir
quantumelixir / alt-poly.cc
Last active December 4, 2017 20:48
Alternative Runtime Polymorphism
// This code presents an alternate style of runtime polymorphism
// fleshed out in Sean Parent's talk on the subject
// (https://www.youtube.com/watch?v=QGcVXgEVMJg). The goal is to treat
// different collections of types polymorphically based on their uses
// in different contexts without requiring client-side inheritance of
// those types depending on each of the uses.
#include <iostream>
#include <memory>
#include <string>
@quantumelixir
quantumelixir / copies-and-moves.cc
Created December 4, 2017 18:51
Understand the semantics of moves vs. copies in C++
// Study to understand the semantics of moves vs. copies in C++.
//
// * These operations are different only for reference types like the
// String class defined below. Unlike value types, a literal bitwise
// copy does not make sense for reference types. Instead, a copy
// typically involves a heap allocation followed by a bitwise copy
// from the object(s) being pointed to.
//
// * The goal of a move operation is to avoid unnecessary copying (and
// the associated heap allocations) when possible. For instance, in