File: test_linop_matrix.c

package info (click to toggle)
bart 0.9.00-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,040 kB
  • sloc: ansic: 116,116; python: 1,329; sh: 726; makefile: 639; javascript: 589; cpp: 106
file content (192 lines) | stat: -rw-r--r-- 4,132 bytes parent folder | download | duplicates (6)
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
/* Copyright 2017. Martin Uecker.
 * All rights reserved. Use of this source code is governed by
 * a BSD-style license which can be found in the LICENSE file.
 *
 * Authors:
 * 2017 Martin Uecker <martin.uecker@med.uni-goettingen.de>
 */

#include <complex.h>

#include "num/multind.h"
#include "num/flpmath.h"
#include "num/rand.h"

#include "linops/someops.h"
#include "linops/linop.h"
#include "linops/lintest.h"

#include "misc/misc.h"
#include "misc/debug.h"

#include "utest.h"




static bool test_linop_matrix(void)
{
	enum { N = 3 };

	int A = 2;
	int B = 3;
	int C = 4;

	long odims[N] = { C, 1, A };
	long idims1[N] = { 1, B, A };
	long idims2[N] = { C, B, 1 };

	complex float* dst1 = md_alloc(N, odims, CFL_SIZE);
	complex float* dst2 = md_alloc(N, odims, CFL_SIZE);
	complex float* src1 = md_alloc(N, idims1, CFL_SIZE);
	complex float* src2 = md_alloc(N, idims2, CFL_SIZE);

	md_gaussian_rand(N, odims, dst1); // test complete fill
	md_gaussian_rand(N, odims, dst2); // test complete fill
	md_gaussian_rand(N, idims1, src1);
	md_gaussian_rand(N, idims2, src2);

	struct linop_s* mat = linop_matrix_create(N, odims, idims2, idims1, src1);

	md_zmatmul(N, odims, dst1, idims1, src1, idims2, src2);

	linop_forward(mat, N, odims, dst2, N, idims2, src2);

	double err = md_znrmse(N, odims, dst2, dst1);

	linop_free(mat);

	md_free(src1);
	md_free(src2);
	md_free(dst1);
	md_free(dst2);

	return (err < UT_TOL);
}



static bool test_linop_matrix_adjoint(void)
{
	enum { N = 3 };

	int A = 2;
	int B = 3;
	int C = 4;

	long odims[N] = { C, 1, A };
	long idims1[N] = { 1, B, A };
	long idims2[N] = { C, B, 1 };

	complex float* src1 = md_alloc(N, idims1, CFL_SIZE);

	md_gaussian_rand(N, idims1, src1);

	struct linop_s* mat = linop_matrix_create(N, odims, idims2, idims1, src1);

	float diff = linop_test_adjoint(mat);

	debug_printf(DP_DEBUG1, "adjoint diff: %f\n", diff);

	bool ret = (diff < 1.E-4f);

	linop_free(mat);
	md_free(src1);

	return ret;
}


static bool test_linop_matrix_normal(void)
{
	enum { N = 3 };

	int A = 2;
	int B = 3;
	int C = 4;

	long odims[N] = { C, 1, A };
	long idims1[N] = { 1, B, A };
	long idims2[N] = { C, B, 1 };

	complex float* src1 = md_alloc(N, idims1, CFL_SIZE);

	md_gaussian_rand(N, idims1, src1);

	struct linop_s* mat = linop_matrix_create(N, odims, idims2, idims1, src1);

	float nrmse = linop_test_normal(mat);

	debug_printf(DP_DEBUG1, "normal nrmse: %f\n", nrmse);

	bool ret = (nrmse < 1.E-6f);

	linop_free(mat);
	md_free(src1);

	return ret;
}


static bool test_linop_matrix_chain(void)
{
	int A = 9;
	int B = 7;
	int C = 3;
	int D = 2;
	int E = 5;

	enum { N = 8 };
	long odims[N] =  { D, C, 1, 1, 1, 1, C, D };
	long idims0[N] = { D, 1, 1, A, E, 1, A, D };
	long idims1[N] = { D, 1, B, A, 1, B, A, D };
	long tdims[N] =  { D, 1, B, 1, E, B, 1, D };
	long idims2[N] = { D, C, B, 1, E, B, C, D };

	complex float* dst1 = md_alloc(N, odims, CFL_SIZE);
	complex float* dst2 = md_alloc(N, odims, CFL_SIZE);
	complex float* src0 = md_alloc(N, idims0, CFL_SIZE);
	complex float* src1 = md_alloc(N, idims1, CFL_SIZE);
	complex float* src2 = md_alloc(N, idims2, CFL_SIZE);

	md_gaussian_rand(N, odims, dst1); // test complete fill
	md_gaussian_rand(N, odims, dst2); // test complete fill
	md_gaussian_rand(N, idims0, src0);
	md_gaussian_rand(N, idims1, src1);
	md_gaussian_rand(N, idims2, src2);

	struct linop_s* mat1 = linop_matrix_create(N, tdims, idims0, idims1, src1);
	struct linop_s* mat2 = linop_matrix_create(N, odims, tdims, idims2, src2);

	struct linop_s* matA = linop_chain(mat1, mat2);

	linop_forward(matA, N, odims, dst1, N, idims0, src0);

	linop_free(matA);

	struct linop_s* matB = linop_matrix_chain(mat1, mat2);

	linop_forward(matB, N, odims, dst2, N, idims0, src0);

	linop_free(matB);

	double err = md_znrmse(N, odims, dst2, dst1);

	linop_free(mat1);
	linop_free(mat2);

	md_free(src0);
	md_free(src1);
	md_free(src2);
	md_free(dst1);
	md_free(dst2);

	return (err < 1.E-5);
}


UT_REGISTER_TEST(test_linop_matrix);
UT_REGISTER_TEST(test_linop_matrix_adjoint);
UT_REGISTER_TEST(test_linop_matrix_normal);
UT_REGISTER_TEST(test_linop_matrix_chain);