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
//! `faer` provides utilities for computing and manipulating the $QR$
//! factorization with and without pivoting. the $QR$ factorization decomposes a
//! matrix into a product of a unitary matrix $Q$ (represented using block
//! householder sequences), and an upper trapezoidal matrix $R$, such that their
//! product is equal to the original matrix (or a column permutation of it in
//! the case where column pivoting is used)
//!
//! # example
//!
//! assume we have an overdetermined system $Ax = b$ with full rank, and that we
//! wish to find the solution that minimizes the 2-norm
//!
//! this is equivalent to computing a matrix $x$ that minimizes the value $||Ax
//! - b||^2$, which is given by the solution $$x = (A^H A)^{-1} A^H b$$
//!
//! if we compute the $QR$ decomposition of $A$, such that $A = QR =
//! Q_{\text{thin}} R_{\text{thin}}$, then we get $$x = R_{\text{thin}}^{-1}
//! Q_{\text{thin}}^H b$$
//!
//! to translate this to code, we can proceed as follows:
//!
//! ```
//! use faer::dyn_stack::{MemBuffer, MemStack, StackReq};
//! use faer::linalg::qr::no_pivoting::factor;
//! use faer::linalg::{householder, triangular_solve};
//! use faer::reborrow::*;
//! use faer::{Conj, Mat, Par, mat};
//! // we start by defining matrices A and B that define our least-squares problem.
//! let a = mat![
//! [-1.14920683, -1.67950492],
//! [-0.93009756, -0.03885086],
//! [1.22579735, 0.88489976],
//! [0.70698973, 0.38928314],
//! [-1.66293762, 0.38123281],
//! [0.27639595, -0.32559289],
//! [-0.37506387, -0.13180778],
//! [-1.20774962, -0.38635657],
//! [0.44373549, 0.84397648],
//! [-1.96779374, -1.42751757_f64],
//! ];
//! let b = mat![
//! [-0.14689786, -0.52845138, -2.26975669],
//! [-1.00844774, -1.38550214, 0.50329459],
//! [1.07941646, 0.71514245, -0.73987761],
//! [0.1281168, -0.23999022, 1.58776697],
//! [-0.49385283, 1.17875407, 2.01019076],
//! [0.65117811, -0.60339895, 0.27217694],
//! [0.85599951, -0.00699227, 0.93607199],
//! [-0.12635444, 0.94945626, 0.86565968],
//! [0.02383305, 0.41515805, -1.2816278],
//! [0.34158312, -0.07552168, 0.56724015_f64],
//! ];
//! // approximate solution computed with numpy
//! let expected_solution = mat![
//! [0.33960324, -0.33812452, -0.8458301], //
//! [-0.25718351, 0.6281214, 1.07071764_f64],
//! ];
//! let rank = Ord::min(a.nrows(), a.ncols());
//! // we choose the recommended block size for the householder factors of our problem.
//! let block_size = factor::recommended_block_size::<f64>(a.nrows(), a.ncols());
//! // we allocate the memory for the operations that we perform
//! let mut mem =
//! MemBuffer::new(StackReq::any_of(&[
//! factor::qr_in_place_scratch::<f64>(
//! a.nrows(),
//! a.ncols(),
//! block_size,
//! Par::Seq,
//! Default::default(),
//! ),
//! householder::apply_block_householder_sequence_transpose_on_the_left_in_place_scratch::<
//! f64,
//! >(a.nrows(), block_size, b.ncols()),
//! ]));
//! let mut stack = MemStack::new(&mut mem);
//! let mut qr = a;
//! let mut h_factor = Mat::zeros(block_size, rank);
//! factor::qr_in_place(
//! qr.as_mut(),
//! h_factor.as_mut(),
//! Par::Seq,
//! stack.rb_mut(),
//! Default::default(),
//! );
//! // now the householder bases are in the strictly lower trapezoidal part of `a`, and the
//! // matrix R is in the upper triangular part of `a`.
//! let mut solution = b.clone();
//! // compute Q^H×B
//! householder::apply_block_householder_sequence_transpose_on_the_left_in_place_with_conj(
//! qr.as_ref(),
//! h_factor.as_ref(),
//! Conj::Yes,
//! solution.as_mut(),
//! Par::Seq,
//! stack.rb_mut(),
//! );
//! solution.truncate(rank, b.ncols());
//! // compute R_thin^{-1} Q_thin^H×B
//! triangular_solve::solve_upper_triangular_in_place(
//! qr.as_ref().split_at_row(rank).0,
//! solution.as_mut(),
//! Par::Seq,
//! );
//! for i in 0..rank {
//! for j in 0..b.ncols() {
//! assert!((solution[(i, j)] - expected_solution[(i, j)]).abs() <= 1e-6);
//! }
//! }
//! ```