OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestGSLMatrix.cpp
Go to the documentation of this file.
1
61#include <gtest/gtest.h>
62#include <cmath>
64#include "Utilities/GSLMatrix.h"
65
66class GSLMatrixTest : public ::testing::Test {
67protected:
68 void SetUp() override {
69 // Test setup
70 }
71};
72
73TEST_F(GSLMatrixTest, MatrixAllocation) {
75 EXPECT_EQ(m->size1, 3);
76 EXPECT_EQ(m->size2, 4);
77 EXPECT_NE(m->data, nullptr);
78
80}
81
82TEST_F(GSLMatrixTest, MatrixSetGet) {
84
85 gsl_matrix_set(m, 0, 0, 1.0);
86 gsl_matrix_set(m, 0, 1, 2.0);
87 gsl_matrix_set(m, 1, 0, 3.0);
88 gsl_matrix_set(m, 1, 1, 4.0);
89
90 EXPECT_DOUBLE_EQ(gsl_matrix_get(m, 0, 0), 1.0);
91 EXPECT_DOUBLE_EQ(gsl_matrix_get(m, 0, 1), 2.0);
92 EXPECT_DOUBLE_EQ(gsl_matrix_get(m, 1, 0), 3.0);
93 EXPECT_DOUBLE_EQ(gsl_matrix_get(m, 1, 1), 4.0);
94
96}
97
98TEST_F(GSLMatrixTest, MatrixSetAll) {
100 gsl_matrix_set_all(m, 5.0);
101
102 for (size_t i = 0; i < 2; ++i) {
103 for (size_t j = 0; j < 2; ++j) {
104 EXPECT_DOUBLE_EQ(gsl_matrix_get(m, i, j), 5.0);
105 }
106 }
107
109}
110
111TEST_F(GSLMatrixTest, MatrixSetZero) {
112 gsl_matrix* m = gsl_matrix_alloc(2, 2);
113 gsl_matrix_set_all(m, 1.0);
115
116 for (size_t i = 0; i < 2; ++i) {
117 for (size_t j = 0; j < 2; ++j) {
118 EXPECT_DOUBLE_EQ(gsl_matrix_get(m, i, j), 0.0);
119 }
120 }
121
123}
124
125TEST_F(GSLMatrixTest, MatrixSetIdentity) {
126 gsl_matrix* m = gsl_matrix_alloc(3, 3);
128
129 for (size_t i = 0; i < 3; ++i) {
130 for (size_t j = 0; j < 3; ++j) {
131 double expected = (i == j) ? 1.0 : 0.0;
132 EXPECT_DOUBLE_EQ(gsl_matrix_get(m, i, j), expected);
133 }
134 }
135
137}
138
139TEST_F(GSLMatrixTest, VectorAllocation) {
141 EXPECT_EQ(v->size, 5);
142 EXPECT_NE(v->data, nullptr);
143
145}
146
147TEST_F(GSLMatrixTest, VectorSetGet) {
149
150 gsl_vector_set(v, 0, 1.0);
151 gsl_vector_set(v, 2, 3.0);
152 gsl_vector_set(v, 4, 5.0);
153
154 EXPECT_DOUBLE_EQ(gsl_vector_get(v, 0), 1.0);
155 EXPECT_DOUBLE_EQ(gsl_vector_get(v, 2), 3.0);
156 EXPECT_DOUBLE_EQ(gsl_vector_get(v, 4), 5.0);
157
159}
160
161TEST_F(GSLMatrixTest, VectorSetAll) {
163 gsl_vector_set_all(v, 2.5);
164
165 for (size_t i = 0; i < 4; ++i) {
166 EXPECT_DOUBLE_EQ(gsl_vector_get(v, i), 2.5);
167 }
168
170}
171
172TEST_F(GSLMatrixTest, VectorScale) {
174 gsl_vector_set(v, 0, 1.0);
175 gsl_vector_set(v, 1, 2.0);
176 gsl_vector_set(v, 2, 3.0);
177
178 gsl_vector_scale(v, 2.0);
179
180 EXPECT_DOUBLE_EQ(gsl_vector_get(v, 0), 2.0);
181 EXPECT_DOUBLE_EQ(gsl_vector_get(v, 1), 4.0);
182 EXPECT_DOUBLE_EQ(gsl_vector_get(v, 2), 6.0);
183
185}
186
190
191 gsl_vector_set(v1, 0, 1.0);
192 gsl_vector_set(v1, 1, 2.0);
193 gsl_vector_set(v1, 2, 3.0);
194
195 gsl_vector_set(v2, 0, 4.0);
196 gsl_vector_set(v2, 1, 5.0);
197 gsl_vector_set(v2, 2, 6.0);
198
199 gsl_vector_add(v1, v2);
200
201 EXPECT_DOUBLE_EQ(gsl_vector_get(v1, 0), 5.0);
202 EXPECT_DOUBLE_EQ(gsl_vector_get(v1, 1), 7.0);
203 EXPECT_DOUBLE_EQ(gsl_vector_get(v1, 2), 9.0);
204
205 gsl_vector_free(v1);
206 gsl_vector_free(v2);
207}
208
209TEST_F(GSLMatrixTest, ComplexMatrix) {
211
212 gsl_complex z1 = gsl_complex_rect(1.0, 2.0);
213 gsl_complex z2 = gsl_complex_rect(3.0, 4.0);
214
215 gsl_matrix_complex_set(m, 0, 0, z1);
216 gsl_matrix_complex_set(m, 0, 1, z2);
217
218 gsl_complex result = gsl_matrix_complex_get(m, 0, 0);
219 EXPECT_DOUBLE_EQ(GSL_REAL(result), 1.0);
220 EXPECT_DOUBLE_EQ(GSL_IMAG(result), 2.0);
221
222 result = gsl_matrix_complex_get(m, 0, 1);
223 EXPECT_DOUBLE_EQ(GSL_REAL(result), 3.0);
224 EXPECT_DOUBLE_EQ(GSL_IMAG(result), 4.0);
225
227}
228
229TEST_F(GSLMatrixTest, ComplexVector) {
231
232 gsl_complex z = gsl_complex_rect(1.0, 2.0);
233 gsl_vector_complex_set(v, 1, z);
234
235 gsl_complex result = gsl_vector_complex_get(v, 1);
236 EXPECT_DOUBLE_EQ(GSL_REAL(result), 1.0);
237 EXPECT_DOUBLE_EQ(GSL_IMAG(result), 2.0);
238
240 result = gsl_vector_complex_get(v, 1);
241 EXPECT_DOUBLE_EQ(GSL_REAL(result), 2.0);
242 EXPECT_DOUBLE_EQ(GSL_IMAG(result), 4.0);
243
245}
#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
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_vector_free(gsl_vector *v)
Free a vector allocated by gsl_vector_alloc.
Definition GSLMatrix.h:160
void gsl_vector_scale(gsl_vector *v, double x)
Scale a vector in-place.
Definition GSLMatrix.h:401
gsl_vector * gsl_vector_alloc(size_t n)
Allocate a zero-initialized real vector of length .
Definition GSLMatrix.h:147
void gsl_matrix_complex_free(gsl_matrix_complex *m)
Free a matrix allocated by gsl_matrix_complex_alloc.
Definition GSLMatrix.h:136
void gsl_vector_complex_scale(gsl_vector_complex *v, gsl_complex x)
Scale a complex vector in-place.
Definition GSLMatrix.h:412
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
gsl_complex gsl_vector_complex_get(const gsl_vector_complex *v, size_t i)
Get complex entry .
Definition GSLMatrix.h:566
void gsl_vector_set_all(gsl_vector *v, double x)
Set all entries to x.
Definition GSLMatrix.h:541
double gsl_vector_get(const gsl_vector *v, size_t i)
Get .
Definition GSLMatrix.h:535
gsl_vector_complex * gsl_vector_complex_alloc(size_t n)
Allocate a zero-initialized complex vector of length .
Definition GSLMatrix.h:171
void gsl_matrix_set_all(gsl_matrix *m, double x)
Set all entries to x.
Definition GSLMatrix.h:471
void gsl_matrix_set_identity(gsl_matrix *m)
Set to identity on the main diagonal.
Definition GSLMatrix.h:486
void gsl_vector_complex_set(gsl_vector_complex *v, size_t i, gsl_complex x)
Set complex entry .
Definition GSLMatrix.h:557
void gsl_vector_complex_free(gsl_vector_complex *v)
Free a vector allocated by gsl_vector_complex_alloc.
Definition GSLMatrix.h:186
void gsl_vector_set(gsl_vector *v, size_t i, double x)
Set .
Definition GSLMatrix.h:528
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
void gsl_vector_add(gsl_vector *a, const gsl_vector *b)
Add another vector in-place.
Definition GSLMatrix.h:423
void gsl_matrix_set_zero(gsl_matrix *m)
Set all entries to zero.
Definition GSLMatrix.h:480
TEST_F(GSLMatrixTest, MatrixAllocation)
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
size_t size1
Definition GSLMatrix.h:30
double * data
Definition GSLMatrix.h:33
size_t size2
Definition GSLMatrix.h:31
Dense complex vector with stride.
Definition GSLMatrix.h:76
Dense real vector with stride.
Definition GSLMatrix.h:61
double * data
Definition GSLMatrix.h:64
size_t size
Definition GSLMatrix.h:62