65 n(A.
ncols()), m(A.
nrows()), Q(A),
R(
n,
n, 0.0), QtB(B), S(
n,
n, 0.0),
66 column_norm(
n, 0.0), piv_col(
n, 0) {
67 assert(B.
size() >= m);
71 for(
int j = 0; j <
n; ++j) {
73 for(
int i = 0; i < m; ++i) temp += Q(i, j) * Q(i, j);
74 dot(j) = column_norm(j) =
sqrt(temp);
80 for(
int j = 0; j <
std::min(m, n); ++j) {
85 for(
int k = j + 1; k <
n; ++k) {
86 if(
dot(k) >
dot(kmax)) kmax = k;
90 std::swap_ranges(Q.col_begin(j), Q.col_end(j), Q.col_begin(kmax));
92 std::swap(piv_col(j), piv_col(kmax));
99 for(
int k = j; k < m; ++k) unorm += Q(k, j) * Q(k, j);
100 unorm = (Q(j, j) > 0.0) ?
sqrt(unorm) : -
sqrt(unorm);
103 for(
int i = 0; i < j; ++i) {
111 for(
int i = j; i < m; ++i) {
117 for(
int k = j + 1; k <
n; ++k) {
120 for(
int i = j; i < m; ++i) sum += Q(i, j) * Q(i, k);
122 for(
int i = j; i < m; ++i) Q(i, k) -= sum * Q(i, j);
127 for(
int i = j + 1; i < m; ++i) sum += Q(i, k) * Q(i, k);
135 for(
int i = j; i < m; ++i) sum += Q(i, j) * QtB(i);
137 for(
int i = j; i < m; ++i) QtB(i) -= Q(i, j) *
sum;
156 double root =
sqrt(p);
157 for(
int i = 0; i <
n; ++i) M(i) *= root;
182 assert(D.
size() >=
n);
186 for(
int j = 0; j <
n; ++j) {
200 for(
int k = j; k <
n; ++k) {
203 if(row_D(k) != 0.0) {
204 double t =
sqrt(
S(k, k) *
S(k, k) + row_D(k) * row_D(k));
205 double s = row_D(k) / t;
206 double c =
S(k, k) / t;
211 t = c * Z(k) + s * QtBpj;
212 QtBpj = -s * Z(k) + c * QtBpj;
216 for(
int i = k + 1; i <
n; ++i) {
217 t = c *
S(k, i) + s * row_D(i);
218 row_D(i) = -s *
S(k, i) + c * row_D(i);
236 for(
int j = 0; j <
n; ++j) {
237 if(
R(j, j) == 0.0 && rank == n) rank = j;
238 if(rank < n) Z(j) = 0.0;
241 for(
int j = rank; j-- > 0;) {
243 for(
int i = j + 1; i < rank; ++i) {
244 sum -=
R(j, i) * Z(i);
246 Z(j) = sum /
R(j, j);
251 for(
int j = 0; j <
n; ++j) {
258 assert(V.
size() ==
n);
260 for(
int i = 0; i <
n; ++i)
X(i) = V(
piv_col(i));
262 for(
int i = 0; i <
n; ++i) {
263 if(
R(i, i) == 0.0)
break;
264 for(
int j = 0; j < i; ++j)
X(j) -=
R(i, j) * V(i);
void R_to_S(const Matrix< double > &R, const Array1D< double > &D, Matrix< double > &S, Vector< double > &Z)
Array1D< double > column_norm
void solveRT(Vector< double > &V) const
Pre-multiply the vector $V$ by $R.transpose()^{-1}$.
int ncols() const
Get number of columns.
void getColNorm(Array1D< double > &) const
Return the original column norms of the matrix $A$.
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
void solveST(Vector< double > &V) const
Pre-multiply the vector $V$ by $S.transpose()^{-1}$.
int size() const
Get array size.
void solveR(Vector< double > &X) const
Solution of $A*X = B$ in the least-squares sense.
constexpr double c
The velocity of light in m/s.
QRSolver(const Matrix< double > &A, const Vector< double > &B, bool pivot)
Constructor.
Tps< T > sqrt(const Tps< T > &x)
Square root.
void solve(const Matrix< double > &R, const Vector< double > &QtB, Vector< double > &X) const
void solveT(const Matrix< double > &R, Vector< double > &V) const
void solveS(const Array1D< double > &D, double p, Vector< double > &X)
Solution of $A*X = B, D*X = 0$ in the least-squares sense.
int nrows() const
Get number of rows.
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)