Reference of auxilliaries

This reference page lists some additional functions and type that are required for the package to work properly. They are not part of the public API and are not exported; however, they may be useful even when separated from the purpose of polynomial optimization.

PolynomialOptimization.@allocdiffMacro
@allocdiff

A macro to evaluate an expression, discarding the resulting value, instead returning the difference in the number of bytes allocated after vs. before evaluation of the expression (which is not guaranteed to be the peak, but allows to capture allocations done in third-party libraries that don't use Julia's GC, contrary to @allocated). In order to provide consistent results, Julia's GC is disabled while the expression is evaluated. Note that on *nix systems, the value is only accurate up to the kibibyte.

source
PolynomialOptimization.keepcol!Function
keepcol!(A::Union{<:Matrix,<:SparseMatrixCSC}, m::AbstractVector{Bool})

Only keep the columns in the matrix A that are true in the vector m (which must have the same length as size(A, 2)). The output is logically the same as A[:, m]; however, note that the data underlying A is mutated: this function does not create a copy. The function returns the new matrix; the old one should no longer be used (it might become invalid, as the sparse matrix struct is immutable).

source
PolynomialOptimization.Solver.unique_outer_groupingsFunction
unique_outer_groupings(grouping::AbstractVector{<:IntMonomial}[, result::Set])

Computes a set of all unique groupings that are produced via groupings * groupings'. For purely complex-valued groupings, this is the full list; as soon as we have a real variable present, it is smaller. Returns a set of FastKey-wrapped monomial indices, a boolean indicating whether the grouping is effectively real-valued, and the number of (real-valued) equalities that arise from this grouping (which should be multiplied by 2 if the groupings are to be multiplied by a complex-valued constraint). If present, the data will be appended to the result parameter.

source

Chordal graphs

The following functions provide a thin wrapper to CliqueTrees.jl.

Sorting of multiple vectors

To simplify the task of sorting one vector and at the same time sorting multiple other vectors according to the same order - a common task that usually requires first computing a sortperm and then indexing all vectors with this permutation - PolynomialOptimization provides a helper function.

PolynomialOptimization.sort_along!Function
sort_along!(v::AbstractVector, along::AbstractVector...; lo=1, hi=length(v),
    o=Base.Order.Forward, relevant=1)

This helper function sorts the vector v as sort! would do, but at the same time puts all vectors in along in the same order as v. Therefore, sort_along!(v, a, b, c) is equivalent to

p = sortperm(v)
permute!.((v, a, b, c), (p,))

but does not require any new intermediate allocations. Note that by increasing relevant (to at most 1 + length(along)), additional vectors in along can be taken into account when breaking ties in v.

Implementation

