This file is indexed.

/usr/include/Bpp/Numeric/Matrix/LUDecomposition.h is in libbpp-core-dev 2.1.0-1.

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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//
// File: LU.h
// Created by: Julien Dutheil
// Created on: Tue Apr 7 16:24 2004
//

/*
   Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)

   This software is a computer program whose purpose is to provide classes
   for numerical calculus.

   This software is governed by the CeCILL  license under French law and
   abiding by the rules of distribution of free software.  You can  use,
   modify and/ or redistribute the software under the terms of the CeCILL
   license as circulated by CEA, CNRS and INRIA at the following URL
   "http://www.cecill.info".

   As a counterpart to the access to the source code and  rights to copy,
   modify and redistribute granted by the license, users are provided only
   with a limited warranty  and the software's author,  the holder of the
   economic rights,  and the successive licensors  have only  limited
   liability.

   In this respect, the user's attention is drawn to the risks associated
   with loading,  using,  modifying and/or developing or reproducing the
   software by the user in light of its specific status of free software,
   that may mean  that it is complicated to manipulate,  and  that  also
   therefore means  that it is reserved for developers  and  experienced
   professionals having in-depth computer knowledge. Users are therefore
   encouraged to load and test the software's suitability as regards their
   requirements in conditions enabling the security of their systems and/or
   data to be ensured and,  more generally, to use and operate it in the
   same conditions as regards security.

   The fact that you are presently reading this means that you have had
   knowledge of the CeCILL license and that you accept its terms.
 */

#ifndef _LU_H_
#define _LU_H_

#include "Matrix.h"
#include "../NumTools.h"
#include "../../Exceptions.h"

// From the STL:
#include <algorithm>
#include <vector>
// for min(), max() below

namespace bpp
{
/**
 * @brief LU Decomposition.
 *
 * [This class and its documentation is adpated from the C++ port of the JAMA library.]
 *
 * For an m-by-n matrix A with m >= n, the LU decomposition is an m-by-n
 * unit lower triangular matrix L, an n-by-n upper triangular matrix U,
 * and a permutation vector piv of length m so that A(piv,:) = L*U.
 * If m < n, then L is m-by-m and U is m-by-n.
 *
 * The LU decompostion with pivoting always exists, even if the matrix is
 * singular, so the constructor will never fail.  The primary use of the
 * LU decomposition is in the solution of square systems of simultaneous
 * linear equations. This will fail if isNonsingular() returns false.
 */
template<class Real>
class LUDecomposition
{
private:
  /* Array for internal storage of decomposition.  */
  RowMatrix<Real> LU;
  RowMatrix<Real> L_;
  RowMatrix<Real> U_;
  size_t m, n;
  int pivsign;
  std::vector<size_t> piv;

private:
  static void permuteCopy(const Matrix<Real>& A, const std::vector<size_t>& piv, size_t j0, size_t j1, Matrix<Real>& X)
  {
    size_t piv_length = piv.size();

    X.resize(piv_length, j1 - j0 + 1);

    for (size_t i = 0; i < piv_length; i++)
    {
      for (size_t j = j0; j <= j1; j++)
      {
        X(i, j - j0) = A(piv[i], j);
      }
    }
  }

  static void permuteCopy(const std::vector<Real>& A, const std::vector<size_t>& piv, std::vector<Real>& X)
  {
    size_t piv_length = piv.size();
    if (piv_length != A.size())
      X.clean();

    X.resize(piv_length);

    for (size_t i = 0; i < piv_length; i++)
    {
      X[i] = A[piv[i]];
    }
  }

public:
  /**
   * @brief LU Decomposition
   *
   * @param  A   Rectangular matrix
   * @return LU Decomposition object to access L, U and piv.
   */

