Skip to content

Commit

Permalink
Reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
markandrus committed Mar 9, 2012
1 parent 7154f97 commit 695e50d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
26 changes: 19 additions & 7 deletions Makefile
@@ -1,13 +1,25 @@
all: main tests
# Makefile for Homework 6
# by Mark <andrus@uchicago.edu>

main: main.hs util.hs
# We must refer to iconv in /usr/lib
ghc -L/usr/lib main.hs -o main -fno-cse
ghc = ghc
flags = -O2

tests: tests.hs util.hs
ghc -L/usr/lib -O2 tests.hs -o tests
# NOTE: the following hack is a consequence of GHC for OS X being compiled against the libiconv in
# `/usr/lib`
extra_libs = -L/usr/lib

bins = main tests
deps = Utils.hs

all: $(bins)

main: Main.hs $(deps)
$(ghc) $(extra_libs) $(flags) $< -o $@

tests: tests.hs $(deps)
$(ghc) $(extra_libs) $(flags) $< -o $@

.PHONY: all clean

clean:
rm -f *.hi *.o main tests
rm -f *.hi *.o $(bins)
23 changes: 6 additions & 17 deletions util.hs → PowerMethod.hs
@@ -1,31 +1,16 @@
{-# LANGUAGE FlexibleContexts #-}
module PowerMethod (powerMethod) where

module Util
( isSymmetric
, powerMethod
) where

import Control.Monad
import Data.List
import Numeric.LinearAlgebra
import System.Random
import Debug.Trace

isSymmetric :: (Element a, Eq a) => Matrix a -> Bool
isSymmetric a = toLists a == toLists (trans a)

vNormalize :: Vector Double -> Vector Double
vNormalize v = recip (norm2 v) `scale` v

mNormalize :: Matrix Double -> Matrix Double
mNormalize m = recip (det m) `scale` m
import Utils

-- |powerIterations takes a seed for a random vector and an array, and returns a (hopefully)
-- convergent list of `r` vectors zipped with their residuals using the power method; we want `r`
-- to converge to the first eigenvector of the array
powerIterations
:: Int -> Matrix Double -> [(Vector Double, Double)]
{-# NOINLINE powerIterations #-}
powerIterations seed arr =
-- NOTE: that `rows arr == cols arr`
let r = (fromList . take (rows arr) $ repeat 1) `add` (randomVector seed Uniform $ rows arr)
Expand All @@ -46,6 +31,10 @@ powerIterations seed arr =
-- to compute `lambda`), a precision parameter `epsilon`, a random seed for `powerIterations` and
-- an array; NOTE: the array should already have eigenvectors 1 through `i` zeroed out
eigVecAndVal :: Int -> Int -> Double -> Matrix Double -> (Vector Double, Double)
-- NOTE: since we implement no checks as to whether the matrix passed in is actually decomposible,
-- i.e., whether or not `r` will converge to the dominant eigenvector, `eigVecAndVal`
-- occasionally stalls calling `powerIterations`--at which point it may be useful to
-- uncomment the following line to enable visual inspection of the matrix
{-eigVecAndVal seed i epsilon arr
| trace ("eigVecAndVal " ++ show seed ++ " " ++ show i ++ " " ++ show epsilon ++ " " ++ show arr)
False = undefined-}
Expand Down
18 changes: 18 additions & 0 deletions Utils.hs
@@ -0,0 +1,18 @@
{-# LANGUAGE FlexibleContexts #-}

module Utils
( isSymmetric
, vNormalize
, mNormalize
) where

import Numeric.LinearAlgebra

isSymmetric :: (Element a, Eq a) => Matrix a -> Bool
isSymmetric a = toLists a == toLists (trans a)

vNormalize :: Vector Double -> Vector Double
vNormalize v = recip (norm2 v) `scale` v

mNormalize :: Matrix Double -> Matrix Double
mNormalize m = recip (det m) `scale` m
3 changes: 2 additions & 1 deletion main.hs
Expand Up @@ -4,7 +4,8 @@ import Data.List
import Numeric.LinearAlgebra
import System.Random

import Util
import Utils
import PowerMethod

main :: IO ()
main = do
Expand Down
3 changes: 2 additions & 1 deletion tests.hs
Expand Up @@ -9,7 +9,8 @@ import System.Random
import Test.QuickCheck
import Text.Printf

import Util
import Utils
import PowerMethod

compare :: (Eq a, Show a) => a -> a -> Property
compare ans ref = printTestCase message (ref==ans)
Expand Down

0 comments on commit 695e50d

Please sign in to comment.