Skip to content

Instantly share code, notes, and snippets.

@Gerjo
Gerjo / LSA-SVD
Created September 19, 2014 15:20
Latent semantic analysis and singular value decomposition to automatically determine a comment's relevancy to the topic it was posted to. Conclusion: it doesn't work yet, more research required.
cat("\014")
library(base)
if(require(tm) == FALSE) {
print("Attempting to install TM support...");
install.packages("tm")
library(tm);
@Gerjo
Gerjo / php-gd-text-warp
Created June 15, 2014 19:45
PHP warping images
<?php
$input = "Warped text!";
$width = 200;
$height = 200;
// Allocate initial image with a solid white background.
$original = imagecreatetruecolor($width, $height);
imagefill($original, 0, 0, imagecolorallocate($original, 255, 255, 255));
@Gerjo
Gerjo / Memory bake
Created April 15, 2014 10:27
C++ memory dump to be imported into JavaScript (WebGL)
define(function(require) {
/// Here's a scene I baked earlier. Bunch of cubes and a plane.
///
///// 6 float triplets
///
/// struct Face {
/// Vector3 a;
/// Vector3 b;
/// Vector3 c;
@Gerjo
Gerjo / integer factorisation
Created March 29, 2014 23:31
Integer factorisation, quick hack to find an optimal texture size given an amount of pixels.
Vector2 primo(int num) {
std::deque<int> primes;
const int root = (int) std::sqrt(num);
std::function<void(int)> recurse;
// Mostly from: http://www.coderenaissance.com/2011/06/finding-prime-factors-in-javascript.html
recurse = [&primes, &recurse, root] (int num) -> void {
@Gerjo
Gerjo / Linear Congruental Generator
Created March 28, 2014 13:33
Linear Congruental Generator, with floating points. Test implementation for eventual usage GLSL.
// Initial seed.
var x = 42;
function rand() {
// Old random number becomes next seed.
x = (1103515245 * x + 12345) % 2147483648;
// Generate a floating point, then modulo to only keep
@Gerjo
Gerjo / pseudo
Last active August 29, 2015 13:57
Pseudo code for space-efficiently saving number ranges.
node::insert(int value) {
// Value falls in range
if(value >= this.min && value <= this.max) {
}
// Extend range
if(value == this.min - 1) {
this.min = value;
@Gerjo
Gerjo / png-opengl
Last active April 30, 2020 13:58
Basic PNG OpenGL texture loading.
struct Textures {
static GLuint LoadPNG(const std::string& filename) {
if( ! FileExists(filename)) {
Exit("Cannot open png: %s.", filename.c_str());
}
FILE* fp = ::fopen(filename.c_str(), "r");
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@Gerjo
Gerjo / lsq
Created January 26, 2014 12:45
LeastSquaresLinearRegression: function(data, getter) {
// Assign a default trivial case getter:
if( ! getter) {
getter = function(row) {
return row;
};
}
var xy = 0;
@Gerjo
Gerjo / newton-and-gradient-decent
Created January 25, 2014 14:39
Newthon-Raphson vs gradient decent; who converges quicker.
// Quick test to see who converges quicker
// squared error
function e(w, t, x, y) {
return Math.pow(t - w * x - w*w*y, 2);
}
// e's derivative
function d(w, t, x, y) {
return 2 * (t - w * x - w*w * y) * (-x - 2*w*y);
@Gerjo
Gerjo / knn
Last active December 30, 2015 00:49
R nearest neighbour (knn) demo.
cat("\014")
library(class)
# I like boolean support.
no <- 0
yes <- 1
# Entry to clasify
test <- c(
"cat", # The correct class