]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Values.hs
Begin updating everything to use Repa arrays (Values3D).
[spline3.git] / src / Values.hs
1 {-# LANGUAGE TypeSynonymInstances #-}
2
3 module Values
4 where
5
6 import Data.Array.Repa (
7 Array,
8 Z(..),
9 (:.)(..),
10 DIM1,
11 DIM2,
12 DIM3,
13 extent,
14 fromList,
15 index,
16 reshape,
17 )
18
19 import Data.Array.Repa.IO.Vector (readVectorFromTextFile)
20 import System.FilePath ()
21 import Test.QuickCheck (Arbitrary(..), Gen)
22
23
24 type Values1D = Array DIM1 Double
25 type Values2D = Array DIM2 Double
26 type Values3D = Array DIM3 Double
27
28
29 instance Arbitrary Values3D where
30 arbitrary = do
31 x_dim <- arbitrary :: Gen Int
32 y_dim <- arbitrary :: Gen Int
33 z_dim <- arbitrary :: Gen Int
34 one_d <- arbitrary :: Gen Values1D
35 let new_shape = (Z :. x_dim :. y_dim :. z_dim)
36 let three_d = reshape new_shape one_d
37 return three_d
38
39
40 instance Arbitrary Values1D where
41 arbitrary = do
42 x <- arbitrary :: Gen [Double]
43 let shape = (Z :. (length x))
44 let one_d = Data.Array.Repa.fromList shape x
45 return one_d
46
47
48 read_values_1d :: FilePath -> IO Values1D
49 read_values_1d path = readVectorFromTextFile path
50
51
52 read_values_3d :: DIM3 -> FilePath -> IO Values3D
53 read_values_3d sh path = do
54 one_d <- read_values_1d path
55 return $ reshape sh one_d
56
57
58 empty3d :: Values3D
59 empty3d = Data.Array.Repa.fromList (Z :. 0 :. 0 :. 0) []
60
61
62 dims :: Values3D -> (Int, Int, Int)
63 dims v3d =
64 let (Z :. x :. y :. z) = extent v3d
65 in
66 (x,y,z)
67
68
69 idx :: Values3D -> Int -> Int -> Int -> Double
70 idx v3d i j k =
71 index v3d shape
72 where
73 shape :: DIM3
74 shape = (Z :. i :. j :. k)