summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric/LinearAlgebra/Real.hs
blob: db15705efab11e53c98166ab88667223fd1508de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE GADTs #-}


{- |
Module      :  Numeric.LinearAlgebra.Real
Copyright   :  (c) Alberto Ruiz 2006-14
License     :  BSD3
Stability   :  provisional

Experimental interface for real arrays with statically checked dimensions.

-}

module Numeric.LinearAlgebra.Real(
    -- * Vector
    R,
    vec2, vec3, vec4, 𝕧, (&),
    -- * Matrix
    L, Sq,
    𝕞,
    (#),(¦),(——),
     Konst(..),
     eye,
     diagR, diag,
     blockAt,
    -- * Products
     (<>),(#>),(<·>),
     -- * Pretty printing
     Disp(..),
     -- * Misc
     Dim, unDim,
    module Numeric.HMatrix
) where


import GHC.TypeLits
import Numeric.HMatrix hiding ((<>),(#>),(<·>),Konst(..),diag, disp,(¦),(——))
import qualified Numeric.HMatrix as LA
import Data.Packed.ST

newtype Dim (n :: Nat) t = Dim t
  deriving Show

unDim :: Dim n t -> t
unDim (Dim x) = x

data Proxy :: Nat -> *


lift1F
  :: (c t -> c t)
  -> Dim n (c t) -> Dim n (c t)
lift1F f (Dim v) = Dim (f v)

lift2F
  :: (c t -> c t -> c t)
  -> Dim n (c t) -> Dim n (c t) -> Dim n (c t)
lift2F f (Dim u) (Dim v) = Dim (f u v)



type R n = Dim n (Vector ℝ)

type L m n = Dim m (Dim n (Matrix ℝ))


infixl 4 &
(&) :: forall n . KnownNat n
    => R n -> ℝ -> R (n+1)
Dim v & x = Dim (vjoin [v', scalar x])
  where
    d = fromIntegral . natVal $ (undefined :: Proxy n)
    v' | d > 1 && size v == 1 = LA.konst (v!0) d
       | otherwise = v


-- vect0 :: R 0
-- vect0 = Dim (fromList[])

𝕧 :: ℝ -> R 1
𝕧 = Dim . scalar


vec2 :: ℝ -> ℝ -> R 2
vec2 a b = Dim $ runSTVector $ do
    v <- newUndefinedVector 2
    writeVector v 0 a
    writeVector v 1 b
    return v

vec3 :: ℝ -> ℝ -> ℝ -> R 3
vec3 a b c = Dim $ runSTVector $ do
    v <- newUndefinedVector 3
    writeVector v 0 a
    writeVector v 1 b
    writeVector v 2 c
    return v


vec4 :: ℝ -> ℝ -> ℝ -> ℝ -> R 4
vec4 a b c d = Dim $ runSTVector $ do
    v <- newUndefinedVector 4
    writeVector v 0 a
    writeVector v 1 b
    writeVector v 2 c
    writeVector v 3 d
    return v




instance forall n t . (Num (Vector t), Numeric t )=> Num (Dim n (Vector t))
  where
    (+) = lift2F (+)
    (*) = lift2F (*)
    (-) = lift2F (-)
    abs = lift1F abs
    signum = lift1F signum
    negate = lift1F negate
    fromInteger x = Dim (fromInteger x)

instance (Num (Matrix t), Numeric t) => Num (Dim m (Dim n (Matrix t)))
  where
    (+) = (lift2F . lift2F) (+)
    (*) = (lift2F . lift2F) (*)
    (-) = (lift2F . lift2F) (-)
    abs = (lift1F . lift1F) abs
    signum = (lift1F . lift1F) signum
    negate = (lift1F . lift1F) negate
    fromInteger x = Dim (Dim (fromInteger x))

--------------------------------------------------------------------------------

class Konst t
  where
    konst :: ℝ -> t

instance forall n. KnownNat n => Konst (R n)
  where
    konst x = Dim (LA.konst x d)
      where
        d = fromIntegral . natVal $ (undefined :: Proxy n)

instance forall m n . (KnownNat m, KnownNat n) => Konst (L m n)
  where
    konst x = Dim (Dim (LA.konst x (m',n')))
      where
        m' = fromIntegral . natVal $ (undefined :: Proxy m)
        n' = fromIntegral . natVal $ (undefined :: Proxy n)

--------------------------------------------------------------------------------

diagR :: forall m n k . (KnownNat m, KnownNat n) => ℝ -> R k -> L m n
diagR x v = Dim (Dim (diagRect x (unDim v) m' n'))
  where
    m' = fromIntegral . natVal $ (undefined :: Proxy m)
    n' = fromIntegral . natVal $ (undefined :: Proxy n)

diag :: KnownNat n => R n -> Sq n
diag = diagR 0

--------------------------------------------------------------------------------

blockAt :: forall m n . (KnownNat m, KnownNat n) =>  ℝ -> Int -> Int -> Matrix Double -> L m n
blockAt x r c a = Dim (Dim res)
  where
    z = scalar x
    z1 = LA.konst x (r,c)
    z2 = LA.konst x (max 0 (m'-(ra+r)), max 0 (n'-(ca+c)))
    ra = min (rows a) . max 0 $ m'-r
    ca = min (cols a) . max 0 $ n'-c
    sa = subMatrix (0,0) (ra, ca) a
    m' = fromIntegral . natVal $ (undefined :: Proxy m)
    n' = fromIntegral . natVal $ (undefined :: Proxy n)
    res = fromBlocks [[z1,z,z],[z,sa,z],[z,z,z2]]

{-
matrix :: (KnownNat m, KnownNat n) => Matrix Double -> L n m
matrix = blockAt 0 0 0
-}

--------------------------------------------------------------------------------

class Disp t
  where
    disp :: Int -> t -> IO ()

instance Disp (L n m)
  where
    disp n (d2 -> a) = do
        if rows a == 1 && cols a == 1
            then putStrLn $ "Const " ++ (last . words . LA.dispf n $ a)
            else putStr "Dim " >> LA.disp n a

instance Disp (R n)
  where
    disp n (unDim -> v) = do
        let su = LA.dispf n (asRow v)
        if LA.size v == 1
            then putStrLn $ "Const " ++ (last . words $ su )
            else putStr "Dim " >> putStr (tail . dropWhile (/='x') $ su)

--------------------------------------------------------------------------------

infixl 3 #
(#) :: L r c -> R c -> L (r+1) c
Dim (Dim m) # Dim v = Dim (Dim (m LA.—— asRow v))


𝕞  :: forall n . KnownNat n => L 0 n
𝕞  = Dim (Dim (LA.konst 0 (0,d)))
  where
    d = fromIntegral . natVal $ (undefined :: Proxy n)

infixl 3 ¦
(¦) :: L r c1 -> L r c2 -> L r (c1+c2)
Dim (Dim a) ¦ Dim (Dim b) = Dim (Dim (a LA.¦ b))

infixl 2 ——
(——) :: L r1 c -> L r2 c -> L (r1+r2) c
Dim (Dim a) —— Dim (Dim b) = Dim (Dim (a LA.—— b))


{-

-}

type Sq n = L n n

type GL = (KnownNat n, KnownNat m) => L m n
type GSq = KnownNat n => Sq n

infixr 8 <>
(<>) :: L m k -> L k n -> L m n
(d2 -> a) <> (d2 -> b) = Dim (Dim (a LA.<> b))

infixr 8 #>
(#>) :: L m n -> R n -> R m
(d2 -> m) #> (unDim -> v) = Dim (m LA.#> v)

infixr 8 <·>
(<·>) :: R n -> R n -> ℝ
(unDim -> u) <·> (unDim -> v) = udot u v


d2 :: forall c (n :: Nat) (n1 :: Nat). Dim n1 (Dim n c) -> c
d2 = unDim . unDim


instance Transposable (L m n) (L n m)
  where
    tr (Dim (Dim a)) = Dim (Dim (tr a))


eye :: forall n . KnownNat n => Sq n
eye = Dim (Dim (ident d))
  where
    d = fromIntegral . natVal $ (undefined :: Proxy n)


--------------------------------------------------------------------------------

test :: (Bool, IO ())
test = (ok,info)
  where
    ok =   d2 (eye :: Sq 5) == ident 5
           && d2 (mTm sm :: Sq 3) == tr ((3><3)[1..]) LA.<> (3><3)[1..]
           && d2 (tm :: L 3 5) == mat 5 [1..15]
           && thingS == thingD
           && precS == precD

    info = do
        print $ u
        print $ v
        print (eye :: Sq 3)
        print $ ((u & 5) + 1) <·> v
        print (tm :: L 2 5)
        print (tm <> sm :: L 2 3)
        print thingS
        print thingD
        print precS
        print precD

    u = vec2 3 5

    v = 𝕧 2 & 4 & 7

    mTm :: L n m -> Sq m
    mTm a = tr a <> a

    tm :: GL
    tm = lmat 0 [1..]

    lmat :: forall m n . (KnownNat m, KnownNat n) => ℝ -> [ℝ] -> L m n
    lmat z xs = Dim . Dim . reshape n' . fromList . take (m'*n') $ xs ++ repeat z
      where
        m' = fromIntegral . natVal $ (undefined :: Proxy m)
        n' = fromIntegral . natVal $ (undefined :: Proxy n)

    sm :: GSq
    sm = lmat 0 [1..]

    thingS = (u & 1) <·> tr q #> q #> v
      where
        q = tm :: L 10 3

    thingD = vjoin [unDim u, 1] LA.<·> tr m LA.#> m LA.#> unDim v
      where
        m = mat 3 [1..30]

    precS = (1::Double) + (2::Double) * ((1 :: R 3) * (u & 6)) <·> konst 2 #> v
    precD = 1 + 2 * vjoin[unDim u, 6] LA.<·> LA.konst 2 (size (unDim u) +1, size (unDim v)) LA.#> unDim v


instance (KnownNat n', KnownNat m') => Testable (L n' m')
  where
    checkT _ = test

{-
do (snd test)
fst test
-}