From e65407f915cc017e44827e816c8bd424d7bf22cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 15 Apr 2019 15:11:07 +0200 Subject: [PATCH] Fix doc --- docs/src/atoms.md | 2 ++ src/MultivariateMoments.jl | 1 + src/moment_matrix.jl | 42 ----------------------------- src/symmatrix.jl | 54 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 42 deletions(-) create mode 100644 src/symmatrix.jl diff --git a/docs/src/atoms.md b/docs/src/atoms.md index a2770ad..bb8b35f 100644 --- a/docs/src/atoms.md +++ b/docs/src/atoms.md @@ -5,6 +5,8 @@ ```@docs MomentMatrix moment_matrix +SymMatrix +symmetric_setindex! ``` ## Atomic measure diff --git a/src/MultivariateMoments.jl b/src/MultivariateMoments.jl index d90c411..0a1f1fc 100644 --- a/src/MultivariateMoments.jl +++ b/src/MultivariateMoments.jl @@ -16,6 +16,7 @@ abstract type AbstractMeasure{T} <: AbstractMeasureLike{T} end include("moment.jl") include("measure.jl") include("expectation.jl") +include("symmatrix.jl") include("moment_matrix.jl") include("atomic.jl") include("extract.jl") diff --git a/src/moment_matrix.jl b/src/moment_matrix.jl index 018222d..5cdaaa5 100644 --- a/src/moment_matrix.jl +++ b/src/moment_matrix.jl @@ -2,48 +2,6 @@ export SymMatrix, MomentMatrix, getmat, moment_matrix, symmetric_setindex! using SemialgebraicSets -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] - """ mutable struct MomentMatrix{T, MT <: AbstractMonomial, MVT <: AbstractVector{MT}} <: AbstractMeasureLike{T} Q::SymMatrix{T} diff --git a/src/symmatrix.jl b/src/symmatrix.jl new file mode 100644 index 0000000..cea13bf --- /dev/null +++ b/src/symmatrix.jl @@ -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]