/home/liu/actions-runner/_work/ccv/ccv/test/unit/algebra.tests.c
Line | Count | Source |
1 | | #include "ccv.h" |
2 | | #include "case.h" |
3 | | |
4 | | TEST_CASE("matrix multiplication") |
5 | 1 | { |
6 | 1 | ccv_dense_matrix_t* a = ccv_dense_matrix_new(3, 2, CCV_64F | CCV_C1, 0, 0); |
7 | 1 | a->data.f64[0] = 0.11; |
8 | 1 | a->data.f64[1] = 0.12; |
9 | 1 | a->data.f64[2] = 0.13; |
10 | 1 | a->data.f64[3] = 0.21; |
11 | 1 | a->data.f64[4] = 0.22; |
12 | 1 | a->data.f64[5] = 0.23; |
13 | 1 | ccv_dense_matrix_t* b = ccv_dense_matrix_new(3, 2, CCV_64F | CCV_C1, 0, 0); |
14 | 1 | b->data.f64[0] = 1011; |
15 | 1 | b->data.f64[1] = 1012; |
16 | 1 | b->data.f64[2] = 1021; |
17 | 1 | b->data.f64[3] = 1022; |
18 | 1 | b->data.f64[4] = 1031; |
19 | 1 | b->data.f64[5] = 1032; |
20 | 1 | ccv_dense_matrix_t* y = 0; |
21 | 1 | ccv_gemm(a, b, 1, 0, 0, CCV_A_TRANSPOSE, (ccv_matrix_t**)&y, 0); |
22 | 1 | double hy[4] = {470.760000, 471.220000, 572.860000, 573.420000}; |
23 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(double, hy, y->data.f64, 4, 1e-6, "2x3, 3x2 matrix multiplication failure"); |
24 | 1 | ccv_matrix_free(a); |
25 | 1 | ccv_matrix_free(b); |
26 | 1 | ccv_matrix_free(y); |
27 | 1 | } |
28 | | |
29 | | TEST_CASE("matrix addition") |
30 | 1 | { |
31 | 1 | ccv_dense_matrix_t* a = ccv_dense_matrix_new(3, 2, CCV_64F | CCV_C1, 0, 0); |
32 | 1 | a->data.f64[0] = 0.11; |
33 | 1 | a->data.f64[1] = 0.12; |
34 | 1 | a->data.f64[2] = 0.13; |
35 | 1 | a->data.f64[3] = 0.21; |
36 | 1 | a->data.f64[4] = 0.22; |
37 | 1 | a->data.f64[5] = 0.23; |
38 | 1 | ccv_dense_matrix_t* b = ccv_dense_matrix_new(3, 2, CCV_64F | CCV_C1, 0, 0); |
39 | 1 | b->data.f64[0] = 1011; |
40 | 1 | b->data.f64[1] = 1012; |
41 | 1 | b->data.f64[2] = 1021; |
42 | 1 | b->data.f64[3] = 1022; |
43 | 1 | b->data.f64[4] = 1031; |
44 | 1 | b->data.f64[5] = 1032; |
45 | 1 | ccv_dense_matrix_t* y = 0; |
46 | 1 | ccv_add(a, b, (ccv_matrix_t**)&y, 0); |
47 | 1 | double hy[6] = {1011.11, 1012.12, 1021.13, 1022.21, 1031.22, 1032.23}; |
48 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(double, hy, y->data.f64, 6, 1e-6, "3x2, 3x2 matrix addition failure"); |
49 | 1 | ccv_matrix_free(a); |
50 | 1 | ccv_matrix_free(b); |
51 | 1 | ccv_matrix_free(y); |
52 | 1 | } |
53 | | |
54 | | TEST_CASE("matrix scale with overflow") |
55 | 1 | { |
56 | 1 | ccv_dense_matrix_t* a = ccv_dense_matrix_new(1, 4, CCV_8U | CCV_C1, 0, 0); |
57 | 1 | a->data.u8[0] = 11; |
58 | 1 | a->data.u8[1] = 111; |
59 | 1 | a->data.u8[2] = 55; |
60 | 1 | a->data.u8[3] = 155; |
61 | 1 | ccv_dense_matrix_t* y = 0; |
62 | 1 | ccv_scale(a, (ccv_matrix_t**)&y, 0, 2); |
63 | 1 | uint8_t hy[4] = {22, 222, 110, 255}; |
64 | 1 | REQUIRE_ARRAY_EQ(uint8_t, hy, y->data.u8, 4, "1x4 matrix scale with overflow failure"); |
65 | 1 | ccv_matrix_free(a); |
66 | 1 | ccv_matrix_free(y); |
67 | 1 | } |
68 | | |
69 | | TEST_CASE("matrix scale") |
70 | 1 | { |
71 | 1 | ccv_dense_matrix_t* a = ccv_dense_matrix_new(3, 2, CCV_64F | CCV_C1, 0, 0); |
72 | 1 | a->data.f64[0] = 0.11; |
73 | 1 | a->data.f64[1] = 0.12; |
74 | 1 | a->data.f64[2] = 0.13; |
75 | 1 | a->data.f64[3] = 0.21; |
76 | 1 | a->data.f64[4] = 0.22; |
77 | 1 | a->data.f64[5] = 0.23; |
78 | 1 | ccv_dense_matrix_t* y = 0; |
79 | 1 | ccv_scale(a, (ccv_matrix_t**)&y, 0, 10); |
80 | 1 | double hy[6] = {1.1, 1.2, 1.3, 2.1, 2.2, 2.3}; |
81 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(double, hy, y->data.f64, 6, 1e-6, "3x2 matrix scale failure"); |
82 | 1 | ccv_matrix_free(a); |
83 | 1 | ccv_matrix_free(y); |
84 | 1 | } |
85 | | |
86 | | TEST_CASE("vector sum") |
87 | 1 | { |
88 | 1 | ccv_dense_matrix_t* a = ccv_dense_matrix_new(3, 2, CCV_64F | CCV_C1, 0, 0); |
89 | 1 | a->data.f64[0] = 0.11; |
90 | 1 | a->data.f64[1] = 0.12; |
91 | 1 | a->data.f64[2] = 0.13; |
92 | 1 | a->data.f64[3] = 0.21; |
93 | 1 | a->data.f64[4] = 0.22; |
94 | 1 | a->data.f64[5] = 0.23; |
95 | 1 | double sum = ccv_sum(a, CCV_SIGNED); |
96 | 1 | ccv_matrix_free(a); |
97 | 1 | REQUIRE_EQ_WITH_TOLERANCE(sum, 1.02, 1e-6, "3x2 vector sum failure"); |
98 | 1 | } |
99 | | |
100 | | TEST_CASE("vector L2 normalize") |
101 | 1 | { |
102 | 1 | int i; |
103 | 1 | ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 10, CCV_32F | CCV_C1, 0, 0); |
104 | 11 | for (i = 0; i < 10; i++10 ) |
105 | 10 | dmt->data.f32[i] = i; |
106 | 1 | ccv_normalize(dmt, (ccv_matrix_t**)&dmt, 0, CCV_L2_NORM); |
107 | 1 | float hm[10] = {0.000000, 0.059235, 0.118470, 0.177705, 0.236940, 0.296174, 0.355409, 0.414644, 0.473879, 0.533114}; |
108 | 1 | REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, hm, dmt->data.f32, 10, 1e-6, "10d vector L2 normalize failure"); |
109 | 1 | ccv_matrix_free(dmt); |
110 | 1 | } |
111 | | |
112 | | TEST_CASE("summed area table without padding") |
113 | 1 | { |
114 | 1 | int i, j; |
115 | 1 | ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(5, 4, CCV_8U | CCV_C3, 0, 0); |
116 | 1 | unsigned char* ptr = dmt->data.u8; |
117 | 6 | for (i = 0; i < dmt->rows; i++5 ) |
118 | 5 | { |
119 | 25 | for (j = 0; j < dmt->cols; j++20 ) |
120 | 20 | { |
121 | 20 | ptr[j * 3] = 1; |
122 | 20 | ptr[j * 3 + 1] = 2; |
123 | 20 | ptr[j * 3 + 2] = 3; |
124 | 20 | } |
125 | 5 | ptr += dmt->step; |
126 | 5 | } |
127 | 1 | ccv_dense_matrix_t* b = 0; |
128 | 1 | ccv_sat(dmt, &b, 0, CCV_NO_PADDING); |
129 | 1 | int sat[60] = { 1, 2, 3, 2, 4, 6, 3, 6, 9, 4, 8, 12, |
130 | 1 | 2, 4, 6, 4, 8, 12, 6, 12, 18, 8, 16, 24, |
131 | 1 | 3, 6, 9, 6, 12, 18, 9, 18, 27, 12, 24, 36, |
132 | 1 | 4, 8, 12, 8, 16, 24, 12, 24, 36, 16, 32, 48, |
133 | 1 | 5, 10, 15, 10, 20, 30, 15, 30, 45, 20, 40, 60 }; |
134 | 1 | REQUIRE_ARRAY_EQ(int, sat, b->data.i32, 60, "4x5 matrix summed area table computation error"); |
135 | 1 | ccv_matrix_free(dmt); |
136 | 1 | ccv_matrix_free(b); |
137 | 1 | } |
138 | | |
139 | | TEST_CASE("summed area table with padding") |
140 | 1 | { |
141 | 1 | int i, j; |
142 | 1 | ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(5, 3, CCV_8U | CCV_C3, 0, 0); |
143 | 1 | unsigned char* ptr = dmt->data.u8; |
144 | 6 | for (i = 0; i < dmt->rows; i++5 ) |
145 | 5 | { |
146 | 20 | for (j = 0; j < dmt->cols; j++15 ) |
147 | 15 | { |
148 | 15 | ptr[j * 3] = 1; |
149 | 15 | ptr[j * 3 + 1] = 2; |
150 | 15 | ptr[j * 3 + 2] = 3; |
151 | 15 | } |
152 | 5 | ptr += dmt->step; |
153 | 5 | } |
154 | 1 | ccv_dense_matrix_t* b = 0; |
155 | 1 | ccv_sat(dmt, &b, 0, CCV_PADDING_ZERO); |
156 | 1 | int sat[72] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
157 | 1 | 0, 0, 0, 1, 2, 3, 2, 4, 6, 3, 6, 9, |
158 | 1 | 0, 0, 0, 2, 4, 6, 4, 8, 12, 6, 12, 18, |
159 | 1 | 0, 0, 0, 3, 6, 9, 6, 12, 18, 9, 18, 27, |
160 | 1 | 0, 0, 0, 4, 8, 12, 8, 16, 24, 12, 24, 36, |
161 | 1 | 0, 0, 0, 5, 10, 15, 10, 20, 30, 15, 30, 45, }; |
162 | 1 | REQUIRE_ARRAY_EQ(int, sat, b->data.i32, 72, "3x5 matrix summed area table (with padding) computation error"); |
163 | 1 | ccv_matrix_free(dmt); |
164 | 1 | ccv_matrix_free(b); |
165 | 1 | } |
166 | | |
167 | | #include "case_main.h" |