Skip to content

Instantly share code, notes, and snippets.

View crabmusket's full-sized avatar

Daniel Buckmaster crabmusket

View GitHub Profile
@crabmusket
crabmusket / negLogDet.hs
Last active January 1, 2016 00:39
Inspecting the -logdet function for convexity along lines.
import Numeric.LinearAlgebra
import Control.Monad (replicateM)
import System.Random (randomIO)
import Graphics.Gnuplot.Simple (plotFunc)
-- Test 100 random lines for convexity.
main = replicateM 100 $ do
-- Two 5x5 symmetric positive definite matrices that determine the line.
a <- randomSnPD 5
v <- randomSnPD 5
@crabmusket
crabmusket / ConstantArithmetic.hs
Last active December 27, 2015 01:29
Convenient constant arithmetic for Haskell's Dimensional package.
module Numeric.Units.Dimensional.ConstantArithmetic where
import qualified Prelude
import qualified Numeric.Units.Dimensional
import Numeric.Units.Dimensional.Prelude
a *. b = a * (b *~ one)
a .* b = (a *~ one) * b
a .*. b = (a *~ one) * (b *~ one)
import qualified Data.ByteString.Char8 as B
import Criterion.Main
import Data.IORef
import Data.Vector
import Data.ByteString.Char8 (ByteString)
import Control.Monad (forever)
import Pipes
import qualified Pipes.Prelude as P
import Data.Char (intToDigit)
import qualified Data.ByteString.Builder as Builder
@crabmusket
crabmusket / recvLn.hs
Last active December 24, 2015 23:49
Working on a function to receive a line of serial data in Haskell.
import qualified Data.ByteString.Char8 as B
import System.Hardware.Serialport (SerialPort, recv)
recvLn :: SerialPort -> IO B.ByteString
recvLn s = do
-- Receive one byte at a time.
first <- recv s 1
rest <- if first == B.singleton '\n'
then return $ B.empty
else recvLn s
@crabmusket
crabmusket / SerialThreepenny.hs
Last active December 24, 2015 09:39
Monitoring a serial connection with a Threepenny UI frontend.
-- Imports for serial port
import qualified Data.ByteString.Char8 as B
import System.Hardware.Serialport
(openSerial, recv, closeSerial, defaultSerialSettings)
-- Imports for threading and stuff
import Control.Monad (void, forever, mapM_)
import Control.Concurrent (forkIO, killThread)
import Control.Concurrent.Chan
(Chan, newChan, dupChan, writeChan, getChanContents)
@crabmusket
crabmusket / Serial.hs
Created October 1, 2013 08:27
Infinite serial listener in Haskell.
import Control.Monad (forever)
import Control.Concurrent (forkIO, killThread)
import qualified Data.ByteString.Char8 as B
import System.Hardware.Serialport
(openSerial, recv, closeSerial, defaultSerialSettings)
port = "COM1" -- Windows
--port = "/dev/ttyUSB0" -- Unix
main = do
@crabmusket
crabmusket / 7-segment.hs
Last active December 18, 2015 08:29
A 7-segment display controller for the LaunchPad written in Atom.
-- http://hackage.haskell.org/package/atom
-- http://hackage.haskell.org/package/atom-msp430
module Main where
import Data.Word
import Data.Bits
import Language.Atom.MSP430
main = mspCompile "g2231" $ mspProgram {
@crabmusket
crabmusket / controllability.hs
Last active December 17, 2015 18:59
Controllability of a system described with a state equation.
module Main where
import Control.Monad (liftM)
import Numeric.LinearAlgebra (readMatrix, multiply, rank, cols, rows, fromBlocks)
main = do
putStrLn "Enter matrix A (in format \"1 2 3; 4 5 6; 7 8 9\")"
a <- getMatrix
putStrLn "Enter matrix B (in format \"1; 2; 3\")"
@crabmusket
crabmusket / monitor.hs
Created May 16, 2013 12:49
Monitor a directory to rebuild pandoc files automatically.
module Main where
import Filesystem (getWorkingDirectory)
import Filesystem.Path (extensions)
import System.FSNotify
import System.Exit
import System.Environment
import System.Cmd
import Control.Concurrent
import Control.Monad
@crabmusket
crabmusket / DistanceScaling.hs
Last active December 16, 2015 18:39
Exponentially scaling distances for rendering objects that are far away using an imprecise floating Z buffer.
module Main where
import Graphics.Gnuplot.Simple
-- Calculates the scaling factor for an object at distance 'dist'. The object
-- will be at scale 1 until 'max'/2, and then will be exponentially scaled down
-- as it increases. As 'dist' approaches infinity, 'dist * factor max dist'
-- approaches 'max'. See http://goo.gl/xLzrv
factor max dist = if dist < max / 2
then 1
else (1 - 1 / 2**(f+1)) / f