19 for (
int i = 0; i <
size; ++i) {
24 std::valarray<double> scale(
N);
25 for (
int i = 0; i <
N; ++i) {
28 double epsilon = DBL_EPSILON;
32 for (
int i = 0; i <
N; ++i) {
34 for (
int j = 0; j <
N; ++j) {
36 if (norm2 > max_norm) {
39 if (max_norm < epsilon) {
46 for (
int r = 0; r < N - 1; ++r) {
49 for (
int i = r; i <
N; ++i) {
51 if (norm_ir > scale[i] * max) {
60 for (
int j = 0; j <
N; ++j) {
64 std::swap(scale[r], scale[p]);
67 double inv_rr_re =
m_lu[
re(r, r)] / max;
68 double inv_rr_im = -
m_lu[
im(r, r)] / max;
71 for (
int i = r + 1; i <
N; ++i) {
72 double ir_re =
m_lu[
re(i, r)];
73 double ir_im =
m_lu[
im(i, r)];
74 m_lu[
re(i, r)] = ir_re * inv_rr_re - ir_im * inv_rr_im;
75 m_lu[
im(i, r)] = ir_re * inv_rr_im + ir_im * inv_rr_re;
79 for (
int j = r + 1; j <
N; ++j) {
80 double& rj_re =
m_lu[
re(r, j)];
81 double& rj_im =
m_lu[
im(r, j)];
82 m_lu[
re(i, j)] -= ir_re * rj_re - ir_im * rj_im;
83 m_lu[
im(i, j)] -= ir_re * rj_im + ir_im * rj_re;
96 for (
int i = 0; i <
N; ++i) {
102 for (
int i = 0; i <
N; ++i) {
103 for (
int j = 0; j < i; ++j) {
110 for (
int i = N - 1; i >= 0; --i) {
111 for (
int j = i + 1; j <
N; ++j) {
116 double real = vec[
re(i)];
117 double imag = vec[
im(i)];
119 vec[
re(i)] = (real *
m_lu[
re(i, i)] + imag *
m_lu[
im(i, i)]) * inv_norm;
120 vec[
im(i)] = (imag *
m_lu[
re(i, i)] - real *
m_lu[
im(i, i)]) * inv_norm;
128 for (
int i = 0; i <
size; ++i) {
131 for (
int i = 0; i <
N; ++i) {
143 for (
int i = 0; i <
N; ++i) {
144 for (
int j = 0; j <
N; ++j) {
151 for (
int i = 0; i <
N; ++i) {
152 for (
int r = 0; r < i; ++r) {
153 double& luir_re =
m_lu[
re(i, r)];
154 double& luir_im =
m_lu[
im(i, r)];
155 for (
int j = 0; j <
N; ++j) {
156 mat[
re(i, j)] -= luir_re * mat[
re(r, j)] - luir_im * mat[
im(r, j)];
157 mat[
im(i, j)] -= luir_re * mat[
im(r, j)] + luir_im * mat[
re(r, j)];
163 for (
int i = N - 1; i >= 0; --i) {
164 for (
int r = i + 1; r <
N; ++r) {
165 double& luir_re =
m_lu[
re(i, r)];
166 double& luir_im =
m_lu[
im(i, r)];
167 for (
int j = 0; j <
N; ++j) {
168 mat[
re(i, j)] -= luir_re * mat[
re(r, j)] - luir_im * mat[
im(r, j)];
169 mat[
im(i, j)] -= luir_re * mat[
im(r, j)] + luir_im * mat[
re(r, j)];
172 for (
int j = 0; j <
N; ++j) {
174 double real = mat[
re(i, j)];
175 double imag = mat[
im(i, j)];
177 mat[
re(i, j)] = (real *
m_lu[
re(i, i)] + imag *
m_lu[
im(i, i)]) * inv;
178 mat[
im(i, j)] = (imag *
m_lu[
re(i, i)] - real *
m_lu[
im(i, i)]) * inv;
190 for (
int i = 0; i <
N; ++i) {
191 double old_re = real;
192 double old_im = imag;
193 real = old_re *
m_lu[
re(i, i)] - old_im *
m_lu[
im(i, i)];
194 imag = old_re * m_lu[
im(i, i)] + old_im * m_lu[
re(i, i)];
197 return cmplx(real, imag);
void mult_inverse(double *mat)
std::valarray< int > m_pivot
void get_inverse(double *mat_inv)
void set_matrix(const double *mat)
std::valarray< double > m_lu