packages feed

chemical-equation-0.0: src/FLINT/Example.hs

module FLINT.Example where

import qualified FLINT.Utility as F
import qualified FLINT.MatrixInteger as FmpzMat

import qualified Data.Foldable as Fold
import Text.Printf (printf)


example :: IO ()
example =
   let m = 3; n = 4 in
   F.withMatrix m n $ \mat ->
   F.withMatrix n n $ \null_basis ->

   do
   {- Set up example matrix:
   [1  2  3  4]
   [2  4  6  8]
   [3  6  9  12]
   (rank 1, nullspace dimension 3)
   -}
   FmpzMat.set_si mat 0 0 1
   FmpzMat.set_si mat 0 1 2
   FmpzMat.set_si mat 0 2 3
   FmpzMat.set_si mat 0 3 4

   FmpzMat.set_si mat 1 0 2
   FmpzMat.set_si mat 1 1 4
   FmpzMat.set_si mat 1 2 6
   FmpzMat.set_si mat 1 3 8

   FmpzMat.set_si mat 2 0 3
   FmpzMat.set_si mat 2 1 6
   FmpzMat.set_si mat 2 2 9
   FmpzMat.set_si mat 2 3 12

   nullspace_dim <- FmpzMat.nullspace null_basis mat

   printf "Matrix rank: %d\n" (toInteger (m + n - nullspace_dim))
   printf "Nullspace dimension: %d\n" (toInteger nullspace_dim)
   printf "\nNullspace basis vectors:\n"

   Fold.for_ [0 .. n-1] $ \i -> do
      Fold.for_ [0 .. nullspace_dim-1] $ \j ->
         printf "%s " =<< F.decimalFromInteger =<< FmpzMat.entry null_basis i j
      putStrLn ""