1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/* Boost example/filter.cpp
* two examples of filters for computing the sign of a determinant
* the second filter is based on an idea presented in
* "Interval arithmetic yields efficient dynamic filters for computational
* geometry" by Brnnimann, Burnikel and Pion, 2001
*
* Copyright 2003 Guillaume Melquiond
*
* Distributed under 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 <boost/numeric/interval.hpp>
#include <iostream>
namespace dummy {
using namespace boost;
using namespace numeric;
using namespace interval_lib;
typedef save_state<rounded_arith_opp<double> > R;
typedef checking_no_nan<double, checking_no_empty<double> > P;
typedef interval<double, policies<R, P> > I;
}
template<class T>
class vector {
T* ptr;
public:
vector(int d) { ptr = (T*)malloc(sizeof(T) * d); }
~vector() { free(ptr); }
const T& operator[](int i) const { return ptr[i]; }
T& operator[](int i) { return ptr[i]; }
};
template<class T>
class matrix {
int dim;
T* ptr;
public:
matrix(int d): dim(d) { ptr = (T*)malloc(sizeof(T) * dim * dim); }
~matrix() { free(ptr); }
int get_dim() const { return dim; }
void assign(const matrix<T> &a) { memcpy(ptr, a.ptr, sizeof(T) * dim * dim); }
const T* operator[](int i) const { return &(ptr[i * dim]); }
T* operator[](int i) { return &(ptr[i * dim]); }
};
typedef dummy::I I_dbl;
/* compute the sign of a determinant using an interval LU-decomposition; the
function answers 1 or -1 if the determinant is positive or negative (and
more importantly, the result must be provable), or 0 if the algorithm was
unable to get a correct sign */
int det_sign_algo1(const matrix<double> &a) {
int dim = a.get_dim();
vector<int> p(dim);
for(int i = 0; i < dim; i++) p[i] = i;
int sig = 1;
I_dbl::traits_type::rounding rnd;
typedef boost::numeric::interval_lib::unprotect<I_dbl>::type I;
matrix<I> u(dim);
for(int i = 0; i < dim; i++) {
const double* line1 = a[i];
I* line2 = u[i];
for(int j = 0; j < dim; j++)
line2[j] = line1[j];
}
// computation of L and U
for(int i = 0; i < dim; i++) {
// partial pivoting
{
int pivot = i;
double max = 0;
for(int j = i; j < dim; j++) {
const I &v = u[p[j]][i];
if (zero_in(v)) continue;
double m = norm(v);
if (m > max) { max = m; pivot = j; }
}
if (max == 0) return 0;
if (pivot != i) {
sig = -sig;
int tmp = p[i];
p[i] = p[pivot];
p[pivot] = tmp;
}
}
// U[i,?]
{
I *line1 = u[p[i]];
const I &pivot = line1[i];
if (boost::numeric::interval_lib::cerlt(pivot, 0.)) sig = -sig;
for(int k = i + 1; k < dim; k++) {
I *line2 = u[p[k]];
I fact = line2[i] / pivot;
for(int j = i + 1; j < dim; j++) line2[j] -= fact * line1[j];
}
}
}
return sig;
}
/* compute the sign of a determinant using a floating-point LU-decomposition
and an a posteriori interval validation; the meaning of the answer is the
same as previously */
int det_sign_algo2(const matrix<double> &a) {
int dim = a.get_dim();
vector<int> p(dim);
for(int i = 0; i < dim; i++) p[i] = i;
int sig = 1;
matrix<double> lui(dim);
{
// computation of L and U
matrix<double> lu(dim);
lu.assign(a);
for(int i = 0; i < dim; i++) {
// partial pivoting
{
int pivot = i;
double max = std::abs(lu[p[i]][i]);
for(int j = i + 1; j < dim; j++) {
double m = std::abs(lu[p[j]][i]);
if (m > max) { max = m; pivot = j; }
}
if (max == 0) return 0;
if (pivot != i) {
sig = -sig;
int tmp = p[i];
p[i] = p[pivot];
p[pivot] = tmp;
}
}
// L[?,i] and U[i,?]
{
double *line1 = lu[p[i]];
double pivot = line1[i];
if (pivot < 0) sig = -sig;
for(int k = i + 1; k < dim; k++) {
double *line2 = lu[p[k]];
double fact = line2[i] / pivot;
line2[i] = fact;
for(int j = i + 1; j < dim; j++) line2[j] -= line1[j] * fact;
}
}
}
// computation of approximate inverses: Li and Ui
for(int j = 0; j < dim; j++) {
for(int i = j + 1; i < dim; i++) {
double *line = lu[p[i]];
double s = - line[j];
for(int k = j + 1; k < i; k++) s -= line[k] * lui[k][j];
lui[i][j] = s;
}
lui[j][j] = 1 / lu[p[j]][j];
for(int i = j - 1; i >= 0; i--) {
double *line = lu[p[i]];
double s = 0;
for(int k = i + 1; k <= j; k++) s -= line[k] * lui[k][j];
lui[i][j] = s / line[i];
}
}
}
// norm of PAUiLi-I computed with intervals
{
I_dbl::traits_type::rounding rnd;
typedef boost::numeric::interval_lib::unprotect<I_dbl>::type I;
vector<I> m1(dim);
vector<I> m2(dim);
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) m1[j] = 0;
const double *l1 = a[p[i]];
for(int j = 0; j < dim; j++) {
double v = l1[j]; // PA[i,j]
double *l2 = lui[j]; // Ui[j,?]
for(int k = j; k < dim; k++) {
using boost::numeric::interval_lib::mul;
m1[k] += mul<I>(v, l2[k]); // PAUi[i,k]
}
}
for(int j = 0; j < dim; j++) m2[j] = m1[j]; // PAUi[i,j] * Li[j,j]
for(int j = 1; j < dim; j++) {
const I &v = m1[j]; // PAUi[i,j]
double *l2 = lui[j]; // Li[j,?]
for(int k = 0; k < j; k++)
m2[k] += v * l2[k]; // PAUiLi[i,k]
}
m2[i] -= 1; // PAUiLi-I
double ss = 0;
for(int i = 0; i < dim; i++) ss = rnd.add_up(ss, norm(m2[i]));
if (ss >= 1) return 0;
}
}
return sig;
}
int main() {
int dim = 20;
matrix<double> m(dim);
for(int i = 0; i < dim; i++) for(int j = 0; j < dim; j++)
m[i][j] = /*1 / (i-j-0.001)*/ cos(1+i*sin(1 + j)) /*1./(1+i+j)*/;
// compute the sign of the determinant of a "strange" matrix with the two
// algorithms, the first should fail and the second succeed
std::cout << det_sign_algo1(m) << " " << det_sign_algo2(m) << std::endl;
}
|