[Haskell-cafe] Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

Henning Thielemann lemming at henning-thielemann.de
Thu Dec 23 00:18:47 CET 2010


On Wed, 22 Dec 2010, gutti wrote:

> question 1.  u see the two commented lines I tried to get ur original line
> running, but didn't know how to specify f

What 'f' ? Do you mean

matrixfunction f x y = liftMatrix2 (zipVectorWith f) x y

?


> ## Code ########
>
> import Numeric.LinearAlgebra
> import Graphics.Plot
>
> matrix1 = fromLists [[0 .. 5],[30 .. 35],[50 .. 55]]
> matrix2 = fromLists [[-1,2],[-3,4],[5,-6]]
>
> -- matrix1 = buildMatrix 3 4 ( (r,c) -> fromIntegral r * fromIntegral c)
> (3><4)
> -- posPart v  =  mapVector (\a -> if a>=0 then a else 0) v
>
> -- function2map a1 a2 = (\a1 a2 -> if a1>=0 then a2/a1 else a1/a2)
> matrixfunction x y = liftMatrix2 (zipVectorWith(\a1 a2 -> if a2>=0 then a1 else 0)) x y


> matrix3 = matrixfunction matrix1 matrix2
>
> disp = putStr . disps 2
>
> main = do
>
>  disp matrix1
>  disp matrix2
> --  disp matrix3
>  mesh matrix1
>
>
> #########
>
>
> question 2: - the compiler comes up with some weired data type problem --
> ghci has no problem this line :
>
> matrixTest_Fail.hs:5:10:
>    Ambiguous type variable `t' in the constraints:
>      `Element t'
>        arising from a use of `fromLists' at matrixTest_Fail.hs:5:10-38
>      `Num t' arising from the literal `1' at matrixTest_Fail.hs:5:22
>    Possible cause: the monomorphism restriction applied to the following:
>      matrix2 :: Matrix t (bound at matrixTest_Fail.hs:5:0)
>    Probable fix: give these definition(s) an explicit type signature
>                  or use -XNoMonomorphismRestriction
>
> ## Code #####
>
> import Numeric.LinearAlgebra
> import Graphics.Plot
>
> matrix1 = fromLists [[1,2],[3,4],[5,6]]
> matrix2 = fromLists [[1,2],[3,4],[5,6]]

Before type inference can work, you need to fix the type of at least one 
number of a set of numbers with known equal type. E.g.

> matrix1 = fromLists [[1,2],[3,4],[5,6::Double]]


or even better, add a type signature:

matrix1 :: Matrix Double



More information about the Haskell-Cafe mailing list