JuliaLang / JuliaLang/LinearAlgebra.jl
UniformScaling should be assignable in setindex!(::AbstractMatrix)
- Dominant language
- Julia
- Stars
- 77
- Forks
- 65
- Avg merge
- 3d 23h
- Merged PRs (30d)
- 10
Description
I was surprised that assignment of a `UniformScaling` into a matrix does not work. For example:
```julia
A = rand(4,6); A[1:4, 1:4] = I #Make this part of A the identity matrix
```
produces this error on Julia 1.3:
```
ERROR: ArgumentError: indexed assignment with a single value to many locations is not supported; perhaps use broadcasting `.=` instead?
```
The issue here is distinct from JuliaLang/LinearAlgebra.jl#455 in that broadcasting semantics are not relevant; the context
of the `setindex!` call defines precisely what the implicit dimensions of `UniformScaling` ought to be in the assignment operation.
Here is a simple implementation that would work as intended in the original statement:
```julia
function Base.setindex!(A::AbstractMatrix, I::UniformScaling, is, js)
n = min(maximum(is), maximum(js))
imin = minimum(is)
jmin = minimum(js)
for i in is, j in js
if i-imin == j-jmin
A[i,j] = I.λ
else
A[i,j] = 0
end
end
end
```
Some use cases that work:
```
julia> A=rand(4,6); A[1:4,1:4] = I;A # Arguably quite common to want
4×6 Array{Float64,2}:
1.0 0.0 0.0 0.0 0.147456 0.745478
0.0 1.0 0.0 0.0 0.763384 0.445349
0.0 0.0 1.0 0.0 0.886189 0.978133
0.0 0.0 0.0 1.0 0.37166 0.272733
julia> A=rand(4,6); A[1:4,1:3] = I;A # Somewhat less common to want
4×6 Array{Float64,2}:
1.0 0.0 0.0 0.58555 0.230785 0.197464
0.0 1.0 0.0 0.364411 0.589343 0.83179
0.0 0.0 1.0 0.758986 0.300437 0.190048
0.0 0.0 0.0 0.208139 0.514587 0.782728
julia> A=rand(4,6); A[2:3,2:4] = I;A #A bit unusual to do this but it would work nonetheless
4×6 Array{Float64,2}:
0.586206 0.219209 0.768404 0.0562488 0.403744 0.402825
0.14575 1.0 0.0 0.0 0.284117 0.860951
0.239251 0.0 1.0 0.0 0.688153 0.0935465
0.171968 0.989361 0.99173 0.327323 0.439517 0.290657
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by locating the AbstractMatrix setindex! entry point and the UniformScaling definition in the Julia LinearAlgebra codebase. Reproduce the indexed assignment examples from the issue, then add coverage for square, rectangular, and offset ranges; done means these assignments write the expected diagonal and zero values without changing unrelated matrix entries.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100