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

Commit f26c373

Browse files
committed
internally use std::valarray
1 parent 2b4fc5c commit f26c373

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

core

11.7 MB
Binary file not shown.

include/lmisolver/ldlt_ext.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <cstddef> // for size_t
55
#include <utility> // for pair
6+
#include <ellalgo/ell_matrix.hpp>
67

78
/**
89
* @brief LDLT factorization for LMI
@@ -14,8 +15,8 @@
1415
* - O(p^2) per iteration, independent of N
1516
*/
1617
template <typename Arr036> class ldlt_ext {
17-
using Vec = Arr036;
18-
using Mat = Arr036;
18+
using Vec = std::valarray<double>;
19+
using Mat = Matrix;
1920
using Rng = std::pair<size_t, size_t>;
2021

2122
public:
@@ -51,7 +52,7 @@ template <typename Arr036> class ldlt_ext {
5152
* such that $v = R^-1 e_p$ is a certificate vector
5253
* to make $v'*A[:p,:p]*v < 0$
5354
*/
54-
auto factorize(const Mat &A) -> bool {
55+
auto factorize(const Arr036 &A) -> bool {
5556
return this->factor([&](size_t i, size_t j) { return A(i, j); });
5657
}
5758

@@ -83,17 +84,17 @@ template <typename Arr036> class ldlt_ext {
8384
this->T(i, i) = d;
8485

8586
if constexpr (Allow_semidefinite) {
86-
if (d < 0.) {
87+
if (d < 0.0) {
8788
// this->stop = i + 1;
8889
stop = i + 1;
8990
break;
9091
}
91-
if (d == 0.) {
92+
if (d == 0.0) {
9293
start = i + 1;
9394
// restart at i + 1, special as an LMI oracle
9495
}
9596
} else { // not Allow_semidefinite
96-
if (d <= 0.) {
97+
if (d <= 0.0) {
9798
stop = i + 1;
9899
break;
99100
}
@@ -125,7 +126,7 @@ template <typename Arr036> class ldlt_ext {
125126
* @param[in] A
126127
* @return double
127128
*/
128-
[[nodiscard]] auto sym_quad(const Vec &A) const -> double;
129+
[[nodiscard]] auto sym_quad(const Arr036 &A) const -> double;
129130

130-
auto sqrt() -> Mat;
131+
auto sqrt() -> Arr036;
131132
};

source/ldlt_ext.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
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)
8+
: witness_vec(0.0, N), _n{N}, T{N} {
9+
}
10+
611
/**
712
* @brief witness that certifies $A$ is not
813
* symmetric positive definite (spd)
@@ -16,11 +21,11 @@ template <typename Arr036> auto ldlt_ext<Arr036>::witness() -> double {
1621
const auto &start = this->p.first;
1722
const auto &n = this->p.second;
1823
auto m = n - 1; // assume stop > 0
19-
this->witness_vec(m) = 1.;
24+
this->witness_vec[m] = 1.0;
2025
for (auto i = m; i > start; --i) {
21-
this->witness_vec(i - 1) = 0.;
26+
this->witness_vec[i - 1] = 0.0;
2227
for (auto k = i; k != n; ++k) {
23-
this->witness_vec(i - 1) -= this->T(k, i - 1) * this->witness_vec(k);
28+
this->witness_vec[i - 1] -= this->T(k, i - 1) * this->witness_vec[k];
2429
}
2530
}
2631
return -this->T(m, m);
@@ -33,7 +38,7 @@ template <typename Arr036> auto ldlt_ext<Arr036>::witness() -> double {
3338
* @return double
3439
*/
3540
template <typename Arr036>
36-
auto ldlt_ext<Arr036>::sym_quad(const typename ldlt_ext<Arr036>::Vec &A) const
41+
auto ldlt_ext<Arr036>::sym_quad(const Arr036 &A) const
3742
-> double {
3843
auto res = double{};
3944
const auto &v = this->witness_vec;
@@ -43,9 +48,9 @@ auto ldlt_ext<Arr036>::sym_quad(const typename ldlt_ext<Arr036>::Vec &A) const
4348
for (auto i = start; i != stop; ++i) {
4449
auto s = double{};
4550
for (auto j = i + 1; j != stop; ++j) {
46-
s += A(i, j) * v(j);
51+
s += A(i, j) * v[j];
4752
}
48-
res += v(i) * (A(i, i) * v(i) + 2 * s);
53+
res += v[i] * (A(i, i) * v[i] + 2 * s);
4954
}
5055
return res;
5156
}
@@ -57,21 +62,16 @@ auto ldlt_ext<Arr036>::sym_quad(const typename ldlt_ext<Arr036>::Vec &A) const
5762

5863
using Arr = xt::xarray<double, xt::layout_type::row_major>;
5964

60-
template <typename Arr036>
61-
ldlt_ext<Arr036>::ldlt_ext(size_t N)
62-
: witness_vec{xt::zeros<double>({N})}, _n{N}, T{xt::zeros<double>({N, N})} {
63-
}
64-
6565
/**
6666
* @brief Return upper triangular matrix $R$ where $A = R^T R$
6767
*
6868
* @return typename ldlt_ext<Arr036>::Mat
6969
*/
7070
template <typename Arr036>
71-
auto ldlt_ext<Arr036>::sqrt() -> typename ldlt_ext<Arr036>::Mat {
71+
auto ldlt_ext<Arr036>::sqrt() -> Arr036 {
7272
assert(this->is_spd());
7373

74-
ldlt_ext<Arr036>::Mat M = xt::zeros<double>({this->_n, this->_n});
74+
Arr036 M = xt::zeros<double>({this->_n, this->_n});
7575
for (auto i = 0U; i != this->_n; ++i) {
7676
M(i, i) = std::sqrt(this->T(i, i));
7777
for (auto j = i + 1; j != this->_n; ++j) {
@@ -81,4 +81,4 @@ auto ldlt_ext<Arr036>::sqrt() -> typename ldlt_ext<Arr036>::Mat {
8181
return M;
8282
}
8383

84-
template class ldlt_ext<Arr>;
84+
template class ldlt_ext<Arr>;

source/lmi_oracle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ auto LmiOracle<Arr036>::assess_feas(const Arr036 &x)
4848
}
4949
auto ep = this->_Q.witness();
5050
auto g = Arr036{xt::zeros<double>({x.size()})};
51+
// auto g = Arr036(x.size(), 0.0);
5152
for (auto i = 0U; i != n; ++i) {
5253
g(i) = this->_Q.sym_quad(this->_F[i]);
5354
}
5455
return {{std::move(g), ep}};
5556
}
5657

5758
using Arr = xt::xarray<double, xt::layout_type::row_major>;
58-
template class LmiOracle<Arr>;
59+
template class LmiOracle<Arr>;

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.2
22+
GIT_TAG 1.3.3
2323
GITHUB_REPOSITORY luk036/ellalgo-cpp
2424
OPTIONS "INSTALL_ONLY YES" # create an installable target
2525
)

0 commit comments

Comments
 (0)