Git Product home page Git Product logo

intervalmatrices.jl's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar juliatagbot avatar lucaferranti avatar mforets avatar schillic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

intervalmatrices.jl's Issues

Symbolic interval matrix power

#83 suggests to reformulate the matrix power with explicit intervals.

When interpreting an interval matrix M as a set of matrices Aᵢ, and we only require that

M^k ⊇ ⋃ᵢ Aᵢ^k

holds, we can do better: Perform the multiplications symbolically: e.g., in 2D, consider M = [a b; c d] and write M^k as a matrix whose entries are polynomials in the original intervals a,b,c,d. Then in the end evaluate the final polynomials with interval arithmetic.

Symbolic interval matrix power type

This issue is related to #88. Proposal: add a new IntervalMatrixPower wrapper which holds the power k of the matrix power. The purpose of this type is to experiment with lazy construction of the matrix power for evaluation using different methods. The new type can also have a cache for the representation of the power using symbols, eg. polynomials.

Fix Documenter integration

Builds fail due to insufficient access rights. This is probably another case where we integrated the key at the wrong Travis URL.

Add "Theory" section in the docs

  • introduce notation for interval matrices [A]

  • revise example from the Quickstart about squaring interval matrices (ref. #80, Kosheleva et al's paper).

  • non-associativity of matrix products

  • SUE

Rename expm functions

The term "expm" comes from pre-1.0 Julia, which used expm for the matrix exponential. It's now called exp, so i propose to rename expm_overapproximation to exp_overapproximation and similarly to the other methods that use expm.

Matrix power via square decomposition

Problem

The k-th integer power of an interval matrix M, M^k, is not well-defined. The reason is that interval-matrix multiplication is not associative. Furthermore, even for a fixed order, the multiplication induces an error due to the intervals. The only safe case is squaring (i.e., k = 2) (see #79).

Idea

Decompose the power k such that many squares are used. In other words: use as few non-square multiplications as possible.

Example

In the example below, we compute M^9.

  • A and B use the naive power operation, which are the worst options. (Interestingly, ^ does something slightly more intelligent. ?> ^ says equivalent to \exp(p\log(A))).
  • C, D, E use the fact that 9 = 2² + 2² + 1.
  • F, G, H use the fact that 9 = (2 + 1)³ + 2 + 1 (I only tried three permutations).
  • As can be seen, the order of the multiplications plays a role.
julia> M = rand(IntervalMatrix)
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-1.88661, 1.81318]     [-0.483837, 0.892705]
 [-0.893564, -0.447015]  [-0.10882, 1.85449]

julia> A = M*M*M*M*M*M*M*M*M  # 9 multiplications
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-4394.68, 4388.85]  [-4300.39, 4320.52]
 [-3599.78, 3582.38]  [-3495.86, 3545.53]

julia> B = M^9
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-3734.67, 3685.31]  [-3311.99, 3430.62]
 [-3602.63, 3458.46]  [-3008.63, 3290.32]

julia> M2 = square(M); M4 = square(M2);

julia> C = M4*M4*M  # 2 multiplications
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-2606.43, 2225.51]  [-1907.27, 2779.07]
 [-2700.44, 2863.45]  [-2624.69, 2006.86]

julia> D = M4*M*M4  # 2 multiplications
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-3126.19, 2073.73]  [-2410.01, 2737.56]
 [-2740.19, 2372.8]   [-2575.55, 2563.49]

julia> E = M*M4*M4  # 2 multiplications
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-3077.05, 2643.18]  [-2871.06, 2697.84]
 [-2781.74, 1880.41]  [-2624.69, 2006.86]

julia> F = square(M2*M) * M2*M  # 3 multiplications
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-3310.01, 2898.24]  [-2427.69, 3330.15]
 [-2954.44, 2930.17]  [-2664.27, 2640.69]

julia> G = M2*M * square(M2*M)  # 3 multiplications
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-3432.39, 2898.24]  [-2646.09, 2878.82]
 [-3104.07, 2437.67]  [-2388.99, 2603.88]

julia> H = M2 * square(M2*M) * M  # 3 multiplications
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-3211.14, 2877.44]  [-2416.47, 3160.68]
 [-3121.46, 3105.47]  [-2767.25, 2662.37]

Heuristics

Presumably there is no optimal way to decompose the power, even when ignoring the order of the multiplications. A heuristics could be to decompose into the largest square numbers.

Sharing results

There is another issue: In the above algorithm/heuristics we may compute certain matrix powers several times. It might be better to have some symbolic representation of the decomposition (as we had in the example) and then compute each term only once. However, that sounds more complicated.

