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

Unsuccessful assignment without any error #56163

Closed
ArmanZarrin97 opened this issue Oct 14, 2024 · 8 comments
Closed

Unsuccessful assignment without any error #56163

ArmanZarrin97 opened this issue Oct 14, 2024 · 8 comments
Labels
sparse Sparse arrays

Comments

@ArmanZarrin97
Copy link

ArmanZarrin97 commented Oct 14, 2024

Hi,

I tried to run this line in one of my functions:

J[:, isl]=[ki... zeros(length(setdiff(1:nBus,ipq)))...]

J is a sparse matrix with size 120*120. J[:, isl] is a column with only four values, which are [11.00 -11.00 44.00 -44.00]. [ki... zeros(length(setdiff(1:nBus,ipq)))...] is a matrix 120*1, of which 23 elements are 50.00 and the others equal to zero. The result of running this code is again a column with only four values, which are [0.00 0.00 0.00 0.00]. However, I expect it to be a column with 23, 24,..., or 27 values, which includes 23 values equal to 50.00.

I didn't encounter any error. I believe it should be a bug. By chance I understood that this assignment doesn't work correctly, But it could be in fact dangerous for the others.

I fixed it in this way:

J[:, isl]=[ki... zeros(length(setdiff(1:nBus,ipq)))...]**[:,1]**

Best,
Arman

@adienes
Copy link
Contributor

adienes commented Oct 14, 2024

hi,

to increase the probability of getting a resolution to your issue, could you please format the example code in a way that makes it easier to replicate and understand exactly what you're running into?

In particular, I would like to able to copy-paste your MWE into my REPL and observe the exact behavior you do, without having to guess at the different parameters / values you're using.

@nsajko nsajko added the needs more info Clarification or a reproducible example is required label Oct 14, 2024
@vtjnash
Copy link
Member

vtjnash commented Oct 14, 2024

I don't understand either. That "fix" gives me the same resulting shape object:

julia> A = reshape(1:12, (12, 1))
12×1 reshape(::UnitRange{Int64}, 12, 1) with eltype Int64:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12

julia> B = reshape(1:12, (12, 1))[:,1]
12-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12

And testing assignment by creating

J = spzeros(12,12)

I see the same result from assigning either A or B to J[:,1]

@ArmanZarrin97
Copy link
Author

Here is the reproducible example:

a=spzeros(120,120)

isl=5

a[10,isl]=1
a[14,isl]=1
a[37,isl]=1
a
R=zeros(40,1)
S=50*ones(40,1)
T=zeros(40,1)
b=[R... S... T...]
a
b
a[:,isl]=b ## This is the concerned assignment
sum(b) # =1200.0
sum(a[:,isl]) # =0.0

@barucden
Copy link
Contributor

barucden commented Oct 15, 2024

Smaller MWE:

using SparseArrays
A = zeros(3, 1)
sparseA = sparse(A)
b = ones(1, 3)
A[:, 1] = b
sparseA[:, 1] = b

# A is:
# 3×1 Matrix{Float64}:
#  1.0
#  1.0
#  1.0

# sparseA is:
# 3×1 SparseMatrixCSC{Float64, Int64} with 1 stored entry:
#  1.0
#
#

Notice that b is a 1×3 matrix. If we make it a vector b = ones(3), then sparseA matches A.

@ArmanZarrin97
Copy link
Author

the smaller MWE doesn't show the problem correctly. I understood that if we make vector before, everything is fine and solved my issue in the same way. Just for the others who will use the package, in the case of a matrix, it cannot do assignment, and I suppose it should raise an error (which it doesn't).

@adienes
Copy link
Contributor

adienes commented Oct 15, 2024

ooie yeah something looks super wrong. I'm not that familiar with sparse arrays but seems like setindex! is pretty bugged in this case. that being said this issue should probably go in SparseArrays.jl here rather than the julia main repo

@ArmanZarrin97
Copy link
Author

Oh Sorry. Actually, I thought I am writing the issue for SparseArrays.jl. I will create an issue there.

@vtjnash vtjnash added sparse Sparse arrays and removed needs more info Clarification or a reproducible example is required labels Oct 15, 2024
@vtjnash
Copy link
Member

vtjnash commented Oct 15, 2024

Moved to JuliaSparse/SparseArrays.jl#569

@vtjnash vtjnash closed this as completed Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sparse Sparse arrays
Projects
None yet
Development

No branches or pull requests

5 participants