numeric-quest-0.2.1: test/Test/RowEchelon.hs
-- Do not edit! Automatically created with doctest-extract from RowEchelon.hs
{-# LINE 26 "RowEchelon.hs" #-}
module Test.RowEchelon where
import Test.DocTest.Base
import qualified Test.DocTest.Driver as DocTest
{-# LINE 27 "RowEchelon.hs" #-}
import RowEchelon as Matrix
import qualified Test.QuickCheck as QC
import Test.QuickCheck ((===))
import Control.Monad (replicateM)
import qualified Data.Foldable as Fold
import qualified Data.NonEmpty.Class as NonEmptyC
import qualified Data.NonEmpty as NonEmpty
import qualified Data.List.Match as Match
import qualified Data.List.HT as ListHT
import Data.NonEmpty ((!:))
import Data.Ratio ((%))
genElementUniform, genElementNearZero :: QC.Gen Integer
genElementUniform = QC.choose (-10,10)
genElementNearZero = fmap (flip rem 11) $ QC.arbitrary
genMatrixForSize :: Int -> Int -> QC.Gen (Matrix Integer)
genMatrixForSize m n = do
fmap (Matrix n) $ replicateM m $ replicateM n genElementNearZero
genMatrix :: QC.Gen (Matrix Integer)
genMatrix = do
m <- QC.choose (0,10)
n <- QC.choose (0,10)
genMatrixForSize m n
shrinkMatrix :: (Eq a) => Matrix a -> [Matrix a]
shrinkMatrix matrix@(Matrix width rows) =
filter (matrix/=) $
map (Matrix width . snd) (ListHT.removeEach rows)
++
[Matrix (width-1) $ map (drop 1) rows]
forMatrix :: (QC.Testable test) => (Matrix Rational -> test) -> QC.Property
forMatrix prop =
QC.forAllShrink genMatrix shrinkMatrix (prop . rationalMatrix)
rationalMatrix :: Matrix Integer -> Matrix Rational
rationalMatrix = fmap (%1)
test :: DocTest.T ()
test = do
DocTest.printPrefix "RowEchelon:142: "
{-# LINE 142 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 142 "RowEchelon.hs" #-}
nullspace (Matrix 2 []) :: Matrix Rational
)
[ExpectedLine [LineChunk "Matrix 2 [[1 % 1,0 % 1],[0 % 1,1 % 1]]"]]
DocTest.printPrefix "RowEchelon:146: "
{-# LINE 146 "RowEchelon.hs" #-}
DocTest.property(
{-# LINE 146 "RowEchelon.hs" #-}
forMatrix $ matrixValid . nullspace
)
DocTest.printPrefix "RowEchelon:149: "
{-# LINE 149 "RowEchelon.hs" #-}
DocTest.property(
{-# LINE 149 "RowEchelon.hs" #-}
forMatrix $ \matrix ->
matrixWidth matrix == matrixHeight (nullspace matrix)
)
DocTest.printPrefix "RowEchelon:154: "
{-# LINE 154 "RowEchelon.hs" #-}
DocTest.property(
{-# LINE 154 "RowEchelon.hs" #-}
forMatrix $ \matrix ->
matrixWidth (nullspace matrix) <= matrixWidth matrix
)
DocTest.printPrefix "RowEchelon:161: "
{-# LINE 161 "RowEchelon.hs" #-}
DocTest.property(
{-# LINE 161 "RowEchelon.hs" #-}
forMatrix $ \matrix ->
matrixWidth matrix <= matrixWidth (nullspace matrix) + matrixHeight matrix
)
DocTest.printPrefix "RowEchelon:166: "
{-# LINE 166 "RowEchelon.hs" #-}
DocTest.property(
{-# LINE 166 "RowEchelon.hs" #-}
forMatrix $ \matrix ->
Fold.all (0==) $ matrixProduct matrix (nullspace matrix)
)
DocTest.printPrefix "RowEchelon:224: "
{-# LINE 224 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 224 "RowEchelon.hs" #-}
scatter (3 !: 1 : 4 : 1 : []) "012" ['a'..'k']
)
[ExpectedLine [LineChunk "\"abc0d1efgh2ijk\""]]
DocTest.printPrefix "RowEchelon:240: "
{-# LINE 240 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 240 "RowEchelon.hs" #-}
layoutEchelonBlocks $ reducedRowEchelon $ matrixFromRows [[0::Rational]]
)
[ExpectedLine [LineChunk "Matrix 1 []"]]
DocTest.printPrefix "RowEchelon:243: "
{-# LINE 243 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 243 "RowEchelon.hs" #-}
layoutEchelonBlocks $ reducedRowEchelon $ matrixFromRows [[1,3,0,5::Rational]]
)
[ExpectedLine [LineChunk "Matrix 3 [[3 % 1,0 % 1,5 % 1]]"]]
DocTest.printPrefix "RowEchelon:246: "
{-# LINE 246 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 246 "RowEchelon.hs" #-}
layoutEchelonBlocks $ reducedRowEchelon $ matrixFromRows [[0,1,3,5::Rational]]
)
[ExpectedLine [LineChunk "Matrix 3 [[0 % 1,3 % 1,5 % 1]]"]]
DocTest.printPrefix "RowEchelon:249: "
{-# LINE 249 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 249 "RowEchelon.hs" #-}
layoutEchelonBlocks $ reducedRowEchelon $ matrixFromRows [[1,3,0,5],[0,0,1,7::Rational]]
)
[ExpectedLine [LineChunk "Matrix 2 [[3 % 1,5 % 1],[0 % 1,7 % 1]]"]]
DocTest.printPrefix "RowEchelon:252: "
{-# LINE 252 "RowEchelon.hs" #-}
DocTest.property(
{-# LINE 252 "RowEchelon.hs" #-}
forMatrix $ \matrix ->
(layoutEchelonBlocks $ reducedRowEchelon $
identity (matrixHeight matrix) ||| matrix)
===
matrix
)
DocTest.printPrefix "RowEchelon:263: "
{-# LINE 263 "RowEchelon.hs" #-}
DocTest.property(
{-# LINE 263 "RowEchelon.hs" #-}
QC.forAll (fmap (flip mod 10) . NonEmpty.mapTail (take 9) <$> QC.arbitrary) $
\blockWidths ->
QC.forAll (traverse (uncurry genMatrixForSize) $
NonEmptyC.zip (0!:[1..]) blockWidths) $
\blocksInt ->
let blocks :: NonEmpty.T [] (Matrix Rational)
blocks = fmap (fmap (%1)) blocksInt
unitBesidesBlock block =
Matrix (matrixWidth block + 1) $
zipWith (:)
(drop 1 $ Match.replicate (matrixRows block) 0 ++ [1])
(matrixRows block)
echelonWithInterleavedEye =
Fold.foldr1 shortBesidesTall $
NonEmpty.mapTail (map unitBesidesBlock) blocks
in
layoutEchelonBlocks (reducedRowEchelon echelonWithInterleavedEye)
===
Fold.foldr1 shortBesidesTall blocks
)
DocTest.printPrefix "RowEchelon:329: "
{-# LINE 329 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 329 "RowEchelon.hs" #-}
reducedRowEchelon $ matrixFromRows [[2,1,3::Rational]]
)
[ExpectedLine [LineChunk "Matrix 0 [] ./ 2 % 1 /. Matrix 2 [[1 % 2,3 % 2]] ./ []"]]
DocTest.printPrefix "RowEchelon:332: "
{-# LINE 332 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 332 "RowEchelon.hs" #-}
reducedRowEchelon $ matrixFromRows [[2],[1],[3::Rational]]
)
[ExpectedLine [LineChunk "Matrix 0 [] ./ 3 % 1 /. Matrix 0 [[]] ./ []"]]
DocTest.printPrefix "RowEchelon:335: "
{-# LINE 335 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 335 "RowEchelon.hs" #-}
reducedRowEchelon $ matrixFromRows [[1,0,2],[0,1,3::Rational]]
)
[ExpectedLine [LineChunk "Matrix 0 [] ./ 1 % 1 /. Matrix 0 [[]] ./ 1 % 1 /. Matrix 1 [[2 % 1],[3 % 1]] ./ []"]]
DocTest.printPrefix "RowEchelon:338: "
{-# LINE 338 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 338 "RowEchelon.hs" #-}
reducedRowEchelon $ matrixFromRows [[1,3,0,5],[0,0,1,7::Rational]]
)
[ExpectedLine [LineChunk "Matrix 0 [] ./ 1 % 1 /. Matrix 1 [[3 % 1]] ./ 1 % 1 /. Matrix 1 [[5 % 1],[7 % 1]] ./ []"]]
DocTest.printPrefix "RowEchelon:341: "
{-# LINE 341 "RowEchelon.hs" #-}
DocTest.property(
{-# LINE 341 "RowEchelon.hs" #-}
forMatrix $ \matrix ->
Fold.all matrixValid $ altOuter $ reducedRowEchelon matrix
)
DocTest.printPrefix "RowEchelon:357: "
{-# LINE 357 "RowEchelon.hs" #-}
DocTest.example(
{-# LINE 357 "RowEchelon.hs" #-}
rowReduction $ Zipper0 [[1,2,3,4,5],[6,7,8,9,10::Rational]] [[0,2,0,0,0],[3,0,0,0,0]]
)
[ExpectedLine [LineChunk "(0,Just (3 % 1,[[],[]],Zipper0 [[0 % 1,0 % 1,0 % 1,0 % 1],[2 % 1,3 % 1,4 % 1,5 % 1],[7 % 1,8 % 1,9 % 1,10 % 1]] [[2 % 1,0 % 1,0 % 1,0 % 1]]))"]]