diff --git a/.drone.star b/.drone.star index 16b3f41d71..86e822729b 100644 --- a/.drone.star +++ b/.drone.star @@ -17,7 +17,7 @@ def main(ctx): things_to_test = [ "special_fun", "distribution_tests", "mp", "misc", "interpolators", "quadrature", "autodiff", "long-running-tests", "float128_tests", "concepts" ] gcc13_things_to_test = [ "special_fun", "distribution_tests", "mp", "misc", "interpolators", "quadrature", "autodiff", "long-running-tests", "float128_tests", "concepts", "new_floats" ] - sanitizer_test = [ "special_fun", "distribution_tests", "misc", "interpolators", "quadrature", "float128_tests" ] + sanitizer_test = [ "special_fun", "distribution_tests", "misc", "interpolators", "quadrature", "float128_tests", "test_reverse_mode_autodiff", "gradient_based_optimizers" ] reverse_mode_autodiff_test = [ "test_reverse_mode_autodiff", "autodiff-long-running-tests", "gradient_based_optimizers" ] cxx17_stds = [ "c++17" ] gnu_cxx17_stds = [ "gnu++17" ] diff --git a/doc/interpolators/catmull_rom.qbk b/doc/interpolators/catmull_rom.qbk index eb4bd61435..abba16cf3f 100644 --- a/doc/interpolators/catmull_rom.qbk +++ b/doc/interpolators/catmull_rom.qbk @@ -205,7 +205,7 @@ However, if `v.capacity() < v.size() + 3`, the `realloc` causes a performance pe [heading Generic Containers] The `Point` type may be stored in a different container than `std::vector`. -For example, here is how to store the points in a Boost.uBLAS vector: +For example, here is how to store the points in a `std::deque`: mypoint3d p0(0.1, 0.2, 0.3); mypoint3d p1(0.2, 0.3, 0.4); @@ -214,7 +214,7 @@ For example, here is how to store the points in a Boost.uBLAS vector: mypoint3d p4(0.5, 0.6, 0.7); mypoint3d p5(0.6, 0.7, 0.8); - boost::numeric::ublas::vector> u(6); + std::deque> u(6); u[0] = p0; u[1] = p1; u[2] = p2; diff --git a/example/inspect_fp.cpp b/example/inspect_fp.cpp index 5de25f2ac7..9ab1f46417 100644 --- a/example/inspect_fp.cpp +++ b/example/inspect_fp.cpp @@ -14,11 +14,7 @@ #include #include -#ifndef BOOST_MATH_STANDALONE -#include -#else #include -#endif #include // for has_denorm_now diff --git a/include/boost/math/filters/detail/wavelet_boundary.hpp b/include/boost/math/filters/detail/wavelet_boundary.hpp index 35a6581070..1f024748b6 100644 --- a/include/boost/math/filters/detail/wavelet_boundary.hpp +++ b/include/boost/math/filters/detail/wavelet_boundary.hpp @@ -11,6 +11,8 @@ #endif #include +#include +#include #ifndef BOOST_MATH_BUILD_MODULE #include @@ -104,90 +106,42 @@ matrix transpose(const matrix& a) return t; } +// Row-major copies of a matrix, for tools::detail. +template +std::vector flatten(const matrix& a) +{ + std::vector f; + for (const std::vector& row : a) f.insert(f.end(), row.begin(), row.end()); + return f; +} + +template +matrix unflatten(const std::vector& f, std::size_t r, std::size_t c) +{ + matrix a(r); + for (std::size_t i = 0; i < r; ++i) a[i].assign(f.begin() + std::ptrdiff_t(i * c), f.begin() + std::ptrdiff_t((i + 1) * c)); + return a; +} + // Gauss-Jordan with partial pivoting; false if a is singular. template -bool inverse(matrix a, matrix& x) +bool inverse(const matrix& a, matrix& x) { - using std::abs; const std::size_t n = a.size(); - x = zeros(n, n); - for (std::size_t i = 0; i < n; ++i) x[i][i] = 1; - for (std::size_t c = 0; c < n; ++c) - { - std::size_t piv = c; - for (std::size_t r = c + 1; r < n; ++r) - if (abs(a[r][c]) > abs(a[piv][c])) piv = r; - if (a[piv][c] == 0) return false; - std::swap(a[c], a[piv]); - std::swap(x[c], x[piv]); - const Real d = a[c][c]; - for (std::size_t j = 0; j < n; ++j) { a[c][j] /= d; x[c][j] /= d; } - for (std::size_t r = 0; r < n; ++r) - { - if (r == c || a[r][c] == 0) continue; - const Real f = a[r][c]; - for (std::size_t j = 0; j < n; ++j) { a[r][j] -= f * a[c][j]; x[r][j] -= f * x[c][j]; } - } - } + std::vector f = flatten(a), fi(n * n); + if (!tools::detail::gauss_jordan_inverse(f.data(), fi.data(), n)) return false; + x = unflatten(fi, n, n); return true; } // The first r columns of Q from the Householder QR of A (m x n), optionally with column pivoting. template -matrix householder_q(matrix A, std::size_t r, bool pivot) +matrix householder_q(const matrix& A, std::size_t r, bool pivot) { - using std::sqrt; - const std::size_t m = A.size(), n = A[0].size(), steps = (std::min)((std::min)(m, n), r); - matrix vs; - std::vector beta; - for (std::size_t k = 0; k < steps; ++k) - { - if (pivot) - { - std::size_t best = k; - Real bn = -1; - for (std::size_t j = k; j < n; ++j) - { - Real s = 0; - for (std::size_t i = k; i < m; ++i) s += A[i][j] * A[i][j]; - if (s > bn) { bn = s; best = j; } - } - if (best != k) - for (std::size_t i = 0; i < m; ++i) std::swap(A[i][k], A[i][best]); - } - std::vector v(m, Real(0)); - Real nx = 0; - for (std::size_t i = k; i < m; ++i) { v[i] = A[i][k]; nx += v[i] * v[i]; } - nx = sqrt(nx); - Real b = 0; - if (nx > 0) - { - v[k] -= v[k] >= 0 ? -nx : nx; - Real vv = 0; - for (std::size_t i = k; i < m; ++i) vv += v[i] * v[i]; - b = vv > 0 ? Real(2) / vv : Real(0); - for (std::size_t j = k; j < n; ++j) - { - Real s = 0; - for (std::size_t i = k; i < m; ++i) s += v[i] * A[i][j]; - s *= b; - for (std::size_t i = k; i < m; ++i) A[i][j] -= s * v[i]; - } - } - vs.push_back(v); - beta.push_back(b); - } - matrix Q = zeros(m, r); - for (std::size_t j = 0; j < r; ++j) Q[j][j] = 1; - for (std::size_t k = vs.size(); k-- > 0;) - for (std::size_t j = 0; j < r; ++j) - { - Real s = 0; - for (std::size_t i = k; i < m; ++i) s += vs[k][i] * Q[i][j]; - s *= beta[k]; - for (std::size_t i = k; i < m; ++i) Q[i][j] -= s * vs[k][i]; - } - return Q; + const std::size_t m = A.size(), n = A[0].size(); + std::vector f = flatten(A), Q(m * r); + tools::detail::householder_qr(f.data(), m, n, r, pivot, Q.data()); + return unflatten(Q, m, r); } // The orthogonal polar factor of a square nonsingular matrix: scaled Newton, then Newton-Schulz @@ -352,30 +306,11 @@ bool cdv_left_edge(const cdv_filter& f, const matrix& BE, std::size_ } // R^T from the QR of M^T (M: n x c): an n x n lower triangular Rt with M M^T = Rt Rt^T auto factor = [&](const matrix& M) { - matrix Mt = transpose(M); - const std::size_t c = Mt.size(); - for (std::size_t k = 0; k < n; ++k) - { - Real nx = 0; - for (std::size_t i = k; i < c; ++i) nx += Mt[i][k] * Mt[i][k]; - nx = sqrt(nx); - if (nx == 0) continue; - std::vector v(c, Real(0)); - for (std::size_t i = k; i < c; ++i) v[i] = Mt[i][k]; - v[k] -= Mt[k][k] >= 0 ? -nx : nx; - Real vv = 0; - for (std::size_t i = k; i < c; ++i) vv += v[i] * v[i]; - for (std::size_t j = k; j < n; ++j) - { - Real d = 0; - for (std::size_t i = k; i < c; ++i) d += v[i] * Mt[i][j]; - d = 2 * d / vv; - for (std::size_t i = k; i < c; ++i) Mt[i][j] -= d * v[i]; - } - } + std::vector Mt = flatten(transpose(M)); + tools::detail::householder_qr(Mt.data(), Mt.size() / n, n, n, false, static_cast(nullptr)); matrix Rt = zeros(n, n); for (std::size_t i = 0; i < n; ++i) - for (std::size_t j = i; j < n; ++j) Rt[j][i] = Mt[i][j]; + for (std::size_t j = i; j < n; ++j) Rt[j][i] = Mt[i * n + j]; return Rt; }; matrix F = factor(D), Ak = A; diff --git a/include/boost/math/optimization/cma_es.hpp b/include/boost/math/optimization/cma_es.hpp index e4f4d75def..b5d67df46f 100644 --- a/include/boost/math/optimization/cma_es.hpp +++ b/include/boost/math/optimization/cma_es.hpp @@ -19,6 +19,7 @@ #endif #include #include +#include // Follows the notation in: // https://arxiv.org/pdf/1604.00772.pdf @@ -37,200 +38,6 @@ BOOST_MATH_NAMESPACE_BEGIN namespace optimization { namespace detail { -// Computes A = V diag(eigenvalues) V^T for a symmetric n x n matrix A stored row-major, -// by Householder tridiagonalization followed by the implicit QL algorithm; -// see Wilkinson & Reinsch, Handbook for Automatic Computation, Vol. II (tred2 and tql2), -// and Golub & Van Loan, Matrix Computations, 4th ed., Section 8.3. -// V is returned row-major, with the eigenvectors as its columns. -// Returns false if some eigenvalue did not converge within max_iterations QL iterations. -template -bool symmetric_eigendecomposition(std::vector A, size_t n, std::vector &V, std::vector &eigenvalues, size_t max_iterations = 30) { - using std::abs; - using std::hypot; - using std::sqrt; - using std::max; - V = std::move(A); - std::vector &d = eigenvalues; - d.resize(n); - std::vector e(n, Real(0)); - // Householder reduction to tridiagonal form; d is the diagonal and e the subdiagonal: - for (size_t j = 0; j < n; ++j) { - d[j] = V[(n - 1)*n + j]; - } - for (size_t i = n - 1; i > 0; --i) { - Real scale = 0; - Real h = 0; - for (size_t k = 0; k < i; ++k) { - scale += abs(d[k]); - } - if (scale == 0) { - e[i] = d[i - 1]; - for (size_t j = 0; j < i; ++j) { - d[j] = V[(i - 1)*n + j]; - V[i*n + j] = 0; - V[j*n + i] = 0; - } - } - else { - for (size_t k = 0; k < i; ++k) { - d[k] /= scale; - h += d[k]*d[k]; - } - Real f = d[i - 1]; - Real g = sqrt(h); - if (f > 0) { - g = -g; - } - e[i] = scale*g; - h -= f*g; - d[i - 1] = f - g; - for (size_t j = 0; j < i; ++j) { - e[j] = 0; - } - for (size_t j = 0; j < i; ++j) { - f = d[j]; - V[j*n + i] = f; - g = e[j] + V[j*n + j]*f; - for (size_t k = j + 1; k < i; ++k) { - g += V[k*n + j]*d[k]; - e[k] += V[k*n + j]*f; - } - e[j] = g; - } - f = 0; - for (size_t j = 0; j < i; ++j) { - e[j] /= h; - f += e[j]*d[j]; - } - const Real hh = f/(h + h); - for (size_t j = 0; j < i; ++j) { - e[j] -= hh*d[j]; - } - for (size_t j = 0; j < i; ++j) { - f = d[j]; - g = e[j]; - for (size_t k = j; k < i; ++k) { - V[k*n + j] -= f*e[k] + g*d[k]; - } - d[j] = V[(i - 1)*n + j]; - V[i*n + j] = 0; - } - } - d[i] = h; - } - // Accumulate the transformations: - for (size_t i = 0; i + 1 < n; ++i) { - V[(n - 1)*n + i] = V[i*n + i]; - V[i*n + i] = 1; - const Real h = d[i + 1]; - if (h != 0) { - for (size_t k = 0; k <= i; ++k) { - d[k] = V[k*n + i + 1]/h; - } - for (size_t j = 0; j <= i; ++j) { - Real g = 0; - for (size_t k = 0; k <= i; ++k) { - g += V[k*n + i + 1]*V[k*n + j]; - } - for (size_t k = 0; k <= i; ++k) { - V[k*n + j] -= g*d[k]; - } - } - } - for (size_t k = 0; k <= i; ++k) { - V[k*n + i + 1] = 0; - } - } - for (size_t j = 0; j < n; ++j) { - d[j] = V[(n - 1)*n + j]; - V[(n - 1)*n + j] = 0; - } - V[(n - 1)*n + n - 1] = 1; - // The QL rotations act on columns of V, so apply them to the rows of V^T, which are contiguous: - for (size_t i = 0; i < n; ++i) { - for (size_t j = 0; j < i; ++j) { - std::swap(V[i*n + j], V[j*n + i]); - } - } - // Implicit QL iterations on the tridiagonal matrix: - for (size_t i = 1; i < n; ++i) { - e[i - 1] = e[i]; - } - e[n - 1] = 0; - Real f = 0; - Real tst1 = 0; - const Real eps = std::numeric_limits::epsilon(); - for (size_t l = 0; l < n; ++l) { - // Find a small subdiagonal element: - tst1 = (max)(tst1, abs(d[l]) + abs(e[l])); - size_t m = l; - while (m < n - 1 && !(abs(e[m]) <= eps*tst1)) { - ++m; - } - // If m == l, d[l] is already an eigenvalue; otherwise, iterate: - size_t iterations = 0; - while (m > l && abs(e[l]) > eps*tst1) { - if (iterations++ == max_iterations) { - break; - } - // Compute the implicit shift: - Real g = d[l]; - Real p = (d[l + 1] - g)/(2*e[l]); - Real r = hypot(p, Real(1)); - if (p < 0) { - r = -r; - } - d[l] = e[l]/(p + r); - d[l + 1] = e[l]*(p + r); - const Real dl1 = d[l + 1]; - Real h = g - d[l]; - for (size_t i = l + 2; i < n; ++i) { - d[i] -= h; - } - f += h; - p = d[m]; - Real c = 1; - Real c2 = c; - Real c3 = c; - const Real el1 = e[l + 1]; - Real s = 0; - Real s2 = 0; - for (size_t i = m; i-- > l;) { - c3 = c2; - c2 = c; - s2 = s; - g = c*e[i]; - h = c*p; - r = hypot(p, e[i]); - e[i + 1] = s*r; - s = e[i]/r; - c = p/r; - p = c*d[i] - s*g; - d[i + 1] = h + s*(c*g + s*d[i]); - for (size_t k = 0; k < n; ++k) { - h = V[(i + 1)*n + k]; - V[(i + 1)*n + k] = s*V[i*n + k] + c*h; - V[i*n + k] = c*V[i*n + k] - s*h; - } - } - p = -s*s2*c3*el1*e[l]/dl1; - e[l] = s*p; - d[l] = c*p; - } - if (iterations > max_iterations) { - return false; - } - d[l] += f; - e[l] = 0; - } - for (size_t i = 0; i < n; ++i) { - for (size_t j = 0; j < i; ++j) { - std::swap(V[i*n + j], V[j*n + i]); - } - } - return true; -} - // y = Mx, or y = M^{T}x if transpose is true, for the n x n matrix M stored row-major. // Both accumulate along the rows of M, so the inner loop is over contiguous memory: template @@ -466,7 +273,7 @@ ArgumentContainer cma_es( // max(1, floor(1/(10n(c_1+c_mu)))) generations." // Note that sigma can be dimensionless, in which case C carries the units. // This is a weird decision-we're not gonna do that! - if (!detail::symmetric_eigendecomposition(C, n, B, D)) { + if (!tools::detail::symmetric_eigendecomposition(C, n, B, D)) { std::ostringstream oss; oss << __FILE__ << ":" << __LINE__ << ":" << __func__; oss << ": Could not decompose the covariance matrix as BDB^{T}."; diff --git a/include/boost/math/optimization/detail/gaussian_process.hpp b/include/boost/math/optimization/detail/gaussian_process.hpp index e17ba85e14..f32ca8d146 100644 --- a/include/boost/math/optimization/detail/gaussian_process.hpp +++ b/include/boost/math/optimization/detail/gaussian_process.hpp @@ -9,6 +9,7 @@ #include #include +#include #ifndef BOOST_MATH_BUILD_MODULE #include #include @@ -19,233 +20,10 @@ BOOST_MATH_NAMESPACE_BEGIN namespace optimization::detail { -// A Gaussian process with a constant mean and an ARD Matern-5/2 or Gaussian kernel, for the Bayesian optimizers: the -// dense linear algebra it needs, then the model. It began as the surrogate of TuRBO (turbo.hpp), whose reference code +// A Gaussian process with a constant mean and an ARD Matern-5/2 or Gaussian kernel, for the Bayesian optimizers. It +// began as the surrogate of TuRBO (turbo.hpp), whose reference code // (github.com/uber-research/TuRBO, gp.py) the fit and its defaults follow; the other choice is the model of BoTorch. -// Hand-rolled dense linear algebra on row-major double arrays. The kernels with long inner loops are written so that -// the compiler can vectorize them without reassociating floating point sums: either as axpy updates of contiguous -// rows, or as several rows' dot products with one shared operand, which gives independent accumulators. - -// s[r] = sum_{k < m} a[r][k] b[k] for four rows a[0..3]. -inline void dot4(double const *const *a, double const *b, size_t m, double *s) { - double s0 = 0, s1 = 0, s2 = 0, s3 = 0, t0 = 0, t1 = 0, t2 = 0, t3 = 0; - size_t k = 0; - for (; k + 1 < m; k += 2) { - double const x = b[k]; - double const y = b[k + 1]; - s0 += a[0][k] * x; - t0 += a[0][k + 1] * y; - s1 += a[1][k] * x; - t1 += a[1][k + 1] * y; - s2 += a[2][k] * x; - t2 += a[2][k + 1] * y; - s3 += a[3][k] * x; - t3 += a[3][k + 1] * y; - } - if (k < m) { - double const x = b[k]; - s0 += a[0][k] * x; - s1 += a[1][k] * x; - s2 += a[2][k] * x; - s3 += a[3][k] * x; - } - s[0] = s0 + t0; - s[1] = s1 + t1; - s[2] = s2 + t2; - s[3] = s3 + t3; -} - -inline double dot(double const *a, double const *b, size_t m) { - double s0 = 0, s1 = 0, s2 = 0, s3 = 0; - size_t k = 0; - for (; k + 3 < m; k += 4) { - s0 += a[k] * b[k]; - s1 += a[k + 1] * b[k + 1]; - s2 += a[k + 2] * b[k + 2]; - s3 += a[k + 3] * b[k + 3]; - } - for (; k < m; ++k) { - s0 += a[k] * b[k]; - } - return (s0 + s1) + (s2 + s3); -} - -// C[i][j] -= sum_{k < len} X[i][k] X[j][k] for lo <= j <= i < hi, where row i of X starts at X + i ldx and C has -// leading dimension ldc. Tiles of 64 rows j stay in cache while every block of four rows i below them goes by. -// Rows i < first of C are left alone; the others get what they would with first = 0. -inline void subtract_gram(double *C, size_t ldc, double const *X, size_t ldx, size_t len, size_t lo, size_t hi, - size_t first = 0) { - constexpr size_t tile = 64; - double s[4]; - double const *rows[4]; - for (size_t j0 = lo; j0 < hi; j0 += tile) { - size_t const j1 = (std::min)(hi, j0 + tile); - for (size_t i = j0; i < hi; i += 4) { - size_t const nb = (std::min)(size_t(4), hi - i), skip = first > i ? first - i : 0; - for (size_t b = 0; b < 4; ++b) { - rows[b] = X + (i + (b < nb ? b : 0)) * ldx; - } - size_t const jend = skip < nb ? (std::min)(j1, i + nb) : j0; - for (size_t j = j0; j < jend; ++j) { - double const *xj = X + j * ldx; - if (j < i) { - dot4(rows, xj, len, s); - for (size_t b = skip; b < nb; ++b) { - C[(i + b) * ldc + j] -= s[b]; - } - } - else { - for (size_t b = (std::max)(j - i, skip); b < nb; ++b) { - C[(i + b) * ldc + j] -= dot(rows[b], xj, len); - } - } - } - } - } -} - -// The Cholesky factorization A = L L^T in place: on input the lower triangle of the n x n matrix A (leading dimension -// n), on output that of L. The upper triangle is not referenced. Returns false if A is not numerically positive -// definite. Right-looking by blocks of 64 columns; within a block, left-looking four rows at a time. If rows i < first -// are those of L already, and the others those of A, the result is that of the whole factorization. -BOOST_MATH_TEST_EXPORT inline bool cholesky(double *A, size_t n, size_t first = 0) { - using std::sqrt; - constexpr size_t block = 64; - double s[4]; - double *rows[4]; - double const *parts[4]; - for (size_t k0 = 0; k0 < n; k0 += block) { - size_t const k1 = (std::min)(n, k0 + block); - // Columns k0 to k1 of L, from the diagonal block down: - for (size_t i = k0; i < n; i += 4) { - size_t const nb = (std::min)(size_t(4), n - i), skip = first > i ? first - i : 0; - for (size_t b = 0; b < 4; ++b) { - rows[b] = A + (i + (b < nb ? b : 0)) * n; - parts[b] = rows[b] + k0; - } - size_t const jend = skip < nb ? (std::min)(k1, i + nb) : k0; - for (size_t j = k0; j < jend; ++j) { - double const *lj = A + j * n; - if (j < i) { - dot4(parts, lj + k0, j - k0, s); - for (size_t b = skip; b < nb; ++b) { - rows[b][j] = (rows[b][j] - s[b]) / lj[j]; - } - continue; - } - // Column j meets these rows on the diagonal: row j first, then the rows below it. - for (size_t b = (std::max)(j - i, skip); b < nb; ++b) { - double const d = rows[b][j] - dot(parts[b], lj + k0, j - k0); - if (b == j - i) { - if (!(d > 0)) { - return false; - } - rows[b][j] = sqrt(d); - } - else { - rows[b][j] = d / lj[j]; - } - } - } - } - // The trailing submatrix loses the contribution of these columns: - subtract_gram(A, n, A + k0, n, k1 - k0, k1, n, first); - } - return true; -} - -// Solves L X = B in place for the n x m matrix B (leading dimension m), where L is the lower triangle of an n x n -// matrix (leading dimension n). -BOOST_MATH_TEST_EXPORT inline void forward_solve(double const *L, size_t n, double *B, size_t m) { - for (size_t i = 0; i < n; ++i) { - double *bi = B + i * m; - for (size_t k = 0; k < i; ++k) { - double const lik = L[i * n + k]; - double const *bk = B + k * m; - for (size_t c = 0; c < m; ++c) { - bi[c] -= lik * bk[c]; - } - } - double const inv = 1 / L[i * n + i]; - for (size_t c = 0; c < m; ++c) { - bi[c] *= inv; - } - } -} - -// Solves L x = b in place for a vector b. -BOOST_MATH_TEST_EXPORT inline void forward_solve(double const *L, size_t n, double *b) { - for (size_t i = 0; i < n; ++i) { - b[i] = (b[i] - dot(L + i * n, b, i)) / L[i * n + i]; - } -} - -// Solves L^T x = b in place, by rows of L: once x_i is known, remove its contribution L_ik x_i from b_k for k < i. -BOOST_MATH_TEST_EXPORT inline void backward_solve(double const *L, size_t n, double *b) { - for (size_t i = n; i-- > 0;) { - double const *li = L + i * n; - double const xi = b[i] / li[i]; - b[i] = xi; - for (size_t k = 0; k < i; ++k) { - b[k] -= li[k] * xi; - } - } -} - -// Solves L L^T x = b in place. -BOOST_MATH_TEST_EXPORT inline void cholesky_solve(double const *L, size_t n, double *b) { - forward_solve(L, n, b); - backward_solve(L, n, b); -} - -// The lower triangle of (L L^T)^{-1} = Z^T Z, where Z = L^{-1}. Z is n x n scratch. Four rows of Z are taken at a -// time, for which each row of Z has zeros in the three places after its diagonal. -inline void cholesky_inverse(double const *L, size_t n, double *Z, double *Kinv) { - for (size_t i = 0; i < n; ++i) { - double *zi = Z + i * n; - std::fill(zi, zi + (std::min)(n, i + 4), 0.0); - size_t k = 0; - for (; k + 3 < i; k += 4) { - double const *l = L + i * n + k, *z0 = Z + k * n, *z1 = z0 + n, *z2 = z1 + n, *z3 = z2 + n; - for (size_t j = 0; j <= k + 3; ++j) { - zi[j] = zi[j] - l[0] * z0[j] - l[1] * z1[j] - l[2] * z2[j] - l[3] * z3[j]; - } - } - for (; k < i; ++k) { - double const lik = L[i * n + k]; - double const *zk = Z + k * n; - for (size_t j = 0; j <= k; ++j) { - zi[j] -= lik * zk[j]; - } - } - double const inv = 1 / L[i * n + i]; - for (size_t j = 0; j < i; ++j) { - zi[j] *= inv; - } - zi[i] = inv; - } - for (size_t a = 0; a < n; ++a) { - double *row = Kinv + a * n; - std::fill(row, row + a + 1, 0.0); - size_t k = a; - for (; k + 3 < n; k += 4) { - double const *z0 = Z + k * n, *z1 = z0 + n, *z2 = z1 + n, *z3 = z2 + n; - double const c0 = z0[a], c1 = z1[a], c2 = z2[a], c3 = z3[a]; - for (size_t b = 0; b <= a; ++b) { - row[b] = row[b] + c0 * z0[b] + c1 * z1[b] + c2 * z2[b] + c3 * z3[b]; - } - } - for (; k < n; ++k) { - double const *zk = Z + k * n; - double const zka = zk[a]; - for (size_t b = 0; b <= a; ++b) { - row[b] += zka * zk[b]; - } - } - } -} - BOOST_MATH_TEST_EXPORT enum class gaussian_process_kernel { matern52, rbf }; // A kernel k(x, y) of unit scale on the unit cube, where r^2 = sum_i (x_i - y_i)^2 / l_i^2 and inv_l2[i] = 1/l_i^2: @@ -449,7 +227,7 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { for (size_t a = 0; a < N; ++a) { alpha_[a] = yt_[a] - mean_; } - cholesky_solve(L_.data(), N, alpha_.data()); + tools::detail::cholesky_solve(L_.data(), N, alpha_.data()); return true; } @@ -471,7 +249,7 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { mu[c] += row[c] * alpha_[a]; } } - forward_solve(L_.data(), N, V_.data(), M); + tools::detail::forward_solve(L_.data(), N, V_.data(), M); // The transpose, M x N, so that the rows of the Gram matrix V^T V are contiguous dot products: Vt_.resize(M * N); for (size_t a = 0; a < N; ++a) { @@ -489,20 +267,20 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { } row[a] = outputscale_ + (observation_noise ? noise_ : 0.0); } - subtract_gram(S.data(), M, Vt_.data(), N, N, 0, M); + tools::detail::subtract_gram(S.data(), M, Vt_.data(), N, N, 0, M); // With the noise, it bounds the covariance below, so jitter should never be needed; if it is, retry from a copy. covariance_ = S; for (size_t a = 0; a < M && jitter > 0; ++a) { S[a * M + a] += jitter; } - bool factored = cholesky(S.data(), M); + bool factored = tools::detail::cholesky(S.data(), M); jitter_ = jitter; for (jitter = (std::max)(1e-8, 10 * jitter); !factored && jitter < 1; jitter *= 10) { S = covariance_; for (size_t a = 0; a < M; ++a) { S[a * M + a] += jitter; } - factored = cholesky(S.data(), M); + factored = tools::detail::cholesky(S.data(), M); jitter_ = jitter; } return factored; @@ -558,7 +336,7 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { v[a] = kernel(Xt_.data() + a * n_, b); mu_B_[m] += v[a] * alpha_[a]; } - forward_solve(L_.data(), N, v, 1); + tools::detail::forward_solve(L_.data(), N, v, 1); for (size_t a = 0; a < N; ++a) { V_B_[a * M + m] = v[a]; } @@ -566,9 +344,9 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { row[j] = kernel(b, B_.data() + j * n_); } row[m] = outputscale_; - subtract_gram(L_B_.data(), M, Vt_B_.data(), N, N, 0, M, m); + tools::detail::subtract_gram(L_B_.data(), M, Vt_B_.data(), N, N, 0, M, m); row[m] += jitter_B_; - m_ = m > 0 && cholesky(L_B_.data(), M, m) ? M : 0; + m_ = m > 0 && tools::detail::cholesky(L_B_.data(), M, m) ? M : 0; refactored_B_ = m_ != M; return !refactored_B_ || condition(std::vector(B_).data(), M); } @@ -594,25 +372,25 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { for (size_t a = 0; a < N + m; ++a) { v[a] = k ? k[a] : kernel(a < N ? Xt_.data() + a * n_ : B_.data() + (a - N) * n_, x, u + m + a); } - forward_solve(L_.data(), N, v); + tools::detail::forward_solve(L_.data(), N, v); for (size_t a = 0; a < N; ++a) { double const *row = V_B_.data() + a * m; for (size_t j = 0; j < m; ++j) { u[j] -= row[j] * v[a]; } } - forward_solve(L_B_.data(), m, u); - double const d2 = outputscale_ - dot(v, v, N) - dot(u, u, m); + tools::detail::forward_solve(L_B_.data(), m, u); + double const d2 = outputscale_ - tools::detail::dot(v, v, N) - tools::detail::dot(u, u, m); bool const floored = !(d2 > variance_floor()); if (grad) { std::fill(grad, grad + n_, 0.0); } if (grad && !floored) { - backward_solve(L_B_.data(), m, u); + tools::detail::backward_solve(L_B_.data(), m, u); for (size_t a = 0; a < N; ++a) { - v[a] -= dot(V_B_.data() + a * m, u, m); + v[a] -= tools::detail::dot(V_B_.data() + a * m, u, m); } - backward_solve(L_.data(), N, v); + tools::detail::backward_solve(L_.data(), N, v); // d k(y, x) / d x_i = -c (x_i - y_i) / l_i^2, for y the N points of X with weights v, then those of B with u: for (size_t a = 0; a < N + m; ++a) { double const *ya = a < N ? Xt_.data() + a * n_ : B_.data() + (a - N) * n_; @@ -633,11 +411,11 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { // numbers). zeta and w may be the same array. void sample_path(double const *zeta, double *a, double *w) const { std::copy(zeta, zeta + m_, w); - backward_solve(L_B_.data(), m_, w); + tools::detail::backward_solve(L_B_.data(), m_, w); for (size_t r = 0; r < N_; ++r) { - a[r] = dot(V_B_.data() + r * m_, w, m_); + a[r] = tools::detail::dot(V_B_.data() + r * m_, w, m_); } - backward_solve(L_.data(), N_, a); + tools::detail::backward_solve(L_.data(), N_, a); for (size_t r = 0; r < N_; ++r) { a[r] = alpha_[r] - a[r]; } @@ -737,7 +515,7 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { } row[a] = outputscale_ + noise_; } - return cholesky(L_.data(), N); + return tools::detail::cholesky(L_.data(), N); } public: @@ -757,7 +535,7 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { for (size_t a = 0; a < N; ++a) { alpha_[a] = yt_[a] - mean_; } - cholesky_solve(L_.data(), N, alpha_.data()); + tools::detail::cholesky_solve(L_.data(), N, alpha_.data()); loss = 0; for (size_t a = 0; a < N; ++a) { loss += (yt_[a] - mean_) * alpha_[a] / 2 + log(L_[a * N + a]); @@ -765,7 +543,7 @@ BOOST_MATH_TEST_EXPORT class gaussian_process { loss /= static_cast(N); Z_.resize(N * N); Kinv_.resize(N * N); - cholesky_inverse(L_.data(), N, Z_.data(), Kinv_.data()); + tools::detail::cholesky_inverse(L_.data(), N, Z_.data(), Kinv_.data()); // d loss / d theta = -tr(W dK/d theta)/(2N), where W = alpha alpha^T - K^{-1}: double trace_noise = 0; double trace_scale = 0; diff --git a/include/boost/math/optimization/detail/pareto_polish_corrector.hpp b/include/boost/math/optimization/detail/pareto_polish_corrector.hpp index 723496162d..1677e84bf4 100644 --- a/include/boost/math/optimization/detail/pareto_polish_corrector.hpp +++ b/include/boost/math/optimization/detail/pareto_polish_corrector.hpp @@ -7,7 +7,7 @@ #ifndef BOOST_MATH_OPTIMIZATION_DETAIL_PARETO_POLISH_CORRECTOR_HPP #define BOOST_MATH_OPTIMIZATION_DETAIL_PARETO_POLISH_CORRECTOR_HPP #include -#include // detail::symmetric_eigendecomposition +#include #include #include #include @@ -198,7 +198,7 @@ Real pareto_polish_reduced_curvature(std::vector W, std::vector G, s } } } - if (!symmetric_eigendecomposition(std::move(W), n, Q, eigenvalues)) { + if (!tools::detail::symmetric_eigendecomposition(std::move(W), n, Q, eigenvalues)) { return std::numeric_limits::quiet_NaN(); } return scale * *std::min_element(eigenvalues.begin(), eigenvalues.end()); diff --git a/include/boost/math/optimization/dycors.hpp b/include/boost/math/optimization/dycors.hpp index 8834d1c2c7..e26e8e4961 100644 --- a/include/boost/math/optimization/dycors.hpp +++ b/include/boost/math/optimization/dycors.hpp @@ -12,6 +12,7 @@ #include #endif #include +#include #ifndef BOOST_MATH_BUILD_MODULE #include #include @@ -236,9 +237,8 @@ template class dycors_engin } } } - // Gauss-Jordan elimination of [P_T | I], with partial pivoting: std::vector A(n * n); - Pinv_.assign(n * n, 0.0); + Pinv_.resize(n * n); PhiT_.resize(n * n); for (size_t j = 0; j < n; ++j) { for (size_t k = 0; k < n; ++k) { @@ -246,35 +246,8 @@ template class dycors_engin } A[j * n] = 1; std::copy(X_.data() + T_[j] * d_, X_.data() + (T_[j] + 1) * d_, A.data() + j * n + 1); - Pinv_[j * n + j] = 1; } - for (size_t j = 0; j < n; ++j) { - size_t p = j; - for (size_t k = j + 1; k < n; ++k) { - if (abs(A[k * n + j]) > abs(A[p * n + j])) { - p = k; - } - } - for (size_t i = 0; i < n; ++i) { - std::swap(A[j * n + i], A[p * n + i]); - std::swap(Pinv_[j * n + i], Pinv_[p * n + i]); - } - double const inv = 1 / A[j * n + j]; - for (size_t i = 0; i < n; ++i) { - A[j * n + i] *= inv; - Pinv_[j * n + i] *= inv; - } - for (size_t k = 0; k < n; ++k) { - double const t = A[k * n + j]; - if (k != j && t != 0) { - for (size_t i = 0; i < n; ++i) { - A[k * n + i] -= t * A[j * n + i]; - Pinv_[k * n + i] -= t * Pinv_[j * n + i]; - } - } - } - } - return true; + return tools::detail::gauss_jordan_inverse(A.data(), Pinv_.data(), n); } // The surrogate is the cubic radial basis function interpolant with a linear tail, diff --git a/include/boost/math/optimization/multilevel_coordinate_search.hpp b/include/boost/math/optimization/multilevel_coordinate_search.hpp index 8f6aa7a163..8b5d2c64f7 100644 --- a/include/boost/math/optimization/multilevel_coordinate_search.hpp +++ b/include/boost/math/optimization/multilevel_coordinate_search.hpp @@ -13,6 +13,7 @@ #endif #include #include +#include #ifndef BOOST_MATH_BUILD_MODULE #include #include @@ -148,7 +149,7 @@ std::vector minimize_quadratic_on_box(std::vector const &g, std::vec move(i, t); } } - // Newton step on the free variables, by a hand-rolled Cholesky factorization: + // Newton step on the free variables, by a Cholesky factorization: free_set.clear(); for (size_t i = 0; i < n; ++i) { if (lo[i] < h[i] && h[i] < hi[i]) { @@ -156,81 +157,63 @@ std::vector minimize_quadratic_on_box(std::vector const &g, std::vec } } size_t const m = free_set.size(); - bool positive_definite = m > 0; - L.assign(m * m, Real(0)); - for (size_t j = 0; j < m && positive_definite; ++j) { - Real d = G[free_set[j] * n + free_set[j]]; - for (size_t k = 0; k < j; ++k) { - d -= L[j * m + k] * L[j * m + k]; - } - if (!(d > 0)) { - positive_definite = false; - // w = (-G_11^{-1} G_1j, 1, 0, ...) has curvature w^T G w = d <= 0, where G_11 = L_11 L_11^T is the leading - // block already factored. Go downhill along it as far as the box allows. - p.assign(m, Real(0)); - p[j] = 1; - for (size_t k = j; k-- > 0;) { - Real s = L[j * m + k]; - for (size_t q = k + 1; q < j; ++q) { - s += L[q * m + k] * p[q]; - } - p[k] = -s / L[k * m + k]; + L.resize(m * m); + for (size_t i = 0; i < m; ++i) { + for (size_t k = 0; k <= i; ++k) { + L[i * m + k] = G[free_set[i] * n + free_set[k]]; + } + } + size_t failed = 0; + bool const positive_definite = m > 0 && tools::detail::cholesky(L.data(), m, 0, &failed); + if (m > 0 && !positive_definite) { + size_t const j = failed; + Real const d = L[j * m + j]; + // w = (-G_11^{-1} G_1j, 1, 0, ...) has curvature w^T G w = d <= 0, where G_11 = L_11 L_11^T is the leading + // block already factored. Go downhill along it as far as the box allows. + p.assign(m, Real(0)); + p[j] = 1; + for (size_t k = j; k-- > 0;) { + Real s = L[j * m + k]; + for (size_t q = k + 1; q < j; ++q) { + s += L[q * m + k] * p[q]; } - Real slope = 0; - for (size_t i = 0; i <= j; ++i) { - slope += r[free_set[i]] * p[i]; + p[k] = -s / L[k * m + k]; + } + Real slope = 0; + for (size_t i = 0; i <= j; ++i) { + slope += r[free_set[i]] * p[i]; + } + if (slope > 0) { + for (auto &pi : p) { + pi = -pi; } - if (slope > 0) { - for (auto &pi : p) { - pi = -pi; + } + if (d < 0 || slope != 0) { + Real t = std::numeric_limits::infinity(); + for (size_t i = 0; i <= j; ++i) { + size_t const k = free_set[i]; + if (p[i] > 0) { + t = (std::min)(t, (hi[k] - h[k]) / p[i]); + } + else if (p[i] < 0) { + t = (std::min)(t, (lo[k] - h[k]) / p[i]); } } - if (d < 0 || slope != 0) { - Real t = std::numeric_limits::infinity(); + if (t > 0 && t < std::numeric_limits::infinity()) { for (size_t i = 0; i <= j; ++i) { size_t const k = free_set[i]; - if (p[i] > 0) { - t = (std::min)(t, (hi[k] - h[k]) / p[i]); - } - else if (p[i] < 0) { - t = (std::min)(t, (lo[k] - h[k]) / p[i]); - } + move(k, t * p[i]); } - if (t > 0 && t < std::numeric_limits::infinity()) { - for (size_t i = 0; i <= j; ++i) { - size_t const k = free_set[i]; - move(k, t * p[i]); - } - } - } - break; - } - L[j * m + j] = sqrt(d); - for (size_t i = j + 1; i < m; ++i) { - Real s = G[free_set[i] * n + free_set[j]]; - for (size_t k = 0; k < j; ++k) { - s -= L[i * m + k] * L[j * m + k]; } - L[i * m + j] = s / L[j * m + j]; } } if (positive_definite) { // Solve L L^T p = -r_F: - p.assign(m, Real(0)); + p.resize(m); for (size_t i = 0; i < m; ++i) { - Real s = -r[free_set[i]]; - for (size_t k = 0; k < i; ++k) { - s -= L[i * m + k] * p[k]; - } - p[i] = s / L[i * m + i]; - } - for (size_t i = m; i-- > 0;) { - Real s = p[i]; - for (size_t k = i + 1; k < m; ++k) { - s -= L[k * m + i] * p[k]; - } - p[i] = s / L[i * m + i]; + p[i] = -r[free_set[i]]; } + tools::detail::cholesky_solve(L.data(), m, p.data()); // The largest step t <= 1 along p that stays in the box: Real t = 1; for (size_t i = 0; i < m; ++i) { diff --git a/include/boost/math/optimization/qlognehvi.hpp b/include/boost/math/optimization/qlognehvi.hpp index a30fa4b794..f77ec4677b 100644 --- a/include/boost/math/optimization/qlognehvi.hpp +++ b/include/boost/math/optimization/qlognehvi.hpp @@ -136,7 +136,7 @@ BOOST_MATH_TEST_EXPORT class qlognehvi_acquisition { for (size_t s = 0; s < N; ++s) { double const *row = zeta.data() + (k * N + s) * count; for (size_t j = first; j < count; ++j) { - values_[s][j][k] = gp.conditioning_mean()[j] + dot(L + j * count, row, j + 1); + values_[s][j][k] = gp.conditioning_mean()[j] + tools::detail::dot(L + j * count, row, j + 1); } gp.sample_path(row, a_[k].data() + s * n, w_[k].data() + s * count); } @@ -181,7 +181,7 @@ BOOST_MATH_TEST_EXPORT class qlognehvi_acquisition { } ws.sd[k] = sqrt(gp.conditional_variance(x, gradient ? ws.dvar.data() + k * d : nullptr, ws.work, kx)); for (size_t s = 0; s < N; ++s) { - ws.y[s * m + k] = gp.mean() + dot(a_[k].data() + s * n, kx, n) + dot(w_[k].data() + s * nb, kx + n, nb) + + ws.y[s * m + k] = gp.mean() + tools::detail::dot(a_[k].data() + s * n, kx, n) + tools::detail::dot(w_[k].data() + s * nb, kx + n, nb) + ws.sd[k] * z_[k * N + s]; } } @@ -601,7 +601,7 @@ class qlognehvi_optimizer { double const *zeta = normals_.data() + (s * m + k) * N; double const *L = models_[k].conditioning_factor().data(); for (size_t j = 0; j < N; ++j) { - double const step = detail::dot(L + j * N, zeta, j + 1); + double const step = tools::detail::dot(L + j * N, zeta, j + 1); f[j * m + k] = models_[k].conditioning_mean()[j] + step; f[(N + j) * m + k] = models_[k].conditioning_mean()[j] - step; } diff --git a/include/boost/math/optimization/turbo.hpp b/include/boost/math/optimization/turbo.hpp index 8e45c90acc..40621ada4a 100644 --- a/include/boost/math/optimization/turbo.hpp +++ b/include/boost/math/optimization/turbo.hpp @@ -417,7 +417,7 @@ template class turbo_engine double best_value = std::numeric_limits::infinity(); for (size_t c = 0; c < M; ++c) { // Without a factor (it should not happen, as the noise bounds the covariance below), the marginals: - double const f = factored ? mu[c] + dot(S_.data() + c * M, z.data(), c + 1) + double const f = factored ? mu[c] + tools::detail::dot(S_.data() + c * M, z.data(), c + 1) : mu[c] + sqrt(gp_.outputscale() + gp_.noise()) * z[c]; if (!taken[c] && (best == M || f < best_value)) { best = c; diff --git a/include/boost/math/tools/detail/linear_algebra/cholesky.hpp b/include/boost/math/tools/detail/linear_algebra/cholesky.hpp new file mode 100644 index 0000000000..c19a708c85 --- /dev/null +++ b/include/boost/math/tools/detail/linear_algebra/cholesky.hpp @@ -0,0 +1,254 @@ +// (C) Copyright Nick Thompson 2026. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_CHOLESKY_HPP +#define BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_CHOLESKY_HPP + +#include +#ifndef BOOST_MATH_BUILD_MODULE +#include +#include +#include +#endif + +BOOST_MATH_NAMESPACE_BEGIN +namespace tools { +namespace detail { + +// Hand-rolled dense linear algebra on row-major arrays. The kernels with long inner loops are written so that +// the compiler can vectorize them without reassociating floating point sums: either as axpy updates of contiguous +// rows, or as several rows' dot products with one shared operand, which gives independent accumulators. + +// s[r] = sum_{k < m} a[r][k] b[k] for four rows a[0..3]. +template void dot4(Real const *const *a, Real const *b, size_t m, Real *s) { + Real s0 = 0, s1 = 0, s2 = 0, s3 = 0, t0 = 0, t1 = 0, t2 = 0, t3 = 0; + size_t k = 0; + for (; k + 1 < m; k += 2) { + Real const x = b[k]; + Real const y = b[k + 1]; + s0 += a[0][k] * x; + t0 += a[0][k + 1] * y; + s1 += a[1][k] * x; + t1 += a[1][k + 1] * y; + s2 += a[2][k] * x; + t2 += a[2][k + 1] * y; + s3 += a[3][k] * x; + t3 += a[3][k + 1] * y; + } + if (k < m) { + Real const x = b[k]; + s0 += a[0][k] * x; + s1 += a[1][k] * x; + s2 += a[2][k] * x; + s3 += a[3][k] * x; + } + s[0] = s0 + t0; + s[1] = s1 + t1; + s[2] = s2 + t2; + s[3] = s3 + t3; +} + +template Real dot(Real const *a, Real const *b, size_t m) { + Real s0 = 0, s1 = 0, s2 = 0, s3 = 0; + size_t k = 0; + for (; k + 3 < m; k += 4) { + s0 += a[k] * b[k]; + s1 += a[k + 1] * b[k + 1]; + s2 += a[k + 2] * b[k + 2]; + s3 += a[k + 3] * b[k + 3]; + } + for (; k < m; ++k) { + s0 += a[k] * b[k]; + } + return (s0 + s1) + (s2 + s3); +} + +// C[i][j] -= sum_{k < len} X[i][k] X[j][k] for lo <= j <= i < hi, where row i of X starts at X + i ldx and C has +// leading dimension ldc. Tiles of 64 rows j stay in cache while every block of four rows i below them goes by. +// Rows i < first of C are left alone; the others get what they would with first = 0. +template +void subtract_gram(Real *C, size_t ldc, Real const *X, size_t ldx, size_t len, size_t lo, size_t hi, size_t first = 0) { + constexpr size_t tile = 64; + Real s[4]; + Real const *rows[4]; + for (size_t j0 = lo; j0 < hi; j0 += tile) { + size_t const j1 = (std::min)(hi, j0 + tile); + for (size_t i = j0; i < hi; i += 4) { + size_t const nb = (std::min)(size_t(4), hi - i), skip = first > i ? first - i : 0; + for (size_t b = 0; b < 4; ++b) { + rows[b] = X + (i + (b < nb ? b : 0)) * ldx; + } + size_t const jend = skip < nb ? (std::min)(j1, i + nb) : j0; + for (size_t j = j0; j < jend; ++j) { + Real const *xj = X + j * ldx; + if (j < i) { + dot4(rows, xj, len, s); + for (size_t b = skip; b < nb; ++b) { + C[(i + b) * ldc + j] -= s[b]; + } + } + else { + for (size_t b = (std::max)(j - i, skip); b < nb; ++b) { + C[(i + b) * ldc + j] -= dot(rows[b], xj, len); + } + } + } + } + } +} + +// The Cholesky factorization A = L L^T in place: on input the lower triangle of the n x n matrix A (leading dimension +// n), on output that of L. The upper triangle is not referenced. Returns false if A is not numerically positive +// definite. Right-looking by blocks of 64 columns; within a block, left-looking four rows at a time. If rows i < first +// are those of L already, and the others those of A, the result is that of the whole factorization. On failure +// *failed, if given, is the column j of the pivot that is not positive: A[j][j] then holds that pivot, and the rows +// before it and row j up to the diagonal are those of L. +BOOST_MATH_TEST_EXPORT template +bool cholesky(Real *A, size_t n, size_t first = 0, size_t *failed = nullptr) { + using std::sqrt; + constexpr size_t block = 64; + Real s[4]; + Real *rows[4]; + Real const *parts[4]; + for (size_t k0 = 0; k0 < n; k0 += block) { + size_t const k1 = (std::min)(n, k0 + block); + // Columns k0 to k1 of L, from the diagonal block down: + for (size_t i = k0; i < n; i += 4) { + size_t const nb = (std::min)(size_t(4), n - i), skip = first > i ? first - i : 0; + for (size_t b = 0; b < 4; ++b) { + rows[b] = A + (i + (b < nb ? b : 0)) * n; + parts[b] = rows[b] + k0; + } + size_t const jend = skip < nb ? (std::min)(k1, i + nb) : k0; + for (size_t j = k0; j < jend; ++j) { + Real const *lj = A + j * n; + if (j < i) { + dot4(parts, lj + k0, j - k0, s); + for (size_t b = skip; b < nb; ++b) { + rows[b][j] = (rows[b][j] - s[b]) / lj[j]; + } + continue; + } + // Column j meets these rows on the diagonal: row j first, then the rows below it. + for (size_t b = (std::max)(j - i, skip); b < nb; ++b) { + Real const d = rows[b][j] - dot(parts[b], lj + k0, j - k0); + if (b == j - i) { + if (!(d > 0)) { + rows[b][j] = d; + if (failed) { + *failed = j; + } + return false; + } + rows[b][j] = sqrt(d); + } + else { + rows[b][j] = d / lj[j]; + } + } + } + } + // The trailing submatrix loses the contribution of these columns: + subtract_gram(A, n, A + k0, n, k1 - k0, k1, n, first); + } + return true; +} + +// Solves L X = B in place for the n x m matrix B (leading dimension m), where L is the lower triangle of an n x n +// matrix (leading dimension n). +BOOST_MATH_TEST_EXPORT template void forward_solve(Real const *L, size_t n, Real *B, size_t m) { + for (size_t i = 0; i < n; ++i) { + Real *bi = B + i * m; + for (size_t k = 0; k < i; ++k) { + Real const lik = L[i * n + k]; + Real const *bk = B + k * m; + for (size_t c = 0; c < m; ++c) { + bi[c] -= lik * bk[c]; + } + } + Real const inv = 1 / L[i * n + i]; + for (size_t c = 0; c < m; ++c) { + bi[c] *= inv; + } + } +} + +// Solves L x = b in place for a vector b. +BOOST_MATH_TEST_EXPORT template void forward_solve(Real const *L, size_t n, Real *b) { + for (size_t i = 0; i < n; ++i) { + b[i] = (b[i] - dot(L + i * n, b, i)) / L[i * n + i]; + } +} + +// Solves L^T x = b in place, by rows of L: once x_i is known, remove its contribution L_ik x_i from b_k for k < i. +BOOST_MATH_TEST_EXPORT template void backward_solve(Real const *L, size_t n, Real *b) { + for (size_t i = n; i-- > 0;) { + Real const *li = L + i * n; + Real const xi = b[i] / li[i]; + b[i] = xi; + for (size_t k = 0; k < i; ++k) { + b[k] -= li[k] * xi; + } + } +} + +// Solves L L^T x = b in place. +BOOST_MATH_TEST_EXPORT template void cholesky_solve(Real const *L, size_t n, Real *b) { + forward_solve(L, n, b); + backward_solve(L, n, b); +} + +// The lower triangle of (L L^T)^{-1} = Z^T Z, where Z = L^{-1}. Z is n x n scratch. Four rows of Z are taken at a +// time, for which each row of Z has zeros in the three places after its diagonal. +template void cholesky_inverse(Real const *L, size_t n, Real *Z, Real *Kinv) { + for (size_t i = 0; i < n; ++i) { + Real *zi = Z + i * n; + std::fill(zi, zi + (std::min)(n, i + 4), Real(0)); + size_t k = 0; + for (; k + 3 < i; k += 4) { + Real const *l = L + i * n + k, *z0 = Z + k * n, *z1 = z0 + n, *z2 = z1 + n, *z3 = z2 + n; + for (size_t j = 0; j <= k + 3; ++j) { + zi[j] = zi[j] - l[0] * z0[j] - l[1] * z1[j] - l[2] * z2[j] - l[3] * z3[j]; + } + } + for (; k < i; ++k) { + Real const lik = L[i * n + k]; + Real const *zk = Z + k * n; + for (size_t j = 0; j <= k; ++j) { + zi[j] -= lik * zk[j]; + } + } + Real const inv = 1 / L[i * n + i]; + for (size_t j = 0; j < i; ++j) { + zi[j] *= inv; + } + zi[i] = inv; + } + for (size_t a = 0; a < n; ++a) { + Real *row = Kinv + a * n; + std::fill(row, row + a + 1, Real(0)); + size_t k = a; + for (; k + 3 < n; k += 4) { + Real const *z0 = Z + k * n, *z1 = z0 + n, *z2 = z1 + n, *z3 = z2 + n; + Real const c0 = z0[a], c1 = z1[a], c2 = z2[a], c3 = z3[a]; + for (size_t b = 0; b <= a; ++b) { + row[b] = row[b] + c0 * z0[b] + c1 * z1[b] + c2 * z2[b] + c3 * z3[b]; + } + } + for (; k < n; ++k) { + Real const *zk = Z + k * n; + Real const zka = zk[a]; + for (size_t b = 0; b <= a; ++b) { + row[b] += zka * zk[b]; + } + } + } +} + +} // namespace detail +} // namespace tools +BOOST_MATH_NAMESPACE_END + +#endif // BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_CHOLESKY_HPP diff --git a/include/boost/math/tools/detail/linear_algebra/gauss_jordan.hpp b/include/boost/math/tools/detail/linear_algebra/gauss_jordan.hpp new file mode 100644 index 0000000000..53c327287e --- /dev/null +++ b/include/boost/math/tools/detail/linear_algebra/gauss_jordan.hpp @@ -0,0 +1,54 @@ +// (C) Copyright Nick Thompson 2026. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_GAUSS_JORDAN_HPP +#define BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_GAUSS_JORDAN_HPP + +#include +#ifndef BOOST_MATH_BUILD_MODULE +#include +#include +#include +#include +#include +#endif + +BOOST_MATH_NAMESPACE_BEGIN +namespace tools { +namespace detail { + +// x = a^-1 for the row-major n x n matrix a, which is destroyed, by Gauss-Jordan elimination with partial pivoting; +// false if a is singular (a zero pivot). +template +bool gauss_jordan_inverse(Real* a, Real* x, std::size_t n) +{ + using std::abs; + for (std::size_t i = 0; i < n * n; ++i) x[i] = 0; + for (std::size_t i = 0; i < n; ++i) x[i * n + i] = 1; + for (std::size_t c = 0; c < n; ++c) + { + std::size_t piv = c; + for (std::size_t r = c + 1; r < n; ++r) + if (abs(a[r * n + c]) > abs(a[piv * n + c])) piv = r; + if (a[piv * n + c] == 0) return false; + if (piv != c) + for (std::size_t j = 0; j < n; ++j) { std::swap(a[c * n + j], a[piv * n + j]); std::swap(x[c * n + j], x[piv * n + j]); } + const Real inv = 1 / a[c * n + c]; + for (std::size_t j = 0; j < n; ++j) { a[c * n + j] *= inv; x[c * n + j] *= inv; } + for (std::size_t r = 0; r < n; ++r) + { + if (r == c || a[r * n + c] == 0) continue; + const Real f = a[r * n + c]; + for (std::size_t j = 0; j < n; ++j) { a[r * n + j] -= f * a[c * n + j]; x[r * n + j] -= f * x[c * n + j]; } + } + } + return true; +} + +} // namespace detail +} // namespace tools +BOOST_MATH_NAMESPACE_END + +#endif // BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_GAUSS_JORDAN_HPP diff --git a/include/boost/math/tools/detail/linear_algebra/lu.hpp b/include/boost/math/tools/detail/linear_algebra/lu.hpp new file mode 100644 index 0000000000..97a38575a2 --- /dev/null +++ b/include/boost/math/tools/detail/linear_algebra/lu.hpp @@ -0,0 +1,82 @@ +// (C) Copyright Nick Thompson 2026. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_LU_HPP +#define BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_LU_HPP + +#include +#include +#include +#ifndef BOOST_MATH_BUILD_MODULE +#include +#include +#include +#include +#include +#endif + +BOOST_MATH_NAMESPACE_BEGIN +namespace tools { +namespace detail { + +// LU with partial pivoting of the row-major n x n matrix a, in place. False when a pivot is not above n eps times +// the largest entry of its column, so the test does not depend on the scaling of the variables. With eps = 0 only +// a zero or non-finite pivot fails. +template +bool lu_factor(std::vector& a, std::vector& piv, std::vector& colmax, std::size_t n, + Real const eps = tools::epsilon()) +{ + using std::abs; + for (std::size_t j = 0; j < n; ++j) + { + colmax[j] = 0; + for (std::size_t i = 0; i < n; ++i) + colmax[j] = (std::max)(colmax[j], Real(abs(a[i * n + j]))); + } + Real const tiny = n * eps; + for (std::size_t k = 0; k < n; ++k) + { + std::size_t p = k; + for (std::size_t i = k + 1; i < n; ++i) + if (abs(a[i * n + k]) > abs(a[p * n + k])) + p = i; + piv[k] = p; + if (p != k) + for (std::size_t j = 0; j < n; ++j) + std::swap(a[k * n + j], a[p * n + j]); + Real const d = a[k * n + k]; + if (!(abs(d) > tiny * colmax[k]) || !(BOOST_MATH_NAMESPACE::isfinite)(d)) + return false; + for (std::size_t i = k + 1; i < n; ++i) + { + Real const l = a[i * n + k] /= d; + for (std::size_t j = k + 1; j < n; ++j) + a[i * n + j] -= l * a[k * n + j]; + } + } + return true; +} + +template +void lu_solve(std::vector const& a, std::vector const& piv, std::size_t n, V& b) +{ + for (std::size_t k = 0; k < n; ++k) + std::swap(b[k], b[piv[k]]); + for (std::size_t i = 1; i < n; ++i) + for (std::size_t j = 0; j < i; ++j) + b[i] -= a[i * n + j] * b[j]; + for (std::size_t i = n; i-- > 0;) + { + for (std::size_t j = i + 1; j < n; ++j) + b[i] -= a[i * n + j] * b[j]; + b[i] /= a[i * n + i]; + } +} + +} // namespace detail +} // namespace tools +BOOST_MATH_NAMESPACE_END + +#endif // BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_LU_HPP diff --git a/include/boost/math/tools/detail/linear_algebra/qr.hpp b/include/boost/math/tools/detail/linear_algebra/qr.hpp new file mode 100644 index 0000000000..8e865fb66c --- /dev/null +++ b/include/boost/math/tools/detail/linear_algebra/qr.hpp @@ -0,0 +1,91 @@ +// (C) Copyright Nick Thompson 2026. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_QR_HPP +#define BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_QR_HPP + +#include +#include +#ifndef BOOST_MATH_BUILD_MODULE +#include +#include +#include +#include +#include +#endif + +BOOST_MATH_NAMESPACE_BEGIN +namespace tools { +namespace detail { + +// Householder QR of the row-major m x n matrix A, in place, optionally with column pivoting: min(m, n, r) +// reflections, which leave R in the upper triangle of A. Q, if not null, receives the first r columns of the +// orthogonal factor (m x r, row-major). +template +void householder_qr(Real* A, std::size_t m, std::size_t n, std::size_t r, bool pivot, Real* Q) +{ + using std::sqrt; + BOOST_MATH_ASSERT(!Q || r <= m); + const std::size_t steps = (std::min)((std::min)(m, n), r); + std::vector> vs; + std::vector beta; + for (std::size_t k = 0; k < steps; ++k) + { + if (pivot) + { + std::size_t best = k; + Real bn = -1; + for (std::size_t j = k; j < n; ++j) + { + Real s = 0; + for (std::size_t i = k; i < m; ++i) s += A[i * n + j] * A[i * n + j]; + if (s > bn) { bn = s; best = j; } + } + if (best != k) + for (std::size_t i = 0; i < m; ++i) std::swap(A[i * n + k], A[i * n + best]); + } + std::vector v(m, Real(0)); + Real nx = 0; + for (std::size_t i = k; i < m; ++i) { v[i] = A[i * n + k]; nx += v[i] * v[i]; } + nx = sqrt(nx); + Real b = 0; + if (nx > 0) + { + v[k] -= v[k] >= 0 ? -nx : nx; + Real vv = 0; + for (std::size_t i = k; i < m; ++i) vv += v[i] * v[i]; + b = vv > 0 ? Real(2) / vv : Real(0); + for (std::size_t j = k; j < n; ++j) + { + Real s = 0; + for (std::size_t i = k; i < m; ++i) s += v[i] * A[i * n + j]; + s *= b; + for (std::size_t i = k; i < m; ++i) A[i * n + j] -= s * v[i]; + } + } + if (Q) + { + vs.push_back(v); + beta.push_back(b); + } + } + if (!Q) return; + for (std::size_t i = 0; i < m * r; ++i) Q[i] = 0; + for (std::size_t j = 0; j < r; ++j) Q[j * r + j] = 1; + for (std::size_t k = vs.size(); k-- > 0;) + for (std::size_t j = 0; j < r; ++j) + { + Real s = 0; + for (std::size_t i = k; i < m; ++i) s += vs[k][i] * Q[i * r + j]; + s *= beta[k]; + for (std::size_t i = k; i < m; ++i) Q[i * r + j] -= s * vs[k][i]; + } +} + +} // namespace detail +} // namespace tools +BOOST_MATH_NAMESPACE_END + +#endif // BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_QR_HPP diff --git a/include/boost/math/tools/detail/linear_algebra/symmetric_eigen.hpp b/include/boost/math/tools/detail/linear_algebra/symmetric_eigen.hpp new file mode 100644 index 0000000000..8b77d0b2b5 --- /dev/null +++ b/include/boost/math/tools/detail/linear_algebra/symmetric_eigen.hpp @@ -0,0 +1,220 @@ +// (C) Copyright Nick Thompson 2026. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_SYMMETRIC_EIGEN_HPP +#define BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_SYMMETRIC_EIGEN_HPP + +#include +#ifndef BOOST_MATH_BUILD_MODULE +#include +#include +#include +#include +#include +#endif + +BOOST_MATH_NAMESPACE_BEGIN +namespace tools { +namespace detail { + +// Computes A = V diag(eigenvalues) V^T for a symmetric n x n matrix A stored row-major, +// by Householder tridiagonalization followed by the implicit QL algorithm; +// see Wilkinson & Reinsch, Handbook for Automatic Computation, Vol. II (tred2 and tql2), +// and Golub & Van Loan, Matrix Computations, 4th ed., Section 8.3. +// V is returned row-major, with the eigenvectors as its columns. +// Returns false if some eigenvalue did not converge within max_iterations QL iterations. +template +bool symmetric_eigendecomposition(std::vector A, size_t n, std::vector &V, std::vector &eigenvalues, size_t max_iterations = 30) { + using std::abs; + using std::hypot; + using std::sqrt; + using std::max; + V = std::move(A); + std::vector &d = eigenvalues; + d.resize(n); + std::vector e(n, Real(0)); + // Householder reduction to tridiagonal form; d is the diagonal and e the subdiagonal: + for (size_t j = 0; j < n; ++j) { + d[j] = V[(n - 1)*n + j]; + } + for (size_t i = n - 1; i > 0; --i) { + Real scale = 0; + Real h = 0; + for (size_t k = 0; k < i; ++k) { + scale += abs(d[k]); + } + if (scale == 0) { + e[i] = d[i - 1]; + for (size_t j = 0; j < i; ++j) { + d[j] = V[(i - 1)*n + j]; + V[i*n + j] = 0; + V[j*n + i] = 0; + } + } + else { + for (size_t k = 0; k < i; ++k) { + d[k] /= scale; + h += d[k]*d[k]; + } + Real f = d[i - 1]; + Real g = sqrt(h); + if (f > 0) { + g = -g; + } + e[i] = scale*g; + h -= f*g; + d[i - 1] = f - g; + for (size_t j = 0; j < i; ++j) { + e[j] = 0; + } + for (size_t j = 0; j < i; ++j) { + f = d[j]; + V[j*n + i] = f; + g = e[j] + V[j*n + j]*f; + for (size_t k = j + 1; k < i; ++k) { + g += V[k*n + j]*d[k]; + e[k] += V[k*n + j]*f; + } + e[j] = g; + } + f = 0; + for (size_t j = 0; j < i; ++j) { + e[j] /= h; + f += e[j]*d[j]; + } + const Real hh = f/(h + h); + for (size_t j = 0; j < i; ++j) { + e[j] -= hh*d[j]; + } + for (size_t j = 0; j < i; ++j) { + f = d[j]; + g = e[j]; + for (size_t k = j; k < i; ++k) { + V[k*n + j] -= f*e[k] + g*d[k]; + } + d[j] = V[(i - 1)*n + j]; + V[i*n + j] = 0; + } + } + d[i] = h; + } + // Accumulate the transformations: + for (size_t i = 0; i + 1 < n; ++i) { + V[(n - 1)*n + i] = V[i*n + i]; + V[i*n + i] = 1; + const Real h = d[i + 1]; + if (h != 0) { + for (size_t k = 0; k <= i; ++k) { + d[k] = V[k*n + i + 1]/h; + } + for (size_t j = 0; j <= i; ++j) { + Real g = 0; + for (size_t k = 0; k <= i; ++k) { + g += V[k*n + i + 1]*V[k*n + j]; + } + for (size_t k = 0; k <= i; ++k) { + V[k*n + j] -= g*d[k]; + } + } + } + for (size_t k = 0; k <= i; ++k) { + V[k*n + i + 1] = 0; + } + } + for (size_t j = 0; j < n; ++j) { + d[j] = V[(n - 1)*n + j]; + V[(n - 1)*n + j] = 0; + } + V[(n - 1)*n + n - 1] = 1; + // The QL rotations act on columns of V, so apply them to the rows of V^T, which are contiguous: + for (size_t i = 0; i < n; ++i) { + for (size_t j = 0; j < i; ++j) { + std::swap(V[i*n + j], V[j*n + i]); + } + } + // Implicit QL iterations on the tridiagonal matrix: + for (size_t i = 1; i < n; ++i) { + e[i - 1] = e[i]; + } + e[n - 1] = 0; + Real f = 0; + Real tst1 = 0; + const Real eps = std::numeric_limits::epsilon(); + for (size_t l = 0; l < n; ++l) { + // Find a small subdiagonal element: + tst1 = (max)(tst1, abs(d[l]) + abs(e[l])); + size_t m = l; + while (m < n - 1 && !(abs(e[m]) <= eps*tst1)) { + ++m; + } + // If m == l, d[l] is already an eigenvalue; otherwise, iterate: + size_t iterations = 0; + while (m > l && abs(e[l]) > eps*tst1) { + if (iterations++ == max_iterations) { + break; + } + // Compute the implicit shift: + Real g = d[l]; + Real p = (d[l + 1] - g)/(2*e[l]); + Real r = hypot(p, Real(1)); + if (p < 0) { + r = -r; + } + d[l] = e[l]/(p + r); + d[l + 1] = e[l]*(p + r); + const Real dl1 = d[l + 1]; + Real h = g - d[l]; + for (size_t i = l + 2; i < n; ++i) { + d[i] -= h; + } + f += h; + p = d[m]; + Real c = 1; + Real c2 = c; + Real c3 = c; + const Real el1 = e[l + 1]; + Real s = 0; + Real s2 = 0; + for (size_t i = m; i-- > l;) { + c3 = c2; + c2 = c; + s2 = s; + g = c*e[i]; + h = c*p; + r = hypot(p, e[i]); + e[i + 1] = s*r; + s = e[i]/r; + c = p/r; + p = c*d[i] - s*g; + d[i + 1] = h + s*(c*g + s*d[i]); + for (size_t k = 0; k < n; ++k) { + h = V[(i + 1)*n + k]; + V[(i + 1)*n + k] = s*V[i*n + k] + c*h; + V[i*n + k] = c*V[i*n + k] - s*h; + } + } + p = -s*s2*c3*el1*e[l]/dl1; + e[l] = s*p; + d[l] = c*p; + } + if (iterations > max_iterations) { + return false; + } + d[l] += f; + e[l] = 0; + } + for (size_t i = 0; i < n; ++i) { + for (size_t j = 0; j < i; ++j) { + std::swap(V[i*n + j], V[j*n + i]); + } + } + return true; +} + +} // namespace detail +} // namespace tools +BOOST_MATH_NAMESPACE_END + +#endif // BOOST_MATH_TOOLS_DETAIL_LINEAR_ALGEBRA_SYMMETRIC_EIGEN_HPP diff --git a/include/boost/math/tools/multivariate_roots.hpp b/include/boost/math/tools/multivariate_roots.hpp index a83ea55aa3..67a1f2b3eb 100644 --- a/include/boost/math/tools/multivariate_roots.hpp +++ b/include/boost/math/tools/multivariate_roots.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #ifndef BOOST_MATH_BUILD_MODULE #include #include @@ -68,58 +69,6 @@ bool copy_jacobian(M const& J, std::vector& lu, std::size_t n, std::false_ return true; } -// LU with partial pivoting of the row-major n x n matrix a, in place. False when a pivot is not above n eps times -// the largest entry of its column, so the test does not depend on the scaling of the variables. -template -bool lu_factor(std::vector& a, std::vector& piv, std::vector& colmax, std::size_t n) -{ - using std::abs; - for (std::size_t j = 0; j < n; ++j) - { - colmax[j] = 0; - for (std::size_t i = 0; i < n; ++i) - colmax[j] = (std::max)(colmax[j], Real(abs(a[i * n + j]))); - } - Real const tiny = n * tools::epsilon(); - for (std::size_t k = 0; k < n; ++k) - { - std::size_t p = k; - for (std::size_t i = k + 1; i < n; ++i) - if (abs(a[i * n + k]) > abs(a[p * n + k])) - p = i; - piv[k] = p; - if (p != k) - for (std::size_t j = 0; j < n; ++j) - std::swap(a[k * n + j], a[p * n + j]); - Real const d = a[k * n + k]; - if (!(abs(d) > tiny * colmax[k]) || !(BOOST_MATH_NAMESPACE::isfinite)(d)) - return false; - for (std::size_t i = k + 1; i < n; ++i) - { - Real const l = a[i * n + k] /= d; - for (std::size_t j = k + 1; j < n; ++j) - a[i * n + j] -= l * a[k * n + j]; - } - } - return true; -} - -template -void lu_solve(std::vector const& a, std::vector const& piv, std::size_t n, V& b) -{ - for (std::size_t k = 0; k < n; ++k) - std::swap(b[k], b[piv[k]]); - for (std::size_t i = 1; i < n; ++i) - for (std::size_t j = 0; j < i; ++j) - b[i] -= a[i * n + j] * b[j]; - for (std::size_t i = n; i-- > 0;) - { - for (std::size_t j = i + 1; j < n; ++j) - b[i] -= a[i * n + j] * b[j]; - b[i] /= a[i * n + i]; - } -} - template auto inf_norm(V const& v) -> typename std::decay::type { diff --git a/include_private/boost/math/tools/remez.hpp b/include_private/boost/math/tools/remez.hpp index 99ef43c597..cd51ebf30d 100644 --- a/include_private/boost/math/tools/remez.hpp +++ b/include_private/boost/math/tools/remez.hpp @@ -15,7 +15,8 @@ #include #include #include -#include +#include +#include #include #include @@ -105,8 +106,8 @@ class remez_minimax { public: typedef std::function function_type; - typedef boost::numeric::ublas::vector vector_type; - typedef boost::numeric::ublas::matrix matrix_type; + typedef std::vector vector_type; + typedef boost::math::tools::matrix matrix_type; remez_minimax(function_type f, unsigned oN, unsigned oD, T a, T b, bool pin = true, bool rel_err = false, int sk = 0, int bits = 0); remez_minimax(function_type f, unsigned oN, unsigned oD, T a, T b, bool pin, bool rel_err, int sk, int bits, const vector_type& points); @@ -637,7 +638,7 @@ T remez_minimax::iterate() template polynomial remez_minimax::numerator()const { - std::unique_ptr a(new T[orderN + 1]); + std::vector a(orderN + 1); if(pinned) a[0] = 0; unsigned terms = pinned ? orderN : orderN + 1; @@ -651,7 +652,7 @@ polynomial remez_minimax::denominator()const { unsigned terms = orderD + 1; unsigned offsetD = pinned ? orderN : (orderN + 1); - std::unique_ptr a(new T[terms]); + std::vector a(terms); a[0] = 1; for(unsigned i = 0; i < orderD; ++i) a[i+1] = solution[i + offsetD]; diff --git a/include_private/boost/math/tools/solve.hpp b/include_private/boost/math/tools/solve.hpp index c0e6c0b25b..6764195194 100644 --- a/include_private/boost/math/tools/solve.hpp +++ b/include_private/boost/math/tools/solve.hpp @@ -12,63 +12,88 @@ #include #include +#include -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable:4996 4267 4244) -#endif +#include +#include +#include -#include -#include -#include +namespace boost{ namespace math{ namespace tools{ -#ifdef _MSC_VER -#pragma warning(pop) -#endif +// A minimal dense row-major matrix: all that solve() and remez.hpp need. +template +class matrix +{ +public: + matrix() : m_rows(0), m_cols(0) {} + matrix(std::size_t rows, std::size_t cols) : m_rows(rows), m_cols(cols), m_data(rows * cols) {} + T& operator()(std::size_t i, std::size_t j) { return m_data[i * m_cols + j]; } + const T& operator()(std::size_t i, std::size_t j) const { return m_data[i * m_cols + j]; } + std::size_t size1() const { return m_rows; } + std::size_t size2() const { return m_cols; } + const std::vector& data() const { return m_data; } +private: + std::size_t m_rows, m_cols; + std::vector m_data; +}; -namespace boost{ namespace math{ namespace tools{ +template +matrix prod(const matrix& A, const matrix& B) +{ + BOOST_MATH_ASSERT(A.size2() == B.size1()); + matrix result(A.size1(), B.size2()); + for(std::size_t i = 0; i < A.size1(); ++i) + for(std::size_t j = 0; j < B.size2(); ++j) + { + T sum = 0; + for(std::size_t k = 0; k < A.size2(); ++k) + sum += A(i, k) * B(k, j); + result(i, j) = sum; + } + return result; +} + +template +std::vector prod(const matrix& A, const std::vector& x) +{ + BOOST_MATH_ASSERT(A.size2() == x.size()); + std::vector result(A.size1()); + for(std::size_t i = 0; i < A.size1(); ++i) + { + T sum = 0; + for(std::size_t k = 0; k < A.size2(); ++k) + sum += A(i, k) * x[k]; + result[i] = sum; + } + return result; +} // // Find x such that Ax = b // -// Caution: this uses undocumented, and untested ublas code, -// however short of writing our own LU-decomposition code -// it's the only game in town. -// template -boost::numeric::ublas::vector solve( - const boost::numeric::ublas::matrix& A_, - const boost::numeric::ublas::vector& b_) +std::vector solve( + const matrix& A_, + const std::vector& b_) { - //BOOST_MATH_ASSERT(A_.size() == b_.size()); - - boost::numeric::ublas::matrix A(A_); - boost::numeric::ublas::vector b(b_); - boost::numeric::ublas::permutation_matrix<> piv(b.size()); - lu_factorize(A, piv); - lu_substitute(A, piv, b); + BOOST_MATH_ASSERT(A_.size1() == b_.size() && A_.size2() == b_.size()); + + const std::size_t n = b_.size(); + std::vector A(A_.data()), b(b_), colmax(n); + std::vector piv(n); + // The systems of the Remez method are close to singular, so only a zero pivot is refused: + if(!detail::lu_factor(A, piv, colmax, n, T(0))) + return std::vector(n, std::numeric_limits::quiet_NaN()); + detail::lu_solve(A, piv, n, b); // // iterate to reduce error: // - boost::numeric::ublas::vector delta(b.size()); - for(unsigned k = 0; k < 1; ++k) - { - noalias(delta) = prod(A_, b); - delta -= b_; - lu_substitute(A, piv, delta); - b -= delta; - - T max_error = 0; - - for(unsigned i = 0; i < delta.size(); ++i) - { - using std::abs; - T err = abs(delta[i] / b[i]); - if(err > max_error) - max_error = err; - } - //std::cout << "Max change in LU error correction: " << max_error << std::endl; - } + std::vector delta = prod(A_, b); + for(std::size_t i = 0; i < delta.size(); ++i) + delta[i] -= b_[i]; + detail::lu_solve(A, piv, n, delta); + for(std::size_t i = 0; i < delta.size(); ++i) + b[i] -= delta[i]; return b; } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f9aa26e933..7b17c4af19 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -65,7 +65,6 @@ project # For simplicities sake, make everything a static lib: static BOOST_ALL_NO_LIB=1 - BOOST_UBLAS_UNSUPPORTED_COMPILER=0 . ../include_private $(ntl-path)/include @@ -1108,6 +1107,7 @@ test-suite misc : [ run test_root_finding_concepts.cpp /boost/test//boost_unit_test_framework ] [ run test_toms748_solve.cpp pch /boost/test//boost_unit_test_framework ] [ run test_multivariate_roots.cpp : : : [ requires cxx14_return_type_deduction cxx14_generic_lambdas ] ] + [ run test_linear_algebra_detail.cpp : : : [ requires cxx14_return_type_deduction cxx14_generic_lambdas ] ] [ run compile_test/test_interpolators_cubic_spline_incl.cpp compile_test_main : : : [ requires cxx11_smart_ptr cxx11_defaulted_functions cxx11_auto_declarations ] [ check-target-builds ../config//is_ci_sanitizer_run "Sanitizer CI run" : no ] ] [ run compile_test/test_interpolators_barycentric_rational_incl.cpp compile_test_main : : : [ requires cxx11_smart_ptr cxx11_defaulted_functions cxx11_auto_declarations cxx11_unified_initialization_syntax ] [ check-target-builds ../config//is_ci_sanitizer_run "Sanitizer CI run" : no ] ] [ run test_octonion.cpp diff --git a/test/compile_test/CMakeLists.txt b/test/compile_test/CMakeLists.txt index 8989983683..3fdd1c047b 100644 --- a/test/compile_test/CMakeLists.txt +++ b/test/compile_test/CMakeLists.txt @@ -14,4 +14,4 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(BOOST_MATH_COMPILE_TEST_WARNINGS -Wall -Wextra -Wshadow -Werror -Wno-deprecated-declarations) endif() -boost_test(TYPE "compile" SOURCES ${SRC_FILES} COMPILE_DEFINITIONS BOOST_MATH_STANDALONE COMPILE_OPTIONS ${BOOST_MATH_COMPILE_TEST_WARNINGS} COMPILE_FEATURES cxx_std_17 LINK_LIBRARIES Boost::math Boost::multiprecision Boost::numeric_ublas Boost::unit_test_framework ) +boost_test(TYPE "compile" SOURCES ${SRC_FILES} COMPILE_DEFINITIONS BOOST_MATH_STANDALONE COMPILE_OPTIONS ${BOOST_MATH_COMPILE_TEST_WARNINGS} COMPILE_FEATURES cxx_std_17 LINK_LIBRARIES Boost::math Boost::multiprecision Boost::unit_test_framework ) diff --git a/test/math_unit_test.hpp b/test/math_unit_test.hpp index f7f25eb902..5d33c664b1 100644 --- a/test/math_unit_test.hpp +++ b/test/math_unit_test.hpp @@ -21,6 +21,7 @@ import std; #include #include #include +#include #endif #include // In module mode these declarations come from `import boost.math;`, which the @@ -35,6 +36,7 @@ import std; # if __has_include() #define BOOST_MATH_HAS_CXX_ABI 1 # include +# include # endif #endif namespace boost { namespace math { namespace test { @@ -47,12 +49,15 @@ namespace detail { inline std::string demangle(char const * name) { - int status = 0; - std::size_t size = 0; -#if BOOST_MATH_HAS_CXX_ABI - std::string s {abi::__cxa_demangle( name, NULL, &size, &status )}; -#else std::string s {name}; +#if BOOST_MATH_HAS_CXX_ABI + int status = 0; + // The buffer is ours to free, and is null if the name could not be demangled: + if (char* demangled = abi::__cxa_demangle(name, nullptr, nullptr, &status)) + { + s = demangled; + std::free(demangled); + } #endif return s; } diff --git a/test/test_adaptive_gauss_kronrod_quadrature.cpp b/test/test_adaptive_gauss_kronrod_quadrature.cpp index 8f360ded87..33abe2fd04 100644 --- a/test/test_adaptive_gauss_kronrod_quadrature.cpp +++ b/test/test_adaptive_gauss_kronrod_quadrature.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #if !defined(TEST1) && !defined(TEST1A) && !defined(TEST2) && !defined(TEST3) # define TEST1 @@ -74,7 +73,7 @@ Real get_termination_condition() template void test_linear() { - std::cout << "Testing linear functions are integrated properly by gauss_kronrod on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing linear functions are integrated properly by gauss_kronrod on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real error; auto f = [](const Real& x) { @@ -91,7 +90,7 @@ void test_linear() template void test_quadratic() { - std::cout << "Testing quadratic functions are integrated properly by gauss-kronrod on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing quadratic functions are integrated properly by gauss-kronrod on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real error; auto f = [](const Real& x) { return 5*x*x + 7*x + 12; }; @@ -108,7 +107,7 @@ void test_quadratic() template void test_ca() { - std::cout << "Testing integration of C(a) on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integration of C(a) on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real L1; Real error; @@ -137,7 +136,7 @@ void test_ca() template void test_three_quadrature_schemes_examples() { - std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; @@ -171,7 +170,7 @@ void test_three_quadrature_schemes_examples() template void test_integration_over_real_line() { - std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; Real L1; @@ -196,7 +195,7 @@ void test_integration_over_real_line() template void test_right_limit_infinite() { - std::cout << "Testing right limit infinite for Gauss-Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing right limit infinite for Gauss-Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; Real L1; @@ -219,7 +218,7 @@ void test_right_limit_infinite() template void test_left_limit_infinite() { - std::cout << "Testing left limit infinite for Gauss-Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing left limit infinite for Gauss-Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; diff --git a/test/test_barycentric_rational.cpp b/test/test_barycentric_rational.cpp index 8df8c4e6b5..213b4776f3 100644 --- a/test/test_barycentric_rational.cpp +++ b/test/test_barycentric_rational.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include "math_unit_test.hpp" @@ -28,7 +27,7 @@ using boost::multiprecision::cpp_bin_float_50; template void test_interpolation_condition() { - std::cout << "Testing interpolation condition for barycentric interpolation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing interpolation condition for barycentric interpolation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(4); boost::random::uniform_real_distribution dis(0.1f, 1); std::vector x(100); @@ -64,7 +63,7 @@ void test_interpolation_condition() template void test_interpolation_condition_high_order() { - std::cout << "Testing interpolation condition in high order for barycentric interpolation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing interpolation condition in high order for barycentric interpolation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(5); boost::random::uniform_real_distribution dis(0.1f, 1); std::vector x(100); @@ -91,7 +90,7 @@ void test_interpolation_condition_high_order() template void test_constant() { - std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(6); boost::random::uniform_real_distribution dis(0.1f, 1); @@ -121,7 +120,7 @@ void test_constant() template void test_constant_high_order() { - std::cout << "Testing that constants are interpolated correctly in high order using barycentric interpolation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that constants are interpolated correctly in high order using barycentric interpolation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(7); boost::random::uniform_real_distribution dis(0.1f, 1); @@ -152,7 +151,7 @@ void test_constant_high_order() template void test_runge() { - std::cout << "Testing interpolation of Runge's 1/(1+25x^2) function using barycentric interpolation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing interpolation of Runge's 1/(1+25x^2) function using barycentric interpolation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(8); boost::random::uniform_real_distribution dis(0.005f, 0.01f); @@ -205,7 +204,7 @@ void test_runge() template void test_prime_near_node() { - std::cout << "Testing the derivative a few ulps from a node on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing the derivative a few ulps from a node on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; // (r(x) - y_i)/(x - x_i) used to cancel catastrophically here: prime(x[7] + 1e-16) was -2. std::vector x(20); std::vector y(20); @@ -234,7 +233,7 @@ void test_prime_near_node() template void test_weights() { - std::cout << "Testing weights are calculated correctly using barycentric interpolation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing weights are calculated correctly using barycentric interpolation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(9); boost::random::uniform_real_distribution dis(0.005, 0.01); diff --git a/test/test_bivariate_statistics.cpp b/test/test_bivariate_statistics.cpp index 60682aabfe..e43587e3c2 100644 --- a/test/test_bivariate_statistics.cpp +++ b/test/test_bivariate_statistics.cpp @@ -16,7 +16,7 @@ #include #include #include "math_unit_test.hpp" -#include +#include #include #include #include @@ -30,7 +30,7 @@ using boost::multiprecision::cpp_complex_50; * Test checklist: * 1) Does it work with multiprecision? * 2) Does it work with .cbegin()/.cend() if the data is not altered? - * 3) Does it work with ublas and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) + * 3) Does it work with std::deque and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) * 4) Does it work with std::forward_list if a forward iterator is all that is required? * 5) Does it work with complex data if complex data is sensible? */ diff --git a/test/test_cardinal_cubic_b_spline.cpp b/test/test_cardinal_cubic_b_spline.cpp index 63cd0fa726..3654e8b985 100644 --- a/test/test_cardinal_cubic_b_spline.cpp +++ b/test/test_cardinal_cubic_b_spline.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -28,7 +27,7 @@ using boost::math::constants::half; template void test_b3_spline() { - std::cout << "Testing evaluation of spline basis functions on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing evaluation of spline basis functions on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; // Outside the support: Real eps = std::numeric_limits::epsilon(); CHECK_EQUAL(boost::math::interpolators::detail::b3_spline(Real(2.5)), Real(0)); @@ -71,7 +70,7 @@ template void test_interpolation_condition() { using std::sqrt; - std::cout << "Testing interpolation condition for cubic b splines on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing interpolation condition for cubic b splines on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::random_device rd; std::mt19937 gen(rd()); boost::random::uniform_real_distribution dis(1, 10); @@ -99,7 +98,7 @@ void test_interpolation_condition() template void test_constant_function() { - std::cout << "Testing that constants are interpolated correctly by cubic b splines on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that constants are interpolated correctly by cubic b splines on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::vector v(500); const Real constant = Real(50.2); for (size_t i = 0; i < v.size(); ++i) @@ -165,7 +164,7 @@ template void test_affine_function() { using std::sqrt; - std::cout << "Testing that affine functions are interpolated correctly by cubic b splines on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that affine functions are interpolated correctly by cubic b splines on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::vector v(500); Real a = 10; Real b = 8; @@ -206,7 +205,7 @@ template void test_quadratic_function() { using std::sqrt; - std::cout << "Testing that quadratic functions are interpolated correctly by cubic b splines on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that quadratic functions are interpolated correctly by cubic b splines on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::vector v(500); Real a = static_cast(1.2); Real b = static_cast(-3.4); @@ -236,7 +235,7 @@ template void test_circ_conic_function() { using std::sqrt; - std::cout << "Testing that conic section of a circle is interpolated correctly by cubic b splines on type " << boost::typeindex::type_id().pretty_name() << '\n'; + std::cout << "Testing that conic section of a circle is interpolated correctly by cubic b splines on type " << boost::math::test::detail::demangle(typeid(Real).name()) << '\n'; std::vector v(500); Real cv = Real(0.1); Real w = Real(2.0); @@ -273,7 +272,7 @@ void test_circ_conic_function() template void test_trig_function() { - std::cout << "Testing that sine functions are interpolated correctly by cubic b splines on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that sine functions are interpolated correctly by cubic b splines on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen; std::vector v(500); Real x0 = 1; diff --git a/test/test_catmull_rom.cpp b/test/test_catmull_rom.cpp index 3bc05e26bb..c986eed7df 100644 --- a/test/test_catmull_rom.cpp +++ b/test/test_catmull_rom.cpp @@ -7,12 +7,11 @@ #include #include #include -#include #include #include #include #include -#include +#include #include "math_unit_test.hpp" #if __has_include() @@ -54,7 +53,7 @@ template void test_linear() { std::cout << "Testing that the Catmull-Rom spline interpolates linear functions correctly on type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = 10*std::numeric_limits::epsilon(); std::vector> v(4); @@ -122,7 +121,7 @@ void test_circle() using std::sin; std::cout << "Testing that the Catmull-Rom spline interpolates circles correctly on type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = 10*std::numeric_limits::epsilon(); std::vector> v(20*sizeof(Real)); @@ -173,7 +172,7 @@ void test_affine_invariance() { std::cout << "Testing that the Catmull-Rom spline is affine invariant in dimension " << dimension << " on type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = 1000*std::numeric_limits::epsilon(); std::vector> v(100); @@ -237,7 +236,7 @@ void test_helix() { using boost::math::constants::pi; std::cout << "Testing that the Catmull-Rom spline interpolates helices correctly on type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = 0.001; std::vector> v(400*sizeof(Real)); @@ -381,7 +380,7 @@ void test_random_access_container() mypoint3d p4(0.5, 0.6, 0.7); mypoint3d p5(0.6, 0.7, 0.8); - boost::numeric::ublas::vector> u(6); + std::deque> u(6); u[0] = p0; u[1] = p1; u[2] = p2; diff --git a/test/test_chebyshev.cpp b/test/test_chebyshev.cpp index f3ffc2e7b8..66b69777d3 100644 --- a/test/test_chebyshev.cpp +++ b/test/test_chebyshev.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include #include @@ -22,7 +21,7 @@ using boost::math::chebyshev_u; template void test_polynomials() { - std::cout << "Testing explicit polynomial representations of the Chebyshev polynomials on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing explicit polynomial representations of the Chebyshev polynomials on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real x = -2; Real tol = 32*std::numeric_limits::epsilon(); @@ -52,7 +51,7 @@ void test_polynomials() template void test_derivatives() { - std::cout << "Testing explicit polynomial representations of the Chebyshev polynomial derivatives on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing explicit polynomial representations of the Chebyshev polynomial derivatives on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real x = -2; Real tol = 4*std::numeric_limits::epsilon(); diff --git a/test/test_chebyshev_transform.cpp b/test/test_chebyshev_transform.cpp index f81d33d05e..43bcd1234c 100644 --- a/test/test_chebyshev_transform.cpp +++ b/test/test_chebyshev_transform.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include diff --git a/test/test_cma_es.cpp b/test/test_cma_es.cpp index bdc3bde124..e1de352a27 100644 --- a/test/test_cma_es.cpp +++ b/test/test_cma_es.cpp @@ -191,7 +191,7 @@ void test_bounds() { // detail::symmetric_eigendecomposition is not exported from the module. template void check_symmetric_eigendecomposition(std::vector const & C, size_t n) { - using boost::math::optimization::detail::symmetric_eigendecomposition; + using boost::math::tools::detail::symmetric_eigendecomposition; std::vector B; std::vector D; CHECK_EQUAL(symmetric_eigendecomposition(C, n, B, D), true); diff --git a/test/test_exp_sinh_quadrature.cpp b/test/test_exp_sinh_quadrature.cpp index 1a93898608..faf130f85a 100644 --- a/test/test_exp_sinh_quadrature.cpp +++ b/test/test_exp_sinh_quadrature.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include "math_unit_test.hpp" #ifdef BOOST_HAS_FLOAT128 @@ -179,7 +178,7 @@ Real get_convergence_tolerance() template void test_right_limit_infinite() { - std::cout << "Testing right limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing right limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; Real error; @@ -235,7 +234,7 @@ void test_right_limit_infinite() template void test_left_limit_infinite() { - std::cout << "Testing left limit infinite for 1/(1+t^2) on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing left limit infinite for 1/(1+t^2) on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; Real error; @@ -273,7 +272,7 @@ void test_nr_examples() using std::pow; using std::exp; using std::sqrt; - std::cout << "Testing type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::cout << std::setprecision(std::numeric_limits::digits10); Real Q; Real Q_expected; @@ -360,7 +359,7 @@ void test_crc() using std::sqrt; using std::log; using std::cos; - std::cout << "Testing integral from CRC handbook on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integral from CRC handbook on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::cout << std::setprecision(std::numeric_limits::digits10); Real Q; Real Q_expected; @@ -492,7 +491,7 @@ void test_crc() template void test_complex_modified_bessel() { - std::cout << "Testing complex modified Bessel function on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing complex modified Bessel function on type " << boost::math::test::detail::demangle(typeid(Complex).name()) << "\n"; typedef typename Complex::value_type Real; Real error; Real L1; @@ -531,7 +530,7 @@ void test_complex_modified_bessel() template void test_complex_exponential_integral_E1(){ - std::cout << "Testing complex exponential integral on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing complex exponential integral on type " << boost::math::test::detail::demangle(typeid(Complex).name()) << "\n"; typedef typename Complex::value_type Real; Real error; Real L1; @@ -596,7 +595,7 @@ void test_non_central_t() template void test_compensated() { - std::cout << "Testing compensated summation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing compensated summation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; // Uncompensated, these are up to 4 and 5 ulps off: exp_sinh integrator(9, true); Real tol = 4 * std::numeric_limits::epsilon(); diff --git a/test/test_gauss_kronrod_quadrature.cpp b/test/test_gauss_kronrod_quadrature.cpp index bcf62bcbe1..570ae2e7f7 100644 --- a/test/test_gauss_kronrod_quadrature.cpp +++ b/test/test_gauss_kronrod_quadrature.cpp @@ -11,7 +11,6 @@ #if !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_SFINAE_EXPR) #include -#include #include #include #include @@ -255,7 +254,7 @@ double expected_error<61>(unsigned id) template void test_linear() { - std::cout << "Testing linear functions are integrated properly by gauss_kronrod on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing linear functions are integrated properly by gauss_kronrod on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real error; auto f = [](const Real& x)->Real { @@ -277,7 +276,7 @@ void test_linear() template void test_quadratic() { - std::cout << "Testing quadratic functions are integrated properly by Gauss Kronrod on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing quadratic functions are integrated properly by Gauss Kronrod on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real error; auto f = [](const Real& x)->Real { return 5*x*x + 7*x + 12; }; @@ -292,7 +291,7 @@ void test_quadratic() template void test_ca() { - std::cout << "Testing integration of C(a) on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integration of C(a) on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_ca_error_id); Real L1; Real error; @@ -319,7 +318,7 @@ void test_ca() template void test_three_quadrature_schemes_examples() { - std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_three_quad_error_id); Real Q; Real Q_expected; @@ -367,7 +366,7 @@ void test_three_quadrature_schemes_examples() template void test_integration_over_real_line() { - std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_integration_over_real_line_error_id); Real Q; Real Q_expected; @@ -384,7 +383,7 @@ void test_integration_over_real_line() template void test_right_limit_infinite() { - std::cout << "Testing right limit infinite for Gauss Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing right limit infinite for Gauss Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_right_limit_infinite_error_id); Real Q; Real Q_expected; @@ -406,7 +405,7 @@ void test_right_limit_infinite() template void test_left_limit_infinite() { - std::cout << "Testing left limit infinite for Gauss Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing left limit infinite for Gauss Kronrod in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_left_limit_infinite_error_id); Real Q; Real Q_expected; @@ -421,7 +420,7 @@ void test_left_limit_infinite() template void test_complex_lambert_w() { - std::cout << "Testing that complex-valued integrands are integrated correctly by Gaussian quadrature on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that complex-valued integrands are integrated correctly by Gaussian quadrature on type " << boost::math::test::detail::demangle(typeid(Complex).name()) << "\n"; typedef typename Complex::value_type Real; Real tol = 10e-9; using boost::math::constants::pi; diff --git a/test/test_gauss_quadrature.cpp b/test/test_gauss_quadrature.cpp index d55e160566..97b926b79c 100644 --- a/test/test_gauss_quadrature.cpp +++ b/test/test_gauss_quadrature.cpp @@ -16,7 +16,6 @@ #include #include #include -#include #include "math_unit_test.hpp" #ifdef BOOST_HAS_FLOAT128 @@ -251,7 +250,7 @@ double expected_error<30>(unsigned id) template void test_linear() { - std::cout << "Testing linear functions are integrated properly by gauss on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing linear functions are integrated properly by gauss on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto f = [](const Real& x) { return 5*x + 7; @@ -269,7 +268,7 @@ void test_linear() template void test_quadratic() { - std::cout << "Testing quadratic functions are integrated properly by Gaussian quadrature on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing quadratic functions are integrated properly by Gaussian quadrature on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto f = [](const Real& x) { return 5*x*x + 7*x + 12; }; Real L1; @@ -283,7 +282,7 @@ void test_quadratic() template void test_ca() { - std::cout << "Testing integration of C(a) on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integration of C(a) on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_ca_error_id); Real L1; @@ -314,7 +313,7 @@ void test_ca() template void test_three_quadrature_schemes_examples() { - std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_three_quad_error_id); Real Q; Real Q_expected; @@ -362,7 +361,7 @@ void test_three_quadrature_schemes_examples() template void test_integration_over_real_line() { - std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_integration_over_real_line_error_id); Real Q; Real Q_expected; @@ -378,7 +377,7 @@ void test_integration_over_real_line() template void test_right_limit_infinite() { - std::cout << "Testing right limit infinite for Gaussian quadrature in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing right limit infinite for Gaussian quadrature in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_right_limit_infinite_error_id); Real Q; Real Q_expected; @@ -399,7 +398,7 @@ void test_right_limit_infinite() template void test_left_limit_infinite() { - std::cout << "Testing left limit infinite for Gaussian quadrature in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing left limit infinite for Gaussian quadrature in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = expected_error(test_left_limit_infinite_error_id); Real Q; Real Q_expected; @@ -415,7 +414,7 @@ template void test_complex_lambert_w() { #ifndef BOOST_MATH_STANDALONE - std::cout << "Testing that complex-valued integrands are integrated correctly by Gaussian quadrature on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that complex-valued integrands are integrated correctly by Gaussian quadrature on type " << boost::math::test::detail::demangle(typeid(Complex).name()) << "\n"; typedef typename Complex::value_type Real; Real tol = 10e-9; using boost::math::constants::pi; diff --git a/test/test_gaussian_process.cpp b/test/test_gaussian_process.cpp index 018722f370..c8688ed01d 100644 --- a/test/test_gaussian_process.cpp +++ b/test/test_gaussian_process.cpp @@ -19,9 +19,9 @@ import boost.math; #include #include #endif -using boost::math::optimization::detail::cholesky; -using boost::math::optimization::detail::cholesky_solve; -using boost::math::optimization::detail::forward_solve; +using boost::math::tools::detail::cholesky; +using boost::math::tools::detail::cholesky_solve; +using boost::math::tools::detail::forward_solve; using boost::math::optimization::detail::gaussian_process; using boost::math::optimization::detail::gaussian_process_kernel; using boost::math::optimization::detail::gaussian_process_parameters; diff --git a/test/test_lanczos_smoothing.cpp b/test/test_lanczos_smoothing.cpp index 8b2f41d3ef..2790406cf7 100644 --- a/test/test_lanczos_smoothing.cpp +++ b/test/test_lanczos_smoothing.cpp @@ -6,7 +6,7 @@ */ #include #include -#include +#include #include #include #include @@ -634,8 +634,8 @@ void test_data_representations() CHECK_ULP_CLOSE(9, dvdt[i], 300); } - boost::numeric::ublas::vector w(500); - boost::numeric::ublas::vector dwdt(500); + std::deque w(500); + std::deque dwdt(500); for(size_t i = 0; i < w.size(); ++i) { w[i] = 9*i + 6; diff --git a/test/test_legendre.hpp b/test/test_legendre.hpp index e8563bf556..fd73299093 100644 --- a/test/test_legendre.hpp +++ b/test/test_legendre.hpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include "functor.hpp" #include "handle_test_result.hpp" @@ -318,7 +318,7 @@ void test_legendre_p_prime() template void test_legendre_p_zeros() { - std::cout << "Testing Legendre zeros on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing Legendre zeros on type " << boost::core::demangle(typeid(Real).name()) << "\n"; using std::sqrt; using std::abs; using boost::math::legendre_p_zeros; diff --git a/test/test_linear_algebra_detail.cpp b/test/test_linear_algebra_detail.cpp new file mode 100644 index 0000000000..89a9c70792 --- /dev/null +++ b/test/test_linear_algebra_detail.cpp @@ -0,0 +1,227 @@ +// Copyright Nick Thompson 2026. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include "math_unit_test.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace la = boost::math::tools::detail; + +// Entries uniform in [-1, 1], plus shift on the diagonal; n x n, row-major. +template +std::vector random_matrix(std::size_t n, Real shift, unsigned seed) +{ + std::mt19937_64 gen(seed); + std::uniform_real_distribution u(-1, 1); + std::vector A(n * n); + for (Real& a : A) + a = static_cast(u(gen)); + for (std::size_t i = 0; i < n; ++i) + A[i * n + i] += shift; + return A; +} + +// A B, or A B^T, for n x n matrices. +template +std::vector mul(std::vector const& A, std::vector const& B, std::size_t n, bool transpose_b = false) +{ + std::vector C(n * n, Real(0)); + for (std::size_t i = 0; i < n; ++i) + for (std::size_t j = 0; j < n; ++j) + for (std::size_t k = 0; k < n; ++k) + C[i * n + j] += A[i * n + k] * (transpose_b ? B[j * n + k] : B[k * n + j]); + return C; +} + +// max |A - B|, where only the lower triangles are compared if lower is set, and B = I if it is empty. +template +Real distance(std::vector const& A, std::vector const& B, std::size_t n, bool lower = false) +{ + using std::abs; + Real d = 0; + for (std::size_t i = 0; i < n; ++i) + for (std::size_t j = 0; j < (lower ? i + 1 : n); ++j) + d = (std::max)(d, Real(abs(A[i * n + j] - (B.empty() ? Real(i == j) : B[i * n + j])))); + return d; +} + +template +void test_lu() +{ + std::size_t const n = 12; + Real const eps = std::numeric_limits::epsilon(); + std::vector const A = random_matrix(n, Real(n), 1); + std::vector x(n), b(n, Real(0)), lu = A, colmax(n); + std::vector piv(n); + for (std::size_t i = 0; i < n; ++i) + { + x[i] = Real(i) - 3; + for (std::size_t j = 0; j < n; ++j) + b[i] += A[i * n + j] * (Real(j) - 3); + } + CHECK_TRUE(la::lu_factor(lu, piv, colmax, n)); + la::lu_solve(lu, piv, n, b); + for (std::size_t i = 0; i < n; ++i) + CHECK_LE(std::abs(b[i] - x[i]), 64 * eps); + + // A zero column is refused whatever eps is; a pivot of 2 eps only by the default: + lu = A; + for (std::size_t i = 0; i < n; ++i) + lu[i * n + 5] = 0; + std::vector singular = lu; + CHECK_TRUE(!la::lu_factor(lu, piv, colmax, n)); + CHECK_TRUE(!la::lu_factor(singular, piv, colmax, n, Real(0))); + std::vector near{1, 1, 1, 1 + 2 * eps}; + lu = near; + CHECK_TRUE(!la::lu_factor(lu, piv, colmax, 2)); + CHECK_TRUE(la::lu_factor(near, piv, colmax, 2, Real(0))); + std::vector c{2, 2 + 2 * eps}; + la::lu_solve(near, piv, 2, c); + CHECK_ULP_CLOSE(Real(1), c[0], 0); + CHECK_ULP_CLOSE(Real(1), c[1], 0); +} + +template +void test_gauss_jordan() +{ + std::size_t const n = 12; + Real const eps = std::numeric_limits::epsilon(); + std::vector const A = random_matrix(n, Real(n), 2); + std::vector a = A, X(n * n); + CHECK_TRUE(la::gauss_jordan_inverse(a.data(), X.data(), n)); + CHECK_LE(distance(mul(A, X, n), std::vector(), n), 64 * eps); + a = A; + for (std::size_t i = 0; i < n; ++i) + a[i * n + 5] = 0; + CHECK_TRUE(!la::gauss_jordan_inverse(a.data(), X.data(), n)); +} + +// n = 150 has more than two blocks of the factorization. +template +void test_cholesky(std::size_t n) +{ + Real const eps = std::numeric_limits::epsilon(); + std::vector const B = random_matrix(n, Real(0), 3); + std::vector A = mul(B, B, n, true); + for (std::size_t i = 0; i < n; ++i) + A[i * n + i] += Real(n); + std::vector L = A; + CHECK_TRUE(la::cholesky(L.data(), n)); + for (std::size_t i = 0; i < n; ++i) + for (std::size_t j = i + 1; j < n; ++j) + L[i * n + j] = 0; + CHECK_LE(distance(mul(L, L, n, true), A, n), 16 * Real(n) * eps); + + // Rows that are those of L already are kept: + std::vector partial = A; + std::copy(L.begin(), L.begin() + static_cast((n / 2) * n), partial.begin()); + CHECK_TRUE(la::cholesky(partial.data(), n, n / 2)); + CHECK_LE(distance(partial, L, n, true), Real(0)); + + std::vector x(n), b(n, Real(0)); + for (std::size_t i = 0; i < n; ++i) + { + x[i] = Real(i % 7) - 3; + for (std::size_t j = 0; j < n; ++j) + b[i] += A[i * n + j] * (Real(j % 7) - 3); + } + std::vector y = b; + la::cholesky_solve(L.data(), n, y.data()); + for (std::size_t i = 0; i < n; ++i) + CHECK_LE(std::abs(y[i] - x[i]), 64 * eps); + + // L^{-1} A = L^T, by the solve with several right-hand sides: + std::vector Lt = A; + la::forward_solve(L.data(), n, Lt.data(), n); + for (std::size_t i = 0; i < n; ++i) + for (std::size_t j = i; j < n; ++j) + CHECK_LE(std::abs(Lt[i * n + j] - L[j * n + i]), 512 * eps); + + std::vector Z(n * n), Ainv(n * n); + la::cholesky_inverse(L.data(), n, Z.data(), Ainv.data()); + for (std::size_t i = 0; i < n; ++i) + for (std::size_t j = i + 1; j < n; ++j) + Ainv[i * n + j] = Ainv[j * n + i]; + CHECK_LE(distance(mul(A, Ainv, n), std::vector(), n), 64 * eps); + + // Not positive definite from column n - 2 on: the pivot is reported, and the rows before it are those of L. + std::vector indefinite = A; + indefinite[(n - 2) * n + n - 2] -= 2 * Real(n); + std::size_t failed = 0; + CHECK_TRUE(!la::cholesky(indefinite.data(), n, 0, &failed)); + CHECK_EQUAL(failed, n - 2); + CHECK_LE(indefinite[failed * n + failed], Real(0)); + CHECK_ULP_CLOSE(L[(n - 3) * n + n - 3], indefinite[(n - 3) * n + n - 3], 0); + CHECK_ULP_CLOSE(L[(n - 2) * n + n - 3], indefinite[(n - 2) * n + n - 3], 0); +} + +template +void test_symmetric_eigen() +{ + std::size_t const n = 12; + Real const eps = std::numeric_limits::epsilon(); + std::vector const B = random_matrix(n, Real(0), 4); + std::vector const A = mul(B, B, n, true); + std::vector V, d; + CHECK_TRUE(la::symmetric_eigendecomposition(A, n, V, d)); + std::vector VD = V; + for (std::size_t i = 0; i < n; ++i) + for (std::size_t j = 0; j < n; ++j) + VD[i * n + j] *= d[j]; + CHECK_LE(distance(mul(VD, V, n, true), A, n), 64 * Real(n) * eps); + CHECK_LE(distance(mul(V, V, n, true), std::vector(), n), 64 * eps); +} + +template +void test_householder_qr(bool pivot) +{ + std::size_t const n = 12; + Real const eps = std::numeric_limits::epsilon(); + std::vector const A = random_matrix(n, Real(0), 5); + std::vector R = A, Q(n * n); + la::householder_qr(R.data(), n, n, n, pivot, Q.data()); + CHECK_LE(distance(mul(Q, Q, n, true), std::vector(), n), 64 * eps); + for (std::size_t i = 0; i < n; ++i) + for (std::size_t j = 0; j < i; ++j) + R[i * n + j] = 0; + if (!pivot) + CHECK_LE(distance(mul(Q, R, n), A, n), 64 * eps); + else + for (std::size_t i = 0; i + 1 < n; ++i) + CHECK_LE(std::abs(R[(i + 1) * n + i + 1]), std::abs(R[i * n + i]) * (1 + 64 * eps)); + // Without Q, the same R: + std::vector R2 = A; + la::householder_qr(R2.data(), n, n, n, pivot, static_cast(nullptr)); + for (std::size_t i = 0; i < n; ++i) + for (std::size_t j = i; j < n; ++j) + CHECK_ULP_CLOSE(R[i * n + j], R2[i * n + j], 0); +} + +template +void test_all() +{ + test_lu(); + test_gauss_jordan(); + test_cholesky(12); + test_cholesky(150); + test_symmetric_eigen(); + test_householder_qr(false); + test_householder_qr(true); +} + +int main() +{ + test_all(); + test_all(); + return boost::math::test::report_errors(); +} diff --git a/test/test_naive_monte_carlo.cpp b/test/test_naive_monte_carlo.cpp index ee626a264f..a73b90ba4a 100644 --- a/test/test_naive_monte_carlo.cpp +++ b/test/test_naive_monte_carlo.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -28,7 +27,7 @@ using boost::math::quadrature::naive_monte_carlo; template void test_pi_multithreaded() { - std::cout << "Testing pi is calculated correctly (multithreaded) using Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing pi is calculated correctly (multithreaded) using Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [](std::vector const & x)->Real { Real r = x[0]*x[0]+x[1]*x[1]; if (r <= 1) { @@ -54,7 +53,7 @@ void test_pi_multithreaded() template void test_pi() { - std::cout << "Testing pi is calculated correctly using Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing pi is calculated correctly using Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [](std::vector const & x)->Real { Real r = x[0]*x[0]+x[1]*x[1]; @@ -84,7 +83,7 @@ void test_pi() template void test_constant() { - std::cout << "Testing constants are integrated correctly using Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing constants are integrated correctly using Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [](std::vector const &)->Real { return 1; @@ -105,7 +104,7 @@ void test_constant() template void test_cancel_and_restart() { - std::cout << "Testing that cancellation and restarting works on naive Monte-Carlo integration on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that cancellation and restarting works on naive Monte-Carlo integration on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real exact = BOOST_MATH_TEST_VALUE(Real, 1.3932039296856768591842462603255); constexpr const Real A = 1.0 / (pi() * pi() * pi()); auto g = [&](std::vector const & x)->Real @@ -141,7 +140,7 @@ void test_zero_threads_and_bad_bounds() template void test_finite_singular_boundary() { - std::cout << "Testing that finite singular boundaries work on naive Monte-Carlo integration on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that finite singular boundaries work on naive Monte-Carlo integration on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; using std::pow; using std::log; using std::log1p; @@ -164,7 +163,7 @@ void test_finite_singular_boundary() template void test_multithreaded_variance() { - std::cout << "Testing that variance computed by naive Monte-Carlo integration converges to integral formula on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that variance computed by naive Monte-Carlo integration converges to integral formula on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real exact_variance = (Real) 1/(Real) 12; auto g = [&](std::vector const & x)->Real { @@ -182,7 +181,7 @@ void test_multithreaded_variance() template void test_variance() { - std::cout << "Testing that variance computed by naive Monte-Carlo integration converges to integral formula on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that variance computed by naive Monte-Carlo integration converges to integral formula on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real exact_variance = (Real) 1/(Real) 12; auto g = [&](std::vector const & x)->Real { @@ -200,7 +199,7 @@ void test_variance() template void test_product() { - std::cout << "Testing that product functions are integrated correctly by naive Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that product functions are integrated correctly by naive Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [&](std::vector const & x)->Real { Real y = 1; @@ -229,7 +228,7 @@ void test_product() template void test_alternative_rng_1() { - std::cout << "Testing that alternative RNGs work correctly using naive Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that alternative RNGs work correctly using naive Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [&](std::vector const & x)->Real { Real y = 1; @@ -272,7 +271,7 @@ void test_alternative_rng_1() template void test_alternative_rng_2() { - std::cout << "Testing that alternative RNGs work correctly using naive Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that alternative RNGs work correctly using naive Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [&](std::vector const & x)->Real { Real y = 1; @@ -312,7 +311,7 @@ void test_alternative_rng_2() template void test_upper_bound_infinite() { - std::cout << "Testing that infinite upper bounds are integrated correctly by naive Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that infinite upper bounds are integrated correctly by naive Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [](std::vector const & x)->Real { return 1.0/(x[0]*x[0] + 1.0); @@ -332,7 +331,7 @@ void test_upper_bound_infinite() template void test_lower_bound_infinite() { - std::cout << "Testing that infinite lower bounds are integrated correctly by naive Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that infinite lower bounds are integrated correctly by naive Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [](std::vector const & x)->Real { return 1.0/(x[0]*x[0] + 1.0); @@ -353,7 +352,7 @@ void test_lower_bound_infinite() template void test_lower_bound_infinite2() { - std::cout << "Testing that infinite lower bounds (2) are integrated correctly by naive Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that infinite lower bounds (2) are integrated correctly by naive Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [](std::vector const & x)->Real { // If x[0] = inf, this should blow up: @@ -374,7 +373,7 @@ void test_lower_bound_infinite2() template void test_double_infinite() { - std::cout << "Testing that double infinite bounds are integrated correctly by naive Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that double infinite bounds are integrated correctly by naive Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [](std::vector const & x)->Real { return 1.0/(x[0]*x[0] + 1.0); @@ -396,7 +395,7 @@ template void test_radovic() { // See: Generalized Halton Sequences in 2008: A Comparative Study, function g1: - std::cout << "Testing that the Radovic function is integrated correctly by naive Monte-Carlo on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that the Radovic function is integrated correctly by naive Monte-Carlo on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto g = [](std::vector const & x)->Real { using std::abs; diff --git a/test/test_norms.cpp b/test/test_norms.cpp index 8554bfd5a0..715ce0452b 100644 --- a/test/test_norms.cpp +++ b/test/test_norms.cpp @@ -12,7 +12,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -37,7 +38,7 @@ using boost::math::tools::total_variation; * Test checklist: * 1) Does it work with multiprecision? * 2) Does it work with .cbegin()/.cend() if the data is not altered? - * 3) Does it work with ublas and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) + * 3) Does it work with std::deque and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) * 4) Does it work with std::forward_list if a forward iterator is all that is required? * 5) Does it work with complex data if complex data is sensible? */ @@ -133,9 +134,9 @@ void test_lp() Real expected = 7*pow(v.size(), static_cast(1)/static_cast(8)); BOOST_TEST(abs(l8 - expected) < tol*abs(expected)); - // Does it work with ublas vectors? + // Does it work with std::deque? // Does it handle the overflow of intermediates? - boost::numeric::ublas::vector w(4); + std::deque w(4); Real bignum = sqrt((std::numeric_limits::max)())/256; for (size_t i = 0; i < w.size(); ++i) { diff --git a/test/test_numerical_differentiation.cpp b/test/test_numerical_differentiation.cpp index 64e84ce956..44f0f62886 100644 --- a/test/test_numerical_differentiation.cpp +++ b/test/test_numerical_differentiation.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -28,7 +27,7 @@ using boost::math::constants::half; template void test_order(size_t points_to_test) { - std::cout << "Testing order " << order << " derivative error estimate on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing order " << order << " derivative error estimate on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::cout << std::setprecision(std::numeric_limits::digits10); //std::cout << std::fixed << std::scientific; auto f = [](Real t) { return boost::math::cyl_bessel_j(1, t); }; @@ -87,7 +86,7 @@ void test_order(size_t points_to_test) template void test_bessel() { - std::cout << "Testing numerical derivatives of Bessel's function on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing numerical derivatives of Bessel's function on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::cout << std::setprecision(std::numeric_limits::digits10); Real eps = std::numeric_limits::epsilon(); @@ -186,7 +185,7 @@ void test_complex_step() using std::complex; using std::isfinite; using std::isnormal; - std::cout << "Testing numerical derivatives of Bessel's function on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing numerical derivatives of Bessel's function on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::cout << std::setprecision(std::numeric_limits::digits10); Real x = -100; while ( x < 100 ) diff --git a/test/test_ooura_fourier_integral.cpp b/test/test_ooura_fourier_integral.cpp index 55f9694e94..59affd0deb 100644 --- a/test/test_ooura_fourier_integral.cpp +++ b/test/test_ooura_fourier_integral.cpp @@ -5,7 +5,6 @@ // or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include -#include #include #include #include "math_unit_test.hpp" @@ -59,7 +58,7 @@ template void test_ooura_eta() { using boost::math::quadrature::detail::ooura_eta; - std::cout << "Testing eta function on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing eta function on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; { Real x = 0; Real alpha = 7; @@ -85,7 +84,7 @@ void test_ooura_sin_nodes_and_weights() using boost::math::quadrature::detail::ooura_sin_node_and_weight; using boost::math::quadrature::detail::ooura_eta; using std::exp; - std::cout << "Testing nodes and weights on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing nodes and weights on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; { long n = 1; Real alpha = 1; @@ -98,7 +97,7 @@ void test_ooura_sin_nodes_and_weights() template void test_ooura_alpha() { - std::cout << "Testing Ooura alpha on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing Ooura alpha on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; using std::sqrt; using std::log1p; using boost::math::quadrature::detail::calculate_ooura_alpha; @@ -144,7 +143,7 @@ void test_node_weight_precision_agreement() template void test_sinc() { - std::cout << "Testing sinc integral on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing sinc integral on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; using std::numeric_limits; auto integrator = get_sin_integrator(); auto f = [](Real x)->Real { return 1/x; }; @@ -164,7 +163,7 @@ void test_sinc() template void test_exp() { - std::cout << "Testing exponential integral on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing exponential integral on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; using std::exp; using std::numeric_limits; auto integrator = get_sin_integrator(); @@ -183,7 +182,7 @@ void test_exp() template void test_root() { - std::cout << "Testing integral of sin(kx)/sqrt(x) on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integral of sin(kx)/sqrt(x) on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; using std::sqrt; using std::numeric_limits; auto integrator = get_sin_integrator(); @@ -211,7 +210,7 @@ Real asymptotic(Real lambda) { template void test_double_osc() { - std::cout << "Testing double oscillation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing double oscillation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; using std::sqrt; using std::numeric_limits; auto integrator = get_sin_integrator(); @@ -227,7 +226,7 @@ template void test_zero_integrand() { // Make sure relative error tolerance doesn't break on zero integrand: - std::cout << "Testing zero integrand on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing zero integrand on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; using std::sqrt; using std::numeric_limits; auto integrator = get_sin_integrator(); @@ -243,7 +242,7 @@ void test_zero_integrand() // template // void test_log() // { -// std::cout << "Testing integral of log(x)sin(x) on type " << boost::typeindex::type_id().pretty_name() << "\n"; +// std::cout << "Testing integral of log(x)sin(x) on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; // using std::log; // using std::exp; // using std::numeric_limits; @@ -259,7 +258,7 @@ void test_zero_integrand() template void test_cos_integral1() { - std::cout << "Testing integral of cos(x)/(x*x+1) on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integral of cos(x)/(x*x+1) on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; using std::exp; using boost::math::constants::half_pi; using boost::math::constants::e; @@ -276,7 +275,7 @@ void test_cos_integral1() template void test_cos_integral2() { - std::cout << "Testing integral of exp(-a*x) on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integral of exp(-a*x) on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; using std::exp; using boost::math::constants::half_pi; using boost::math::constants::e; @@ -296,7 +295,7 @@ void test_cos_integral2() template void test_nodes() { - std::cout << "Testing nodes and weights on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing nodes and weights on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto sin_integrator = get_sin_integrator(); auto const & big_nodes = sin_integrator.big_nodes(); diff --git a/test/test_remez.cpp b/test/test_remez.cpp index caa6441972..32e5d3a3eb 100644 --- a/test/test_remez.cpp +++ b/test/test_remez.cpp @@ -10,7 +10,7 @@ # pragma warning(disable : 4267) // conversion from 'size_t' to 'const unsigned int', possible loss of data # pragma warning(disable : 4180) // qualifier applied to function type has no meaning; ignored # pragma warning(disable : 4224) // nonstandard extension used : formal parameter 'function_ptr' was previously defined as a type -# pragma warning(disable : 4100) // unreferenced formal parameter (in ublas/functional) +# pragma warning(disable : 4100) // unreferenced formal parameter #endif #include diff --git a/test/test_roots.cpp b/test/test_roots.cpp index a3a23643f9..a909965af2 100644 --- a/test/test_roots.cpp +++ b/test/test_roots.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include "table_type.hpp" #include #include @@ -448,7 +448,7 @@ template void test_complex_newton() { typedef typename ComplexType::value_type Real; - std::cout << "Testing complex Newton's Method on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing complex Newton's Method on type " << boost::core::demangle(typeid(Real).name()) << "\n"; using std::abs; using std::sqrt; using boost::math::tools::complex_newton; diff --git a/test/test_signal_statistics.cpp b/test/test_signal_statistics.cpp index f935dadb51..4bfa918320 100644 --- a/test/test_signal_statistics.cpp +++ b/test/test_signal_statistics.cpp @@ -16,7 +16,7 @@ #include #endif #include -#include +#include #include #include #include @@ -32,7 +32,7 @@ using boost::math::constants::two_pi; * Test checklist: * 1) Does it work with multiprecision? * 2) Does it work with .cbegin()/.cend() if the data is not altered? - * 3) Does it work with ublas and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) + * 3) Does it work with std::deque and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) * 4) Does it work with std::forward_list if a forward iterator is all that is required? * 5) Does it work with complex data if complex data is sensible? * 6) Does it work with integer data if sensible? @@ -83,8 +83,8 @@ void test_hoyer_sparsity() hs = boost::math::statistics::hoyer_sparsity(u1); BOOST_TEST(abs(hs) < tol); - // Does it work with a boost ublas vector? - boost::numeric::ublas::vector u2(3); + // Does it work with a std::deque? + std::deque u2(3); u2[0] = 1; u2[1] = 1; u2[2] = 1; diff --git a/test/test_sinh_sinh_quadrature.cpp b/test/test_sinh_sinh_quadrature.cpp index 43b39dc894..5e0506305d 100644 --- a/test/test_sinh_sinh_quadrature.cpp +++ b/test/test_sinh_sinh_quadrature.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include "math_unit_test.hpp" #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900) @@ -137,7 +136,7 @@ std::pair>, std::vector> > gener template void test_nr_examples() { - std::cout << "Testing type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real integration_limit = sqrt(boost::math::tools::epsilon()); std::cout << std::setprecision(std::numeric_limits::digits10); Real Q; @@ -191,7 +190,7 @@ void test_nr_examples() template void test_crc() { - std::cout << "Testing CRC formulas on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing CRC formulas on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real integration_limit = sqrt(boost::math::tools::epsilon()); std::cout << std::setprecision(std::numeric_limits::digits10); Real Q; @@ -231,7 +230,7 @@ template void test_dirichlet_eta() { typedef typename Complex::value_type Real; - std::cout << "Testing Dirichlet eta function on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing Dirichlet eta function on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Complex Q; const sinh_sinh integrator(10); diff --git a/test/test_tanh_sinh_quadrature.cpp b/test/test_tanh_sinh_quadrature.cpp index d81060e420..b57d3be749 100644 --- a/test/test_tanh_sinh_quadrature.cpp +++ b/test/test_tanh_sinh_quadrature.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #if __has_include() # include @@ -219,7 +218,7 @@ Real get_convergence_tolerance() template void test_linear() { - std::cout << "Testing linear functions are integrated properly by tanh_sinh on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing linear functions are integrated properly by tanh_sinh on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto integrator = get_integrator(); auto f = [](const Real& x) { @@ -241,7 +240,7 @@ void test_linear() template void test_quadratic() { - std::cout << "Testing quadratic functions are integrated properly by tanh_sinh on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing quadratic functions are integrated properly by tanh_sinh on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto integrator = get_integrator(); auto f = [](const Real& x) { return 5*x*x + 7*x + 12; }; Real error; @@ -255,7 +254,7 @@ void test_quadratic() template void test_singular() { - std::cout << "Testing integration of endpoint singularities on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integration of endpoint singularities on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real error; Real L1; auto integrator = get_integrator(); @@ -273,7 +272,7 @@ void test_singular() template void test_ca() { - std::cout << "Testing integration of C(a) on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integration of C(a) on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real tol = 10 * boost::math::tools::epsilon(); Real error; Real L1; @@ -337,7 +336,7 @@ void test_ca() template void test_three_quadrature_schemes_examples() { - std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integral in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; @@ -384,7 +383,7 @@ void test_three_quadrature_schemes_examples() template void test_integration_over_real_line() { - std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing integrals over entire real line in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; Real error; @@ -422,7 +421,7 @@ void test_integration_over_real_line() template void test_right_limit_infinite() { - std::cout << "Testing right limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing right limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; Real error; @@ -455,7 +454,7 @@ void test_right_limit_infinite() template void test_left_limit_infinite() { - std::cout << "Testing left limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing left limit infinite for tanh_sinh in 'A Comparison of Three High Precision Quadrature Schemes' on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; const auto& integrator = get_integrator(); @@ -477,7 +476,7 @@ void test_horrible() BOOST_IF_CONSTEXPR (std::is_fundamental::value) #endif { - std::cout << "Testing Trefenthen's horrible integral on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing Trefenthen's horrible integral on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; // We only know the integral to double precision, so requesting a higher tolerance doesn't make sense. Real Q; Real Q_expected; @@ -500,7 +499,7 @@ void test_horrible() template void test_nr_examples() { - std::cout << "Testing singular integrals from NR 4.5.4 on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing singular integrals from NR 4.5.4 on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; Real error; @@ -526,7 +525,7 @@ void test_nr_examples() template void test_early_termination() { - std::cout << "Testing Clenshaw & Curtis's example of integrand which fools termination schemes on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing Clenshaw & Curtis's example of integrand which fools termination schemes on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; Real error; @@ -546,7 +545,7 @@ void test_early_termination() template void test_crc() { - std::cout << "Testing CRC formulas on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing CRC formulas on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real Q; Real Q_expected; Real error; @@ -712,7 +711,7 @@ void test_sf() { using std::sqrt; // Test some special functions that we already know how to evaluate: - std::cout << "Testing special functions on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing special functions on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; const auto& integrator = get_integrator(); // incomplete beta: @@ -756,7 +755,7 @@ template void test_2_arg() { BOOST_MATH_STD_USING - std::cout << "Testing 2 argument functors on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing 2 argument functors on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; const auto& integrator = get_integrator(); diff --git a/test/test_trapezoidal.cpp b/test/test_trapezoidal.cpp index 0b34533da2..37ff72a3f7 100644 --- a/test/test_trapezoidal.cpp +++ b/test/test_trapezoidal.cpp @@ -6,7 +6,6 @@ */ #include #include -#include //#include #include #include @@ -33,7 +32,7 @@ using boost::math::quadrature::trapezoidal; template void test_complex_bessel() { - std::cout << "Testing that complex-valued integrands are integrated correctly by the adaptive trapezoidal routine on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that complex-valued integrands are integrated correctly by the adaptive trapezoidal routine on type " << boost::math::test::detail::demangle(typeid(Complex).name()) << "\n"; typedef typename Complex::value_type Real; Complex z{2, 3}; int n = 2; @@ -65,7 +64,7 @@ void test_complex_bessel() template void test_I0_complex() { - std::cout << "Testing that complex-argument I0 is calculated correctly by the adaptive trapezoidal routine on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that complex-argument I0 is calculated correctly by the adaptive trapezoidal routine on type " << boost::math::test::detail::demangle(typeid(Complex).name()) << "\n"; typedef typename Complex::value_type Real; Complex z{2, 3}; using boost::math::constants::pi; @@ -94,7 +93,7 @@ void test_I0_complex() template void test_erfc() { - std::cout << "Testing that complex-argument erfc is calculated correctly by the adaptive trapezoidal routine on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that complex-argument erfc is calculated correctly by the adaptive trapezoidal routine on type " << boost::math::test::detail::demangle(typeid(Complex).name()) << "\n"; typedef typename Complex::value_type Real; Complex z{2, -1}; using boost::math::constants::pi; @@ -126,7 +125,7 @@ void test_erfc() template void test_constant() { - std::cout << "Testing constants are integrated correctly by the adaptive trapezoidal routine on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing constants are integrated correctly by the adaptive trapezoidal routine on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto f = [](Real)->Real { return boost::math::constants::half(); }; Real Q = trapezoidal(f, static_cast(0.0), static_cast(10.0)); @@ -144,7 +143,7 @@ void test_rational_periodic() { using boost::math::constants::two_pi; using boost::math::constants::third; - std::cout << "Testing that rational periodic functions are integrated correctly by trapezoidal rule on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that rational periodic functions are integrated correctly by trapezoidal rule on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto f = [](Real x)->Real { using std::cos; return 1 / (5 - 4 * cos(x)); }; @@ -157,7 +156,7 @@ void test_rational_periodic() template void test_bump_function() { - std::cout << "Testing that bump functions are integrated correctly by trapezoidal rule on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that bump functions are integrated correctly by trapezoidal rule on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto f = [](Real x)->Real { if( x>= 1 || x <= -1) { @@ -176,7 +175,7 @@ void test_bump_function() template void test_zero_function() { - std::cout << "Testing that zero functions are integrated correctly by trapezoidal rule on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that zero functions are integrated correctly by trapezoidal rule on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto f = [](Real)->Real { return (Real) 0;}; Real tol = 100* boost::math::tools::epsilon(); Real Q = trapezoidal(f, (Real) -1, (Real) 1, tol); @@ -186,7 +185,7 @@ void test_zero_function() template void test_sinsq() { - std::cout << "Testing that sin(x)^2 is integrated correctly by the trapezoidal rule on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that sin(x)^2 is integrated correctly by the trapezoidal rule on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; auto f = [](Real x)->Real { using std::sin; return sin(10 * x) * sin(10 * x); }; Real tol = 100* boost::math::tools::epsilon(); Real Q = trapezoidal(f, (Real) 0, (Real) boost::math::constants::pi(), tol); @@ -199,7 +198,7 @@ void test_slowly_converging() { using std::sqrt; using std::abs; - std::cout << "Testing that non-periodic functions are correctly integrated by the trapezoidal rule, even if slowly, on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that non-periodic functions are correctly integrated by the trapezoidal rule, even if slowly, on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; // This function is not periodic, so it should not be fast to converge: auto f = [](Real x)->Real { using std::sqrt; return sqrt(1 - x*x); }; @@ -219,7 +218,7 @@ void test_rational_sin() using std::sin; using boost::math::constants::two_pi; using boost::math::constants::half; - std::cout << "Testing that a rational sin function is integrated correctly by the trapezoidal rule on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that a rational sin function is integrated correctly by the trapezoidal rule on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; Real a = 5; auto f = [=](Real x)->Real { using std::sin; Real t = a + sin(x); return 1.0f / (t*t); }; diff --git a/test/test_univariate_statistics.cpp b/test/test_univariate_statistics.cpp index 75cfb55ec7..82a140bd9a 100644 --- a/test/test_univariate_statistics.cpp +++ b/test/test_univariate_statistics.cpp @@ -14,7 +14,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -34,7 +35,7 @@ using std::abs; * Test checklist: * 1) Does it work with multiprecision? * 2) Does it work with .cbegin()/.cend() if the data is not altered? - * 3) Does it work with ublas and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) + * 3) Does it work with std::deque and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) * 4) Does it work with std::forward_list if a forward iterator is all that is required? * 5) Does it work with complex data if complex data is sensible? */ @@ -174,8 +175,8 @@ void test_mean(ExecutionPolicy&& exec) mu = boost::math::statistics::mean(exec, l.begin(), l.end()); BOOST_TEST(abs(mu - 4) < tol); - // Does it work with ublas vectors? - boost::numeric::ublas::vector w(7); + // Does it work with std::deque? + std::deque w(7); for (size_t i = 0; i < w.size(); ++i) { w[i] = i+1; @@ -549,8 +550,8 @@ void test_median(ExecutionPolicy&& exec) m = boost::math::statistics::median(exec, w); BOOST_TEST_EQ(m, 2); - // Does it work with ublas? - boost::numeric::ublas::vector w1(3); + // Does it work with std::deque? + std::deque w1(3); w1[0] = 1; w1[1] = 2; w1[2] = 3; @@ -608,8 +609,8 @@ void test_median_absolute_deviation(ExecutionPolicy&& exec) m = boost::math::statistics::median_absolute_deviation(exec, w, 0); BOOST_TEST_EQ(m, 2); - // boost.ublas vector? - boost::numeric::ublas::vector u(6); + // std::deque? + std::deque u(6); u[0] = 1; u[1] = 2; u[2] = -3; @@ -667,8 +668,8 @@ void test_median_absolute_deviation(ExecutionPolicy&& exec) m = boost::math::statistics::median_absolute_deviation(exec, w); BOOST_TEST_EQ(m, 1); - // boost.ublas vector? - boost::numeric::ublas::vector u2(6); + // std::deque? + std::deque u2(6); u2[0] = 1; u2[1] = 2; u2[2] = -3; @@ -737,7 +738,7 @@ void test_gini_coefficient(ExecutionPolicy&& exec) gini = boost::math::statistics::gini_coefficient(exec, w); BOOST_TEST(abs(gini) < tol); - boost::numeric::ublas::vector w1(3); + std::deque w1(3); w1[0] = 1; w1[1] = 1; w1[2] = 1; @@ -785,7 +786,7 @@ void test_integer_gini_coefficient(ExecutionPolicy&& exec) gini = boost::math::statistics::gini_coefficient(exec, w); BOOST_TEST(abs(gini) < tol); - boost::numeric::ublas::vector w1(3); + std::deque w1(3); w1[0] = 1; w1[1] = 1; w1[2] = 1; diff --git a/test/test_univariate_statistics_backwards_compatible.cpp b/test/test_univariate_statistics_backwards_compatible.cpp index 2b16b0f34e..0cde6f0f3b 100644 --- a/test/test_univariate_statistics_backwards_compatible.cpp +++ b/test/test_univariate_statistics_backwards_compatible.cpp @@ -16,7 +16,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -30,7 +31,7 @@ using std::abs; * Test checklist: * 1) Does it work with multiprecision? * 2) Does it work with .cbegin()/.cend() if the data is not altered? - * 3) Does it work with ublas and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) + * 3) Does it work with std::deque and std::array? (Checking Eigen and Armadillo will make the CI system really unhappy.) * 4) Does it work with std::forward_list if a forward iterator is all that is required? * 5) Does it work with complex data if complex data is sensible? */ @@ -209,8 +210,8 @@ void test_mean() mu = boost::math::statistics::mean(l.begin(), l.end()); BOOST_TEST(abs(mu - 4) < tol); - // Does it work with ublas vectors? - boost::numeric::ublas::vector w(7); + // Does it work with std::deque? + std::deque w(7); for (std::size_t i = 0; i < w.size(); ++i) { w[i] = Real(i+1); @@ -576,8 +577,8 @@ void test_median() m = boost::math::statistics::median(w); BOOST_TEST_EQ(m, 2); - // Does it work with ublas? - boost::numeric::ublas::vector w1(3); + // Does it work with std::deque? + std::deque w1(3); w1[0] = 1; w1[1] = 2; w1[2] = 3; @@ -635,8 +636,8 @@ void test_median_absolute_deviation() m = boost::math::statistics::median_absolute_deviation(w, 0); BOOST_TEST_EQ(m, 2); - // boost.ublas vector? - boost::numeric::ublas::vector u(6); + // std::deque? + std::deque u(6); u[0] = 1; u[1] = 2; u[2] = -3; @@ -694,8 +695,8 @@ void test_median_absolute_deviation() m = boost::math::statistics::median_absolute_deviation(w); BOOST_TEST_EQ(m, 1); - // boost.ublas vector? - boost::numeric::ublas::vector u2(6); + // std::deque? + std::deque u2(6); u2[0] = 1; u2[1] = 2; u2[2] = -3; @@ -764,7 +765,7 @@ void test_gini_coefficient() gini = boost::math::statistics::gini_coefficient(w); BOOST_TEST(abs(gini) < tol); - boost::numeric::ublas::vector w1(3); + std::deque w1(3); w1[0] = 1; w1[1] = 1; w1[2] = 1; @@ -813,7 +814,7 @@ void test_integer_gini_coefficient() gini = boost::math::statistics::gini_coefficient(w); BOOST_TEST(abs(gini) < tol); - boost::numeric::ublas::vector w1(3); + std::deque w1(3); w1[0] = 1; w1[1] = 1; w1[2] = 1; diff --git a/test/test_vector_barycentric_rational.cpp b/test/test_vector_barycentric_rational.cpp index 5961449356..f7acbf80c2 100644 --- a/test/test_vector_barycentric_rational.cpp +++ b/test/test_vector_barycentric_rational.cpp @@ -8,9 +8,8 @@ #include #include #include -#include +#include #include -#include #include #include #include "math_unit_test.hpp" @@ -27,7 +26,7 @@ template void test_agreement_with_1d() { std::cout << "Testing with 1D interpolation on type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(4723); boost::random::uniform_real_distribution dis(0.1f, 1); std::vector t(100); @@ -77,7 +76,7 @@ template void test_interpolation_condition_eigen() { std::cout << "Testing interpolation condition for barycentric interpolation on Eigen vectors of type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(4723); boost::random::uniform_real_distribution dis(0.1f, 1); std::vector t(100); @@ -109,7 +108,7 @@ template void test_interpolation_condition_std_array() { std::cout << "Testing interpolation condition for barycentric interpolation on std::array vectors of type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(4723); boost::random::uniform_real_distribution dis(0.1f, 1); std::vector t(100); @@ -143,14 +142,14 @@ void test_interpolation_condition_std_array() template -void test_interpolation_condition_ublas() +void test_interpolation_condition_deque() { - std::cout << "Testing interpolation condition for barycentric interpolation ublas vectors of type " - << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing interpolation condition for barycentric interpolation std::deque of type " + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(4723); boost::random::uniform_real_distribution dis(0.1f, 1); std::vector t(100); - std::vector> y(100); + std::vector> y(100); t[0] = dis(gen); y[0].resize(2); y[0][0] = dis(gen); @@ -164,11 +163,11 @@ void test_interpolation_condition_ublas() } std::vector t_copy = t; - std::vector> y_copy = y; + std::vector> y_copy = y; boost::math::interpolators::vector_barycentric_rational interpolator(std::move(t), std::move(y)); - boost::numeric::ublas::vector z(2); + std::deque z(2); for (size_t i = 0; i < t_copy.size(); ++i) { interpolator(z, t_copy[i]); @@ -180,7 +179,7 @@ void test_interpolation_condition_ublas() template void test_interpolation_condition_high_order() { - std::cout << "Testing interpolation condition in high order for barycentric interpolation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing interpolation condition in high order for barycentric interpolation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(5); boost::random::uniform_real_distribution dis(0.1f, 1); std::vector t(100); @@ -213,7 +212,7 @@ template void test_constant_eigen() { std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on Eigen vectors of type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(6); boost::random::uniform_real_distribution dis(0.1f, 1); @@ -256,7 +255,7 @@ template void test_constant_std_array() { std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on std::array vectors of type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(6); boost::random::uniform_real_distribution dis(0.1f, 1); @@ -298,7 +297,7 @@ void test_constant_std_array() template void test_constant_high_order() { - std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing that constants are interpolated correctly using barycentric interpolation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(6); boost::random::uniform_real_distribution dis(0.1f, 1); @@ -341,7 +340,7 @@ template void test_prime_near_node() { std::cout << "Testing the derivative a few ulps from a node on std::array vectors of type " - << boost::typeindex::type_id().pretty_name() << "\n"; + << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::vector t(20); std::vector> y(20); for (size_t i = 0; i < t.size(); ++i) @@ -374,7 +373,7 @@ void test_prime_near_node() template void test_weights() { - std::cout << "Testing weights are calculated correctly using barycentric interpolation on type " << boost::typeindex::type_id().pretty_name() << "\n"; + std::cout << "Testing weights are calculated correctly using barycentric interpolation on type " << boost::math::test::detail::demangle(typeid(Real).name()) << "\n"; std::mt19937 gen(9); boost::random::uniform_real_distribution dis(0.005, 0.01); @@ -419,7 +418,7 @@ void vector_barycentric_rational() test_constant_std_array(); test_constant_high_order(); test_interpolation_condition_eigen(); - test_interpolation_condition_ublas(); + test_interpolation_condition_deque(); test_interpolation_condition_std_array(); test_interpolation_condition_high_order(); test_agreement_with_1d(); @@ -432,7 +431,7 @@ void vector_barycentric_rational() test_constant_std_array(); test_constant_high_order(); test_interpolation_condition_eigen(); - test_interpolation_condition_ublas(); + test_interpolation_condition_deque(); test_interpolation_condition_std_array(); test_interpolation_condition_high_order(); test_agreement_with_1d(); diff --git a/tools/Jamfile.v2 b/tools/Jamfile.v2 index 483eae998e..6ab5d090ba 100644 --- a/tools/Jamfile.v2 +++ b/tools/Jamfile.v2 @@ -30,7 +30,6 @@ project static borland:static BOOST_ALL_NO_LIB=1 - BOOST_UBLAS_UNSUPPORTED_COMPILER=0 . ../include_private /boost/multiprecision//boost_multiprecision diff --git a/tools/lanczos_generator.cpp b/tools/lanczos_generator.cpp index 9fabecf3b5..7406d52a7a 100644 --- a/tools/lanczos_generator.cpp +++ b/tools/lanczos_generator.cpp @@ -7,9 +7,7 @@ #include #include #include -#include -#include -#include +#include #include #include #include @@ -38,8 +36,7 @@ #include #include -using boost::numeric::ublas::matrix; -using boost::numeric::ublas::vector; +using boost::math::tools::matrix; struct lanczos_spot_data { diff --git a/tools/minimax/Jamfile.v2 b/tools/minimax/Jamfile.v2 index f0226cc320..f1755bbfa7 100644 --- a/tools/minimax/Jamfile.v2 +++ b/tools/minimax/Jamfile.v2 @@ -33,7 +33,6 @@ project borland:static ../../.. BOOST_ALL_NO_LIB=1 - BOOST_UBLAS_UNSUPPORTED_COMPILER=0 . ../include_private $(gmp_path) diff --git a/tools/minimax/main.cpp b/tools/minimax/main.cpp index edfd5133bb..389e5989fa 100644 --- a/tools/minimax/main.cpp +++ b/tools/minimax/main.cpp @@ -4,9 +4,6 @@ // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #define BOOST_TEST_MODULE foobar -#define BOOST_UBLAS_TYPE_CHECK_EPSILON (type_traits::type_sqrt (boost::math::tools::epsilon ())) -#define BOOST_UBLAS_TYPE_CHECK_MIN (type_traits::type_sqrt ( boost::math::tools::min_value())) -#define BOOST_UBLAS_NDEBUG #include "multiprecision.hpp" @@ -131,7 +128,7 @@ void show(const char*, const char*) int prec = 2 + (target_precision * 3010LL)/10000; std::cout << std::scientific << std::setprecision(prec); set_output_precision(prec); - boost::numeric::ublas::vector v = p_remez->zero_points(); + std::vector v = p_remez->zero_points(); std::cout << " Zeros = {\n"; unsigned i; @@ -257,7 +254,7 @@ void do_test(T, const char* name) // (in theory) the largest deviation should occur. For good // measure we'll test at the zeros as well: // - boost::numeric::ublas::vector + std::vector zeros(p_remez->zero_points()), cheb(p_remez->chebyshev_points());