forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdallinearsystem.cpp
167 lines (157 loc) · 5.9 KB
/
gdallinearsystem.cpp
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
/******************************************************************************
*
* Project: GDAL
* Purpose: Linear system solver
* Author: VIZRT Development Team.
*
* This code was provided by Gilad Ronnen (gro at visrt dot com) with
* permission to reuse under the following license.
*
******************************************************************************
* Copyright (c) 2004, VIZRT Inc.
* Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
* Copyright (c) 2019, Martin Franzke <martin dot franzke at telekom dot de>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
/*! @cond Doxygen_Suppress */
#include "cpl_port.h"
#include "cpl_conv.h"
#include "gdallinearsystem.h"
#ifdef HAVE_ARMADILLO
#include "armadillo_headers.h"
#endif
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <cmath>
CPL_CVSID("$Id$")
#ifndef HAVE_ARMADILLO
namespace
{
// LU decomposition of the quadratic matrix A
// see https://en.wikipedia.org/wiki/LU_decomposition#C_code_examples
bool solve( GDALMatrix & A, GDALMatrix & RHS, GDALMatrix & X, double eps )
{
assert(A.getNumRows() == A.getNumCols());
if(eps < 0) return false;
int const m = A.getNumRows();
int const n = RHS.getNumCols();
// row permutations
std::vector<int> perm(m);
for(int iRow = 0; iRow < m; ++iRow)
perm[iRow] = iRow;
for(int step = 0; step < m - 1; ++step)
{
// determine pivot element
int iMax = step;
double dMax = std::abs(A(step, step));
for(int i = step + 1; i < m; ++i)
{
if(std::abs(A(i, step)) > dMax)
{
iMax = i;
dMax = std::abs(A(i, step));
}
}
if(dMax <= eps)
{
CPLError( CE_Failure, CPLE_AppDefined, "GDALLinearSystemSolve: matrix not invertible" );
return false;
}
// swap rows
if(iMax != step)
{
std::swap(perm[iMax], perm[step]);
for(int iCol = 0; iCol < m; ++iCol)
{
std::swap(A(iMax, iCol), A(step, iCol));
}
}
for(int iRow = step + 1; iRow < m; ++iRow)
{
A(iRow, step) /= A(step, step);
}
for(int iCol = step + 1; iCol < m; ++iCol)
{
for(int iRow = step + 1; iRow < m; ++iRow)
{
A(iRow, iCol) -= A(iRow, step) * A(step, iCol);
}
}
}
// LUP solve;
for(int iCol = 0; iCol < n; ++iCol)
{
for (int iRow = 0; iRow < m; ++iRow)
{
X(iRow, iCol) = RHS(perm[iRow], iCol);
for (int k = 0; k < iRow; ++k)
{
X(iRow, iCol) -= A(iRow, k) * X(k, iCol);
}
}
for (int iRow = m - 1; iRow >= 0; --iRow)
{
for (int k = iRow + 1; k < m; ++k)
{
X(iRow, iCol) -= A(iRow, k) * X(k, iCol);
}
X(iRow, iCol) /= A(iRow, iRow);
}
}
return true;
}
}
#endif
/************************************************************************/
/* GDALLinearSystemSolve() */
/* */
/* Solves the linear system A*X_i = RHS_i for each column i */
/* where A is a square matrix. */
/************************************************************************/
bool GDALLinearSystemSolve( GDALMatrix & A, GDALMatrix & RHS, GDALMatrix & X )
{
assert(A.getNumRows() == RHS.getNumRows());
assert(A.getNumCols() == X.getNumRows());
assert(RHS.getNumCols() == X.getNumCols());
try
{
#ifdef HAVE_ARMADILLO
arma::mat matA( A.data(), A.getNumRows(), A.getNumCols(), false, true );
arma::mat matRHS(RHS.data(), RHS.getNumRows(), RHS.getNumCols(), false, true );
arma::mat matOut(X.data(), X.getNumRows(), X.getNumCols(), false, true);
#if ARMA_VERSION_MAJOR > 6 || (ARMA_VERSION_MAJOR == 6 && ARMA_VERSION_MINOR >= 500 )
// Perhaps available in earlier versions, but didn't check
return arma::solve( matOut, matA, matRHS,
arma::solve_opts::equilibrate + arma::solve_opts::no_approx);
#else
return arma::solve( matOut, matA, matRHS );
#endif
#else //HAVE_ARMADILLO
return solve(A, RHS, X, 0);
#endif
}
catch(std::exception const & e) {
CPLError( CE_Failure, CPLE_AppDefined,
"GDALLinearSystemSolve: %s", e.what() );
return false;
}
}
/*! @endcond */