{-# LANGUAGE NoImplicitPrelude #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE FlexibleInstances #-}{- |
Copyright : (c) Henning Thielemann 2009, Mikael Johansson 2006
Maintainer : numericprelude@henning-thielemann.de
Stability : provisional
Portability : requires multi-parameter type classes
Routines and abstractions for Matrices and
basic linear algebra over fields or rings.
We stick to simple Int indices.
Although advanced indices would be nice
e.g. for matrices with sub-matrices,
this is not easily implemented since arrays
do only support a lower and an upper bound
but no additional parameters.
ToDo:
- Matrix inverse, determinant
-}moduleMathObj.Matrix(T,Dimension,format,transpose,rows,columns,index,fromRows,fromColumns,fromList,dimension,numRows,numColumns,zipWith,zero,one,diagonal,scale,random,randomR,)whereimportqualifiedAlgebra.ModuleasModuleimportqualifiedAlgebra.VectorasVectorimportqualifiedAlgebra.RingasRingimportqualifiedAlgebra.AdditiveasAdditiveimportAlgebra.Module((*>),)importAlgebra.Ring((*),fromInteger,scalarProduct,)importAlgebra.Additive((+),(-),subtract,)importqualifiedSystem.RandomasRndimportData.Array(Array,array,listArray,accumArray,elems,bounds,(!),ixmap,range,)importqualifiedData.ListasListimportControl.Monad(liftM2,)importControl.Exception(assert,)importData.Function.HT(powerAssociative,)importData.Tuple.HT(swap,mapFst,)importData.List.HT(outerProduct,)importText.Show.HT(concatS,)importNumericPrelude.Numeric(Int,)importNumericPrelude.Basehiding(zipWith,){- |
A matrix is a twodimensional array, indexed by integers.
-}dataTa=Cons(Array(Dimension,Dimension)a)deriving(Eq,Ord,Read)typeDimension=Int{- |
Transposition of matrices is just transposition in the sense of Data.List.
-}transpose::Ta->Tatranspose(Consm)=let(lower,upper)=boundsminCons(ixmap(swaplower,swapupper)swapm)rows::Ta->[[a]]rowsmM@(Consm)=let((lr,lc),(ur,uc))=boundsminouterProduct(indexmM)(range(lr,ur))(range(lc,uc))columns::Ta->[[a]]columnsmM@(Consm)=let((lr,lc),(ur,uc))=boundsminouterProduct(flip(indexmM))(range(lc,uc))(range(lr,ur))index::Ta->Dimension->Dimension->aindex(Consm)ij=m!(i,j)fromRows::Dimension->Dimension->[[a]]->TafromRowsmn=Cons.array(indexBoundsmn).concat.List.zipWith(\r->map(\(c,x)->((r,c),x)))allIndices.map(zipallIndices)fromColumns::Dimension->Dimension->[[a]]->TafromColumnsmn=Cons.array(indexBoundsmn).concat.List.zipWith(\r->map(\(c,x)->((c,r),x)))allIndices.map(zipallIndices)fromList::Dimension->Dimension->[a]->TafromListmnxs=Cons(listArray(indexBoundsmn)xs)appPrec::IntappPrec=10instance(Showa)=>Show(Ta)whereshowsPrecpm=showParen(p>=appPrec)(showString"Matrix.fromRows ".showsPrecappPrec(rowsm))format::(Showa)=>Ta->Stringformatm=formatSm""formatS::(Showa)=>Ta->ShowSformatS=concatS.map(\r->showString"(".concatSr.showString")\n").map(List.intersperse(' ':).map(showsPrec11)).rowsdimension::Ta->(Dimension,Dimension)dimension(Consm)=uncurrysubtract(boundsm)+(1,1)numRows::Ta->DimensionnumRows=fst.dimensionnumColumns::Ta->DimensionnumColumns=snd.dimension-- These implementations may benefit from a better exception than-- just assertions to validate dimensionalitiesinstance(Additive.Ca)=>Additive.C(Ta)where(+)=zipWith(+)(-)=zipWith(-)zero=zero11zipWith::(a->b->c)->Ta->Tb->TczipWithopmM@(Consm)nM@(Consn)=letd=dimensionmMem=elemsmen=elemsninassert(d==dimensionnM)$uncurryfromListd(List.zipWithopemen)zero::(Additive.Ca)=>Dimension->Dimension->Tazeromn=fromListmn$List.repeatAdditive.zero-- List.replicate (fromInteger (m*n)) zeroone::(Ring.Ca)=>Dimension->Taonen=Cons$accumArray(flipconst)Additive.zero(indexBoundsnn)(map(\i->((i,i),Ring.one))(indexRangen))diagonal::(Additive.Ca)=>[a]->Tadiagonalxs=letn=List.lengthxsinCons$accumArray(flipconst)Additive.zero(indexBoundsnn)(zip(map(\i->(i,i))allIndices)xs)scale::(Ring.Ca)=>a->Ta->Tascales=Vector.functorScalesinstance(Ring.Ca)=>Ring.C(Ta)wheremM*nM=assert(numColumnsmM==numRowsnM)$fromList(numRowsmM)(numColumnsnM)$liftM2scalarProduct(rowsmM)(columnsnM)fromIntegern=fromList11[fromIntegern]mM^n=assert(numColumnsmM==numRowsmM)$assert(n>=Additive.zero)$powerAssociative(*)(one(numColumnsmM))mMninstanceFunctorTwherefmapf(Consm)=Cons(fmapfm)instanceVector.CTwherezero=Additive.zero(<+>)=(+)(*>)=scaleinstanceModule.Cab=>Module.Ca(Tb)wherex*>m=fmap(x*>)mrandom::(Rnd.RandomGeng,Rnd.Randoma)=>Dimension->Dimension->g->(Ta,g)random=randomAuxRnd.randomrandomR::(Rnd.RandomGeng,Rnd.Randoma)=>Dimension->Dimension->(a,a)->g->(Ta,g)randomRmnrng=randomAux(Rnd.randomRrng)mn{-
could be made nicer with the State monad,
but I like to keep dependencies minimal
-}randomAux::(Rnd.RandomGeng,Rnd.Randoma)=>(g->(a,g))->Dimension->Dimension->g->(Ta,g)randomAuxrndmng0=mapFst(fromListmn)$swap$List.mapAccumL(\g_i->swap$rndg)g0(indexRange(m*n)){-
What more do we need from our matrix type? We have addition,
subtraction and multiplication, and thus composition of generic
free-module-maps. We're going to want to solve linear equations with
or without fields underneath, so we're going to want an implementation
of the Gaussian algorithm as well as most probably Smith normal
form. Determinants are cool, and these are to be calculated either
with the Gaussian algorithm or some other goodish method.
-}{-
{- |
We'll want generic linear equation solving, returning one solution,
any solution really, or nothing. Basically, this is asking for the
preimage of a given vector over the given map, so
a_11 x_1 + .. + a_1n x_n = y_1
...
a_m1 x_1 + .. + a_mn a_n = y_m
has really x_1,...,x_n as a preimage of the vector y_1,..,y_m under
the map (a_ij), since obviously y_1,..,y_m = (a_ij) x_1,..,x_n
So, generic linear equation solving boils down to the function
-}
preimage :: (Ring.C a) => T a -> T a -> Maybe (T a)
preimage a y = assert
(numRows a == numRows y && -- they match
numColumns y == 1) -- and y is a column vector
Nothing
-}{-
Cf. /usr/lib/hugs/demos/Matrix.hs
-}-- these functions control whether we use 0 or 1 based indicesindexRange::Dimension->[Dimension]indexRangen=[0..(n-1)]indexBounds::Dimension->Dimension->((Dimension,Dimension),(Dimension,Dimension))indexBoundsmn=((0,0),(m-1,n-1))allIndices::[Dimension]allIndices=[0..]