From 7f23aabba933c8e7ef44dbe21e35fa8fa0300f49 Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Sat, 10 Aug 2019 03:41:07 -0400 Subject: Internal.Matrix builds. --- packages/base/src/Internal/Matrix.hs | 41 +++++++++---------------------- packages/base/src/Internal/Specialized.hs | 24 +++++++++++++++++- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/packages/base/src/Internal/Matrix.hs b/packages/base/src/Internal/Matrix.hs index 225b039..05633de 100644 --- a/packages/base/src/Internal/Matrix.hs +++ b/packages/base/src/Internal/Matrix.hs @@ -7,6 +7,7 @@ {-# LANGUAGE ViewPatterns #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE ConstrainedClassMethods #-} +{-# LANGUAGE ConstraintKinds #-} -- | -- Module : Internal.Matrix @@ -29,6 +30,7 @@ import Foreign.Marshal.Array(newArray) import Foreign.Ptr ( Ptr ) import Foreign.Storable ( Storable ) import Data.Complex ( Complex ) +import Data.Typeable import Foreign.C.Types ( CInt(..) ) import Foreign.C.String ( CString, newCString ) import System.IO.Unsafe ( unsafePerformIO ) @@ -77,13 +79,13 @@ trans m@Matrix { irows = r, icols = c, xRow = xr, xCol = xc } = m { irows = c, icols = r, xRow = xc, xCol = xr } -cmat :: (Element t) => Matrix t -> Matrix t +cmat :: (Typeable t) => Matrix t -> Matrix t cmat m | rowOrder m = m | otherwise = extractAll RowMajor m -fmat :: (Element t) => Matrix t -> Matrix t +fmat :: (Typeable t) => Matrix t -> Matrix t fmat m | colOrder m = m | otherwise = extractAll ColumnMajor m @@ -100,17 +102,19 @@ a #! b = a # b # id -------------------------------------------------------------------------------- -copy :: Element t => MatrixOrder -> Matrix t -> IO (Matrix t) +copy :: Typeable t => MatrixOrder -> Matrix t -> IO (Matrix t) copy ord m = extractR ord m 0 (idxs[0,rows m-1]) 0 (idxs[0,cols m-1]) -extractAll :: Element t => MatrixOrder -> Matrix t -> Matrix t +extractAll :: Typeable t => MatrixOrder -> Matrix t -> Matrix t extractAll ord m = unsafePerformIO (copy ord m) +type Element t = (Typeable t, Storable t) + {- | Creates a vector by concatenation of rows. If the matrix is ColumnMajor, this operation requires a transpose. >>> flatten (ident 3) [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0] -it :: (Num t, Element t) => Vector t +it :: (Num t, Typeable t) => Vector t -} flatten :: Element t => Matrix t -> Vector t @@ -120,7 +124,7 @@ flatten m -- | the inverse of 'Data.Packed.Matrix.fromLists' -toLists :: (Element t) => Matrix t -> [[t]] +toLists :: Element t => Matrix t -> [[t]] toLists = map toList . toRows @@ -285,34 +289,11 @@ instance (Storable t, NFData t) => NFData (Matrix t) d = dim v v = xdat m ---------------------------------------------------------------- - -type Extr x = CInt -> CInt -> CIdxs (CIdxs (OM x (OM x (IO CInt)))) - -foreign import ccall unsafe "extractD" c_extractD :: Extr Double -foreign import ccall unsafe "extractF" c_extractF :: Extr Float -foreign import ccall unsafe "extractC" c_extractC :: Extr (Complex Double) -foreign import ccall unsafe "extractQ" c_extractQ :: Extr (Complex Float) -foreign import ccall unsafe "extractI" c_extractI :: Extr CInt -foreign import ccall unsafe "extractL" c_extractL :: Extr Z - ---------------------------------------------------------------- - --------------------------------------------------------------------------------- - --------------------------------------------------------------------------------- - --------------------------------------------------------------------------------- - ---------------------------------------------------------------------------- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- - -------------------------------------------------------------------------------- -- | Transpose an array with dimensions @dims@ by making a copy using @strides@. For example, for an array with 3 indices, -- @(reorderVector strides dims v) ! ((i * dims ! 1 + j) * dims ! 2 + k) == v ! (i * strides ! 0 + j * strides ! 1 + k * strides ! 2)@ -- This function is intended to be used internally by tensor libraries. -reorderVector :: Element a +reorderVector :: Typeable a => Vector CInt -- ^ @strides@: array strides -> Vector CInt -- ^ @dims@: array dimensions of new array @v@ -> Vector a -- ^ @v@: flattened input array diff --git a/packages/base/src/Internal/Specialized.hs b/packages/base/src/Internal/Specialized.hs index c063369..46587d2 100644 --- a/packages/base/src/Internal/Specialized.hs +++ b/packages/base/src/Internal/Specialized.hs @@ -10,7 +10,29 @@ {-# LANGUAGE KindSignatures #-} {-# LANGUAGE ViewPatterns #-} {-# LANGUAGE LambdaCase #-} -module Internal.Specialized where +module Internal.Specialized + ( Mod(..) + , MatrixOrder(..) + , Matrix(..) + , createMatrix + , matrixFromVector + , cols + , rows + , size + , shSize + , shDim + , constantD + , extractR + , setRect + , sortI + , sortV + , compareV + , selectV + , remapM + , rowOp + , gemm + , reorderV + ) where import Control.Monad import Control.DeepSeq ( NFData(..) ) -- cgit v1.2.3