  // This is the constructor in JAMA C++ port. However, it seems to have some bug...
  // We use the original JAMA's 'experimental' port, which gives good results instead.
  /*	  LUDecomposition (const Matrix<Real> &A) : LU(A), m(A.getNumberOfRows()), n(A.getNumberOfColumns()), piv(A.getNumberOfRows())
        {

        // Use a "left-looking", dot-product, Crout/Doolittle algorithm.


        for (size_t i = 0; i < m; i++) {
        piv[i] = i;
        }
        pivsign = 1;
        //Real *LUrowi = 0;;
        vector<Real> LUrowi;
        vector<Real> LUcolj;

        // Outer loop.

        for (size_t j = 0; j < n; j++) {

        // Make a copy of the j-th column to localize references.

        LUcolj = LU.col(j);
        //for (size_t i = 0; i < m; i++) {
      //  LUcolj[i] = LU(i,j);
      //}

      // Apply previous transformations.

      for (size_t i = 0; i < m; i++) {
        //LUrowi = LU[i];
        LUrowi = LU.row(i);

        // Most of the time is spent in the following dot product.

        size_t kmax = NumTools::min<size_t>(i,j);
        double s = 0.0;
        for (size_t k = 0; k < kmax; k++) {
        s += LUrowi[k] * LUcolj[k];
        }

        LUrowi[j] = LUcolj[i] -= s;
        }

        // Find pivot and exchange if necessary.

        size_t p = j;
        for (size_t i = j+1; i < m; i++) {
        if (NumTools::abs<Real>(LUcolj[i]) > NumTools::abs<Real>(LUcolj[p])) {
        p = i;
        }
        }
        if (p != j) {
        size_t k=0;
        for (k = 0; k < n; k++) {
        double t = LU(p,k);
        LU(p,k) = LU(j,k);
        LU(j,k) = t;
        }
        k = piv[p];
        piv[p] = piv[j];
        piv[j] = k;
        pivsign = -pivsign;
        }

        // Compute multipliers.

        if ((j < m) && (LU(j,j) != 0.0)) {
        for (size_t i = j+1; i < m; i++) {
        LU(i,j) /= LU(j,j);
        }
        }
        }
        }
   */

  LUDecomposition (const Matrix<Real>& A) :
    LU(A),
    L_(A.getNumberOfRows(), A.getNumberOfColumns()),
    U_(A.getNumberOfColumns(), A.getNumberOfColumns()),
    m(A.getNumberOfRows()),
    n(A.getNumberOfColumns()),
    pivsign(1),
    piv(A.getNumberOfRows())
  {
    for (size_t i = 0; i < m; i++)
    {
      piv[i] = i;
    }
    // Main loop.
    for (size_t k = 0; k < n; k++)
    {
      // Find pivot.
      size_t p = k;
      for (size_t i = k + 1; i < m; i++)
      {
        if (NumTools::abs<Real>(LU(i, k)) > NumTools::abs<Real>(LU(p, k)))
        {
          p = i;
        }
      }
      // Exchange if necessary.
      if (p != k)
      {
        for (size_t j = 0; j < n; j++)
        {
          double t = LU(p, j); LU(p, j) = LU(k, j); LU(k, j) = t;
        }
        size_t t = piv[p]; piv[p] = piv[k]; piv[k] = t;
        pivsign = -pivsign;
      }
      // Compute multipliers and eliminate k-th column.
      if (LU(k, k) != 0.0)
      {
        for (size_t i = k + 1; i < m; i++)
        {
          LU(i, k) /= LU(k, k);
          for (size_t j = k + 1; j < n; j++)
          {
            LU(i, j) -= LU(i, k) * LU(k, j);
          }
        }
      }
    }
  }

  /**
   * @brief Return lower triangular factor
   *
   * @return L
   */
  const RowMatrix<Real>& getL()
  {
    for (size_t i = 0; i < m; i++)
    {
      for (size_t j = 0; j < n; j++)
      {
        if (i > j)
        {
          L_(i, j) = LU(i, j);
        }
        else if (i == j)
        {
          L_(i, j) = 1.0;
        }
        else
        {
          L_(i, j) = 0.0;
        }
      }
    }
    return L_;
  }

  /**
   * @brief Return upper triangular factor
   *
   * @return U portion of LU factorization.
   */
  const RowMatrix<Real>& getU ()
  {
    for (size_t i = 0; i < n; i++)
    {
      for (size_t j = 0; j < n; j++)
      {
        if (i <= j)
        {
          U_(i, j) = LU(i, j);
        }
        else
        {
          U_(i, j) = 0.0;
        }
      }
    }
    return U_;
  }

  /**
   * @brief Return pivot permutation vector
   *
   * @return piv
   */
  std::vector<size_t> getPivot () const
  {
    return piv;
  }


