File: test_ops.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 (132 lines) | stat: -rw-r--r-- 3,613 bytes parent folder | download | duplicates (5)
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
/* Copyright 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:
 * 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/iovec.h"
#include "num/ops.h"

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

#include "utest.h"



static bool test_op_stack(void)
{
	enum { N = 3 };
	long dims[N] = { 8, 4, 1 };
	long dims2[N] = { 8, 4, 2 };

	const auto a = operator_identity_create(N, dims);
	const auto b = operator_zero_create(N, dims);
	const auto c = operator_null_create(N, dims);
	const auto d = operator_combi_create(2, MAKE_ARRAY(OP_PASS(b), OP_PASS(c)));
	const auto e = operator_stack(2, 2, OP_PASS(a), OP_PASS(d));

	complex float* in = md_alloc(N, dims2, CFL_SIZE);
	complex float* out = md_alloc(N, dims2, CFL_SIZE);

	md_zfill(N, dims2, in, 1.);
	md_zfill(N, dims2, out, 100.);

	operator_apply(e, N, dims2, out, N, dims2, in);

	double err = fabsf(md_znorm(N, dims2, in) - sqrtf(2.) * md_znorm(N, dims2, out));

	operator_free(e);

	md_free(in);
	md_free(out);

	return (err < UT_TOL);
}

UT_REGISTER_TEST(test_op_stack);



static bool test_op_extract(void)
{
	enum { N = 3 };
	long dims[N] = { 8, 4, 1 };
	long dims2[N] = { 8, 4, 2 };

	const auto a = operator_identity_create(N, dims);
	const auto b = operator_zero_create(N, dims);
	const auto c = operator_null_create(N, dims);
	const auto d = operator_combi_create(2, MAKE_ARRAY(OP_PASS(b), OP_PASS(c)));
	const auto e = operator_extract_create(OP_PASS(a), 0, N, dims2, (long[]){ 0, 0, 0 });
	const auto f = operator_extract_create(OP_PASS(e), 1, N, dims2, (long[]){ 0, 0, 0 });
	const auto g = operator_extract_create(OP_PASS(d), 0, N, dims2, (long[]){ 0, 0, 1 });
	const auto h = operator_extract_create(OP_PASS(g), 1, N, dims2, (long[]){ 0, 0, 1 });
	const auto i = operator_combi_create(2, MAKE_ARRAY(OP_PASS(f), OP_PASS(h)));
	const auto j = operator_dup_create(OP_PASS(i), 0, 2);
	const auto k = operator_dup_create(OP_PASS(j), 1, 2);

	complex float* in = md_alloc(N, dims2, CFL_SIZE);
	complex float* out = md_alloc(N, dims2, CFL_SIZE);

	md_zfill(N, dims2, in, 1.);
	md_zfill(N, dims2, out, 100.);

	operator_apply(k, N, dims2, out, N, dims2, in);

	double err = fabsf(md_znorm(N, dims2, in) - sqrtf(2.) * md_znorm(N, dims2, out));

	operator_free(k);

	md_free(in);
	md_free(out);

	return (err < UT_TOL);
}

UT_REGISTER_TEST(test_op_extract);


static bool test_op_combi_inout(void)
{
	enum { N = 1 };
	long dimsi1[N] = { 2 };
	long dimso1[N] = { 3 };
	long dimsi2[N] = { 4 };
	long dimso2[N] = { 5 };

	const auto ai = operator_null_create(N, dimsi1);
	const auto ao = operator_zero_create(N, dimso1);
	auto a = operator_combi_create(2, MAKE_ARRAY(OP_PASS(ai), OP_PASS(ao)));
	const auto bi = operator_null_create(N, dimsi2);
	const auto bo = operator_zero_create(N, dimso2);
	auto b = operator_combi_create(2, MAKE_ARRAY(OP_PASS(bi), OP_PASS(bo)));

	const auto c = operator_combi_create(2, MAKE_ARRAY(a, b));

	bool ok = true;

	ok &= (operator_arg_in_domain(a, 0)->dims[0] == operator_arg_in_domain(c, 0)->dims[0]);
	ok &= (operator_arg_in_domain(b, 0)->dims[0] == operator_arg_in_domain(c, 1)->dims[0]);

	ok &= (operator_arg_out_codomain(a, 0)->dims[0] == operator_arg_out_codomain(c, 0)->dims[0]);
	ok &= (operator_arg_out_codomain(b, 0)->dims[0] == operator_arg_out_codomain(c, 1)->dims[0]);

	operator_free(a);
	operator_free(b);
	operator_free(c);

	return ok;
}

UT_REGISTER_TEST(test_op_combi_inout);