-
-
Save caiorss/29eaaea26b0c0f7f3416 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
disp a | |
disp v | |
let nld t = (negate . log . det) (a + scale t v) | |
domain = [0, 0.05 ..1] | |
opts = [] | |
in plotFunc opts domain nld | |
randomSnPD n = fmap toSPD (randomSquare n) | |
-- Add transpose to make it symmetric, then add nI to ensure PD. | |
where toSPD m = m + trans m + scale (fromIntegral n) (ident n) | |
randomSquare n = fmap matFromSeed randomIO | |
where matFromSeed s = reshape n $ randomVector s Uniform (n * n) | |
disp = putStr . dispf 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment