1 -- Glasgow Haskell 0.403 : BLAS ( Basic Linear Algebra System )
2 -- **********************************************************************
3 -- * *
4 -- * FILE NAME : vector.hs DATE : 4-3-1991 *
5 -- * *
6 -- * CONTENTS : Vector datatype and operations implemented by using *
7 -- * array type of Haskell. *
8 -- * Vectors are index from 1 to N, where N is the dimension *
9 -- * of the vector, ie the number of elements in the vector. *
10 -- **********************************************************************
13 --
14 -- Haskell provides indexable arrays, which may be thought of as
15 -- functions whose domains are isomorphic to contiguous subsets of
16 -- the integers. Such a restricted class of functions are intended
17 -- to be very efficiently implementable; in particular, the programmer
18 -- has a reasonable expectation of rapid access to the components.
19 -- To ensure the possibility of such an implementation, arrays are
20 -- treated, not as general fucntions, but as data.
22 -- Data of array type is Array i e , where i is index type and e
23 -- is element type.
25 -- Index classes of Haskell arrays:
26 -- ================================
27 --
28 -- For an one dimension array, its index is of Int type, Char type,etc.
29 -- While for a two dimension array, its index is of pair type.
30 --
31 -- Index must be within the bound of that array. The bound of an one
32 -- dimension array is a pair which gives the lower and upper bound
33 -- of its index value.
34 --
35 -- Function ( inRange :: (a,a) -> a -> Bool ) checks if an index is
36 -- in the range of the bound. Function ( range :: (a,a) -> [a] ) returns
37 -- a list of all indices in this range.
39 -- Array construction operations(functions):
40 -- =========================================
41 --
42 -- array :: (Ix a) => (a,a) -> [Assoc a b] -> Array a b
43 -- data Assoc a b = a := b
44 --
45 -- (!) :: (Ix a) => Array a b -> a -> b
46 -- bounds :: (Ix a) => Array a b -> (a,a)
47 -- assocs :: (Ix a) => (Array a b) -> [Assoc a b]
48 -- indices :: (Ix a) => (Array a b) -> [a]
49 -- rangesize :: (Ix a) => (a, a) -> Int
50 --
51 -- Array accumulating operations(functions):
52 -- =========================================
53 --
54 -- accumArray :: (Ix a) => (b->c->b) -> b -> (a,a) -> [Assoc a c]
55 -- -> Array a b
56 -- accumArray "accumulating function" "initial value"
57 -- "bounds" "association list"
59 -- Array increment update operations(functions):
60 -- =============================================
61 --
62 -- (//) :: (Ix a) => Array a b -> Assoc a b -> Array a b
63 -- It takes an array and an Assoc pair and returns an array
64 -- identical to the first argument except for the one element
65 -- specified by the second argument.
66 --
67 -- accum :: (Ix a) => (b->c->b) -> Array a b -> [Assoc a c] -> Array a b
68 -- accum "accumulating function" "array to be updated" "association list"
70 -- Other operations(functions):
71 -- ============================
72 --
73 -- listArray :: (Ix a) => (a, a) -> [b] -> Array a b
74 -- elems :: (Ix a) => (Array a a) -> [a]
75 --
76 -- amap :: (Ix a) => (b->c) -> Array a b -> Array a c
77 -- ixmap :: (Ix a, Ix b) => (b,b) -> (b->a) -> Array a c -> Array b c
80 module Vector(Vec, makevec, boundvec, vecsub, incrvec, updvec, maxupdvec,
81 vecprod, displayvec) where
83 import Data.Array
84 import Basics
86 data Vec a = VEC Int (Array Int a)
88 displayvec :: (Show a) => Vec a -> [Char]
90 vecprod :: (Num a) => Vec a -> Vec a -> a
92 updvec :: Vec a -> [(Int,a)] -> Vec a
94 maxupdvec :: (Num a, Ord a) => Vec a -> [(Int,a)] -> Vec a
96 incrvec :: (Num a) => Vec a -> [(Int,a)] -> Vec a
98 vecsub :: Vec a -> Int -> a
100 boundvec :: Vec a -> Int
102 makevec :: Int -> (Int -> a) -> Vec a
104 makevec n f = VEC n (array (1,n) [ (i,f i) | i <- [1..n] ])
106 boundvec (VEC n _) = n
108 vecsub (VEC n va) i = va ! i
110 updvec (VEC n va) s =
111 VEC n (accum f va s)
112 where
113 f b c = c
115 maxupdvec (VEC n va) s = VEC n (accum max va s)
117 incrvec (VEC n va) s = VEC n (accum (+) va s)
119 vecprod v1 v2 =
120 sum [(vecsub v1 i) * (vecsub v2 i) | i <- [1..n] ]
121 where
122 n = boundvec v1
124 displayvec v =
125 "< " ++
126 concat ([(showrj 8 (vecsub v i) ) | i<- [1..n] ] ) ++
127 ">\n"
128 where
129 n = boundvec v