/usr/include/CGAL/Polynomial/determinant.h is in libcgal-dev 4.7-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | // Copyright (c) 2008 Max-Planck-Institute Saarbruecken (Germany)
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
//
//
// Author(s) : Michael Hemmer
// ============================================================================
// TODO: The comments are all original EXACUS comments and aren't adapted. So
// they may be wrong now.
#ifndef CGAL_POLYNOMIAL_DETERMINANT_H
#define CGAL_POLYNOMIAL_DETERMINANT_H
namespace CGAL {
#include <CGAL/basic.h>
namespace internal {
// TODO: Own simple matrix and vector to avoid importing the whole matrix stuff
// from EXACUS.
// This is to be replaced by a corresponding CGAL Matrix and Vector.
template< class Coeff >
struct Simple_matrix
: public std::vector< std::vector< Coeff > > {
typedef Coeff NT;
Simple_matrix() {
}
Simple_matrix( int m ) {
initialize( m, m, Coeff(0) );
}
Simple_matrix( int m, int n, Coeff x = Coeff(0) ) {
initialize( m, n, x );
}
void swap_rows(int i, int j) {
std::vector< Coeff > swap = this->operator[](i);
this->operator[](i) = this->operator[](j);
this->operator[](j) = swap;
}
void swap_columns(int i, int j) {
for(int k = 0; k < m; k++) {
Coeff swap = this->operator[](k).operator[](i);
this->operator[](k).operator[](i)
= this->operator[](k).operator[](j);
this->operator[](k).operator[](j) = swap;
}
}
int row_dimension() const { return m; }
int column_dimension() const { return n; }
private:
void initialize( int m, int n, Coeff x ) {
this->reserve( m );
this->m = m;
this->n = n;
for( int i = 0; i < m; ++i ) {
this->push_back( std::vector< Coeff >() );
this->operator[](i).reserve(n);
for( int j = 0; j < n; ++j ) {
this->operator[](i).push_back( x );
}
}
}
int m,n;
};
template< class Coeff >
struct Simple_vector
: public std::vector< Coeff > {
Simple_vector( int m ) {
this->reserve( m );
for( int i = 0; i < m; ++i )
this->push_back( Coeff(0) );
}
Coeff operator*( const Simple_vector<Coeff>& v2 ) const {
CGAL_precondition( v2.size() == this->size() );
Coeff result(0);
for( unsigned i = 0; i < this->size(); ++i )
result += ( this->operator[](i) * v2[i] );
return result;
}
};
// call for auto-selection of best routine (exact NT)
template <class M> inline
typename M::NT determinant (const M& matrix,
int n,
Integral_domain_without_division_tag,
::CGAL::Boolean_tag<true> )
{
return det_berkowitz(matrix, n);
}
// call for auto-selection of best routine (inexact NT)
template <class M> inline
typename M::NT determinant (const M& matrix,
int n,
Integral_domain_without_division_tag,
::CGAL::Boolean_tag<false> )
{
typedef typename M::NT NT;
NT type = NT(0);
return inexact_determinant_select(matrix, n, type);
}
// (other datatypes)
template <class M, class other> inline
typename M::NT inexact_determinant_select (const M& matrix,
int n,
other /* type */)
{
return det_berkowitz(matrix, n);
}
/*! \ingroup CGAL_determinant
* \brief Will determine and execute a suitable determinant routine and
* return the determinant of \a A.
* (specialisation for CGAL::Matrix_d)
*/
template <class NT > inline
NT determinant(const internal::Simple_matrix<NT>& A)
{
CGAL_assertion(A.row_dimension()==A.column_dimension());
return determinant(A,A.column_dimension());
}
/*! \ingroup CGAL_determinant
* \brief Will determine and execute a suitable determinant routine and
* return the determinant of \a A. Needs the dimension \a n of \a A as
* its second argument.
*/
template <class M> inline
typename M::NT determinant(const M& matrix,
int n)
{
typedef typename M::NT NT;
typedef typename Algebraic_structure_traits<NT>::Algebraic_category Algebraic_category;
typedef typename Algebraic_structure_traits<NT>::Is_exact Is_exact;
return internal::determinant (matrix, n, Algebraic_category(), Is_exact());
}
// Part of det_berkowitz
// Computes sum of all clows of length k
template <class M>
inline
std::vector<typename M::NT>
clow_lengths (const M& A,int k,int n)
{
typedef typename M::NT NT;
int i, j, l;
typename internal::Simple_vector<NT> r(k-1);
typename internal::Simple_vector<NT> s(k-1);
typename internal::Simple_vector<NT> t(k-1);
std::vector<NT> rMks(k);
typename internal::Simple_matrix<NT> MM(k-1);
for (i=n-k+2;i<=n;++i)
for (j=n-k+2;j<=n;++j)
MM[i-n+k-2][j-n+k-2] = A[i-1][j-1];
i = n-k+1;
l = 1;
for (j=n-k+2;j<=n;++j,++l)
{
r[l-1] = A[i-1][j-1];
s[l-1] = A[j-1][i-1];
}
rMks[0] = A[i-1][i-1];
rMks[1] = r*s;
for (i=2;i<k;++i)
{
// r = r * M;
for (j=0;j<k-1;++j)
for (l=0;l<k-1;++l)
t[j] += r[l] * MM[l][j];
for (j=0;j<k-1;++j)
{
r[j] = t[j];
t[j] = NT(0);
}
rMks[i] = r*s;
}
return rMks;
}
/*! \ingroup CGAL_determinant
* \brief Computes the determinant of \a A according to the method proposed
* by Berkowitz.
* (specialisation for CGAL::Matrix_d)
*
* Note that this routine is completely free of divisions!
*/
template <class NT > inline
NT det_berkowitz(const internal::Simple_matrix<NT>& A)
{
CGAL_assertion(A.row_dimension()==A.column_dimension());
return det_berkowitz(A,A.column_dimension());
}
template <class M, class OutputIterator> inline
OutputIterator minors_berkowitz (const M& A,OutputIterator minors,int n,int m=0)
{
CGAL_precondition(n>0);
CGAL_precondition(m<=n);
typedef typename M::NT NT;
// If default value is set, reset it to the second parameter
if(m==0) {
m=n;
}
int i, j, k, offset;
std::vector<NT> rMks;
NT a;
typename internal::Simple_matrix<NT> B(n+1); // not square in original
typename internal::Simple_vector<NT> p(n+1);
typename internal::Simple_vector<NT> q(n+1);
for (k=1;k<=n;++k)
{
// compute vector q = B*p;
if (k == 1)
{
p[0] = NT(-1);
q[0] = p[0];
p[1] = A[n-1][n-1];
q[1] = p[1];
}
else if (k == 2)
{
p[0] = NT(1);
q[0] = p[0];
p[1] = -A[n-2][n-2] - A[n-1][n-1];
q[1] = p[1];
p[2] = -A[n-2][n-1] * A[n-1][n-2] + A[n-2][n-2] * A[n-1][n-1];
q[2] = p[2];
}
else if (k == n)
{
rMks = internal::clow_lengths<M>(A,k,n);
// Setup for last row of matrix B
i = n+1;
B[i-1][n-1] = NT(-1);
for (j=1;j<=n;++j)
B[i-1][i-j-1] = rMks[j-1];
p[i-1] = NT(0);
for (j=1;j<=n;++j)
p[i-1] = p[i-1] + B[i-1][j-1] * q[j-1];
}
else
{
rMks = internal::clow_lengths<M>(A,k,n);
// Setup for matrix B (diagonal after diagonal)
for (i=1;i<=k;++i)
B[i-1][i-1] = NT(-1);
for (offset=1;offset<=k;++offset)
{
a = rMks[offset-1];
for (i=1;i<=k-offset+1;++i)
B[offset+i-1][i-1] = a;
}
// Multiply s.t. p=B*q
for (i=1;i<=k;++i)
{
p[i-1] = NT(0);
for (j=1;j<=i;++j)
p[i-1] = p[i-1] + B[i-1][j-1] * q[j-1];
}
p[i-1] = NT(0);
for (j=1;j<=k;++j)
p[i-1] = p[i-1] + B[i-1][j-1] * q[j-1];
for (i=1;i<=k+1;++i)
q[i-1] = p[i-1];
}
if(k > n-m) {
(*minors)=p[k];
++minors;
}
}
return minors;
}
/*! \ingroup CGAL_determinant
* \brief Computes the determinant of \a A according to the method proposed
* by Berkowitz. Needs the dimension \a n of \a A as its second argument.
*
* Note that this routine is completely free of divisions!
*/
template <class M> inline
typename M::NT det_berkowitz (const M& A,
int n)
{
typedef typename M::NT NT;
if(n==0) {
return NT(1);
}
NT det[1];
minors_berkowitz(A,det,n,1);
return det[0];
}
} // namespace internal
} //namespace CGAL
#endif // CGAL_POLYNOMIAL_DETERMINANT_H
|