Comments (2)
Good point, I will look into this.
Another possibility would be to remove the constraints in the class declaration, something like:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleContexts #-}
import Numeric.LinearAlgebra
class Mul2 a b c | a b -> c
where
infixl 7 <<>>
(<<>>) :: a -> b -> c
instance Product t => Mul2 (Matrix t) (Matrix t) (Matrix t)
where
(<<>>) = mXm
instance Product t => Mul2 (Matrix t) (Vector t) (Vector t)
where
(<<>>) m v = flatten $ m <> asColumn v
instance Product t => Mul2 (Vector t) (Matrix t) (Vector t)
where
(<<>>) v m = flatten $ asRow v <> m
newtype DiagMatrix e = DiagMatrix {getDiag :: Vector e}
instance (Num (Vector t), Container Vector t) => Mul2 (DiagMatrix t) (Matrix t) (Matrix t)
where
(<<>>) dm m = asColumn (getDiag dm) * m
But in ghci we need type annotations for the elements in both arguments of the operator.
I will think about your suggestion.
from hmatrix.
How about this:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
import Numeric.LinearAlgebra hiding (Mul(..))
newtype DiagMatrix e = DiagMatrix {getDiag :: Vector e} deriving Show
diagl l = (DiagMatrix . fromList) l
class Mul t a b c | a b -> c
where
infixl 7 <>
(<>) :: a t -> b t -> c t
instance Product t => Mul t Matrix Matrix Matrix
where
(<>) = mXm
instance Product t => Mul t Matrix Vector Vector
where
(<>) m v = flatten $ m `mXm` asColumn v
instance Product t => Mul t Vector Matrix Vector
where
(<>) v m = flatten $ asRow v `mXm` m
instance (Container Vector t) => Mul t DiagMatrix Matrix Matrix
where
(<>) dm m = repmat (asColumn (getDiag dm)) 1 (cols m) `mul` m
instance (Container Vector t) => Mul t Matrix DiagMatrix Matrix
where
(<>) m dm = repmat (asRow (getDiag dm)) (rows m) 1 `mul` m
instance (Container Vector t) => Mul t DiagMatrix DiagMatrix DiagMatrix
where
DiagMatrix a <> DiagMatrix b = DiagMatrix (mul a b)
> let m = (2><3) [1..]
> diagl [3,10] <> m
(2><3)
[ 3.0, 6.0, 9.0
, 40.0, 50.0, 60.0 ]
> m <> diagl [1,2,3]
(2><3)
[ 1.0, 4.0, 9.0
, 4.0, 10.0, 18.0 ]
> diagl [1,2,3::Double] <> diagl [5,6,7]
DiagMatrix {getDiag = fromList [5.0,12.0,21.0]}
from hmatrix.
Related Issues (20)
- Unit test 'randomGaussian' fails (at :~3~:) HOT 1
- Numeric.GSL.Root.uniRoot Bisection prints debug stuff
- Missing Show instance for Constraints HOT 1
- expm hangs on a zero matrix HOT 1
- "On entry to DORMQR parameter number 10 had an illegal value" - perhaps lapack/NUMA related? HOT 3
- Fails to build with gcc-10: * Missing (or bad) C library: gfortran HOT 3
- Build failures with GHC-9.0 HOT 4
- Error: On entry to DGEMM parameter number 13 had an illegal value HOT 2
- Build fails on DragonflyBSD; `struct random_data` not defined HOT 2
- Matrix multiplication produces wrong result HOT 2
- I can't define a Numeric instance for a newtype of Double (Internal modules need to be exposed)
- where is the CI script HOT 1
- Documentation PDF File requires update
- hmatrix-tests returns an error upon installation
- Clarify hmatrix-gsl's SimulatedAnnealing API
- Problem with Eq instance for Matrix with degenerate matrices HOT 2
- Windows install issue HOT 1
- Inquiry about Element Instance for Rational Numbers
- Running a linear programming problem always generate an access violation HOT 3
- My package depends on `hmatrix-glpk` and the build fails on Hackage HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hmatrix.