Skip to content

Instantly share code, notes, and snippets.

View quantumelixir's full-sized avatar

Angel System quantumelixir

View GitHub Profile
@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 / 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 / factorial.c
Created January 15, 2011 14:23
Compute factorial of a given number using two different methods (uses GMP)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <gmp.h>
long int NAIVE = 30;
typedef unsigned long int ulong;
@quantumelixir
quantumelixir / cpuid.s
Created January 15, 2011 14:18
CPUID in GNU Assembly
.section .data
output:
.ascii "The processor Vendor ID is: "
vendor_id:
.ascii "xxxxYYYYzzzz\n"
.section .bss
.section .text
@quantumelixir
quantumelixir / permutations_combinations.py
Created January 15, 2011 10:46
Permutations and Combinations in Python
def permute(l) :
if len(l) == 1 :
yield l
else :
for i in range(len(l)) :
for j in permute(l[:i] + l[i + 1:]) :
yield [l[i]] + j
def combinations(l, r) :
if r == 1 :
@quantumelixir
quantumelixir / list_all_paths.py
Created January 15, 2011 10:39
Compare two methods of listing all paths between two nodes in a graph
#!/usr/bin/env python
import timeit
graph = {'A': ['B', 'C'],
'B': ['C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C']}
@quantumelixir
quantumelixir / partitions.py
Created January 15, 2011 10:34
Compare two methods of computing partitions using Python's "yield" construct
#!/usr/bin/env python
def partitions(n, curtot = 0, cur = []) :
if curtot == n :
yield cur
t = cur[-1] if cur else 1
for i in xrange(t, n - curtot + 1) :
@quantumelixir
quantumelixir / quizzy.py
Created January 15, 2011 10:29
Solve Quizzy's Word Challenge
#1. The input is a list of rings. Each ring is a list of letters.
#2. Once you choose a letter from ring k then you can only choose letters from
# ring k, k + 1, ... Outermost ring is ring 0. And ring depth increases inwards.
#3. points[k] is the points for getting the letter chr(k + ord('A'))
from bisect import bisect, bisect_left
from time import time
try:
import psyco
@quantumelixir
quantumelixir / quines.py
Created January 15, 2011 10:24
Some quines I made in Python
#Quine1
e=r'''print "e=r''"+"'"+e+"''"+"'\n"+e'''
print "e=r''"+"'"+e+"''"+"'\n"+e
#Quine2
x='%s=%s;print x%%("x",chr(39)+x+chr(39))';print x%("x",chr(39)+x+chr(39))
#Quine3
y="%"
z='y="%s"\nz=%s\nprint z%s(y,repr(z),y)'
@quantumelixir
quantumelixir / golomb.py
Created January 15, 2011 10:19
Finds the n-th golomb number
import bisect
import timeit
#Finds the Nth golumb number from the
#sequence x(n) using a binary search
def findNthGolumb(n, x, upto) :
pos = bisect.bisect(x, n, 1, upto)
return pos - 1
#Creates the sequence x(n) where x(n) is k