Skip to content

Instantly share code, notes, and snippets.

View psycharo-zz's full-sized avatar

Timur psycharo-zz

  • EPFL
  • Lausanne, Switzerland
View GitHub Profile
@psycharo-zz
psycharo-zz / date2string.hpp
Created April 23, 2014 08:00
get a (date) timestamp in c++
#include <ctime>
using namespace std;
inline std::string date_string()
{
time_t rawtime;
std::time(&rawtime);
struct tm *tinfo = std::localtime(&rawtime);
char buffer[12];
@psycharo-zz
psycharo-zz / ls.hpp
Last active August 29, 2015 13:58
get a list of directories in (posix) c++
#include <dirent.h>
vector<string> list_dir(const string &path)
{
vector<string> result;
dirent *pdir;
DIR *dir = opendir(path.c_str()); // open current directory
while (pdir = readdir(dir))
result.push_back(pdir->d_name);
closedir(dir);
@psycharo-zz
psycharo-zz / arma2opencv.hpp
Last active May 4, 2020 10:54
convertions between armadillo and opencv
// convert an OpenCV multi-channel matrix to Armadillo cube. A copy is made
template <typename T, int NC>
Cube<T> to_arma(const cv::Mat_<cv::Vec<T, NC>> &src)
{
vector<cv::Mat_<T>> channels;
Cube<T> dst(src.cols, src.rows, NC);
for (int c = 0; c < NC; ++c)
channels.push_back({src.rows, src.cols, dst.slice(c).memptr()});
cv::split(src, channels);
return dst;
@psycharo-zz
psycharo-zz / median.hpp
Last active August 29, 2015 13:55
streaming median
template <typename Iterator>
typename std::iterator_traits<Iterator>::value_type
median(Iterator _start, Iterator _end)
{
typedef typename std::iterator_traits<Iterator>::value_type value_t;
std::vector<value_t> low;
std::vector<value_t> high;
high.push_back(*_start);
@psycharo-zz
psycharo-zz / gengmm.cpp
Created November 24, 2013 16:56
generate 1D gmm, armadillo
vec generate_gmm(size_t N, const vec &mean, const vec &stddev, const vec &weights)
{
vec result(N);
const size_t K = weights.size();
size_t filled = 0;
for (size_t k = 0; k < K; ++k)
{
size_t size_k = weights(k) * N;
result.subvec(filled, filled + size_k - 1) = mean(k) + randn(size_k) * stddev(k);
filled += size_k;
@psycharo-zz
psycharo-zz / kmeans.h
Last active December 26, 2015 19:19
simplistic kmeans implementation
#include <cfloat>
#include <armadillo>
using namespace arma;
/**
* K-means algorithm
* @param K the number of clusters
* @param means cluster centers
* @param counts sizes of clusters
* @param resps assignments to clusters
@psycharo-zz
psycharo-zz / randn.h
Created October 8, 2013 11:26
generate gaussians using GSL
double randn(double mean, double sigma)
{
gsl_rng *gnr = gsl_rng_alloc(gsl_rng_default);
return mean + gsl_ran_gaussian(gnr, sigma);
gsl_rng_free(gnr);
}
@psycharo-zz
psycharo-zz / gist:6600549
Created September 17, 2013 20:59
disable dashboard on mac
defaults write com.apple.dashboard mcx-disabled -boolean YES && killall Dock
@psycharo-zz
psycharo-zz / matlab.sh
Created June 29, 2013 15:39
update matlab libs
ln -s /usr/lib/x86_64-linux-gnu/libgfortran.so.3 ~/bin/matlab2013a/sys/os/glnxa64/libgfortran.so.3
@psycharo-zz
psycharo-zz / foreachtuple.cpp
Created June 24, 2013 17:54
iterate through tuple (c) someone on stackexchange;)
// for-each for tuples
namespace std {
template<int I, class Tuple, typename F> struct for_each_impl {
static void for_each(const Tuple& t, F f) {
for_each_impl<I - 1, Tuple, F>::for_each(t, f);
f(get<I>(t));
}
};
template<class Tuple, typename F> struct for_each_impl<0, Tuple, F> {
static void for_each(const Tuple& t, F f) {