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

13 messages
Open this post in threaded view
|
Report Content as Inappropriate

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

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 On Mon, 20 Dec 2010, gutti wrote: > In Matlab the following line of code: > V3 = V1.*(V2>0) What you certainly need is a zipWith function on matrices that lets you write    Matrix.zipWith (\a1 a2 -> if a2>0 then a1 else 0) v1 v2 I can't see such a function in Matrix, but in Vector (zipVectorWith) that can be lifted to Matrices by (Matrix.liftMatrix2). Maybe there is some magic type class that already handles this - if there wouldn't be the Element constraint, it would be the Applicative type class and the liftA2 function. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 In reply to this post by gutti Hi Phil, On 12/20/2010 10:49 PM, gutti wrote: > > Hi all, > > In Matlab the following line of code: > V3 = V1.*(V2>0) > > (V2>0) gives a Bool-Vector with ones (trues) and zero's where elements > of V2 are>  0; Then this Bool vector is used to multiply all elements in V1 > to zero > where the condition V2>0 is not fulfilled. > > How can I do that in Haskell ?  (I haven't seen bol operations or mapping > functions into vectors, arrays > in the HMatrix.pdf). Vectorized boolean operations are not yet implemented but I hope to get them ready soon, including a "find" function. In the meantime you can use zipVectorWith, as mentioned by Henning. We could also use signum, but this is not recommended (signum 0 is 0): import Numeric.LinearAlgebra vec = fromList :: [Double] -> Vector Double cond x = (signum (x-scalar eps) + 1 ) / 2 v1 = vec [10..20] v2 = vec [-5..5] v3 = v1 * cond v2  > v3 11 |> [0.0,0.0,0.0,0.0,0.0,0.0,16.0,17.0,18.0,19.0,20.0] -Alberto > > . -- Many thanks in advance Phil > > > _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 On Tue, 21 Dec 2010, Alberto Ruiz wrote: > Vectorized boolean operations are not yet implemented but I hope to get them > ready soon, including a "find" function. In the meantime you can use > zipVectorWith, as mentioned by Henning. I would not find it a great idea to support the MatLab style of handling booleans by 0 and 1. It's the whole point of Haskell's type safety to distinguish between numbers and booleans. MatLab even weakly distinguishs between numbers and booleans. If you use a vector of logical values (MatLab.logical corresponds to Haskell.Bool) as index, then it works like 'filter', e.g.   logical indices:  [1 2 3 4 5 6]([0 0 1 1 0 0]) = [3 4]   number indices:   [1 2 3 4 5 6]([0 0 1 1 0 0]) -> zero index not allowed   Writing v2>0 in Haskell would mean to compare a matrix with a scalar. You would need a custom '>' operator and new type hacks in order to support all combinations of matrix and scalar operands. I think a Matrix.zipWith function would be the cleanest and most efficient way we can have! _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 > thanks for the quick and comprehensive help. - I managed to implement > Hennings suggestion with mapVector and zipWithVector.  -- However have a > type inference problem with zipVectorWith -- probably a stupid beginners > mistake. (have a look below). I want to look into the matrix thing as well, > but that might take a bit. It is    Matrix.zipWith f x y = liftMatrix2 (zipVectorWith f) x y > I see the point, that its probably not the "cleanest" way (bool to 0 & 1) > but its damn convinient (laziness at its best). Is it really? Certainly, if you are used to. I am scared if someone multiplies the result of a comparison with something else. I find the 'if' most natural for such applications, and I like the Matrix.zipWith because it expresses that corresponding elements of matrices are combined and that it is no operation that is special for matrices (such as matrix multiplication or inversion or factorization or determinant). > Maybe there could be a haskell way to implement the "lazy" matlab matrix and > vector operation syntax (like explicit function for bool 2 num) You are free to implement any function, also higher order, also with infix syntax, that you need frequently. :-) > ######## Code > > import Numeric.LinearAlgebra > import Graphics.Plot > > time = 101 |> [0, 0.1 .. 100 :: Double]; > > vector1 = sin(time); > vector2 = vector1*0.9; I had to look twice, whether this is C or Haskell. It could be both of them. :-) I would certainly write: vector1, vector2 :: Vector Double vector1 = sin time vector2 = vector1*0.9 > posPart:: Vector Double -> Vector Double > posPart v  =  mapVector (\a -> if a>=0 then a else 0) v How about: posPart = mapVector (max 0) > v3:: Vector Double -> Vector Double -> Vector Double > v3 v1 v2 = zipVectorWith(\a1 a2 -> if a1>=0 then a2/a1 else a1/a2) v1 v2 > > main = do > > >  -- print(v3) > mplot [v3] v3 is a function and 'mplot' seems to expect a vector. > mplot [posPart vector1] _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 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 _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|
Report Content as Inappropriate

Re: Matlab Style Logic Operations ala V1.*(V2>0) on Vectors and Matrices with HMatrix ??

 On Sat, 25 Dec 2010, gutti wrote: > this line works : > > matrixfunction x y = liftMatrix2 (zipVectorWith(\a1 a2 -> if a2>=0 then a1 else 0)) x y > > but when I use this line : > > matrixfunction f x y = liftMatrix2 (zipVectorWith f) x y > > how can / do I have to define f in a seperate line a way, that it works and > gives the same result ? The same way you have literally replaced    (\a1 a2 -> if a2>=0 then a1 else 0)   by 'f' at the right hand side, you can use that phrase as argument to the parameter 'f':    matrixfunction (\a1 a2 -> if a2>=0 then a1 else 0) x y The lambda expression (\ ...) really is just a notation for a function. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe