Skip to content

Instantly share code, notes, and snippets.

View richwandell's full-sized avatar
:shipit:
Coding

Rich Wandell richwandell

:shipit:
Coding
View GitHub Profile
#[macro_export]
macro_rules! num_enum {
($name:ident $type:ident [$($keys:tt)*] $impl:ident) => {
impl From<$type> for $name {
fn from(v: $type) -> Self {
match v {
$(x if x == $name::$keys as $type => $name::$keys),*,
_ => panic!("Cannot convert {}", v)
}
}
@richwandell
richwandell / idk.rs
Created February 6, 2022 14:54
not sure how to do this
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use glutin_window::GlutinWindow as Window;
use graphics::{clear, Transformed};
use graphics::Context;
use image::{ImageBuffer, Rgba};
use opengl_graphics::{GlGraphics, GlyphCache, OpenGL, Texture, TextureSettings};
use piston::{Button, PressEvent};
use piston::event_loop::{Events, EventSettings};
@richwandell
richwandell / WASM.md
Last active December 24, 2020 04:22
Wasm byte reference

Function definition

Function types are encoded by the byte 0x60 followed by the respective vectors of parameter and result types.

Example:

(module
    (func $add (param $lhs i32) (result i32)
 get_local $lhs)
@richwandell
richwandell / main.rs
Last active September 22, 2019 16:39
I am learning rust and had some trouble understanding how to pass a value to a rust thread. I believe this example is correct, please let me know if something here is wrong.
use std::thread;
use std::time::Duration;
use std::collections::LinkedList;
extern crate num_cpus;
fn main() {
let num = num_cpus::get();
let mut handles = LinkedList::new();
@richwandell
richwandell / huffman.js
Created October 8, 2018 14:55
Huffman Coding
buildHuffmanTree(charData) {
//Sort by lowest frequency
let cmp = (a, b) => {
if(a.fre < b.fre) {
return -1;
}
return 1;
};
//initially sorted list of nodes
let data = charData
@richwandell
richwandell / kalman.js
Created February 5, 2018 00:58
nd kalman filter
var cest,
pest = math.ones(2),
mea,
kg,
eest = math.ones(2),
emea = math.ones(2);
function kalman(m){
mea = m;
sigmoid(x, c1 = 1, c2 = 0) {
return 1 / (1 + Math.pow(Math.E, -((c1 * x) - c2)));
}
derivative(x) {
let sig = this.sigmoid(x);
return sig * (1 - sig);
}
cost(X, y) {
epoch(d){
//keep track of how many times we go through the dataset
this.ni++;
//Shuffle our data first
let data = this.shuffle(d);
const N = data.length;
for(let i = 0; i < N; i ++){
const x = data[i][0];
const y = data[i][1];
epoch(data){
//keep track of how many times we go through the dataset
this.ni++;
const N = data.length;
let bGrad = 0;
let mGrad = 0;
for(let i = 0; i < N; i ++){
const x = data[i][0];
const y = data[i][1];
const diff = (y - this.line(x));
@richwandell
richwandell / neuralnetwork.js
Created March 5, 2017 05:30
Neural Network using math.js
function NeuralNetwork(){
this.inputLayerSize = 2;
this.outputLayerSize = 1;
this.hiddenLayerSize = 3;
this.W1 = math.randomInt([this.inputLayerSize, this.hiddenLayerSize])
.map(function(row){
return row.map(function(el){ return Math.random(); });
});
this.W2 = math.randomInt([this.hiddenLayerSize, this.outputLayerSize])
.map(function(row){