-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-diff.c
62 lines (54 loc) · 1.38 KB
/
test-diff.c
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
/* Copyright 2018, Mansour Moufid <[email protected]> */
#undef NDEBUG
#include <assert.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "nu.h"
#include "test.h"
static const float pi = (float) M_PI;
int
main(void)
{
float error;
float *x;
float *y;
float *z;
error = 1e-5f;
for (size_t k = 3; k <= nu_diff_kmax; k++) {
for (size_t n = k + 1; n <= 10; n++) {
x = nu_array_alloc(n, sizeof(float));
y = nu_array_alloc(n, sizeof(float));
z = nu_array_alloc(n, sizeof(float));
assert(x != NULL);
assert(y != NULL);
assert(z != NULL);
for (size_t i = 0; i < n; i++) {
x[i] = (float) i;
z[i] = 1.f;
}
nu_diff(1.f, k, y, x, n);
assert(nu_array_eq(y, z, n, error));
free(x);
free(y);
free(z);
}
}
error = 1e-4f;
size_t k = 3;
size_t n = 1000;
float h = (2.f * pi) / n;
x = nu_array_alloc(n, sizeof(float));
y = nu_array_alloc(n, sizeof(float));
z = nu_array_alloc(n, sizeof(float));
nu_array_linspace(x, 0.f, 2.f * pi, n);
nu_array_cos(y, x, n);
nu_array_sin(x, x, n);
nu_diff(h, k, z, x, n);
assert(nu_array_eq(y, z, n, error));
free(x);
free(y);
free(z);
return 0;
}