]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Values.hs
Go back to the "simplified" determinant formula in an attempt to avoid overflows.
[spline3.git] / src / Values.hs
1 {-# LANGUAGE FlexibleInstances #-}
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 unsafeIndex,
16 reshape,
17 size
18 )
19
20 import Data.Array.Repa.IO.Vector (readVectorFromTextFile,
21 writeVectorToTextFile)
22 import System.FilePath ()
23 import Test.QuickCheck (Arbitrary(..), Gen, choose, vectorOf)
24
25
26 import ScaleFactor
27
28
29 type Values sh = Array sh Double
30 type Values1D = Values DIM1
31 type Values2D = Values DIM2
32 type Values3D = Values DIM3
33
34
35 instance Arbitrary Values3D where
36 arbitrary = do
37 -- I declare not to care about empty lists.
38 x_dim <- choose (1, 27)
39 y_dim <- choose (1, 27)
40 z_dim <- choose (1, 27)
41 elements <- vectorOf (x_dim * y_dim * z_dim) (arbitrary :: Gen Double)
42 let new_shape = (Z :. x_dim :. y_dim :. z_dim)
43 let three_d = Data.Array.Repa.fromList new_shape elements
44 return three_d
45
46
47 read_values_1d :: FilePath -> IO Values1D
48 read_values_1d = readVectorFromTextFile
49
50
51 read_values_3d :: DIM3 -> FilePath -> IO Values3D
52 read_values_3d sh path = do
53 one_d <- read_values_1d path
54 return $ reshape sh one_d
55
56 write_values_1d :: Values3D -> FilePath -> IO ()
57 write_values_1d v3d path = do
58 let size3d = size $ extent v3d
59 let shape1d = (Z :. size3d)
60 let v1d = reshape shape1d v3d
61 writeVectorToTextFile v1d path
62
63 empty3d :: Values3D
64 empty3d = Data.Array.Repa.fromList (Z :. 0 :. 0 :. 0) []
65
66
67 dims :: Values3D -> (Int, Int, Int)
68 dims v3d =
69 let (Z :. x :. y :. z) = extent v3d
70 in
71 (x,y,z)
72
73
74 idx :: Values3D -> Int -> Int -> Int -> Double
75 idx v3d i j k =
76 unsafeIndex v3d shape
77 where
78 shape :: DIM3
79 shape = (Z :. i :. j :. k)
80
81
82 zoom_shape :: ScaleFactor -> DIM3 -> DIM3
83 zoom_shape (sfx, sfy, sfz) sh =
84 let (Z :. x :. y :. z) = sh
85 x' = x * sfx
86 y' = y * sfy
87 z' = z * sfz
88 in
89 (Z :. x' :. y' :. z')
90
91
92 drop_z :: DIM3 -> DIM2
93 drop_z (Z :. 1 :. y :. x) = (Z :. y :. x)
94 drop_z _ = error "can't drop the z-dimension unless its size is 1"