mmult returning double

Showing 1-4 of 4 messages
mmult returning double Carson 11/13/10 11:03 AM
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
Re: mmult returning double Fernando Saldanha 4/18/13 3:49 PM
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.
Re: mmult returning double Mike Anderson 4/18/13 4:44 PM
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.
Re: mmult returning double Alex Ott 6/2/13 3:26 AM
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