Newbie question: assigning results of matrix multiplication to a matrix element?

408 views
Skip to first unread message

Jason Fleischer

unread,
Aug 16, 2013, 9:17:10 PM8/16/13
to julia...@googlegroups.com


Hi all,

Sorry to ask what is probably a very basic question, but I just downloaded Julia yesterday.

As a MATLAB user trying to see if Julia is for me,  the following is very puzzling behavior to me.  I would expect to be able to assign the a size[1] array of floats to a single element in another array of floats.  The syntax I came up with to make this work is very very strange to me.  Am I missing something obvious?


julia> foo=ones(10,1)
10x1 Float64 Array:
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0

julia> foo'*foo
1x1 Float64 Array:
 10.0

julia> bar=zeros(10,1)
10x1 Float64 Array:
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0

julia> bar[1]=foo'*foo
ERROR: no method convert(Type{Float64},Array{Float64,2})
 in setindex! at array.jl:409

julia> bar[1]=(foo'*foo)[1]
10.0

julia> 


Thanks,
Jason

John Myles White

unread,
Aug 16, 2013, 10:25:52 PM8/16/13
to julia...@googlegroups.com
Julia is substantially more strict about typing than Matlab is. Although Matlab will allow matrices to behave like scalars in many contexts, Julia very rarely does.

The (foo * foo)[1] syntax seems completely right to me. If you're interested in dot products, you probably want the dot() function, which produces a scalar.

-- John

Stefan Karpinski

unread,
Aug 16, 2013, 10:53:21 PM8/16/13
to Julia Users
That said, in this case I think it would be reasonable to allow the assignment into the matrix since assigning a matrix into a matrix should work. This is just a matter of adding the right method.

Andreas Lobinger

unread,
Aug 17, 2013, 8:42:36 AM8/17/13
to julia...@googlegroups.com
Hello colleague,


On Saturday, August 17, 2013 4:25:52 AM UTC+2, John Myles White wrote:
Julia is substantially more strict about typing than Matlab is. Although Matlab will allow matrices to behave like scalars in many contexts, Julia very rarely does.

Can you show me a mathematical textbook that explains the difference between a scalar and a 1x1 matrix?

I understand that julia's strict type system enables this high gains in performance in the compiler, but at the same time puts back the responsiblity of deciding about the type to the programmer. And keeping away this decision from the programmer (although is creates a lot of side effects) is one of these things that put matlab on top of the list for e.g. rapid prototyping.


John Myles White

unread,
Aug 17, 2013, 9:39:05 AM8/17/13
to julia...@googlegroups.com
I think some of the other Matlab converts will have to address your concerns more, but I think the real problem is that no computer system has ever behaved all that much like a math textbook. In this specific regard, Julia could be more accommodating, but the math/computer divide always shows up and needs to be accepted.

 -- John

Steven G. Johnson

unread,
Aug 17, 2013, 10:29:36 AM8/17/13
to julia...@googlegroups.com


On Saturday, August 17, 2013 8:42:36 AM UTC-4, Andreas Lobinger wrote:
On Saturday, August 17, 2013 4:25:52 AM UTC+2, John Myles White wrote:
Julia is substantially more strict about typing than Matlab is. Although Matlab will allow matrices to behave like scalars in many contexts, Julia very rarely does.

Can you show me a mathematical textbook that explains the difference between a scalar and a 1x1 matrix?

Sure.   Look at any mathematical textbook's definition of matrix multiplication.   Note that it requires you to multiply an m x n matrix by an n x p matrix -- the inner dimensions must match.   Ergo, 1x1 matrices cannot be multiplied by m x n matrices unless m == 1.    However, the same textbook will tell you that matrices can be multiplied by scalars.

The fact that Matlab allows 1x1 matrices to be multiplied by m x n matrices for m != 1 is, it could be argued, a misfeature.

The basic mathematical issue here is that, although all 1-dimensional vector spaces over the real (or complex) numbers (including scalars and 1x1 matrices and even 1-parameter families of functions like a * cos(x) for all a, with the usual + operations) are *isomorphic*, that does not mean that they are the *same*.

--SGJ

pjab...@gmail.com

unread,
Aug 18, 2013, 9:51:09 AM8/18/13
to julia...@googlegroups.com
Conceptual clarity at its finest! Now, any other approach to matrix operations is awkward

Billou Bielour

unread,
Aug 19, 2013, 6:05:43 AM8/19/13
to julia...@googlegroups.com
Is there any case where it wouldn't be safe to convert 1x1 matrices into scalar for assignment ? 

If not assignment should implicitly convert 1x1 matrices into scalar imo, it's so much simpler to use.

Same thing for Nx1 and 1xN matrices into vector.

Andreas Lobinger

unread,
Aug 19, 2013, 8:34:33 AM8/19/13
to julia...@googlegroups.com
Hello colleagues,


On Saturday, August 17, 2013 4:29:36 PM UTC+2, Steven G. Johnson wrote:
On Saturday, August 17, 2013 8:42:36 AM UTC-4, Andreas Lobinger wrote:
On Saturday, August 17, 2013 4:25:52 AM UTC+2, John Myles White wrote:
Julia is substantially more strict about typing than Matlab is. Although Matlab will allow matrices to behave like scalars in many contexts, Julia very rarely does.
Can you show me a mathematical textbook that explains the difference between a scalar and a 1x1 matrix?
Sure.   Look at any mathematical textbook's definition of matrix multiplication.  

picture the scene:

I closed the laptop, said goodby to the cat, closed the garden door, got on the bike and at the second street corner, it crossed my mind: someone will come up with matrix multiplication...

Yes, you are right, in the context of some (complex) mathematical operations there is a difference betweeen 1x1 matrices and scalars.

But still, both are a single number (carrying a certain amount of information) but are treated differently.


ggggg

unread,
Aug 19, 2013, 12:15:29 PM8/19/13
to julia...@googlegroups.com
I agree that any assignment operation where the sizes differ only by singleton dimension should work, and the resulting operation should be unambiguous.  

Before I found the julia-users group I created issues for these topics.  I probably jumped the gun since I didn't have much experience with the julia community, but it was the only place I had found to give feedback.  The issues are:


Andreas can you give a concrete example of an expression that should have a different result (other than shape differing by singleton dimensions) with a scalar vs a 1x1 matrix?

Jason Fleischer

unread,
Aug 19, 2013, 1:07:25 PM8/19/13
to julia...@googlegroups.com

Thanks, John.  That's a great way to think about it.

Jason Fleischer

unread,
Aug 19, 2013, 1:44:21 PM8/19/13
to julia...@googlegroups.com
Thanks Stefan.  

Can you point me in the right direction on how to go about doing this, or are you considering adding this feature to Julia itself?  If I were to go about programming this, would it be best to tackle this through a conversion method, a promotion rule or something else?  
Reply all
Reply to author
Forward
0 new messages