Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: automatically convert 1x1 matrix to a scaler number in proper cases #4797

Closed
wenxgwen opened this issue Nov 13, 2013 · 5 comments
Labels
won't change Indicates that work won't continue on an issue or pull request

Comments

@wenxgwen
Copy link

It will be more convenient that Julia can automatically convert 1x1 matrix to a scaler number in proper cases.

The julia session :

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" to list help topics
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.2.0-rc2-prerelease+4331
 _/ |\__'_|_|_|\__'_|  |  Commit 902b446 2013-10-29 23:03:55 UTC
|__/                   |  i686-linux-gnu

julia> reload("test1")
ERROR: no method convert(Type{Float64},Array{Int32,1})
 in setindex! at array.jl:413
 in include at boot.jl:238
 in include_from_node1 at loading.jl:114
 in reload_path at loading.jl:140
 in reload at loading.jl:73
at /home/wen/Dropbox/prog/julia/STK/test1.jl:9

julia> 

The Julia code test1.jl:

K=[1 0; 0 2]

T=zeros(Float64,2,2)

l=[1; 1]
# The following works:  
T[2,2] =(l'*K*l)[1,1]
# The following does not work
T[1,1] =(l'*K*l)

# (l'*K*l) is a 1x1 Array, and Julia cannot treat it as a scaler number

A general and uniform way to handle this and other issues is to allow automatic (type?) conversion (when needed)
between array of size (n), (n,1), (n,1,1),(n,1,1,1) etc
(Just like we automatically convert real number to complex number when needed)

Xiao-Gang

[jiahao: edit for formatting. Please escape code with triple backquotes (```) so that Github formats it correctly]

@jiahao
Copy link
Member

jiahao commented Nov 13, 2013

Related discussions: #3262 #4270 #2591 #2686

@ViralBShah
Copy link
Member

This is something that we have taken a measured decision on, as @jiahao 's link to related discussions shows.

@StefanKarpinski
Copy link
Sponsor Member

Actually, this is a little different since it really has nothing to do with other indexing and vector, matrix, tensor behaviors. Assigning any single-element array into a scalar location in another array should probably just work.

@ViralBShah
Copy link
Member

Would we want to treat single-element array assignments into an array location differently from other array assignments into an array location? What if the assignment is into Array{Any} on the left, where one may actually want the single element array assigned, rather than the scalar?

@JeffBezanson
Copy link
Sponsor Member

As has been discussed in those other issues, what we would do is allow
conversion to a lower-dimensional thing if trailing dimensions are 1.
On Nov 13, 2013 11:59 AM, "Viral B. Shah" [email protected] wrote:

Would we want to treat single-element array assignments from other array
assignments? What if the assignment is into Array{Any}, where one may
actually want the single element array assigned?


Reply to this email directly or view it on GitHubhttps://github.com//issues/4797#issuecomment-28412197
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
won't change Indicates that work won't continue on an issue or pull request
Projects
None yet
Development

No branches or pull requests

5 participants