| Portability | non-portable |
|---|---|
| Stability | experimental |
| Maintainer | Edward Kmett <ekmett@gmail.com> |
| Safe Haskell | None |
Sparse.Matrix
Contents
Description
Sparse Matrices in Morton order
- data Mat a = Mat !Int !(Vector Word) !(Vector Word) !(Vector a)
- data Key = Key !Word !Word
- fromList :: Vectored a => [(Key, a)] -> Mat a
- singleton :: Vectored a => Key -> a -> Mat a
- transpose :: Vectored a => Mat a -> Mat a
- ident :: (Vectored a, Num a) => Int -> Mat a
- empty :: Vectored a => Mat a
- size :: Mat a -> Int
- null :: Mat a -> Bool
- class (Vectored a, Num a) => Eq0 a where
- addWith :: Vectored a => (a -> a -> a) -> Mat a -> Mat a -> Mat a
- multiplyWith :: Vectored a => (a -> a -> a) -> (Maybe (Heap a) -> Stream (Key, a)) -> Mat a -> Mat a -> Mat a
- class (Vector (Vec a) a, Monoid (Vec a a)) => Vectored a where
- type Vec a :: * -> *
- _Mat :: Vectored a => Iso' (Mat a) (Vector Vector (Vec a) (Key, a))
- keys :: Lens' (Mat a) (Vector Key)
- values :: Lens (Mat a) (Mat b) (Vector a) (Vector b)
Sparse Matrices
Instances
| (Functor f, Contravariant f, Vectored a) => Contains f (Mat a) | |
| (Applicative f, Vectored a) => Ixed f (Mat a) | |
| (Applicative f, Vectored a, ~ * a b) => Each f (Mat a) (Mat b) a b | |
| (Vectored a, Eq (Vector a)) => Eq (Mat a) | |
| (Vectored a, Eq0 a) => Num (Mat a) | |
| (Vectored a, Ord (Vector a)) => Ord (Mat a) | |
| (Vectored a, Read a) => Read (Mat a) | |
| (Vectored a, Show a) => Show (Mat a) | |
| NFData (Vector a) => NFData (Mat a) | |
| Vectored a => Vectored (Mat a) | |
| (Vectored a, Eq0 a) => Eq0 (Mat a) |
Keys
Key i j logically orders the keys as if the bits of the keys i and j
were interleaved. This is equivalent to storing the keys in "Morton Order".
>>>Key 100 200 ^. _1100
>>>Key 100 200 ^. _2200
Construction
singleton :: Vectored a => Key -> a -> Mat aSource
singleton makes a matrix with a singleton value at a given location
ident :: (Vectored a, Num a) => Int -> Mat aSource
ident n makes an n x n identity matrix
>>>ident 4fromList [(Key 0 0,1),(Key 1 1,1),(Key 2 2,1),(Key 3 3,1)]
Consumption
Distinguishable Zero
class (Vectored a, Num a) => Eq0 a whereSource
Methods
Return whether or not the element is 0.
It may be okay to never return True, but you won't be
able to thin spurious zeroes introduced into your matrix.
nonZero :: (x -> y -> a) -> x -> y -> Maybe aSource
Remove results that are equal to zero from a simpler function.
When used with addWith or multiplyWith's additive argument
this can help retain the sparsity of the matrix.
addMats :: Mat a -> Mat a -> Mat aSource
Add two matrices. By default this assumes isZero can
possibly return True after an addition. For some
ring-like structures, this doesn't hold. There you can
use:
addMats=addWith(+)
By default this will use
addMats=addWith0$nonZero(+)
addHeap :: Maybe (Heap a) -> Stream (Key, a)Source
Convert from a Heap to a Stream.
If addition of non-zero valus in your ring-like structure cannot yield zero, then you can use
addHeap=streamHeapWith(+)
instead of the default definition:
addHeap=streamHeapWith0$nonZero(+)
Customization
addWith :: Vectored a => (a -> a -> a) -> Mat a -> Mat a -> Mat aSource
Merge two matrices where the indices coincide into a new matrix. This provides for generalized addition, but where the summation of two non-zero entries is necessarily non-zero.
multiplyWith :: Vectored a => (a -> a -> a) -> (Maybe (Heap a) -> Stream (Key, a)) -> Mat a -> Mat a -> Mat aSource
Multiply two matrices using the specified multiplication and addition operation.
Storage
class (Vector (Vec a) a, Monoid (Vec a a)) => Vectored a Source
Instances
| Vectored Double | |
| Vectored Float | |
| Vectored Int | |
| Vectored Int8 | |
| Vectored Int16 | |
| Vectored Int32 | |
| Vectored Int64 | |
| Vectored Integer | |
| Vectored Word | |
| Vectored Word8 | |
| Vectored Word16 | |
| Vectored Word32 | |
| Vectored Word64 | |
| Vectored () | |
| Vectored Key | |
| (Vectored a, RealFloat a) => Vectored (Complex a) | |
| Vectored a => Vectored (Mat a) |