-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from JuliaAlgebra/bl/symmetric_setindex!
Add symmetric_setindex!
- Loading branch information
Showing
5 changed files
with
68 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
```@docs | ||
MomentMatrix | ||
moment_matrix | ||
SymMatrix | ||
symmetric_setindex! | ||
``` | ||
|
||
## Atomic measure | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
struct SymMatrix{T} <: AbstractMatrix{T} | ||
Q::Vector{T} | ||
n::Int | ||
end | ||
Symmetric ``n \\times n`` matrix storing the vectorized upper triangular | ||
part of the matrix in the `Q` vector (this corresponds to the vectorized | ||
form of `MathOptInterface.PositiveSemidefiniteConeTriangle`). | ||
It implement the `AbstractMatrix` interface except for `setindex!` as it might | ||
break its symmetry. The [`symmetric_setindex!`](@ref) function should be used | ||
instead. | ||
""" | ||
struct SymMatrix{T} <: AbstractMatrix{T} | ||
Q::Vector{T} | ||
n::Int | ||
end | ||
|
||
# i <= j | ||
#function trimap(i, j, n) # It was used when Q was the lower triangular part | ||
# div(n*(n+1), 2) - div((n-i+1)*(n-i+2), 2) + j-i+1 | ||
#end | ||
|
||
# j <= i | ||
function trimap(i, j) | ||
div((i-1)*i, 2) + j | ||
end | ||
|
||
function trimat(::Type{T}, f, n, σ) where {T} | ||
Q = Vector{T}(undef, trimap(n, n)) | ||
for i in 1:n | ||
for j in 1:i | ||
Q[trimap(i, j)] = f(σ[i], σ[j]) | ||
end | ||
end | ||
SymMatrix{T}(Q, n) | ||
end | ||
|
||
Base.size(Q::SymMatrix) = (Q.n, Q.n) | ||
|
||
""" | ||
symmetric_setindex!(Q::SymMatrix, value, i::Integer, j::Integer) | ||
Set `Q[i, j]` and `Q[j, i]` to the value `value`. | ||
""" | ||
function symmetric_setindex!(Q::SymMatrix, value, i::Integer, j::Integer) | ||
Q.Q[trimap(max(i, j), min(i, j))] = value | ||
end | ||
|
||
function Base.getindex(Q::SymMatrix, i::Integer, j::Integer) | ||
return Q.Q[trimap(max(i, j), min(i, j))] | ||
end | ||
Base.getindex(Q::SymMatrix, I::Tuple) = Q[I...] | ||
Base.getindex(Q::SymMatrix, I::CartesianIndex) = Q[I.I] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters