upgrading copyright year from 2015 to 2016
[hkl.git] / tests / hkl-matrix-t.c
blobbadc083b229db748faa49382884bbd02e469abe3
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-2016 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 assignement(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 = m1;
67 ok(TRUE == hkl_matrix_cmp(&m1, &m), __func__);
70 static void init_from_euler(void)
72 HklMatrix m_ref = {{{ 1./2., -1./2., sqrt(2)/2.},
73 { sqrt(2.)/4.+1./2., -sqrt(2.)/4.+1./2., -1./2.},
74 {-sqrt(2.)/4.+1./2., sqrt(2.)/4.+1./2., 1./2.}}};
75 HklMatrix m;
77 hkl_matrix_init_from_euler(&m, 45.*HKL_DEGTORAD, 45.*HKL_DEGTORAD, 45.*HKL_DEGTORAD);
78 ok(TRUE == hkl_matrix_cmp(&m_ref, &m), __func__);
81 static void init_from_two_vector(void)
83 HklVector v1 = {{0.0, 1.0, 2.0}};
84 HklVector v2 = {{1.0, 2.0, 3.0}};
85 HklMatrix m_ref = {{{0.0, 5.0 / sqrt(30.0), -1.0 / sqrt(6.0)},
86 {1.0 / sqrt(5.0), 2.0 / sqrt(30.0), 2.0 / sqrt(6.0)},
87 {2.0 / sqrt(5.0),-1.0 / sqrt(30.0), -1.0 / sqrt(6.0)}}
89 HklMatrix m;
91 hkl_matrix_init_from_two_vector(&m, &v1, &v2);
92 ok(TRUE == hkl_matrix_cmp(&m_ref, &m), __func__);
95 static void times_vector(void)
97 HklMatrix m = {{{ 1.0, 3.0,-2.0},
98 {10.0, 5.0, 5.0},
99 {-3.0, 2.0, 0.0}}
101 HklVector v = {{1, 2, 3}};
102 HklVector v_ref = {{1, 35, 1}};
104 hkl_matrix_times_vector(&m, &v);
105 ok(0 == hkl_vector_cmp(&v_ref, &v), __func__);
108 static void times_matrix(void)
110 HklMatrix m_ref = {{{37., 14., 13.},
111 {45., 65., 5.},
112 {17., 1., 16.}}
115 HklMatrix m = {{{ 1., 3.,-2.},
116 {10., 5., 5.},
117 {-3., 2., 0.}}
120 hkl_matrix_times_matrix(&m, &m);
121 ok(TRUE == hkl_matrix_cmp(&m_ref, &m), __func__);
124 static void transpose(void)
126 HklMatrix m_ref = {{{37., 14., 13.},
127 {45., 65., 5.},
128 {17., 1., 16.}}
131 HklMatrix m = {{{37., 45., 17.},
132 {14., 65., 1.},
133 {13., 5., 16.}}
136 hkl_matrix_transpose(&m);
137 ok(TRUE == hkl_matrix_cmp(&m_ref, &m), __func__);
140 int main(void)
142 plan(17);
144 init();
145 cmp();
146 assignement();
147 init_from_euler();
148 init_from_two_vector();
149 times_vector();
150 times_matrix();
151 transpose();
153 return 0;