Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

sum of matrix always results in a 1x1 matrix #22

Open
MDeiml opened this issue Nov 19, 2015 · 4 comments
Open

sum of matrix always results in a 1x1 matrix #22

MDeiml opened this issue Nov 19, 2015 · 4 comments
Assignees

Comments

@MDeiml
Copy link

MDeiml commented Nov 19, 2015

If you apply the sum function to a list of nxm matrices you get a 1x1 matrix as result e.g.
sum [M.fromLists [[1,1]], M.fromLists [[2,2]]] == ( 3 )

@Daniel-Diaz
Copy link
Owner

Right, and I guess this is cause by the sum function actually evaluating your sum as:

M.fromLists [[1,1]] + M.fromLists [[2,2]] + fromInteger 0

Function fromInteger will create the 1x1 matrix ( 0 ), and this is the reason you are getting a 1x1 matrix as a result.

I guess an alternative sum function for matrices will help?

@MDeiml
Copy link
Author

MDeiml commented Jan 17, 2016

Actually M.fromLists [[1,1]] + M.fromLists [[2,2]] + fromInteger 0 crashes with

getElem: Trying to get the (1,2) element from a 1x1 matrix.

What really gets evaluated is fromInteger 0 + fromLists [[1,1]]
Maybe elementwise should be rewritten to accept matrices of different size?

@MDeiml
Copy link
Author

MDeiml commented Jan 17, 2016

This should work:

elementwise f m m' = matrix (max (nrows m) (nrows m')) (max (ncols m) (ncols m')) $
  \k -> f (m ! k) (m' ! k)

@Daniel-Diaz Daniel-Diaz self-assigned this Jan 18, 2016
@Daste
Copy link

Daste commented Aug 17, 2016

Pheraps creating a function like:
elementWiseMonoid :: (Monoid a,Monoid b) => ( a -> b -> c ) -> Matrix a -> Matrix b -> Matrix c elementWiseMonoid f m m' = elementWiseUnsafe f (setSize rows columns m) (setSize rows columns m') where rows = max ( nrows m ) ( nrows m' ) columns = max ( ncols m ) ( ncols m' )
and using it to instantiate Num could help.
I did not test that.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants