mmult returning double

43 views
Skip to first unread message

Carson

unread,
Nov 13, 2010, 2:03:18 PM11/13/10
to Incanter
Curious if anyone else is annoyed with mmult returning a double when
the result of the matrix multiplication is a 1x1 matrix? It happens
to me in some "edge cases", then the rest of my code that relies on
the result of mmult being a matrix screws up (eg, (first (mmult mat1
mat2)) usually gives me the first row, unless mat2 ends up being one
column and mat1 with one row, then you end up asking for "first" of a
double).

So instead, I have to check the result of mmult to see if it's a
matrix? to fix that edge-case.

Turns out it's because of the next to last 3 lines in the defn of
mmult:

(defn mmult
([& args]
(reduce (fn [A B]
(let [a (if (matrix? A) A (matrix A))
b (if (matrix? B) B (matrix B))
result (Matrix. (.zMult ^Matrix a ^Matrix b nil))]
(if (and (= (.rows result) 1) (= (.columns result) 1))
(.getQuick result 0 0)
result)))
args)))

Just curious if anyone else runs into this, and if you find it
annoying too?

Carson

Fernando Saldanha

unread,
Apr 18, 2013, 6:49:46 PM4/18/13
to inca...@googlegroups.com
Quite annoying. But R has the same problem. In fact, it's worse. If you select just one row or one column of a matrix the result is a vector. But there is a way to avoid it, specifying "drop = FALSE". Incanter does not seem to have the same flexibility.

Mike Anderson

unread,
Apr 18, 2013, 7:44:46 PM4/18/13
to inca...@googlegroups.com
On Sunday, 14 November 2010 03:03:18 UTC+8, Carson wrote:
Curious if anyone else is annoyed with mmult returning a double when
the result of the matrix multiplication is a 1x1 matrix?  It happens
to me in some "edge cases", then the rest of my code that relies on
the result of mmult being a matrix screws up (eg, (first (mmult mat1
mat2)) usually gives me the first row, unless mat2 ends up being one
column and mat1 with one row, then you end up asking for "first" of a
double).

This is annoying and a really bad design IMHO. It means you can't easily write higher order matrix functions with matrices, since you always have to write code to handle special cases.

The result of multiplying a (i*j) matrix with a (j*k) matrix should always be a (i*k) matrix for any i,j,k 

I'd suggest fixing this to return a 1x1 matrix, even if it causes some breakage. It would also be a requirement for core.matrix compatibility where we are quite strict on matrix dimensionality.

Alex Ott

unread,
Jun 2, 2013, 6:26:36 AM6/2/13
to inca...@googlegroups.com
Current version, with fresh clatrix (0.3.0-SNAPSHOT) always returns the matrices - I've only changed the predict function so it will return number instead of matrix.  Although I haven't committed this change yet, as one problem with fresh clatrix is still unresolved - conjing of matrix with vector...

--
 
---
You received this message because you are subscribed to the Google Groups "Incanter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to incanter+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
With best wishes,                    Alex Ott
http://alexott.net/
Twitter: alexott_en (English), alexott (Russian)
Skype: alex.ott
Reply all
Reply to author
Forward
0 new messages