Code Monkey home page Code Monkey logo

Comments (2)

albertoruiz avatar albertoruiz commented on August 15, 2024

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.

albertoruiz avatar albertoruiz commented on August 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.