+ coeff = one / (sqrt (four*(fromInteger k) - two)) :: a
+
+
+-- | A matrix containing 'big_N' functions indexed by their
+-- element/number. Each row in the matrix represents a finite element
+-- (i.e. an interval in the mesh). Within row @i@, column @j@ contains
+-- the @j@th 'big_N' basis function.
+--
+-- Any given 'big_N' will probably wind up in this matrix multiple
+-- times; the basis functions don't change depending on the
+-- interval. Only the /number/ of basis functions does. Computing
+-- them this way allows us to easily construct a lookup \"table\" of
+-- the proper dimensions.
+--
+-- The second example below relies on the fact that @big_N 3@ and
+-- @big_N 6@ expand to Legendre polynomials (2,4) and (5,7)
+-- respectively and so should be orthogonal over [-1,1].
+--
+-- Examples:
+--
+-- >>> import Data.Vector.Fixed ( N5 )
+-- >>> import Integration.Gaussian ( gaussian )
+-- >>> import Linear.Matrix ( Col5, fromList )
+-- >>> import Naturals ( N19 )
+--
+-- >>> let p = fromList [[3],[3],[5],[4],[5]] :: Col5 Int
+-- >>> let mesh = undefined :: Col5 (Double,Double)
+-- >>> let params = Params mesh p :: Params N5 N5 N19 Double
+-- >>> let big_ns = big_N_matrix params
+-- >>> let n1 = big_ns !!! (1,0)
+-- >>> let n4 = big_ns !!! (4,0)
+-- >>> n1 1.5 == n4 1.5
+-- True
+-- >>> let n1 = big_ns !!! (1,3)
+-- >>> let n2 = big_ns !!! (2,4)
+-- >>> gaussian (\x -> (n1 x) * (n2 x)) < 1e-12
+-- True
+--
+big_N_matrix :: (Arity m, Arity n, Arity l, Algebraic.C a, RealField.C a)
+ => Params m n l a
+ -> Mat m (S n) (a -> a)
+big_N_matrix params =
+ construct lambda
+ where
+ maxdeg i = (max_degrees params) !!! (i,0)
+ lambda i j = if j > maxdeg i
+ then (const $ fromInteger 0)
+ else big_N (toInteger j)
+
+
+
+-- | Compute the global load vector F.
+--
+-- Examples:
+--
+-- >>> import Data.Vector.Fixed ( N3, N4 )
+-- >>> import Linear.Matrix ( Col4, frobenius_norm, fromList )
+-- >>> import Naturals ( N7 )
+--
+-- >>> let big_A = const (1::Double)
+-- >>> let c x = sin x
+-- >>> let f x = x*(sin x)
+-- >>> let bdy = Left (Dirichlet (0,1::Double))
+-- >>> let pde = PDE big_A c f bdy
+--
+-- >>> let i1 = (0.0,1/3)
+-- >>> let i2 = (1/3,2/3)
+-- >>> let i3 = (2/3,4/5)
+-- >>> let i4 = (4/5,1.0)
+-- >>> let mesh = fromList [[i1], [i2], [i3], [i4]] :: Col4 (Double,Double)
+-- >>> let pvec = fromList [[2],[3],[2],[1]] :: Col4 Int
+-- >>> let params = Params mesh pvec :: Params N4 N3 N7 Double
+--
+-- >>> let f1 = [0.0418]
+-- >>> let f2 = [0.0805]
+-- >>> let f3 = [0.1007]
+-- >>> let f4 = [-0.0045]
+-- >>> let f5 = [-0.0332]
+-- >>> let f6 = [-0.0054]
+-- >>> let f7 = [-0.0267]
+-- >>> let expected = fromList [f1,f2,f3,f4,f5,f6,f7] :: Col N7 Double
+-- >>> let actual = big_F pde params
+-- >>> frobenius_norm (actual - expected) < 1e-4
+-- True
+--
+big_F :: forall m n l a.
+ (Arity l, Arity m, Arity n,
+ Algebraic.C a, RealField.C a, ToRational.C a)
+ => PDE a
+ -> Params m n l a
+ -> Col l a
+big_F pde params =
+ ifoldl2 accum zero (big_N_matrix params)
+ where
+ accum :: Int -> Int -> Col l a -> (a -> a) -> Col l a
+ accum i j prev_F this_N =
+ prev_F + this_F
+ where
+ two = fromInteger 2
+ (x1,x2) = (mesh params) !!! (i,0)
+ q = affine_inv (x1,x2)
+ integrand x = ((f pde) (q x)) * (this_N x)
+
+ -- The pointer matrix numbers from 1 so subtract one here to
+ -- get the right index.
+ global_idx = ((pointer params) !!! (i,j)) - 1
+ this_F = construct lambda
+ lambda k _ = if k == global_idx
+ then (gaussian integrand)*(x2 - x1) / two
+ else fromInteger 0