[Haskell-cafe] HMatrix Vector/Matrix interpolation ala Matlab interp/interp2 ??
gutti
philipp.guttenberg at gmx.net
Tue Jan 25 23:16:49 CET 2011
Hi Henning,
thanks for the code review -- reason that I don't use the type declaration a
lot -- It causes trouble , because I don't yet fully understand it.
When I declare what I think is right is fails - see Message at bottom -- so
what's wrong ?
By the way I just used lists so far - no arrays -- Why is !! inefficient ? -
what is better - Cheers Phil
1 import Data.List
2
3 -- Input Data
4 xi :: [Double]
5 xi = [0 .. 10]
6 yi :: [Double]
7 yi = [2, 3, 5, 6, 7, 8, 9, 10 , 9, 8, 7]
8 x = 11 :: Double
9
10 -- Functions
11 limIndex :: [a] -> Int -> Int
12 limIndex xi idx
13 | idx < 0 = 0
14 | idx > (length xi)-2 = (length xi)-2
15 | otherwise = idx
16
17 getIndex :: [a] -> Double -> Int
18 getIndex xi x = limIndex xi (maybe (length xi) id (findIndex (>x) xi)-1)
19
20 getPnts :: [a] -> [a] -> Int -> [a]
21 getPnts xi yi idx = [xi !! idx, xi !! (idx+1), yi !! idx, yi !! (idx+1)]
22
23 interp :: [a] -> [a] -> Double -> Double
24 interp xi yi x =
25 let pts = getPnts xi yi (getIndex xi x)
26 in (pts!!3-pts!!2)/(pts!!1-pts!!0)*(x-pts!!0)+pts!!2
27
28 -- Calc
29 y = interp xi yi x
30
31 main = do
32 -- Output Data
33 print (y)
=== Compiler Error Message ===
interp_v4.hs:18:66:
Couldn't match expected type `Double' against inferred type `a'
`a' is a rigid type variable bound by
the type signature for `getIndex' at interp_v4.hs:17:13
Expected type: [Double]
Inferred type: [a]
In the second argument of `findIndex', namely `xi'
In the third argument of `maybe', namely `(findIndex (> x) xi)'
interp_v4.hs:26:39:
Couldn't match expected type `Double' against inferred type `a'
`a' is a rigid type variable bound by
the type signature for `interp' at interp_v4.hs:23:11
In the second argument of `(-)', namely `pts !! 0'
In the second argument of `(*)', namely `(x - pts !! 0)'
In the first argument of `(+)', namely
`(pts !! 3 - pts !! 2) / (pts !! 1 - pts !! 0) * (x - pts !! 0)'
--
View this message in context: http://haskell.1045720.n5.nabble.com/HMatrix-Vector-Matrix-interpolation-ala-Matlab-interp-interp2-tp3352833p3357089.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
More information about the Haskell-Cafe
mailing list