-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add scale, scale! and copy! for triangular matrices #12683
Conversation
@@ -283,6 +283,155 @@ function -(A::UnitUpperTriangular) | |||
UpperTriangular(Anew) | |||
end | |||
|
|||
# copy and scale | |||
function copy!(A::UpperTriangular,B::UpperTriangular) | |||
copy!(A.data,B.data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to my tests, it is faster to just loop and copy the upper triangle.
7210458
to
e300aa4
Compare
@stevengj Thank you for reviewing the patch. I tried my best to overcome the issues you pointed out. Two of them I don't know how to solve are:
function scale{R<:Real,S<:Complex}(X::AbstractArray{R}, s::S)
Y = Array(promote_type(R,S), size(X))
copy!(Y, X)
scale!(Y, s)
end in
julia> Array(BigFloat, 5, 5)
5x5 Array{Base.MPFR.BigFloat,2}:
#undef #undef #undef #undef #undef
#undef #undef #undef #undef #undef
#undef #undef #undef #undef #undef
#undef #undef #undef #undef #undef
#undef #undef #undef #undef #undef and thus that julia> A = convert(Matrix{BigFloat},randn(5,5)); similar(A)
5x5 Array{Base.MPFR.BigFloat,2}:
#undef #undef #undef #undef #undef
#undef #undef #undef #undef #undef
#undef #undef #undef #undef #undef
#undef #undef #undef #undef #undef
#undef #undef #undef #undef #undef As the scale functions fill just the triangle, the remaining half-or-so of the matrix still has Any suggestions? |
end | ||
end | ||
end | ||
function copy{T<:AbstractTriangular}(A::T) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this method necessary? I think the copy
method for AbstractArray
can handle this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't noticed that, thank you!
@andreasnoack Thank you for suggesting to fall back to |
Sorry. The other comments had been folded because of new pushes to the branch. I don't think |
@andreasnoack Thank you, I implemented the change you suggested to function full!{T,S}(A::LowerTriangular{T,S})
B = A.data
tril!(B)
B
end
function full!{T,S}(A::UnitLowerTriangular{T,S})
B = A.data
tril!(B)
for i = 1:size(A,1)
B[i,i] = 1
end
B
end
function full!{T,S}(A::UpperTriangular{T,S})
B = A.data
triu!(B)
B
end
function full!{T,S}(A::UnitUpperTriangular{T,S})
B = A.data
triu!(B)
for i = 1:size(A,1)
B[i,i] = 1
end
B
end but there might be several other places as well. It looks like we don't really want to access the |
@mfasi It doesn't look like you have pushed your latest changes. |
e300aa4
to
7549546
Compare
@andreasnoack I hadn't because some of the tests failed. I pushed them, so that you can have a look, but the tests still fail. |
@andreasnoack I think that I might limit myself to just add julia> A = UpperTriangular(convert(Matrix{BigFloat},randn(5,5)));
julia> scale(A,0.5)
ERROR: BoundsError: attempt to access 5x5 Base.LinAlg.UpperTriangular{Base.MPFR.BigFloat,Array{Base.MPFR.BigFloat,2}}:
5.203245023508947797452606209844816476106643676757812500000000000000000000000000e-01 … 9.935906547387056697573370911413803696632385253906250000000000000000000000000000e-01
0.000000000000000000000000000000000000000000000000000000000000000000000000000000 8.351518361159303571739087601599749177694320678710937500000000000000000000000000e-01
0.000000000000000000000000000000000000000000000000000000000000000000000000000000 1.277831052353999519155536290782038122415542602539062500000000000000000000000000
0.000000000000000000000000000000000000000000000000000000000000000000000000000000 1.189765274846499742977812275057658553123474121093750000000000000000000000000000
0.000000000000000000000000000000000000000000000000000000000000000000000000000000 5.026842940210519694588242600730154663324356079101562500000000000000000000000000e-01
at index [2,1]
in generic_scale! at linalg/generic.jl:20
in scale at linalg/generic.jl:5 |
@mfasi But |
ef20861
to
3df7014
Compare
@andreasnoack I think that as long as we explicitly use My last push tries to solve the |
end | ||
return A | ||
end | ||
copy{S<:AbstractTriangular{BigFloat}}(A::S) = S(copy(A.data)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be there. If it gives an error then there is another bug to big. Special casing BigFloat
is not an option. There could be many other element types with the same problem, e.g. DecFP
and Matrix
.
3df7014
to
4a3fa05
Compare
@andreasnoack I had never thought about data types not in the test, thank you for pointing this out. I think that properly solving the issue with As far as I can tell, this commit solves a bug (scale for triangular matrices) and adds two methods I need, without breaking anything else. I don't know whether the behaviour of |
Add scale, scale! and copy! for triangular matrices
@mfasi Thanks. If you still see visible undefs from working with |
@andreasnoack Thank you, I think I know a way to produce the |
Add the methods
scale!
andcopy!
for triangular matrices. This addition is motivated by a few improvements suggested by @stevengj in #12584.I tried to add the
scale
andcopy
methods as well. Not sure I did something reasonable there, as somewhere along the line I started listening to the compilation warnings without really understanding what was going on.