Skip to content

Instantly share code, notes, and snippets.

Avatar
😐

Jan Wedekind wedesoft

😐
View GitHub Profile
@wedesoft
wedesoft / pi.clj
Last active Nov 14, 2020
Calculate Pi in Clojure
View pi.clj
; 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 Nov 2, 2020
Prime number sieve of Eratosthenes in Clojure
View eratosthenes.clj
; 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 Oct 28, 2020
Save and load images using JMagick and Clojure
View imageio.clj
(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 Dec 7, 2018
download and view MNIST data
View mnist.scm
(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 Jul 15, 2018
Minimal OpenGL program with texture and shaders
View raw-opengl.c
// 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 Nov 6, 2017
Long short-term memory (LSTM) Tensorflow implementation training on Shakespeare's work
View lstm-predict.py
#!/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 Nov 6, 2017
Recurrent Neural Network for generating Shakespeare style text
View rnn-predict.py
#!/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 Nov 6, 2017
MNIST autoencoder implemented using Tensorflow
View autoencoder-run.py
#!/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 Nov 6, 2017
Tensorflow implementation of Restricted Boltzmann Machine (RBM) applied to MNIST dataset
View mnist-dream.py
#!/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
View grid.pov
// calibration grid
You can’t perform that action at this time.