OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestGSLLinalg.cpp
Go to the documentation of this file.
1
45#include <gtest/gtest.h>
46#include <cmath>
48#include "Utilities/GSLLinalg.h"
49#include "Utilities/GSLMatrix.h"
50
51class GSLLinalgTest : public ::testing::Test {
52protected:
53 void SetUp() override {
54 // Test setup
55 }
56};
57
61 int signum;
62
63 // A = [2 1 0]
64 // [1 2 1]
65 // [0 1 2]
66 gsl_matrix_set(A, 0, 0, 2.0);
67 gsl_matrix_set(A, 0, 1, 1.0);
68 gsl_matrix_set(A, 0, 2, 0.0);
69 gsl_matrix_set(A, 1, 0, 1.0);
70 gsl_matrix_set(A, 1, 1, 2.0);
71 gsl_matrix_set(A, 1, 2, 1.0);
72 gsl_matrix_set(A, 2, 0, 0.0);
73 gsl_matrix_set(A, 2, 1, 1.0);
74 gsl_matrix_set(A, 2, 2, 2.0);
75
76 int result = gsl_linalg_LU_decomp(A, p, &signum);
77 EXPECT_EQ(result, 0);
78
81}
82
86 int signum;
87
88 // A = [1 2]
89 // [3 4]
90 // det(A) = 1*4 - 2*3 = -2
91 gsl_matrix_set(A, 0, 0, 1.0);
92 gsl_matrix_set(A, 0, 1, 2.0);
93 gsl_matrix_set(A, 1, 0, 3.0);
94 gsl_matrix_set(A, 1, 1, 4.0);
95
96 gsl_linalg_LU_decomp(A, p, &signum);
97 double det = gsl_linalg_LU_det(A, signum);
98
99 EXPECT_NEAR(det, -2.0, 1e-10);
100
103}
104
106 gsl_matrix* A = gsl_matrix_alloc(2, 2);
107 gsl_matrix* invA = gsl_matrix_alloc(2, 2);
109 int signum;
110
111 // A = [1 2]
112 // [3 4]
113 // A^-1 = [-2 1]
114 // [1.5 -0.5]
115 gsl_matrix_set(A, 0, 0, 1.0);
116 gsl_matrix_set(A, 0, 1, 2.0);
117 gsl_matrix_set(A, 1, 0, 3.0);
118 gsl_matrix_set(A, 1, 1, 4.0);
119
120 gsl_linalg_LU_decomp(A, p, &signum);
121 gsl_linalg_LU_invert(A, p, invA);
122
123 EXPECT_NEAR(gsl_matrix_get(invA, 0, 0), -2.0, 1e-10);
124 EXPECT_NEAR(gsl_matrix_get(invA, 0, 1), 1.0, 1e-10);
125 EXPECT_NEAR(gsl_matrix_get(invA, 1, 0), 1.5, 1e-10);
126 EXPECT_NEAR(gsl_matrix_get(invA, 1, 1), -0.5, 1e-10);
127
129 gsl_matrix_free(invA);
131}
132
133TEST_F(GSLLinalgTest, LUInvertIdentity) {
134 gsl_matrix* A = gsl_matrix_alloc(3, 3);
135 gsl_matrix* invA = gsl_matrix_alloc(3, 3);
137 int signum;
138
140
141 gsl_linalg_LU_decomp(A, p, &signum);
142 gsl_linalg_LU_invert(A, p, invA);
143
144 // I^-1 = I
145 for (size_t i = 0; i < 3; ++i) {
146 for (size_t j = 0; j < 3; ++j) {
147 double expected = (i == j) ? 1.0 : 0.0;
148 EXPECT_NEAR(gsl_matrix_get(invA, i, j), expected, 1e-10);
149 }
150 }
151
153 gsl_matrix_free(invA);
155}
156
157TEST_F(GSLLinalgTest, ComplexLUDecomp) {
160 int signum;
161
162 gsl_complex z1 = gsl_complex_rect(1.0, 0.0);
163 gsl_complex z2 = gsl_complex_rect(0.0, 0.0);
164
165 gsl_matrix_complex_set(A, 0, 0, z1);
166 gsl_matrix_complex_set(A, 0, 1, z2);
167 gsl_matrix_complex_set(A, 1, 0, z2);
168 gsl_matrix_complex_set(A, 1, 1, z1);
169
170 int result = gsl_linalg_LU_decomp_complex(A, p, &signum);
171 EXPECT_EQ(result, 0);
172
175}
176
177TEST_F(GSLLinalgTest, ComplexLUDet) {
180 int signum;
181
182 // Identity matrix: det = 1
183 gsl_complex z1 = gsl_complex_rect(1.0, 0.0);
184 gsl_complex z2 = gsl_complex_rect(0.0, 0.0);
185
186 gsl_matrix_complex_set(A, 0, 0, z1);
187 gsl_matrix_complex_set(A, 0, 1, z2);
188 gsl_matrix_complex_set(A, 1, 0, z2);
189 gsl_matrix_complex_set(A, 1, 1, z1);
190
191 gsl_linalg_LU_decomp_complex(A, p, &signum);
192 gsl_complex det = gsl_linalg_LU_det_complex(A, signum);
193
194 EXPECT_NEAR(GSL_REAL(det), 1.0, 1e-10);
195 EXPECT_NEAR(GSL_IMAG(det), 0.0, 1e-10);
196
199}
200
201TEST_F(GSLLinalgTest, ComplexLUInvert) {
205 int signum;
206
207 // Identity matrix
208 gsl_complex z1 = gsl_complex_rect(1.0, 0.0);
209 gsl_complex z2 = gsl_complex_rect(0.0, 0.0);
210
211 gsl_matrix_complex_set(A, 0, 0, z1);
212 gsl_matrix_complex_set(A, 0, 1, z2);
213 gsl_matrix_complex_set(A, 1, 0, z2);
214 gsl_matrix_complex_set(A, 1, 1, z1);
215
216 gsl_linalg_LU_decomp_complex(A, p, &signum);
218
219 // I^-1 = I
220 gsl_complex result = gsl_matrix_complex_get(invA, 0, 0);
221 EXPECT_NEAR(GSL_REAL(result), 1.0, 1e-10);
222 EXPECT_NEAR(GSL_IMAG(result), 0.0, 1e-10);
223
227}
#define GSL_REAL(z)
Definition GSLCompat.h:117
#define GSL_IMAG(z)
Definition GSLCompat.h:118
gsl_complex gsl_complex_rect(double x, double y)
Construct .
Definition GSLComplex.h:49
int gsl_linalg_LU_decomp_complex(gsl_matrix_complex *A, gsl_permutation *p, int *signum)
Alias for gsl_linalg_complex_LU_decomp.
Definition GSLLinalg.h:319
int gsl_linalg_LU_invert_complex(const gsl_matrix_complex *LU, const gsl_permutation *p, gsl_matrix_complex *inverse)
Alias for gsl_linalg_complex_LU_invert.
Definition GSLLinalg.h:336
void gsl_permutation_free(gsl_permutation *p)
Free a permutation allocated by gsl_permutation_alloc.
Definition GSLLinalg.h:52
int gsl_linalg_LU_invert(const gsl_matrix *LU, const gsl_permutation *p, gsl_matrix *inverse)
Invert a matrix from its LU decomposition (real).
Definition GSLLinalg.h:139
gsl_permutation * gsl_permutation_alloc(size_t n)
Allocate an identity permutation of size .
Definition GSLLinalg.h:40
gsl_complex gsl_linalg_LU_det_complex(const gsl_matrix_complex *LU, int signum)
Alias for gsl_linalg_complex_LU_det.
Definition GSLLinalg.h:327
double gsl_linalg_LU_det(const gsl_matrix *LU, int signum)
Determinant from LU decomposition (real).
Definition GSLLinalg.h:124
int gsl_linalg_LU_decomp(gsl_matrix *A, gsl_permutation *p, int *signum)
In-place LU decomposition with partial pivoting (real).
Definition GSLLinalg.h:66
gsl_matrix * gsl_matrix_alloc(size_t n1, size_t n2)
Allocate a zero-initialized real matrix of size .
Definition GSLMatrix.h:94
void gsl_matrix_set(gsl_matrix *m, size_t i, size_t j, double x)
Set .
Definition GSLMatrix.h:453
double gsl_matrix_get(const gsl_matrix *m, size_t i, size_t j)
Get .
Definition GSLMatrix.h:463
gsl_complex gsl_matrix_complex_get(const gsl_matrix_complex *m, size_t i, size_t j)
Get complex entry .
Definition GSLMatrix.h:510
void gsl_matrix_free(gsl_matrix *m)
Free a matrix allocated by gsl_matrix_alloc.
Definition GSLMatrix.h:108
void gsl_matrix_complex_free(gsl_matrix_complex *m)
Free a matrix allocated by gsl_matrix_complex_alloc.
Definition GSLMatrix.h:136
void gsl_matrix_complex_set(gsl_matrix_complex *m, size_t i, size_t j, gsl_complex x)
Set complex entry .
Definition GSLMatrix.h:500
void gsl_matrix_set_identity(gsl_matrix *m)
Set to identity on the main diagonal.
Definition GSLMatrix.h:486
gsl_matrix_complex * gsl_matrix_complex_alloc(size_t n1, size_t n2)
Allocate a zero-initialized complex matrix of size .
Definition GSLMatrix.h:120
TEST_F(GSLLinalgTest, LUDecomp)
void SetUp() override
Complex number stored as .
Definition GSLComplex.h:27
Dense complex matrix in row-major storage.
Definition GSLMatrix.h:45
Dense real matrix in row-major storage.
Definition GSLMatrix.h:29
Permutation vector for LU decomposition.
Definition GSLLinalg.h:30