Skip to content

Instantly share code, notes, and snippets.

View mtao's full-sized avatar

Michael Tao mtao

View GitHub Profile
@mtao
mtao / embedded_loops.cpp
Created March 23, 2020 16:39
three alternatives for breaking out of embedded for loops
#include <iostream>
int main(int argc, char* argv[]) {
{
// a typical embedded loop check can require a check for success and
// multiple checks to unroll
bool success = false;
int i = 0, j = 0;
for (i = 0; i < 50; ++i) {
for (j = 0; j < 50; ++j) {
@mtao
mtao / ang_check.py
Last active March 19, 2020 04:40
an implementation of a robust angle checker
import numpy as np
import math
import time
# https://docs.python.org/3/howto/sorting.html#sortinghowto
def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
class K:
def __init__(self, obj, *args):
self.obj = obj
@mtao
mtao / active_set.h
Created April 19, 2019 22:31
some old code i had for compressing matrices due to active/inactive sets
#ifndef INDEX_MANAGEMENT_H
#define INDEX_MANAGEMENT_H
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Sparse>
@mtao
mtao / stl2obj.py
Last active April 8, 2019 03:34
a simple stl to obj converter. currently doesn't support normals yet
import sys
import numpy as np
from struct import unpack
def strlist_to_vec(arr):
return tuple(float(x) for x in arr)
class Facet:
dtype = np.dtype([
('N', np.float32,(3)),
@mtao
mtao / grid_vertex_stacker.cpp
Created March 26, 2019 21:38
using https://github.com/mtao/core for a grid datastructure, this code creates a somewhat-virtual matrix that produces a horizontal stack of grid vertices and use-driven data (extra vertices). based off of https://eigen.tuxfamily.org/dox/TopicCustomizing_NullaryExpr.html
#include <Eigen/Dense>
#include <iostream>
#include <mtao/geometry/grid/grid.h>
template<class ArgType, int D>
struct col_offset_helper {
typedef Eigen::Matrix<typename ArgType::Scalar,
D,
Eigen::Dynamic,
Eigen::ColMajor,
D,
@mtao
mtao / cwiseProduct_vs_asDiagonal
Last active January 14, 2019 17:54
Had odd performance using asDiagonal at https://github.com/mtao/core/blob/master/include/mtao/eigen/mic0_preconditioner.hpp#L160 so I switched to cwiseProduct. They certainly have deferring performance, especially with and without O2. A sheet looking at the difference is here: https://docs.google.com/spreadsheets/d/1IYLwVsEOQORsDH2eoJXpEP8_oWe8p…
#include <Eigen/Dense>
#include <chrono>
#include <iostream>
template <typename Func>
int run(int size, const std::string& name, Func&& f, bool print=false) {
Eigen::VectorXd D = Eigen::VectorXd::Random(size);
Eigen::VectorXd x = Eigen::VectorXd::Random(size);
@mtao
mtao / slice_iter.py
Last active July 23, 2018 16:02
coroutine-based tool for iterating high order tensors
def make_range(s):
if type(s) == range:
return s
elif type(s) == slice:
return range(s.start,s.stop,s.step if s.step else 1)
elif type(s) == tuple:
return range(*s)
else:
return range(s)
@mtao
mtao / get_five.sh
Last active March 28, 2018 18:21
emulator for some algorithms problem with solution (scrape an entire database when only able to query {prefix -> first N lexicographical matches} for some static N). Base strategy is to use lexicographical depth-first search with some shortcutting (when there's N results we can hope there's more and attach ourselves to a longer common prefix).
if [ ! -e words.txt ]; then
wget https://raw.githubusercontent.com/dwyl/english-words/master/words.txt > /dev/null 2&>1
cat words.txt | tr '[:upper:]' '[:lower:]'| sort -u | grep -v "-" | grep -v "\." | grep -v "'" | grep -v "/" | grep -v "&" > /tmp/words_sorted.txt
fi
rm /tmp/my_dict.txt
prefix="$1"
grep "^$1" words.txt | head -n 5
charset=$( cat /tmp/words_sorted.txt | grep -o . | sort -u | tr -d "\n" )
@mtao
mtao / inset.cpp
Last active March 19, 2018 14:47
Implementation of the code pointed out in http://belkadan.com/blog/2018/03/My-Little-Optimization/. I wasn't aware that template <int... N>(&...name)[N] worked
#include <iostream>
#include <string_view>
template <size_t... N>
bool isInSet(const std::string_view s, const char (&...S)[N]) {
return (operator==(s,S) || ...);
}
int main(int argc, char * argv[]) {
@mtao
mtao / libigl_glfw_xmonad.patch
Created February 4, 2018 20:07
glfw patch for libigl on xmonad
diff --git a/src/x11_platform.h b/src/x11_platform.h
index 7b46025..b48968f 100644
--- a/src/x11_platform.h
+++ b/src/x11_platform.h
@@ -112,6 +112,7 @@ typedef struct _GLFWwindowX11
XIC ic;
GLFWbool overrideRedirect;
+ GLFWbool visuallyMapped;