  /**
   * @brief Compute determinant using LU factors.
   *
   * @return determinant of A, or 0 if A is not square.
   */
  Real det() const
  {
    if (m != n)
    {
      return Real(0);
    }
    Real d = Real(pivsign);
    for (size_t j = 0; j < n; j++)
    {
      d *= LU(j, j);
    }
    return d;
  }

  /**
   * @brief Solve A*X = B
   *
   * @param  B [in]  A Matrix with as many rows as A and any number of columns.
   * @param  X [out]  A RowMatrix that will be changed such that L*U*X = B(piv,:).
   * @return  the lowest diagonal term (in absolute value), for further checkings
   *             of non-singularity of LU.
   *
   * If B is nonconformant or LU is singular, an Exception is raised.
   *
   */
  Real solve (const Matrix<Real>& B, Matrix<Real>& X) const throw (BadIntegerException, ZeroDivisionException)
  {
    /* Dimensions: A is mxn, X is nxk, B is mxk */

    if (B.getNumberOfRows() != m)
    {
      throw BadIntegerException("Wrong dimension in LU::solve", static_cast<int>(B.getNumberOfRows()));
    }

    Real minD = NumTools::abs<Real>(LU(0, 0));
    for (size_t i = 1; i < m; i++)
    {
      Real currentValue = NumTools::abs<Real>(LU(i, i));
      if (currentValue < minD)
        minD = currentValue;
    }

    if (minD < NumConstants::SMALL())
    {
      throw ZeroDivisionException("Singular matrix in LU::solve.");
    }

    // Copy right hand side with pivoting
    size_t nx = B.getNumberOfColumns();

    permuteCopy(B, piv, 0, nx - 1, X);

    // Solve L*Y = B(piv,:)
    for (size_t k = 0; k < n; k++)
    {
      for (size_t i = k + 1; i < n; i++)
      {
        for (size_t j = 0; j < nx; j++)
        {
          X(i, j) -= X(k, j) * LU(i, k);
        }
      }
    }
    // Solve U*X = Y;
    // !!! Do not use unsigned int with -- loop!!!
    // for (int k = n-1; k >= 0; k--) {
    size_t k = n;

    do
    {
      k--;
      for (size_t j = 0; j < nx; j++)
      {
        X(k, j) /= LU(k, k);
      }
      for (size_t i = 0; i < k; i++)
      {
        for (size_t j = 0; j < nx; j++)
        {
          X(i, j) -= X(k, j) * LU(i, k);
        }
      }
    }
    while (k > 0);

    return minD;
  }


  /**
   * @brief Solve A*x = b, where x and b are vectors of length equal	to the number of rows in A.
   *
   * @param  b [in] a vector (Array1D> of length equal to the first dimension	of A.
   * @param  x [out] a vector that will be changed so that so that L*U*x = b(piv).
   * @return  the lowest diagonal term (in absolute value), for further checkings
   *             of non-singularity of LU.
   *
   * If B is nonconformant or LU is singular, an Exception is raised.
   */
  Real solve (const std::vector<Real>& b, std::vector<Real>& x)  const throw (BadIntegerException, ZeroDivisionException)
  {
    /* Dimensions: A is mxn, X is nxk, B is mxk */

    if (b.dim1() != m)
    {
      throw BadIntegerException("Wrong dimension in LU::solve", b.dim1());
    }

    Real minD = NumTools::abs<Real>(LU(0, 0));
    for (size_t i = 1; i < m; i++)
    {
      Real currentValue = NumTools::abs<Real>(LU(i, i));
      if (currentValue < minD)
        minD = currentValue;
    }

    if (minD < NumConstants::SMALL())
    {
      throw ZeroDivisionException("Singular matrix in LU::solve.");
    }

    permuteCopy(b, piv, x);

    // Solve L*Y = B(piv)
    for (size_t k = 0; k < n; k++)
    {
      for (size_t i = k + 1; i < n; i++)
      {
        x[i] -= x[k] * LU(i, k);
      }
    }

    // Solve U*X = Y;
    // for (size_t k = n-1; k >= 0; k--) {
    size_t k = n;
    do
    {
      k--;
      x[k] /= LU(k, k);
      for (size_t i = 0; i < k; i++)
      {
        x[i] -= x[k] * LU(i, k);
      }
    }
    while (k > 0);

    return minD;
  }
}; /* class LU */
} // end of namespace bpp.

#endif // _LU_H_