Note that the sorting functions are exactly identical to the ones used in Julia 1.8 (there was a big overhaul in 1.9 which made everything extremely complicated; we don't reproduce the new behavior). This means that either InsertionSort or QuickSort are used.

source

FastVector

To improve the speed in some implementation details, PolynomialOptimization provides a "fast" vector type. This is basically just a wrapper around the stdlib Vector; however, it actually takes proper advantage of sizehints. The fact that Julia does this badly has been known for quite some time (#24909), but the default implementation has not changed (this will be different starting from Julia 1.11). Our own FastVec is a bit more specific than the PushVector, but also allows for more aggressive optimization. FastVec can directly be used in ccall with a pointer element type.

PolynomialOptimization.FastVector.FastVecType
FastVec{V}(undef, n::Integer; buffer::Integer=n)

Creates a new FastVec, which is a vector of elements of type V. The elements are initially undefined; there are n items. The vector has a capacity of size buffer; while it can hold arbitrarily many, pushing to the vector is fast as long as the capacity is not exceeded.

source
Base.sizehint!Method
sizehint!(v::FastVec, len::Integer)

Changes the size of the internal buffer that is kept available to quickly manage pushing into the vector. If len is smaller than the actual length of this vector, this is a no-op.

source
Base.resize!Method
resize!(v::FastVec, n::Integer)

Ensures that the internal buffer can hold at least n items (meaning that larger buffers will not be shrunk, but smaller ones will be increased to exactly n) and sets the length of the vector to n.

source
Base.empty!Method
empty!(v::FastVec)

Clears the vector without freeing the internal buffer.

source
PolynomialOptimization.FastVector.prepare_push!Function
prepare_push!(v::FastVec, new_items::Integer)

Prepares pushing (or appending) at last new_items in the future, in one or multiple calls. This ensures that the internal buffer is large enough to hold all the new items that will be pushed without allocations in between.

source
Base.push!Method
push!(v::FastVec, el)

Adds el to the end of v, increasing the length of v by one. If there is not enough space, grows v. Note that if you made sure beforehand that the capacity of v is sufficient for the addition of this element, consider calling unsafe_push! instead, which avoids the length check.

source
PolynomialOptimization.FastVector.unsafe_push!Function
unsafe_push!(v::FastVec, el)

Adds el to the end of v, increasing the length of v by one. This function assumes that the internal buffer of v holds enough space to add at least one element; if this is not the case, it will lead to memory corruption. Call push! instead if you cannot guarantee the necessary buffer size.

source
Base.insert!Method
insert!(v::FastVec, index::Integer, el)

Insert an el into v at the given index. index is the index of item in the resulting v. Note that if you made sure beforehand that the capacity of v is sufficient for the addition of this element, consider calling unsafe_insert! instead, which avoids the length check.

source
PolynomialOptimization.FastVector.unsafe_insert!Function
unsafe_insert!(v::FastVec, index::Integer, el)

Insert an el into v at the given index. index is the index of item in the resulting v. This function assumes that the internal buffer of v holds enough space to add at least one element; if this is not the case, it will lead to memory corruption. Call insert! instead if you cannot guarantee the necessary buffer size.

source
Base.append!Method
append!(v::FastVec, els)

Appends all items in els to the end of v, increasing the length of v by length(els). If there is not enough space, grows v. Note that if you made sure beforehand that the capacity of v is sufficient for the addition of these elements, consider calling unsafe_append! instead, which avoids the length check.

source
PolynomialOptimization.FastVector.unsafe_append!Function
unsafe_append!(v::FastVec, els)

Appends all items in els to the end of v, increasing the length of v by length(els). This function assumes that the internal buffer of v holds enough space to add at least all elements in els; if this is not the case, it will lead to memory corruption. Call append! instead if you cannot guarantee the necessary buffer size.

source
Base.prepend!Method
prepend!(v::FastVec, els)

Prepends all items in els to the beginning of v, increasing the length v by length(els). If there is not enough space, grows v. Note that if you made sure beforehand that the capacity of v is sufficient for the addition of these elements, consider calling unsafe_prepend! instead, which avoids the length check.

source
PolynomialOptimization.FastVector.unsafe_prepend!Function
unsafe_prepend!(v::FastVec, els::AbstractVector)

Prepends all items in els to the beginning of v, increasing the length v by length(els). This function assumes that the internal buffer of v holds enough space to add at least all elements in els; if this is not the case, it will lead to memory corruption. Call prepend! instead if you cannot guarantee the necessary buffer size.

source
Base.splice!Method
splice!(v::FastVec, index::Integer, [replacement]) -> item

Remove the item at the given index, and return the removed item. Subsequent items are shifted left to fill the resulting gap. If specified, replacement values from an ordered collection will be spliced in place of the removed item. No unsafe version of this function exists. To insert replacement before an index n without removing any items, use splice!(collection, n:n-1, replacement).

source
Base.similarMethod
similar(v::FastVec)

Creates a FastVec of the same type and length as v. All the common variants to supply different element types or lengths are also available; when changing the length, you might additionally specify the keyword argument buffer that also allows to change the internal buffer size.

source
Base.copyto!Method
copyto!(dest::FastVec, doffs::Integer, src::FastVec, soffs::Integer, n::Integer)
copyto!(dest::Array, doffs::Integer, src::FastVec, soffs::Integer, n::Integer)
copyto!(dest::FastVec, doffs::Integer, src::Array, soffs::Integer, n::Integer)

Implements the standard copyto! operation between FastVecs and also mixed with source or destination as an array.

source
Base.deleteat!Method
deleteat!(a::FastVec, i)

Remove the item at the given i and return the modified a. Subsequent items are shifted to fill the resulting gap. The internal buffer size is not modified. The index must be either an integer or a unit range.

source
PolynomialOptimization.FastVector.finish!Function
finish!(v::FastVec)

Returns the Vector representation that internally underlies the FastVec instance. This function should only be called when no further operations on the FastVec itself are carried out.

source