Skip to content

Instantly share code, notes, and snippets.

View wedesoft's full-sized avatar

Jan Wedekind wedesoft

View GitHub Profile
@wedesoft
wedesoft / pi.clj
Last active November 14, 2020 22:33
Calculate Pi in Clojure
; Calculate Pi in Clojure
; https://rosettacode.org/wiki/Pi
; https://en.wikipedia.org/wiki/Spigot_algorithm
(defn pi-digits [& {:keys [q r t k n l]}]
(if (< (- (+ (* 4 q) r) t) (* n t))
(cons n
(lazy-seq
(pi-digits :q (* q 10)
:r (* 10 (- r (* n t)))
:t t
@wedesoft
wedesoft / eratosthenes.clj
Last active November 2, 2020 18:53
Prime number sieve of Eratosthenes in Clojure
; This one is really nice and compact but it quickly runs out of stack space:
(defn primes [i] (cons i (lazy-seq (filter #(not (zero? (mod % i))) (primes (inc i))))))
(take 100 (primes 2))
; (2 3 5 7 ...)
(take 1000 (primes 2))
; Error printing return value (StackOverflowError) at user/primes (NO_SOURCE_FILE:1).
; This one is slightly less elegant but works for large numbers:
(defn primes [i p]
(if (some #(zero? (mod i %)) p)
@wedesoft
wedesoft / imageio.clj
Created October 28, 2020 21:14
Save and load images using JMagick and Clojure
(import '[magick MagickImage ImageInfo ColorspaceType])
(defn spit-image [file-name width height data]
"Save an RGB image"
(let [info (ImageInfo.)
image (MagickImage.)]
(.constituteImage image width height "RGB" data)
(.setSize info (str width \x height))
(.setDepth info 8)
(.setColorspace info ColorspaceType/RGBColorspace)
@wedesoft
wedesoft / mnist.scm
Last active December 7, 2018 12:58
download and view MNIST data
(use-modules (oop goops) (ice-9 binary-ports) (rnrs bytevectors) (aiscm core) (system foreign) (aiscm xorg) (ice-9 format)
; http://yann.lecun.com/exdb/mnist/
(define f (open-file "train-labels-idx1-ubyte" "rb"))
(define magic (bytevector-u32-ref (get-bytevector-n f 4) 0 (endianness big)))
(define n (bytevector-u32-ref (get-bytevector-n f 4) 0 (endianness big)))
(define bv (get-bytevector-n f n))
(define labels (make (multiarray <ubyte> 1) #:memory (bytevector->pointer bv) #:shape (list n)))
(define f (open-file "train-images-idx3-ubyte" "rb"))
(define magic (bytevector-u32-ref (get-bytevector-n f 4) 0 (endianness big)))
@wedesoft
wedesoft / raw-opengl.c
Created July 15, 2018 20:43
Minimal OpenGL program with texture and shaders
// Minimal OpenGL shader example using OpenGL directly
#include <math.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GL/glut.h>
const char *vertexSource = "#version 130\n\
in mediump vec3 point;\n\
in mediump vec2 texcoord;\n\
@wedesoft
wedesoft / lstm-predict.py
Last active November 6, 2017 10:11
Long short-term memory (LSTM) Tensorflow implementation training on Shakespeare's work
#!/usr/bin/env python3
import sys
import numpy as np
import tensorflow as tf
class CharVec:
def __init__(self, text):
self.chars = np.array([ord(c) for c in sorted(set(text))])
@wedesoft
wedesoft / rnn-predict.py
Last active November 6, 2017 10:11
Recurrent Neural Network for generating Shakespeare style text
#!/usr/bin/env python3
import sys
import numpy as np
import tensorflow as tf
class CharVec:
def __init__(self, text):
self.chars = np.array([ord(c) for c in sorted(set(text))])
@wedesoft
wedesoft / autoencoder-run.py
Last active November 6, 2017 10:12
MNIST autoencoder implemented using Tensorflow
#!/usr/bin/env python3
# http://blog.aloni.org/posts/backprop-with-tensorflow/
import random
import numpy as np
import tensorflow as tf
import pickle
import gzip
import cv2
@wedesoft
wedesoft / mnist-dream.py
Last active November 6, 2017 10:12
Tensorflow implementation of Restricted Boltzmann Machine (RBM) applied to MNIST dataset
#!/usr/bin/env python3
# http://blog.aloni.org/posts/backprop-with-tensorflow/
import random
import pickle
import gzip
import numpy as np
import tensorflow as tf
import cv2
@wedesoft
wedesoft / grid.pov
Created April 7, 2017 13:21
grid.pov
// calibration grid