module Numerics.LinearEquationSystem (
solveTridiagonal
) where
import Algebra.VectorSpace
import Data.Vector (Vector, (!))
import qualified Data.Vector as V
solveTridiagonal
:: VectorSpace vec
=> Vector Double
-> Vector Double
-> Vector Double
-> Vector vec
-> Vector vec
solveTridiagonal :: forall vec.
VectorSpace vec =>
Vector Double
-> Vector Double -> Vector Double -> Vector vec -> Vector vec
solveTridiagonal Vector Double
a Vector Double
b Vector Double
c Vector vec
d
= let n :: Int
n = Vector vec -> Int
forall a. Vector a -> Int
V.length Vector vec
d
ifor :: Vector a -> (Int -> a -> b) -> Vector b
ifor = ((Int -> a -> b) -> Vector a -> Vector b)
-> Vector a -> (Int -> a -> b) -> Vector b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Int -> a -> b) -> Vector a -> Vector b
forall a b. (Int -> a -> b) -> Vector a -> Vector b
V.imap
c' :: Vector Double
c' = Vector Double -> (Int -> Double -> Double) -> Vector Double
forall {a} {b}. Vector a -> (Int -> a -> b) -> Vector b
ifor Vector Double
c ((Int -> Double -> Double) -> Vector Double)
-> (Int -> Double -> Double) -> Vector Double
forall a b. (a -> b) -> a -> b
$ \Int
i Double
c_i -> case Int
i of
Int
0 -> Double
c_i Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Vector Double
bVector Double -> Int -> Double
forall a. Vector a -> Int -> a
!Int
i
Int
_ -> Double
c_i Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Vector Double
bVector Double -> Int -> Double
forall a. Vector a -> Int -> a
!Int
i Double -> Double -> Double
forall a. Num a => a -> a -> a
- Vector Double
aVector Double -> Int -> Double
forall a. Vector a -> Int -> a
!(Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Vector Double
c'Vector Double -> Int -> Double
forall a. Vector a -> Int -> a
!(Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
d' :: Vector vec
d' = Vector vec -> (Int -> vec -> vec) -> Vector vec
forall {a} {b}. Vector a -> (Int -> a -> b) -> Vector b
ifor Vector vec
d ((Int -> vec -> vec) -> Vector vec)
-> (Int -> vec -> vec) -> Vector vec
forall a b. (a -> b) -> a -> b
$ \Int
i vec
d_i -> case Int
i of
Int
0 -> vec
d_i vec -> Double -> vec
forall v. VectorSpace v => v -> Double -> v
/. Vector Double
bVector Double -> Int -> Double
forall a. Vector a -> Int -> a
!Int
i
Int
_ -> (vec
d_i vec -> vec -> vec
forall v. VectorSpace v => v -> v -> v
-. Vector Double
aVector Double -> Int -> Double
forall a. Vector a -> Int -> a
!(Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Double -> vec -> vec
forall v. VectorSpace v => Double -> v -> v
*. Vector vec
d'Vector vec -> Int -> vec
forall a. Vector a -> Int -> a
!(Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)) vec -> Double -> vec
forall v. VectorSpace v => v -> Double -> v
/. (Vector Double
bVector Double -> Int -> Double
forall a. Vector a -> Int -> a
!Int
i Double -> Double -> Double
forall a. Num a => a -> a -> a
- Vector Double
aVector Double -> Int -> Double
forall a. Vector a -> Int -> a
!(Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Vector Double
c'Vector Double -> Int -> Double
forall a. Vector a -> Int -> a
!(Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
x :: Vector vec
x = Vector vec -> (Int -> vec -> vec) -> Vector vec
forall {a} {b}. Vector a -> (Int -> a -> b) -> Vector b
ifor Vector vec
d' ((Int -> vec -> vec) -> Vector vec)
-> (Int -> vec -> vec) -> Vector vec
forall a b. (a -> b) -> a -> b
$ \Int
i vec
d'_i -> case Int
i of
Int
_ | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1 -> vec
d'_i
Int
_ -> vec
d'_i vec -> vec -> vec
forall v. VectorSpace v => v -> v -> v
-. Vector Double
c'Vector Double -> Int -> Double
forall a. Vector a -> Int -> a
!Int
i Double -> vec -> vec
forall v. VectorSpace v => Double -> v -> v
*. Vector vec
xVector vec -> Int -> vec
forall a. Vector a -> Int -> a
!(Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
in Vector vec
x