rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

test_real22.h (7717B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys 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.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "rsys.h"
     17 #include <float.h>
     18 #include <math.h>
     19 
     20 #define REALX_DIMENSION__ 2
     21 #define REALY_DIMENSION__ 2
     22 #include "realXY_begin.h"
     23 
     24 #define REAL REAL_TYPE__
     25 #define REAL_COMPATIBLE REAL_TYPE_COMPATIBLE__
     26 
     27 #define CHECK_REAL2(A, B)                                                      \
     28   {                                                                            \
     29     const REAL* a__ = (A);                                                     \
     30     const REAL* b__ = (B);                                                     \
     31     int i__;                                                                   \
     32     FOR_EACH(i__, 0, 2)                                                        \
     33       CHK(a__[i__] ==  b__[i__]);                                              \
     34   } (void)0
     35 #define CHECK_REAL22(A, B)                                                     \
     36   {                                                                            \
     37     const REAL* a__ = (A);                                                     \
     38     const REAL* b__ = (B);                                                     \
     39     int i__;                                                                   \
     40     FOR_EACH(i__, 0, 4)                                                        \
     41       CHK(a__[i__] ==  b__[i__]);                                              \
     42   } (void)0
     43 
     44 int
     45 main(int argc, char** argv)
     46 {
     47   REAL a[4], b[4], dst[4], c[4];
     48   REAL_COMPATIBLE d[4];
     49   int i;
     50   (void)argc, (void)argv;
     51 
     52   REALXY_FUNC__(set)(c, REALXY_FUNC__(splat)(a, -1.0));
     53   FOR_EACH(i, 0, 4) {
     54     CHK(a[i] ==  -1.0);
     55     CHK(c[i] ==  -1.0);
     56   }
     57   REALXY_FUNC__(set)(a, REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0));
     58   FOR_EACH(i, 0, 4) {
     59     CHK(a[i] ==  (REAL)i);
     60   }
     61   CHECK_REAL22(REALXY_FUNC__(set_identity)(a),
     62     REALXY_CTOR__(c, 1.0, 0.0, 0.0, 1.0));
     63   CHECK_REAL22(a, REALXY_CTOR__(c, 1.0, 0.0, 0.0, 1.0));
     64   CHK(REALXY_FUNC__(is_identity)(a) ==  1);
     65 
     66   REALXY_FUNC__(set)(c, a);
     67   FOR_EACH(i, 0, REALX_DIMENSION__*REALY_DIMENSION__) {
     68     REAL_TYPE__ r = c[i];
     69     c[i] = c[i] + REAL_EPSILON__;
     70     CHK(REALXY_FUNC__(is_identity)(c) ==  0);
     71     c[i] = r;
     72   }
     73 
     74   d[0] = (REAL_COMPATIBLE)0.1;
     75   d[1] = (REAL_COMPATIBLE)(1.0/3.0);
     76   d[2] = (REAL_COMPATIBLE)0.3;
     77   d[3] = (REAL_COMPATIBLE)-0.7;
     78   REALXY_CTOR__(c,
     79     (REAL)(REAL_COMPATIBLE)0.1,
     80     (REAL)(REAL_COMPATIBLE)(1.0/3.0),
     81     (REAL)(REAL_COMPATIBLE)0.3,
     82     (REAL)(REAL_COMPATIBLE)-0.7);
     83   CHECK_REAL22(REALXY_CAST__(dst, d), c);
     84 
     85   REALXY_FUNC__(splat)(a, -1.0);
     86   CHECK_REAL22(REALXY_FUNC__(set_row)(a, REALX_CTOR__(c, 0.0, 1.0), 0),
     87     REALXY_CTOR__(c, 0.0, -1.0, 1.0, -1.0));
     88   CHECK_REAL22(a, REALXY_CTOR__(c, 0.0, -1.0, 1.0, -1.0));
     89   CHECK_REAL22(REALXY_FUNC__(set_row)(a, REALX_CTOR__(c, 2.0, 3.0), 1),
     90     REALXY_CTOR__(c, 0.0, 2.0, 1.0, 3.0));
     91   CHECK_REAL22(a, REALXY_CTOR__(c, 0.0, 2.0, 1.0, 3.0));
     92 
     93   CHECK_REAL22(REALXY_FUNC__(transpose)(a, a),
     94     REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0));
     95   CHECK_REAL22(a, REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0));
     96   CHECK_REAL22(REALXY_FUNC__(transpose)(b, a),
     97     REALXY_CTOR__(c, 0.0, 2.0, 1.0, 3.0));
     98   CHECK_REAL22(b, REALXY_CTOR__(c, 0.0, 2.0, 1.0, 3.0));
     99 
    100   REALXY_FUNC__(splat)(a, -1.0);
    101   CHECK_REAL22(REALXY_FUNC__(set_col)(a, REALX_CTOR__(c, 0.0, 1.0), 0),
    102     REALXY_CTOR__(c, 0.0, 1.0, -1.0, -1.0));
    103   CHECK_REAL22(a, REALXY_CTOR__(c, 0.0, 1.0, -1.0, -1.0));
    104   CHECK_REAL22(REALXY_FUNC__(set_col)(a, REALX_CTOR__(c, 2.0, 3.0), 1),
    105     REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0));
    106   CHECK_REAL22(a, REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0));
    107 
    108   CHECK_REAL2(REALXY_FUNC__(row)(b, a, 0), REALX_CTOR__(c, 0.0, 2.0));
    109   CHECK_REAL2(REALXY_FUNC__(row)(b, a, 1), REALX_CTOR__(c, 1.0, 3.0));
    110 
    111   CHECK_REAL2(REALXY_FUNC__(col)(b + 2, a, 0), REALX_CTOR__(c, 0.0, 1.0));
    112   CHECK_REAL2(REALXY_FUNC__(col)(b, a, 1), REALX_CTOR__(c, 2.0, 3.0));
    113 
    114   CHECK_REAL2(REALXY_FUNC__(col_ptr)(a, 0), REALX_CTOR__(c, 0.0, 1.0));
    115   CHECK_REAL2(REALXY_FUNC__(col_ptr)(a, 1), REALX_CTOR__(c, 2.0, 3.0));
    116   CHECK_REAL2(REALXY_FUNC__(col_cptr)(a, 0), REALX_CTOR__(c, 0.0, 1.0));
    117   CHECK_REAL2(REALXY_FUNC__(col_cptr)(a, 1), REALX_CTOR__(c, 2.0, 3.0));
    118 
    119   REALXY_CTOR__(a, 1.0, 2.0, 3.0, 4.0);
    120   CHECK_REAL2(REALXY_REALX_FUNC__(mul)(dst, a, REALX_CTOR__(c, 1.0, 2.0)),
    121     REALX_CTOR__(c, 7.0, 10.0));
    122   CHECK_REAL2(dst, REALX_CTOR__(c, 7.0, 10.0));
    123   CHECK_REAL2(REALX_REALXY_FUNC__(mul)(dst, REALX_CTOR__(c, 1.0, 2.0), a),
    124     REALX_CTOR__(c, 5.0, 11.0));
    125   CHECK_REAL2(dst, REALX_CTOR__(c, 5.0, 11.0));
    126   CHECK_REAL22(REALXY_FUNC__(mul)(dst, a, -1.0),
    127     REALXY_CTOR__(c, -1.0, -2.0, -3.0, -4.0));
    128   CHECK_REAL22(dst, REALXY_CTOR__(c, -1.0, -2.0, -3.0, -4.0));
    129 
    130   REALXY_CTOR__(b, 2.0, 9.0, 8.0, 1.0);
    131   CHECK_REAL22(REALXY_REALXY_FUNC__(mul)(dst, a, b),
    132     REALXY_CTOR__(c, 29.0, 40.0, 11.0, 20.0));
    133   CHECK_REAL22(dst, REALXY_CTOR__(c, 29.0, 40.0, 11.0, 20.0));
    134   CHECK_REAL22(REALXY_REALXY_FUNC__(mul)(b, a, b),
    135     REALXY_CTOR__(c, 29.0, 40.0, 11.0, 20.0));
    136   CHECK_REAL22(b, REALXY_CTOR__(c, 29.0, 40.0, 11.0, 20.0));
    137 
    138   REALXY_CTOR__(a, 0.0, 1.0, 2.0, 3.0);
    139   REALXY_CTOR__(b, 1.0, 2.0, 3.0, 4.0);
    140   CHECK_REAL22(REALXY_FUNC__(add)(dst, a, b),
    141     REALXY_CTOR__(c, 1.0, 3.0, 5.0, 7.0));
    142   CHECK_REAL22(dst, REALXY_CTOR__(c, 1.0, 3.0, 5.0, 7.0));
    143   CHECK_REAL22(REALXY_FUNC__(sub)(dst, dst, b),
    144     REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0));
    145 	CHECK_REAL22(dst, REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0));
    146   CHECK_REAL22(REALXY_FUNC__(minus)(a, b),
    147     REALXY_CTOR__(c,  -1.0, -2.0, -3.0, -4.0));
    148   CHECK_REAL22(a, REALXY_CTOR__(c,  -1.0, -2.0, -3.0, -4.0));
    149 
    150   REALXY_FUNC__(set)(a, b);
    151   CHK(REALXY_FUNC__(eq)(a, b) ==  1);
    152   REALXY_FUNC__(add)(a, a, REALXY_FUNC__(splat)(c, REAL_EPSILON__));
    153   CHK(REALXY_FUNC__(eq)(a, b) ==  0);
    154   CHK(REALXY_FUNC__(eq_eps)(a, b, REAL_EPSILON__) ==  1);
    155   CHK(REALXY_FUNC__(eq_eps)(a, b, REAL_EPSILON__ * (REAL)0.9) ==  0);
    156 
    157   REALXY_FUNC__(set)(a, REALXY_CTOR__(c, 1, 3, 2, 4));
    158   CHK(REALXY_FUNC__(det)(a) ==  -2.0);
    159   CHK(REALXY_FUNC__(inverse)(b, a) ==  -2.0);
    160   CHECK_REAL22(b, REALXY_CTOR__(c, -2.0, 1.5, 1.0, -0.5));
    161   CHK(REALXY_FUNC__(invtrans)(a, a) ==  -2.0);
    162   CHECK_REAL22(a, REALXY_CTOR__(c, -2.0, 1.0, 1.5, -0.5));
    163 
    164   CHK(REALXY_FUNC__(rotation)(c, (REAL_TYPE__)PI/4) == c);
    165 
    166   REALX_CTOR__(a, 1, 0);
    167   CHK(REALXY_REALX_FUNC__(mul)(a, c, a) == a);
    168   REALX_FUNC__(splat)(c, (REAL_TYPE__)(sqrt(2.0)/2.0));
    169   CHK(REALX_FUNC__(eq_eps)(a, c, (REAL_TYPE__)1.e-6));
    170 
    171   REALX_CTOR__(b, 0, -1);
    172   CHK(REALXY_REALX_FUNC__(mul)(b, c, b) == b);
    173   REALX_CTOR__(c, (REAL_TYPE__)(sqrt(2.0)/2.0), -(REAL_TYPE__)(sqrt(2.0)/2.0));
    174   CHK(REALX_FUNC__(eq_eps)(b, c, (REAL_TYPE__)1.e-6));
    175 
    176   CHK(REALXY_FUNC__(rotation)(c, -(REAL_TYPE__)PI/2) == c);
    177 
    178   REALX_CTOR__(a, 1, 0);
    179   CHK(REALXY_REALX_FUNC__(mul)(a, c, a) == a);
    180   REALX_CTOR__(c, 0, -1);
    181   CHK(REALX_FUNC__(eq_eps)(a, c, (REAL_TYPE__)1.e-6));
    182 
    183   REALX_CTOR__(b, 0, -1);
    184   CHK(REALXY_REALX_FUNC__(mul)(b, c, b) == b);
    185   REALX_CTOR__(c, -1, 0);
    186   CHK(REALX_FUNC__(eq_eps)(b, c, (REAL_TYPE__)1.e-6));
    187 
    188   return 0;
    189 }
    190 
    191 #include "realXY_end.h"