-
Notifications
You must be signed in to change notification settings - Fork 152
Porting SDiagonal from Bridge.jl #240
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
Changes from 4 commits
aebf3bc
1eeb98b
57f2526
0772fb6
1244a2c
ec68f06
c41d829
cf7b1da
38c9311
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # Originally contributed by D. Getz (https://github.com/getzdan), M. Schauer | ||
| # at https://github.com/mschauer/Bridge.jl under MIT License | ||
|
|
||
| import Base: getindex,setindex!,==,-,+,*,/,\,transpose,ctranspose,convert, size, abs, real, imag, conj, eye, inv | ||
|
||
| import Base.LinAlg: ishermitian, issymmetric, isposdef, factorize, diag, trace, det, logdet, expm, logm, sqrtm | ||
|
|
||
| @generated function scalem{T, M, N}(a::SMatrix{M,N, T}, b::SVector{N, T}) | ||
|
||
| expr = vec([:(a[$j,$i]*b[$i]) for j=1:M, i=1:N]) | ||
| :(SMatrix{M,N,T}($(expr...))) | ||
|
||
| end | ||
| @generated function scalem{T, M, N}(a::SVector{M,T}, b::SMatrix{M, N, T}) | ||
| expr = vec([:(b[$j,$i]*a[$j]) for j=1:M, i=1:N]) | ||
| :(SMatrix{M,N,T}($(expr...))) | ||
| end | ||
|
|
||
| struct SDiagonal{N,T} <: StaticMatrix{N, N, T} | ||
| diag::SVector{N,T} | ||
| SDiagonal{N,T}(diag::SVector{N,T}) where {N,T} = new(diag) | ||
| end | ||
| diagtype{N,T}(::Type{SDiagonal{N,T}}) = SVector{N,T} | ||
| diagtype{N}(::Type{SDiagonal{N}}) = SVector{N} | ||
| diagtype(::Type{SDiagonal}) = SVector | ||
|
|
||
| # this is to deal with convert.jl | ||
| @inline (::Type{SD})(a::AbstractVector) where {SD <: SDiagonal} = SDiagonal(diagtype(SD)(a)) | ||
|
||
| @inline (::Type{SD})(a::Tuple) where {SD <: SDiagonal} = SDiagonal(diagtype(SD)(a)) | ||
| @inline (::Type{SDiagonal}){N,T}(a::SVector{N,T}) = SDiagonal{N,T}(a) | ||
|
||
|
|
||
| @generated function SDiagonal{N,T}(a::SMatrix{N,N,T}) | ||
|
||
| expr = [:(a[$i,$i]) for i=1:N] | ||
| :(SDiagonal{N,T}($(expr...))) | ||
| end | ||
|
|
||
|
|
||
| convert{N,T}(::Type{SDiagonal{N,T}}, D::SDiagonal{N,T}) = D | ||
| convert{N,T}(::Type{SDiagonal{N,T}}, D::SDiagonal) = SDiagonal{N,T}(convert(SVector{N,T}, D.diag)) | ||
|
||
|
|
||
| size{N}(D::SDiagonal{N}) = (N,N) | ||
|
||
|
|
||
| function size{N}(D::SDiagonal{N},d::Int) | ||
|
||
| if d<1 | ||
| throw(ArgumentError("dimension must be ≥ 1, got $d")) | ||
| end | ||
| return d<=2 ? N : 1 | ||
| end | ||
|
|
||
| Base.@propagate_inbounds function getindex{N,T}(D::SDiagonal{N,T}, i::Int, j::Int) | ||
|
||
| @boundscheck checkbounds(D, i, j) | ||
| if i == j | ||
|
||
| @inbounds return D.diag[i] | ||
| else | ||
| zero(T) | ||
| end | ||
| end | ||
|
|
||
| # avoid linear indexing? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice. Unfortunately most the static arrays internal code assumes a linear indexing style - we should change this, but it won't be easy... |
||
| Base.@propagate_inbounds function getindex{N,T}(D::SDiagonal{N,T}, k::Int) | ||
|
||
| i, j = ind2sub(size(D), k) | ||
| D[i,j] | ||
| end | ||
|
|
||
| ishermitian{T<:Real}(D::SDiagonal{T}) = true | ||
| ishermitian(D::SDiagonal) = all(D.diag .== real(D.diag)) | ||
| issymmetric(D::SDiagonal) = true | ||
| isposdef(D::SDiagonal) = all(D.diag .> 0) | ||
|
|
||
| factorize(D::SDiagonal) = D | ||
|
|
||
| ==(Da::SDiagonal, Db::SDiagonal) = Da.diag == Db.diag | ||
| -(A::SDiagonal) = SDiagonal(-A.diag) | ||
| +(Da::SDiagonal, Db::SDiagonal) = SDiagonal(Da.diag + Db.diag) | ||
| -(Da::SDiagonal, Db::SDiagonal) = SDiagonal(Da.diag - Db.diag) | ||
| -(A::SDiagonal, B::SMatrix) = eye(typeof(B))*A - B | ||
|
|
||
| *{T<:Number}(x::T, D::SDiagonal) = SDiagonal(x * D.diag) | ||
| *{T<:Number}(D::SDiagonal, x::T) = SDiagonal(D.diag * x) | ||
| /{T<:Number}(D::SDiagonal, x::T) = SDiagonal(D.diag / x) | ||
| *(Da::SDiagonal, Db::SDiagonal) = SDiagonal(Da.diag .* Db.diag) | ||
| *(D::SDiagonal, V::SVector) = D.diag .* V | ||
|
||
| *(V::SVector, D::SDiagonal) = D.diag .* V | ||
|
||
| *(A::SMatrix, D::SDiagonal) = scalem(A,D.diag) | ||
|
||
| *(D::SDiagonal, A::SMatrix) = scalem(D.diag,A) | ||
| \(D::SDiagonal, b::SVector) = D.diag .\ b | ||
|
||
|
|
||
| conj(D::SDiagonal) = SDiagonal(conj(D.diag)) | ||
| transpose(D::SDiagonal) = D | ||
| ctranspose(D::SDiagonal) = conj(D) | ||
|
|
||
| diag(D::SDiagonal) = D.diag | ||
| trace(D::SDiagonal) = sum(D.diag) | ||
| det(D::SDiagonal) = prod(D.diag) | ||
| logdet{N,T<:Real}(D::SDiagonal{N,T}) = sum(log.(D.diag)) | ||
| function logdet{N,T<:Complex}(D::SDiagonal{N,T}) #Make sure branch cut is correct | ||
| x = sum(log.(D.diag)) | ||
| -pi<imag(x)<pi ? x : real(x)+(mod2pi(imag(x)+pi)-pi)*im | ||
| end | ||
|
|
||
| eye{N,T}(::Type{SDiagonal{N,T}}) = SDiagonal(ones(SVector{N,T})) | ||
|
|
||
| expm(D::SDiagonal) = SDiagonal(exp.(D.diag)) | ||
| logm(D::SDiagonal) = SDiagonal(log.(D.diag)) | ||
| sqrtm(D::SDiagonal) = SDiagonal(sqrt.(D.diag)) | ||
|
|
||
| \(D::SDiagonal, B::SMatrix) = scalem(1 ./ D.diag, B) | ||
|
||
| /(B::SMatrix, D::SDiagonal) = scalem(1 ./ D.diag, B) | ||
| \(Da::SDiagonal, Db::SDiagonal) = SDiagonal(Db.diag ./ Da.diag) | ||
| /(Da::SDiagonal, Db::SDiagonal) = SDiagonal(Da.diag ./ Db.diag ) | ||
|
|
||
| function inv{N,T}(D::SDiagonal{N,T}) | ||
| for i = 1:N | ||
|
||
| if D.diag[i] == zero(T) | ||
| throw(SingularException(i)) | ||
| end | ||
| end | ||
| SDiagonal(inv.(D.diag)) | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| @testset "SDiagonal" begin | ||
| @testset "Constructors" begin | ||
| @test SDiagonal{1,Int64}((1,)).diag === SVector{1,Int64}((1,)) | ||
| @test SDiagonal{1,Float64}((1,)).diag === SVector{1,Float64}((1,)) | ||
|
|
||
| @test SDiagonal{4,Float64}((1, 1.0, 1, 1)).diag.data === (1.0, 1.0, 1.0, 1.0) | ||
|
|
||
| # Bad input | ||
| @test_throws Exception SMatrix{1,Int}() | ||
| @test_throws Exception SMatrix{2,Int}((1,)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| # From SMatrix | ||
| @test SDiagonal(SMatrix{2,2,Int}((1,2,3,4))).diag.data === (1,4) | ||
|
|
||
| end | ||
|
|
||
| @testset "Methods" begin | ||
|
|
||
| m = SDiagonal(@SVector [11, 12, 13, 14]) | ||
| m2 = diagm([11, 12, 13, 14]) | ||
|
|
||
| b = @SVector [2,-1,2,1] | ||
| b2 = Vector(b) | ||
|
|
||
| @test isimmutable(m) == true | ||
|
|
||
| @test m[1,1] === 11 | ||
| @test m[2,2] === 12 | ||
| @test m[3,3] === 13 | ||
| @test m[4,4] === 14 | ||
|
|
||
| for i in 1:4 | ||
| for j in 1:4 | ||
| i == j || @test m[i,j] === 0 | ||
| end | ||
| end | ||
|
|
||
| @test_throws Exception m[5,5] | ||
|
|
||
| @test_throws Exception m[1,5] | ||
|
|
||
|
|
||
| @test size(m) === (4, 4) | ||
| @test size(typeof(m)) === (4, 4) | ||
| @test size(SDiagonal{4}) === (4, 4) | ||
|
|
||
| @test size(m, 1) === 4 | ||
| @test size(m, 2) === 4 | ||
| @test size(typeof(m), 1) === 4 | ||
| @test size(typeof(m), 2) === 4 | ||
|
|
||
| @test length(m) === 4*4 | ||
|
|
||
| @test_throws Exception m[1] = 1 | ||
|
|
||
| @test m*b == @SVector [22,-12,26,14] | ||
| @test m\b == m2\b | ||
| @test m*m == m2*m | ||
|
|
||
| @test ishermitian(m) == ishermitian(m2) | ||
| @test isposdef(m) == isposdef(m2) | ||
| @test issymmetric(m) == issymmetric(m2) | ||
|
|
||
| @test m' == m | ||
| @test 2m == m + m | ||
| @test 0m == m - m | ||
|
|
||
| @test m\m == eye(SDiagonal{4,Float64}) | ||
|
|
||
|
|
||
| end | ||
| end | ||
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.
By the way, if you're porting someone else's code, and you want to attribute them, it's also quite possible to make them the author in git using something like
git commit --author="Some Body <[email protected]>"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.
When I do this kind of thing, I'd be inclined to add the original chunk of code (in probably non-working form) under the other author's name as a single commit (
@mentioning them to make sure they're happy with that). Then add any necessary changes under your own name in further commits.The nice thing about doing it this way is you avoid baking authorship into comments which people feel they can't remove (like the one above), but you also get to do proper attribution which I feel is quite important.
Just some thoughts, I'm happy this was merged already.
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.
When you port entire files (like in this case) it is also possible to preserve the full git history http://gbayer.com/development/moving-files-from-one-git-repository-to-another-preserving-history/