Skip to content
This repository was archived by the owner on Aug 6, 2023. It is now read-only.

Commit 283335b

Browse files
committed
use new EllAlgo 1.3.6
1 parent 3c12f4c commit 283335b

File tree

6 files changed

+116
-101
lines changed

6 files changed

+116
-101
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ option(INSTALL_ONLY "Enable for installation only" OFF)
1212
# Note: update this to your new project's name and version
1313
project(
1414
LmiSolver
15-
VERSION 1.3.4
15+
VERSION 1.3.5
1616
LANGUAGES CXX
1717
)
1818

bench/BM_lmi.cpp

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#include <tuple> // for tuple
77
#include <type_traits> // for move
88
#include <vector> // for vector
9-
#include <xtensor/xlayout.hpp> // for layout_type, layout_type::ro...
9+
#include <xtensor/xarray.hpp> // for xarray_container
10+
#include <xtensor/xlayout.hpp> // for layout_type, layout_type::row...
1011
#include <xtensor/xmath.hpp> // for sum
11-
#include <xtensor/xoperation.hpp> // for operator*
12+
#include <xtensor/xoperation.hpp> // for xfunction_type_t, operator*
13+
#include <xtensor/xreducer.hpp> // for xreducer
1214
#include <xtensor/xtensor_forward.hpp> // for xarray
1315

