Skip to content

Instantly share code, notes, and snippets.

View mtao's full-sized avatar

Michael Tao mtao

View GitHub Profile
@mtao
mtao / eigen_slice_eats_two.cpp
Last active September 23, 2022 04:52
Eigen always eats the first two values
[mtao@ruemo ~/hg/testcode/c++/eigen/head]% cat main.cpp
#include <Eigen/Dense>
#include <iostream>
int main(int,char**)
{
Eigen::Matrix<double,Eigen::Dynamic,1> x(20);
std::iota(x.begin(),x.end(),0);
// 0 1 2 3 4....
std::cout << x.transpose() << std::endl;
x = x.head(x.size()-1);
@mtao
mtao / eigen_boost_serialization.hpp
Last active March 15, 2024 02:49
Boost serialization for Eigen Matrix and SparseMatrix structures (plus triplets as a helper in SparseMatrix)
/*
Copyright (c) 2015 Michael Tao
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@mtao
mtao / filereader.cpp
Created July 31, 2014 18:33
file_token_reader
#include <fstream>
#include <sstream>
#include <iostream>
#include <iterator>
#include <vector>
int main(int argc, char * argv[]) {
if(argc < 2) {
std::cout << "Nothing here!" << std::endl;
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
int main(int argc, char* argv[]) {
std::vector<int> indices(1e6);
/*
std::transform(indices.begin(),indices.end(), indices.begin(), [&indices](int& v) -> int {
return std::distance(indices.data(),&v);
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <sstream>
template <typename T>
std::string to_csv(const std::vector<T>& vec) {
std::stringstream ss;
std::copy(vec.begin(),vec.end()-1,std::ostream_iterator<T>(ss,","));
@mtao
mtao / pif.hpp
Last active January 19, 2017 16:32
a header dsl for pif who loves perl
#ifndef PIFDSL_HPP
#define PIFDSL_HPP
#include <map>
namespace pif {
using hash = std::map<string,string>;
}
//example:
@mtao
mtao / imgui_impl_glut.cpp
Last active August 30, 2021 10:03
imgui glut bindings
// ImGui GLUT binding with OpenGL
// In this binding, ImTextureID is used to store an OpenGL 'GLuint' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.
// If your context is GL3/GL3 then prefer using the code in opengl3_example.
// You *might* use this code with a GL3/GL4 context but make sure you disable the programmable pipeline by calling "glUseProgram(0)" before ImGui::Render().
// We cannot do that from GL2 code because the function doesn't exist.
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
@mtao
mtao / active_set.cpp
Created February 26, 2017 00:50
a tool for extracting variables with boundary conditions. currently only have dirichlet there
#include "active_set.h"
#include <set>
template <>
const ActiveSet::IndexPair& ActiveSet::getIndexPair<ActiveSet::VariableType::Free>() const {
return m_active_pair;
}
template <>
const ActiveSet::IndexPair& ActiveSet::getIndexPair<ActiveSet::VariableType::Dirichlet>() const {
return m_dirichlet_pair;
#include <iostream>
#include <Eigen/Core>
//Mimics http://stackoverflow.com/a/40958644
template <typename Scalar, int D, int E, int... Is>
auto eigen_unpack_impl(const Eigen::Matrix<Scalar, D, E>& a, std::integer_sequence<int,Is...>) {
return std::tie( a(Is)... );
}
@mtao
mtao / unpacking_to_structured_bindings.cpp
Created April 17, 2017 14:21
an experiment on how to use structured bindings with prvalues (it took me a bit to realize the constness doesn't affect the double& embedded within)
#include <iostream>
#include <tuple>
//sample vector class
struct Vec3 {
public:
Vec3(const std::initializer_list<double>& a) { std::copy(a.begin(),a.end(),data); }
double& operator()(size_t i) { return data[i]; }
private: