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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* Copyright 2018-2019. 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:
* 2018-2019 Martin Uecker <martin.uecker@med.uni-goettingen.de>
*/
#include <complex.h>
#include <math.h>
#include "num/multind.h"
#include "num/flpmath.h"
#include "num/rand.h"
#include "num/ops.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_plus(void)
{
enum { N = 3 };
long dims[N] = { 8, 4, 2 };
complex float val1a = 2.;
complex float val1b = 3.;
struct linop_s* diaga = linop_cdiag_create(N, dims, 0, &val1a);
struct linop_s* diagb = linop_cdiag_create(N, dims, 0, &val1b);
struct linop_s* plus = linop_plus(diaga, diagb);
complex float val2 = 5.;
struct linop_s* diag2 = linop_cdiag_create(N, dims, 0, &val2);
complex float* in = md_alloc(N, dims, CFL_SIZE);
complex float* dst1 = md_alloc(N, dims, CFL_SIZE);
complex float* dst2 = md_alloc(N, dims, CFL_SIZE);
md_gaussian_rand(N, dims, in);
linop_forward(plus, N, dims, dst1, N, dims, in);
linop_forward(diag2, N, dims, dst2, N, dims, in);
double err = md_znrmse(N, dims, dst1, dst2);
linop_free(diaga);
linop_free(diagb);
linop_free(plus);
linop_free(diag2);
md_free(in);
md_free(dst1);
md_free(dst2);
return (err < UT_TOL);
}
UT_REGISTER_TEST(test_linop_plus);
static bool test_linop_stack(void)
{
enum { N = 3 };
long dims[N] = { 8, 4, 1 };
long dims2[N] = { 8, 4, 2 };
complex float val1a = 2.;
complex float val1b = 3.;
struct linop_s* diaga = linop_cdiag_create(N, dims, 0, &val1a);
struct linop_s* diagb = linop_cdiag_create(N, dims, 0, &val1b);
struct linop_s* stack = linop_stack(2, 2, diaga, diagb);
complex float* in = md_alloc(N, dims2, CFL_SIZE);
complex float* out = md_alloc(N, dims2, CFL_SIZE);
md_zfill(N, dims2, in, 1.);
bool ok = true;
double n, n2, err;
linop_forward(stack, N, dims2, out, N, dims2, in);
n = powf(md_znorm(N, dims2, out), 2);
n2 = (powf(val1a, 2.) + powf(val1b, 2.)) * md_calc_size(N, dims);
err = fabs(n - n2);
#ifdef __clang__
ok &= (err < 100. * UT_TOL);
#else
ok &= (err < UT_TOL);
#endif
linop_adjoint(stack, N, dims2, out, N, dims2, in);
n = powf(md_znorm(N, dims2, out), 2);
n2 = (powf(val1a, 2.) + powf(val1b, 2.)) * md_calc_size(N, dims);
err = fabs(n - n2);
#ifdef __clang__
ok &= (err < 100. * UT_TOL);
#else
ok &= (err < UT_TOL);
#endif
linop_normal(stack, N, dims2, out, in);
n = powf(md_znorm(N, dims2, out), 2);
n2 = (powf(val1a, 4.) + powf(val1b, 4.)) * md_calc_size(N, dims);
err = fabs(n - n2);
ok &= (err < 1.E-3);
linop_free(diaga);
linop_free(diagb);
linop_free(stack);
md_free(in);
md_free(out);
return ok;
}
UT_REGISTER_TEST(test_linop_stack);
static bool test_linop_null(void)
{
long dims[1] = { 5 };
const struct linop_s* l = linop_null_create(1, dims, 1, dims);
bool ok = true;
ok &= operator_zero_or_null_p(l->forward);
ok &= operator_zero_or_null_p(l->adjoint);
ok &= operator_zero_or_null_p(l->normal);
linop_free(l);
return ok;
}
UT_REGISTER_TEST(test_linop_null);
static bool test_linop_extract(void)
{
enum { N = 4 };
long dims[N] = { 8, 4, 6, 4};
long dims2[N] = { 8, 4, 2, 4};
complex float val1a = 2.;
long pos[N];
for (int i = 0; i < N; i++)
pos[i] = 0;
struct linop_s* diaga = linop_cdiag_create(N, dims, 0, &val1a);
struct linop_s* extract = linop_extract_create(N, pos, dims2, dims);
struct linop_s* diaga1 = linop_chain_FF(diaga, extract);
complex float* in = md_alloc(N, dims, CFL_SIZE);
complex float* out = md_alloc(N, dims2, CFL_SIZE);
md_zfill(N, dims, in, 1.);
bool ok = true;
double n, n2, err;
linop_forward(diaga1, N, dims2, out, N, dims, in);
n = powf(md_znorm(N, dims2, out), 2);
n2 = powf(val1a, 2.) * md_calc_size(N, dims2);
err = fabs(n - n2);
#ifdef __clang__
ok &= (err < 500. * UT_TOL);
#else
ok &= (err < UT_TOL);
#endif
md_zfill(N, dims2, out, 1.);
linop_adjoint(diaga1, N, dims, in, N, dims2, out);
n = powf(md_znorm(N, dims, in), 2);
n2 = powf(val1a, 2.) * md_calc_size(N, dims2);
err = fabs(n - n2);
#ifdef __clang__
ok &= (err < 500. * UT_TOL);
#else
ok &= (err < UT_TOL);
#endif
md_zfill(N, dims, in, 1.);
linop_normal(diaga1, N, dims, in, in);
n = powf(md_znorm(N, dims, in), 2);
n2 = powf(val1a, 4.) * md_calc_size(N, dims2);
err = fabs(n - n2);
ok &= (err < 1.E-3);
linop_free(diaga1);
md_free(in);
md_free(out);
return ok;
}
UT_REGISTER_TEST(test_linop_extract);
static bool test_linop_permute(void)
{
enum { N = 4 };
long idims[N] = { 8, 4, 6, 3};
unsigned int perm[N] = {0, 3, 2, 1};
long odims[N];
md_permute_dims(N, perm, odims, idims);
complex float* src = md_alloc(N, idims, CFL_SIZE);
complex float* src2 = md_alloc(N, idims, CFL_SIZE);
complex float* dst1 = md_alloc(N, idims, CFL_SIZE);
complex float* dst2 = md_alloc(N, idims, CFL_SIZE);
md_gaussian_rand(N, idims, src);
md_permute(N, perm, odims, dst1, idims, src, CFL_SIZE);
auto lop = linop_permute_create(N, perm, idims);
linop_forward(lop, N, odims, dst2, N, idims, src);
bool ok = (0 == md_zrmse(N, odims, dst1, dst2));
linop_adjoint(lop, N, idims, src2, N, odims, dst2);
ok = ok && (0 == md_zrmse(N, idims, src, src2));
ok = ok && (UT_TOL > linop_test_adjoint(lop));
md_free(dst1);
md_free(dst2);
md_free(src);
md_free(src2);
linop_free(lop);
UT_ASSERT(ok);
}
UT_REGISTER_TEST(test_linop_permute);
|