Skip to content

Instantly share code, notes, and snippets.

@aztennenbaum
aztennenbaum / fast aproximate powf.c
Last active February 6, 2024 22:34
fast inverse squareroot, generalized to arbitrary powers in the range [1,-1]
#include <stdio.h>
#include <stdint.h>
#include <float.h>
#include <math.h>
float q_pow(float x, float y)
{
union {
float f;
uint32_t i;
//+++++++++++++++++++++++++++ PUBLIC-DOMAIN SOFTWARE ++++++++++++++++++++++++++
// Functions: TransposetoAxes AxestoTranspose
// Purpose: Transform in-place between Hilbert transpose and geometrical axes
// Example: b=5 bits for each of n=3 coordinates.
// 15-bit Hilbert integer = A B C D E F G H I J K L M N O is stored
// as its Transpose
// X[0] = A D G J M X[2]|
// X[1] = B E H K N <-------> | /X[1]
// X[2] = C F I L O axes |/
// high low 0------ X[0]
@aztennenbaum
aztennenbaum / test-vec.cpp
Created July 28, 2019 23:34
builds w/ clang, infinite memory usage w/ gcc
#ifndef _OST_VEC_H_
#define _OST_VEC_H_
#include <cmath>
#include <string>
#include <algorithm>
#include <limits>
#include <tuple>
#include <cassert>
#include <iostream>
#include <typeinfo>
@aztennenbaum
aztennenbaum / kalmanDeriv.m
Last active May 27, 2022 04:26
Baysian derivation of kalman filter
Theorems:
1. Linear transform of a normal random variable (matrix cookbook, 8.1.4):
Assume:
X ~ N(x,Px),
f(v) = A*v
Then:
f(X) ~ N(A*x,A*Px*A')
2. Conditional Distribution (matrix cookbook, 8.1.3):
[X;Y]~N([x0;y_expected],[Pxx,Pxy;Pxy';Pyy]);
template<class MapPtr, class UnaryPredicate>
void stdmap_ptr_remove_if(MapPtr m, UnaryPredicate p)
{
/* See https://stackoverflow.com/questions/800955/remove-if-equivalent-for-stdmap */
for( auto it = m->begin(); it != m->end(); ) {
auto v=it->second;
if (!p(v)) {
++it;
} else {
delete v;
//Parallel
T2 kdsearch (const T2 in, const T lower, const T upper) {
auto mid=lower+std::distance(lower,upper)/2;
if (lb<mid && mid<ub) {
auto handle=std::async(kdsearch<...>,std::get<1>(...),mid+1,upper);
return std::make_pair(check_and_push<check>(kdsearch<...>(std::get<0>(...),lower,mid),mid,mid+1),handle.get());
}
return std::make_pair((lb<mid)?kdsearch<...>(std::get<0>(...),lower,mid) :std::get<0>(...),
(mid<ub)?kdsearch<...>(std::get<1>(...),mid+1,upper):std::get<1>(...));
}
//https://stackoverflow.com/questions/1797380/template-specialization-with-a-templatized-type
template <class T, class ForwardIterator>
ForwardIterator push_ordered_interval(const T first, const T last, ForwardIterator result) {
if (*it == first) {
*it=last;
} else {
*(++it)=first;
*(++it)=last;
}
@aztennenbaum
aztennenbaum / custom_iterator.cpp
Created April 15, 2019 20:56 — forked from jeetsukumaran/custom_iterator.cpp
Sample C++/STL custom iterator
// Sample custom iterator.
// By perfectly.insane (http://www.dreamincode.net/forums/index.php?showuser=76558)
// From: http://www.dreamincode.net/forums/index.php?showtopic=58468
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cassert>