1416
#include "benchmark/benchmark.h" // for BENCHMARK, State, BENCHMARK_...
@@ -20,6 +22,7 @@
2022
*/
2123
template <typename Oracle> class my_oracle {
2224
using Arr = xt::xarray<double, xt::layout_type::row_major>;
25+
using Vec = std::valarray<double>;
2326
using Cut = std::pair<Arr, double>;
2427

2528
private:
@@ -138,36 +141,37 @@ static void LMI_old(benchmark::State &state) {
138141
}
139142
BENCHMARK(LMI_old);
140143

141-
/**
142-
* @brief
143-
*
144-
* @param[in,out] state
145-
*/
146-
static void LMI_No_Trick(benchmark::State &state) {
147-
using Arr = xt::xarray<double, xt::layout_type::row_major>;
148-
149-
// const auto c = Arr {1.0, -1.0, 1.0};
150-
const auto F1 = std::vector<Arr>{{{-7.0, -11.0}, {-11.0, 3.0}},
151-
{{7.0, -18.0}, {-18.0, 8.0}},
152-
{{-2.0, -8.0}, {-8.0, 1.0}}};
153-
const auto B1 = Arr{{33.0, -9.0}, {-9.0, 26.0}};
154-
const auto F2 = std::vector<Arr>{
155-
{{-21.0, -11.0, 0.0}, {-11.0, 10.0, 8.0}, {0.0, 8.0, 5.0}},
156-
{{0.0, 10.0, 16.0}, {10.0, -10.0, -10.0}, {16.0, -10.0, 3.0}},
157-
{{-5.0, 2.0, -17.0}, {2.0, -6.0, 8.0}, {-17.0, 8.0, 6.0}}};
158-
const auto B2 = Arr{{14.0, 9.0, 40.0}, {9.0, 91.0, 10.0}, {40.0, 10.0, 15.0}};
159-
160-
while (state.KeepRunning()) {
161-
auto P = my_oracle<LmiOracle<Arr>>(F1, B1, F2, B2, Arr{1.0, -1.0, 1.0});
162-
auto E = Ell<Arr>(10.0, Arr{0.0, 0.0, 0.0});
163-
E.no_defer_trick = true;
164-
auto t = 1e100; // std::numeric_limits<double>::max()
165-
[[maybe_unused]] const auto rslt = cutting_plane_optim(P, E, t);
166-
}
167-
}
168-
169-
// Register the function as a benchmark
170-
BENCHMARK(LMI_No_Trick);
144+
// /**
145+
// * @brief
146+
// *
147+
// * @param[in,out] state
148+
// */
149+
// static void LMI_No_Trick(benchmark::State &state) {
150+
// using Arr = xt::xarray<double, xt::layout_type::row_major>;
151+
//
152+
// // const auto c = Arr {1.0, -1.0, 1.0};
153+
// const auto F1 = std::vector<Arr>{{{-7.0, -11.0}, {-11.0, 3.0}},
154+
// {{7.0, -18.0}, {-18.0, 8.0}},
155+
// {{-2.0, -8.0}, {-8.0, 1.0}}};
156+
// const auto B1 = Arr{{33.0, -9.0}, {-9.0, 26.0}};
157+
// const auto F2 = std::vector<Arr>{
158+
// {{-21.0, -11.0, 0.0}, {-11.0, 10.0, 8.0}, {0.0, 8.0, 5.0}},
159+
// {{0.0, 10.0, 16.0}, {10.0, -10.0, -10.0}, {16.0, -10.0, 3.0}},
160+
// {{-5.0, 2.0, -17.0}, {2.0, -6.0, 8.0}, {-17.0, 8.0, 6.0}}};
161+
// const auto B2 = Arr{{14.0, 9.0, 40.0}, {9.0, 91.0, 10.0},
162+
// {40.0, 10.0, 15.0}};
163+
//
164+
// while (state.KeepRunning()) {
165+
// auto P = my_oracle<LmiOracle<Arr>>(F1, B1, F2, B2, Arr{1.0, -1.0, 1.0});
166+
// auto E = Ell<Arr>(10.0, Arr{0.0, 0.0, 0.0});
167+
// E.no_defer_trick = true;
168+
// auto t = 1e100; // std::numeric_limits<double>::max()
169+
// [[maybe_unused]] const auto rslt = cutting_plane_optim(P, E, t);
170+
// }
171+
// }
172+
//
173+
// // Register the function as a benchmark
174+
// BENCHMARK(LMI_No_Trick);
171175

172176
BENCHMARK_MAIN();
173177

include/lmisolver/ldlt_ext.hpp

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// -*- coding: utf-8 -*-
22
#pragma once
33

4+
#include <cassert> // for assert
45
#include <cstddef> // for size_t
56
#include <ellalgo/ell_matrix.hpp>
67
#include <utility> // for pair
@@ -16,16 +17,15 @@
1617
*/
1718
template <typename Arr036> class ldlt_ext {
1819
using Vec = std::valarray<double>;
19-
using Mat = Matrix;
2020
using Rng = std::pair<size_t, size_t>;
2121

2222
public:
2323
Rng p{0U, 0U}; //!< the rows where the process starts and stops
2424
Vec witness_vec; //!< witness vector
25+
const size_t _n; //!< dimension
2526

2627
private:
27-
const size_t _n; //!< dimension
28-
Mat T; //!< temporary storage
28+
Matrix T; //!< temporary storage
2929

3030
// static Vec zeros_vec(size_t n);
3131
// static Mat zeros_mat(size_t n);
@@ -36,7 +36,7 @@ template <typename Arr036> class ldlt_ext {
3636
*
3737
* @param[in] N dimension
3838
*/
39-
explicit ldlt_ext(size_t N);
39+
explicit ldlt_ext(size_t N) : witness_vec(0.0, N), _n{N}, T{N} {}
4040

4141
ldlt_ext(const ldlt_ext &) = delete;
4242
ldlt_ext &operator=(const ldlt_ext &) = delete;
@@ -52,7 +52,21 @@ template <typename Arr036> class ldlt_ext {
5252
* such that $v = R^-1 e_p$ is a certificate vector
5353
* to make $v'*A[:p,:p]*v < 0$
5454
*/
55-
auto factorize(const Arr036 &A) -> bool {
55+
// auto factorize(const Arr036 &A) -> bool {
56+
// return this->factor([&](size_t i, size_t j) { return A(i, j); });
57+
// }
58+
59+
/**
60+
* @brief Perform LDLT Factorization
61+
*
62+
* @param[in] A Symmetric Matrix
63+
*
64+
* If $A$ is positive definite, then $p$ is zero.
65+
* If it is not, then $p$ is a positive integer,
66+
* such that $v = R^-1 e_p$ is a certificate vector
67+
* to make $v'*A[:p,:p]*v < 0$
68+
*/
69+
template <typename Mat> auto factorize(const Mat &A) -> bool {
5670
return this->factor([&](size_t i, size_t j) { return A(i, j); });
5771
}
5872

@@ -132,7 +146,35 @@ template <typename Arr036> class ldlt_ext {
132146
* @param[in] A
133147
* @return double
134148
*/
135-
[[nodiscard]] auto sym_quad(const Arr036 &A) const -> double;
149+
template <typename Mat> auto sym_quad(const Mat &A) const -> double {
150+
auto res = double{};
151+
const auto &v = this->witness_vec;
152+
// const auto& [start, stop] = this->p;
153+
const auto &start = this->p.first;
154+
const auto &stop = this->p.second;
155+
for (auto i = start; i != stop; ++i) {
156+
auto s = double{};
157+
for (auto j = i + 1; j != stop; ++j) {
158+
s += A(i, j) * v[j];
159+
}
160+
res += v[i] * (A(i, i) * v[i] + 2 * s);
161+
}
162+
return res;
163+
}
164+
165+
/**
166+
* @brief Return upper triangular matrix $R$ where $A = R^T R$
167+
*
168+
* @return typename ldlt_ext<Arr036>::Mat
169+
*/
170+
template <typename Mat> auto sqrt(Mat &M) -> void {
171+
assert(this->is_spd());
136172

137-
auto sqrt() -> Arr036;
173+
for (auto i = 0U; i != this->_n; ++i) {
174+
M(i, i) = std::sqrt(this->T(i, i));
175+
for (auto j = i + 1; j != this->_n; ++j) {
176+
M(i, j) = this->T(j, i) * M(i, i);
177+
}
178+
}
179+
}
138180
};

source/ldlt_ext.cpp

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
#include <cmath> // for sqrt
44
#include <lmisolver/ldlt_ext.hpp>
55

6-
template <typename Arr036>
7-
ldlt_ext<Arr036>::ldlt_ext(size_t N) : witness_vec(0.0, N), _n{N}, T{N} {}
8-
96
/**
107
* @brief witness that certifies $A$ is not
118
* symmetric positive definite (spd)
@@ -29,28 +26,7 @@ template <typename Arr036> auto ldlt_ext<Arr036>::witness() -> double {
2926
return -this->T(m, m);
3027
}
3128

32-
/**
33-
* @brief Calculate v'*{A}(p,p)*v
34-
*
35-
* @param[in] A
36-
* @return double
37-
*/
38-
template <typename Arr036>
39-
auto ldlt_ext<Arr036>::sym_quad(const Arr036 &A) const -> double {
40-
auto res = double{};
41-
const auto &v = this->witness_vec;
42-
// const auto& [start, stop] = this->p;
43-
const auto &start = this->p.first;
44-
const auto &stop = this->p.second;
45-
for (auto i = start; i != stop; ++i) {
46-
auto s = double{};
47-
for (auto j = i + 1; j != stop; ++j) {
48-
s += A(i, j) * v[j];
49-
}
50-
res += v[i] * (A(i, i) * v[i] + 2 * s);
51-
}
52-
return res;
53-
}
29+
template auto ldlt_ext<std::valarray<double>>::witness() -> double;
5430

5531
#include <xtensor/xarray.hpp> // for xarray
5632
#include <xtensor/xcontainer.hpp> // for xcontainer
@@ -59,22 +35,4 @@ auto ldlt_ext<Arr036>::sym_quad(const Arr036 &A) const -> double {
5935

6036
using Arr = xt::xarray<double, xt::layout_type::row_major>;
6137

62-
/**
63-
* @brief Return upper triangular matrix $R$ where $A = R^T R$
64-
*
65-
* @return typename ldlt_ext<Arr036>::Mat
66-
*/
67-
template <typename Arr036> auto ldlt_ext<Arr036>::sqrt() -> Arr036 {
68-
assert(this->is_spd());
69-
70-
Arr036 M = xt::zeros<double>({this->_n, this->_n});
71-
for (auto i = 0U; i != this->_n; ++i) {
72-
M(i, i) = std::sqrt(this->T(i, i));
73-
for (auto j = i + 1; j != this->_n; ++j) {
74-
M(i, j) = this->T(j, i) * M(i, i);
75-
}
76-
}
77-
return M;
78-
}
79-
80-
template class ldlt_ext<Arr>;
38+
template auto ldlt_ext<Arr>::witness() -> double;

specific.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ endif(xtensor_ADDED)
1919

2020
CPMAddPackage(
2121
NAME EllAlgo
22-
GIT_TAG 1.3.5
22+
GIT_TAG 1.3.6
2323
GITHUB_REPOSITORY luk036/ellalgo-cpp
2424
OPTIONS "INSTALL_ONLY YES" # create an installable target
2525
)

test/source/test_ldlt.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
11
#include <doctest/doctest.h> // for ResultBuilder, TestCase, CHECK
22

3-
#include <lmisolver/ldlt_ext.hpp> // for ldlt_ext
4-
#include <xtensor/xarray.hpp> // for xarray_container
5-
#include <xtensor/xcontainer.hpp> // for xcontainer<>::inner_shape_type
6-
#include <xtensor/xlayout.hpp> // for layout_type, layout_type::row...
7-
#include <xtensor/xtensor_forward.hpp> // for xarray
3+
#include <lmisolver/ldlt_ext.hpp> // for ldlt_ext
4+
// #include <xtensor/xarray.hpp> // for xarray_container
5+
// #include <xtensor/xcontainer.hpp> // for xcontainer<>::inner_shape_type
6+
// #include <xtensor/xlayout.hpp> // for layout_type,
7+
// layout_type::row... #include <xtensor/xtensor_forward.hpp> // for xarray
88
// #include <xtensor/xarray.hpp>
9+
#include <ellalgo/ell_matrix.hpp>
910

10-
using Arr = xt::xarray<double, xt::layout_type::row_major>;
11+
// using Arr = xt::xarray<double, xt::layout_type::row_major>;
12+
using Vec = std::valarray<double>;
1113

1214
TEST_CASE("Cholesky test 1") {
13-
const auto m1 = Arr{{25.0, 15.0, -5.0}, {15.0, 18.0, 0.0}, {-5.0, 0.0, 11.0}};
14-
auto Q1 = ldlt_ext<Arr>(m1.shape()[0]);
15+
Matrix m1(3U);
16+
m1.row(0) = Vec{25.0, 15.0, -5.0};
17+
m1.row(1) = Vec{15.0, 18.0, 0.0};
18+
m1.row(2) = Vec{-5.0, 0.0, 11.0};
19+
auto Q1 = ldlt_ext<Vec>(3);
1520
CHECK(Q1.factorize(m1));
1621
}
1722

1823
TEST_CASE("Cholesky test 2") {
19-
const auto m2 = Arr{{18.0, 22.0, 54.0, 42.0},
20-
{22.0, -70.0, 86.0, 62.0},
21-
{54.0, 86.0, -174.0, 134.0},
22-
{42.0, 62.0, 134.0, -106.0}};
23-
auto Q2 = ldlt_ext<Arr>(m2.shape()[0]);
24+
Matrix m2(4U);
25+
m2.row(0) = Vec{18.0, 22.0, 54.0, 42.0};
26+
m2.row(1) = Vec{22.0, -70.0, 86.0, 62.0};
27+
m2.row(2) = Vec{54.0, 86.0, -174.0, 134.0};
28+
m2.row(3) = Vec{42.0, 62.0, 134.0, -106.0};
29+
30+
auto Q2 = ldlt_ext<Vec>(4);
2431
Q2.factorize(m2);
2532
CHECK(!Q2.is_spd());
2633
// CHECK(Q2.p.second == 2);
2734
}
2835

2936
TEST_CASE("Cholesky test 3") {
30-
const auto m3 = Arr{{0.0, 15.0, -5.0}, {15.0, 18.0, 0.0}, {-5.0, 0.0, 11.0}};
31-
auto Q3 = ldlt_ext<Arr>(m3.shape()[0]);
37+
Matrix m3(3U);
38+
m3.row(0) = Vec{0.0, 15.0, -5.0};
39+
m3.row(1) = Vec{15.0, 18.0, 0.0};
40+
m3.row(2) = Vec{-5.0, 0.0, 11.0};
41+
42+
auto Q3 = ldlt_ext<Vec>(3);
3243
Q3.factorize(m3);
3344
CHECK(!Q3.is_spd());
3445
const auto ep3 = Q3.witness();
35-
Arr v = xt::zeros<double>({m3.shape()[0]});
46+
Vec v(0.0, 3);
3647
Q3.set_witness_vec(v);
3748

3849
// CHECK(Q3.p.second == 1);

0 commit comments

Comments
 (0)