Skip to content

Instantly share code, notes, and snippets.

View Alrecenk's full-sized avatar

Alrecenk Alrecenk

View GitHub Profile
// A Generic Min Heap Priority Queue for Unity that uses supplied float values for its ordering.
using System.Collections.Generic;
using UnityEngine;
public class PriorityQueue<T> where T : class{
struct ValueItem {
public T item;
public float value;
public ValueItem(T i, float v) {
@Alrecenk
Alrecenk / fastexp.java
Last active August 12, 2022 23:05
Very fast accurate approximation to Math.exp in java using floating point bit hacks.
/* fast floating point exp function
* must initialize table with buildexptable before using
Based on
A Fast, Compact Approximation of the Exponential Function
Nicol N. Schraudolph 1999
Adapted to single precision to improve speed and added adjustment table to improve accuracy.
Alrecenk 2014
std::string header = ...;
Variant glb = Variant::parseJSON(header);
glb["meshes"][glb["nodes"][glb["scenes"][0]["nodes"][0]]["mesh"]].printFormatted();
@Alrecenk
Alrecenk / insland.java
Created September 30, 2017 09:18
Island Counting: Flood Fill vs Hash Deduping
package scratch;
import java.util.*;
public class islands {
public static void main(String args[]){
boolean map[][] = generateIslands(180, 50, new Random(287), 25, 15, .3);
System.out.println(drawMap(map));
@Alrecenk
Alrecenk / LeastSquaresTrain.java
Last active August 1, 2016 04:16
This code provides all functions necessary to perform and apply a least squares fit of a polynomial from multiple inputs to multiple outputs. The fit is performed using an in-place LDL Cholesky decomposition based on the Cholesky–Banachiewicz algorithm.
//performs a least squares fit of a polynomial function of the given degree
//mapping each input[k] vector to each output[k] vector
//returns the coefficients in a matrix
public static double[][] fitpolynomial(double input[][], double output[][], int degree){
double[][] X = new double[input.length][];
//Run the input through the polynomialization and add the bias term
for (int k = 0; k < input.length; k++){
X[k] = polynomial(input[k], degree);
}
int inputs = X[0].length ;//number of inputs after the polynomial
@Alrecenk
Alrecenk / RotationForestsplit.java
Created November 5, 2013 04:43
The core learning algorithm for the rotation forest that calculates the best split based on approximate information gain.
//splits this node if it should and returns whether it did
//data is assumed to be a set of presorted lists where data[k][j] is the jth element of data when sorted by axis[k]
public boolean split(int minpoints){
//if already split or one class or not enough points remaining then don't split
if (branchnode || totalpositive == 0 || totalnegative == 0 || totalpositive + totalnegative < minpoints){
return false;
}else{
int bestaxis = -1, splitafter=-1;
double bestscore = Double.MAX_VALUE;//any valid split will beat no split
int bestLp=0, bestLn=0;
@Alrecenk
Alrecenk / normalize.java
Last active December 26, 2015 22:09
Normalizing a vector to length one, normalizing a data point into a distribution of mean zero and standard deviation of one, and generating a vector by a normal distribution. Different operations that are named similarly and might be confusion.
//makes a vector of length one
public static void normalize(double a[]){
double scale = 0 ;
for(int k=0;k<a.length;k++){
scale+=a[k]*a[k];
}
scale = 1/Math.sqrt(scale);
for(int k=0;k<a.length;k++){
a[k]*=scale ;
}
@Alrecenk
Alrecenk / pseudobootstrap.java
Last active December 26, 2015 04:39
Bootstrap aggregation for a random forest algorithm.
//bootstrap aggregating of training data for a random forest
Random rand = new Random(seed);
treenode tree[] = new treenode[trees] ;
for(int k=0;k<trees;k++){
ArrayList<Datapoint> treedata = new ArrayList<Datapoint>()
for (int j = 0; j < datapermodel; j++){
//add a random data point to the training data for this tree
int nj = Math.abs(rand.nextInt())%data.size();
treedata.add(alldata.get(nj)) ;
}
@Alrecenk
Alrecenk / pseudonaivetreelearn.java
Last active December 26, 2015 01:59
Pseudocode for a naive implementation of a decision tree learning algorithm.
int splitvariable=-1; // split on this variable
double splitvalue ;//split at this value
// total positives and negatives used for leaf node probabilities
int totalpositives,totalnegatives ;
Datapoint trainingdata[]; //the training data in this node
treenode leftnode,rightnode;//This node's children if it's a branch
//splits this node greedily using approximate information gain
public void split(){
double bestscore = Maxvalue ;//lower is better so default is very high number
@Alrecenk
Alrecenk / RotationForestSimple.java
Last active December 25, 2015 17:49
An optimized rotation forest algorithm for binary classification on fixed length feature vectors.
/*A rotation forest algorithm for binary classification with fixed length feature vectors.
*created by Alrecenk for inductivebias.com Oct 2013
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class RotationForestSimple{
double mean[] ; //the mean of each axis for normalization