Skip to content

Commit

Permalink
Add symmetric_setindex!
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Apr 15, 2019
1 parent 3071c57 commit 9d883fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/moment_matrix.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export SymMatrix, MomentMatrix, getmat, moment_matrix
export SymMatrix, MomentMatrix, getmat, moment_matrix, symmetric_setindex!

using SemialgebraicSets

Expand Down Expand Up @@ -29,8 +29,17 @@ 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)
Q.Q[trimap(max(i, j), min(i, j))]
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]
Expand Down
10 changes: 10 additions & 0 deletions test/moment_matrix.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
@testset "SymMatrix" begin
Q = SymMatrix([1, 2, 3], 2)
symmetric_setindex!(Q, 4, 1, 1)
@test Q.Q == [4, 2, 3]
symmetric_setindex!(Q, 5, 1, 2)
@test Q.Q == [4, 5, 3]
symmetric_setindex!(Q, 6, 2, 2)
@test Q.Q == [4, 5, 6]
end

@testset "MomentMatrix" begin
Mod.@polyvar x y
@test_throws ArgumentError moment_matrix(measure([1], [x]), [y])
Expand Down

0 comments on commit 9d883fe

Please sign in to comment.