upgrading copyright year from 2016 to 2017
[hkl.git] / tests / hkl-matrix-t.c
blob9dc245aea82f87eae58ee9a03f0b9b26ccac1323
1 /* This file is part of the hkl library.
3 * The hkl library is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * The hkl library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2003-2017 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
22 #include "hkl.h"
23 #include <tap/basic.h>
24 #include <tap/float.h>
26 #include "hkl-vector-private.h"
27 #include "hkl-matrix-private.h" /* we will check also the private API */
29 static void init(void)
31 HklMatrix m;
33 hkl_matrix_init(&m, 1, 1, 0, 0, 1, 0, 0, 0, 1);
34 is_double(1., m.data[0][0], HKL_EPSILON, __func__);
35 is_double(1., m.data[0][1], HKL_EPSILON, __func__);
36 is_double(0., m.data[0][2], HKL_EPSILON, __func__);
37 is_double(0., m.data[1][0], HKL_EPSILON, __func__);
38 is_double(1., m.data[1][1], HKL_EPSILON, __func__);
39 is_double(0., m.data[1][2], HKL_EPSILON, __func__);
40 is_double(0., m.data[2][0], HKL_EPSILON, __func__);
41 is_double(0., m.data[2][1], HKL_EPSILON, __func__);
42 is_double(1., m.data[2][2], HKL_EPSILON, __func__);
45 static void cmp(void)
47 HklMatrix m1 = {{{0.0, 1.0, 2.0},
48 {3.0, 4.0, 5.0},
49 {6.0, 7.0, 8.0}}};
51 HklMatrix m2 = {{{1.0, 1.0, 2.0},
52 {3.0, 4.0, 5.0},
53 {6.0, 7.0, 8.0}}};
55 ok(TRUE == hkl_matrix_cmp(&m1, &m1), __func__);
56 ok(FALSE == hkl_matrix_cmp(&m1, &m2), __func__);
59 static void dup(void)
61 HklMatrix m1 = {{{0.0, 1.0, 2.0},
62 {3.0, 4.0, 5.0},
63 {6.0, 7.0, 8.0}}};
64 HklMatrix *m;
66 m = hkl_matrix_dup(&m1);
67 ok(TRUE == hkl_matrix_cmp(&m1, m), __func__);
68 hkl_matrix_free(m);
71 static void assignement(void)
73 HklMatrix m1 = {{{0.0, 1.0, 2.0},
74 {3.0, 4.0, 5.0},
75 {6.0, 7.0, 8.0}}};
76 HklMatrix m;
78 m = m1;
79 ok(TRUE == hkl_matrix_cmp(&m1, &m), __func__);
82 static void init_from_euler(void)
84 HklMatrix m_ref = {{{ 1./2., -1./2., sqrt(2)/2.},
85 { sqrt(2.)/4.+1./2., -sqrt(2.)/4.+1./2., -1./2.},
86 {-sqrt(2.)/4.+1./2., sqrt(2.)/4.+1./2., 1./2.}}};
87 HklMatrix m;
89 hkl_matrix_init_from_euler(&m, 45.*HKL_DEGTORAD, 45.*HKL_DEGTORAD, 45.*HKL_DEGTORAD);
90 ok(TRUE == hkl_matrix_cmp(&m_ref, &m), __func__);
93 static void init_from_two_vector(void)
95 HklVector v1 = {{0.0, 1.0, 2.0}};
96 HklVector v2 = {{1.0, 2.0, 3.0}};
97 HklMatrix m_ref = {{{0.0, 5.0 / sqrt(30.0), -1.0 / sqrt(6.0)},
98 {1.0 / sqrt(5.0), 2.0 / sqrt(30.0), 2.0 / sqrt(6.0)},
99 {2.0 / sqrt(5.0),-1.0 / sqrt(30.0), -1.0 / sqrt(6.0)}}
101 HklMatrix m;
103 hkl_matrix_init_from_two_vector(&m, &v1, &v2);
104 ok(TRUE == hkl_matrix_cmp(&m_ref, &m), __func__);
107 static void times_vector(void)
109 HklMatrix m = {{{ 1.0, 3.0,-2.0},
110 {10.0, 5.0, 5.0},
111 {-3.0, 2.0, 0.0}}
113 HklVector v = {{1, 2, 3}};
114 HklVector v_ref = {{1, 35, 1}};
116 hkl_matrix_times_vector(&m, &v);
117 ok(0 == hkl_vector_cmp(&v_ref, &v), __func__);
120 static void times_matrix(void)
122 HklMatrix m_ref = {{{37., 14., 13.},
123 {45., 65., 5.},
124 {17., 1., 16.}}
127 HklMatrix m = {{{ 1., 3.,-2.},
128 {10., 5., 5.},
129 {-3., 2., 0.}}
132 hkl_matrix_times_matrix(&m, &m);
133 ok(TRUE == hkl_matrix_cmp(&m_ref, &m), __func__);
136 static void transpose(void)
138 HklMatrix m_ref = {{{37., 14., 13.},
139 {45., 65., 5.},
140 {17., 1., 16.}}
143 HklMatrix m = {{{37., 45., 17.},
144 {14., 65., 1.},
145 {13., 5., 16.}}
148 hkl_matrix_transpose(&m);
149 ok(TRUE == hkl_matrix_cmp(&m_ref, &m), __func__);
152 int main(void)
154 plan(18);
156 init();
157 cmp();
158 dup();
159 assignement();
160 init_from_euler();
161 init_from_two_vector();
162 times_vector();
163 times_matrix();
164 transpose();
166 return 0;