Skip to content

Instantly share code, notes, and snippets.

View piti118's full-sized avatar

Piti Ongmongkolkul piti118

  • Mahidol University International College
  • Nakorn Pathom, Thailand
View GitHub Profile
@piti118
piti118 / python_array_product.py
Created February 9, 2011 13:57
find array product like http://www.python.org/dev/peps/pep-0211/ (takes variable number of argument) [1,2,3]@[4,5]@[6,7] = [ [1,4,6],[1,4,7],[1,5,6],[1,5,7],[2,4,6]... and so on ]
# find array product like http://www.python.org/dev/peps/pep-0211/
# which means
# [1,2,3]@[4,5]@[6,7] = [1,4,6],[1,4,7],[1,5,6],[1,5,7],[2,4,6]... and so on
def aprod(*arg):
numarg = len(arg)
lenarray = map(len,arg)
finallen = reduce(lambda x,y:x*y,lenarray)
divisor = map(lambda i: reduce(lambda x,y:x*y,lenarray[i+1:],1),xrange(numarg))
index = [map(lambda n,l: (x/n)%l,divisor,lenarray) for x in xrange(finallen)]
#if you just want index for loop you can return index here
@piti118
piti118 / Makefile
Created September 27, 2011 06:42
Hacked Makefile to compile py2cairo on osx 10.6 with libcairo from brew and python2.7
#py2cairo is notoriously broken under osx due to
#1) osx cairo version is too old
#2) waf is trying so hard to build a universal binary i386 and x86_64 and I found no way to override this
#replace Makefile in src/ with this one
#this will create _cairo.so
src = $(wildcard *.c)
#$(warning $(src))
obj = $(patsubst %.c,%.o,${src})
#$(warning $(obj))
lib: ${obj}
@piti118
piti118 / flow.py
Created October 18, 2011 07:36
Script to estimate the effectiveness of using boat engines as water propeller (using energy budget).
t_flow = 3000.#m^3/s total chaopraya flow
t_area = 4000.#m^2 total crossection of chaopraya
rho = 1000.#kg/m^3 water density
#non interference
#P is power per boat (watts)
#A is effective area (m^3)
#n is number of boats
def flowdiff(P,A,n):
a_area = A #affected Area
@piti118
piti118 / The result
Created October 19, 2011 02:46
script to compare various boat placement strategies
For faraway assumption
npara nseries totalnewflow
(1, 1000, 4174.006179381851)
(2, 500, 4174.008036625053)
(4, 250, 4174.011751293362)
(5, 200, 4174.013608718646)
(8, 125, 4174.019181358588)
(10, 100, 4174.022896755448)
(20, 50, 4174.04147738306)
(25, 40, 4174.05076997475)
@piti118
piti118 / gist:1505174
Created December 21, 2011 08:13
C++ missing std::string printf
#include <cstdio>
#include <cstdarg>
#include <string>
//missing string printf
//this is safe and convenient but not exactly efficient
inline std::string format(const char* fmt, ...){
int size = 512;
char* buffer = 0;
buffer = new char[size];
va_list vl;
@piti118
piti118 / gzstream.h
Created December 21, 2011 22:42
Header-only version of gzstream
// ============================================================================
// gzstream, C++ iostream classes wrapping the zlib compression library.
// Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
@piti118
piti118 / globber.h
Created December 22, 2011 16:25
An STL glob wrapper for linux and mac
#include <glob.h>
#include <vector>
#include <string>
inline std::vector<std::string> glob(const std::string& pat){
using namespace std;
glob_t glob_result;
glob(pat.c_str(),GLOB_TILDE,NULL,&glob_result);
vector<string> ret;
for(unsigned int i=0;i<glob_result.gl_pathc;++i){
ret.push_back(string(glob_result.gl_pathv[i]));
@piti118
piti118 / vector_inserter.h
Created December 23, 2011 11:00
Vector Inserter
#include <vector>
//vector_inserter(idea taken from boost for those who don't wanna install boost)
//try vector<int> v; v+=1,2,3,4,5,6;
template <class T> class vector_inserter{
public:
std::vector<T>& v;
vector_inserter(std::vector<T>& v):v(v){}
vector_inserter& operator,(const T& val){v.push_back(val);return *this;}
};
template <class T> vector_inserter<T>& operator+=(std::vector<T>& v,const T& x){
@piti118
piti118 / format.h
Created April 14, 2012 02:09
The missing std::string sprintf like function.
#include <string>
#include <cstdarg>
//missing string printf
//this is safe and convenient but not exactly efficient
inline std::string format(const char* fmt, ...){
int size = 512;
char* buffer = 0;
buffer = new char[size];
va_list vl;
@piti118
piti118 / PODCache.h
Created April 18, 2012 14:08
POD caching class with persistent and expiration capability. (You can throw any POD struct at it)
#ifndef PODCACHE_H
#define PODCACHE_H
#include <tr1/unordered_map>
#include <cstring>
#include <limits.h>
#include <fstream>
#include <cassert>
#include <queue>
//simple serializable map for struct
template <class TK> class PODCache_bcmp{