Faster infinity-norm computation

Currently the computation of the infinity norm uses left and right:

return LinearAlgebra.norm(max.(abs.(left(A)), abs.(right(A))), Inf)

However, these methods allocate one new vector (of the size of the matrix) each. Since in the end we are only interested in a single value, this is not necessary.

function infinity_norm(A)
    res = abs(A[1, 1].left)
    for itv in A
        res = max(res, abs(itv.left), itv.right)
    end
    return res
end

(Note that I omitted the abs on itv.right for efficiency.)

We should also evaluate if the following is faster (rationale: write only when necessary).

function infinity_norm2(A)
    res = abs(A[1, 1].left)
    for itv in A
        m = max(abs(itv.left), itv.right)
        if m > res
            res = m
        end
    end
    return res
end

Truncated exponential sum

Write a function that computes the sum \sum_{i=i0}^{p} A^i * t^i / i!}. Note that if i0=0 one should use the exact computation of At + 1/2A^2 t^2 (matrix W).

  • outsource existing code to new function (#123)
  • make code parametric in i0

Bug in exp_overapproximation

julia> K = copy(Pint.s.A)
4×4 IntervalMatrix{Float64,IntervalArithmetic.Interval{Float64},Array{IntervalArithmetic.Interval{Float64},2}}:
     [0, 0]    [0, 0]  [-2500, -2500]    [2500, 2500]
     [0, 0]    [0, 0]          [0, 0]  [-2500, -2500]
   [10, 10]    [0, 0]    [-100, -100]          [0, 0]
 [-10, -10]  [10, 10]          [0, 0]      [-10, -10]

julia> expm_overapproximation(K, 0.002, 12)
4×4 IntervalMatrix{Float64,IntervalArithmetic.Interval{Float64},Array{IntervalArithmetic.Interval{Float64},2}}:
 [-5619.76, 5621.58]  [-5620.62, 5620.72]    [-5615.96, 5625.38]
 [-5620.62, 5620.72]  [-5619.72, 5621.62]     [-5625.46, 5615.88]
 [-5620.65, 5620.69]  [-5620.67, 5620.67]     [-5620.62, 5620.72]
 [-5620.69, 5620.65]  [-5620.65, 5620.69]     [-5619.79, 5621.55]

julia> expm_overapproximation_old(K, 0.002, 12)
4×4 IntervalMatrix{Float64,IntervalArithmetic.Interval{Float64},Array{IntervalArithmetic.Interval{Float64},2}}:
  [0.600556, 1.21044]   [-0.256488, 0.353395]     [4.40467, 5.01456]  
 [-0.256488, 0.353395]   [0.64621, 1.2561]        [-5.09231, -4.48241] 
 [-0.287424, 0.322458]  [-0.30463, 0.305252]      [-0.259597, 0.350285]
 [-0.32378, 0.286103]   [-0.285792, 0.324091]      [0.578608, 1.18849] 

julia> exp(mid(K) * 0.002)
4×4 Array{Float64,2}:
  0.905497   0.0484535    -4.37927     4.70961  
  0.0484535  0.951152     -0.0777469  -4.78736  
  0.0175171  0.000310987   0.77567     0.0453436
 -0.0188385  0.0191494     0.0453436   0.883549 

where expm_overapproximation_old is using the function expm_overapproximation before we made the changes that entered in v0.2.0, so it could be in PR#18 or PR#19, or it could be something else.

Note that in this example the intervals in K have width zero. The huge intervals [-5619.76, 5621.58] seem like a bug.

Improve quadratic_expansion

Here are some optimizations for the function quadratic_expansion (below):

  • Swap iteration over i/j (iterate over columns in Julia).
  • Compute t^2/2 only once.
  • Compute S first (avoids double access to each cell).
  • Inline k ≠ i conditions (have two loops from 1 to i-1 and from i+1 to n) (ifs in loops are performance killers); the version for k ≠ i && k ≠ j needs three loops.
    I think there was some neat Julia trick to make this elegant, but I forgot; the body can be written as a macro to avoid code duplication.
  • Generalize to numeric type of A (no performance improvement, but nice to have).

function quadratic_expansion(A::IntervalMatrix, t)
n = LinearAlgebra.checksquare(A)
@inline function κ(aii, t)
if -1/t aii
return -1/2
else
return min(aii.left*t+aii.left^2*t^2/2, aii.right*t+aii.right^2*t^2/2)
end
end
W = similar(A)
for i in 1:n
for j in 1:n
if i j
W[i, j] = A[i, j] * (t + (A[i, i] + A[j, j])*(t^2/2.))
S = 0
for k in 1:n
if k i && k j
S = S + A[i, k] * A[k, j]
end
end
W[i, j] = W[i, j] + S * (t^2/2.)
else
u = A[i, i].left * t + A[i, i].left^2 * t^2/2
v = A[i, i].right * t + A[i, i].right^2 * t^2/2
W[i, i] = ClosedInterval(κ(A[i, i], t), max(u, v))
S = 0
for k in 1:n
if k i
S = S + A[i, k] * A[k, i]
end
end
W[i, i] = W[i, i] + S * (t^2/2.)
end
end
end
return W
end

Matrix square

Given an interval matrix A, we can compute exactly. This is not true for higher powers. See [1, Section 6] for an algorithm.

[1] Olga Kosheleva, Vladik Kreinovich, Günter Mayer, Hung T. Nguyen: Computing the cube of an interval matrix is NP-Hard. SAC 2005. [PDF]

Rump fast interval matrix multiplication

It would be great to implement Rump’s method for efficiently multiplying interval matrices using only floating-point matrix operations (with rounding mode changes).

Improve expm_overapproximation

Here are some optimizations for the function expm_overapproximation (below):

  • Do not explicitly allocate the identity matrices (2x) resp. the [-1, 1] matrix but rather modify the matrices where they are used later.
  • Introduce a variable for t^i/factorial(i) in the loop and then just multiply with t/i instead of recomputing from scratch.
  • Swap the order in the last loop (saves one multiplication of A)
  • Put the assertion to the top.
  • Generalize to numeric type of A (no performance improvement, but nice to have).

function expm_overapproximation(A::IntervalMatrix{T, <: AbstractInterval{T}}, t, p) where {T}
n = size(A, 1)
Id = IntervalMatrix(fill(zero(T)±zero(T), (n , n)))
for i in 1:n
Id[i, i] = one(T)±zero(T)
end
Γ = IntervalMatrix(fill(zero(T)±one(T), (n , n)))
nA = norm(A, Inf)
c = nA * t / (p + 2)
@assert c < 1
E = Γ * ((nA*t)^(p+1) * (1/factorial(p + 1) * 1/(1-c)))
S = IntervalMatrix(fill(zero(T)±zero(T), (n , n)))
Ai = A * A * A
for i in 3:p
S = S + Ai * (t^i/factorial(i))
Ai = Ai * A
end
W = quadratic_expansion(A, t)
return Id + W + S + E
end

Constructor for identity matrix

It is handy to have a function for creating an identity matrix.

Maybe Diagonal(one_interval, n) "just works" (not tested).

Add copy method

julia> A = IntervalMatrix([-1.0 ± 0.05 -4.0 ± 0.05;
                           4.0 ± 0.05 -1.0 ± 0.05])
2×2 IntervalMatrix{Float64,IntervalArithmetic.Interval{Float64},Array{IntervalArithmetic.Interval{Float64},2}}:
 [-1.05001, -0.949999]  [-4.05001, -3.94999] 
  [3.94999, 4.05001]    [-1.05001, -0.949999]

julia> typeof(A)
IntervalMatrix{Float64,IntervalArithmetic.Interval{Float64},Array{IntervalArithmetic.Interval{Float64},2}}

julia> B = copy(A)
2×2 Array{IntervalArithmetic.Interval{Float64},2}:
 [-1.05001, -0.949999]  [-4.05001, -3.94999] 
  [3.94999, 4.05001]    [-1.05001, -0.949999]

julia> typeof(B)  # not an IntervalMatrix!
Array{IntervalArithmetic.Interval{Float64},2}

Overload subtraction

A - B does not return an IntervalMatrix.

julia> rand(IntervalMatrix) - rand(IntervalMatrix)
2×2 Array{Interval{Float64},2}:
  [0.16067, 1.02546]    [1.04121, 1.29847] 
 [-2.29271, -1.06139]  [-0.32639, 0.903041]

Compute eigenvalues of an interval matrix

There are several references, see e.g.

See also the package: https://github.com/dpsanders/IntervalEigenvalues.jl

Define product between interval matrix and matrix

julia> M
2×2 IntervalMatrix{Float64,Interval{Float64},Array{Interval{Float64},2}}:
 [-11.0001, -9]  [-41, -38.9999]
  [38.9999, 41]       [-11.0001, -9]


julia> mid(M)
2×2 Array{Float64,2}:
 -10.0  -40.0
  40.0  -10.0

julia> M * mid(M)
2×2 Array{Interval{Float64},2}:
 [-1550, -1449.99]                      [749.999, 850.001]
         [-850.001, -749.999]  [-1550, -1449.99]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.