RINGMesh  Version 5.0.0
A programming library for geological model meshes
test-matrix.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2017, Association Scientifique pour la Geologie et ses
3  * Applications (ASGA). All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ASGA nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASGA BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * http://www.ring-team.org
28  *
29  * RING Project
30  * Ecole Nationale Superieure de Geologie - GeoRessources
31  * 2 Rue du Doyen Marcel Roubault - TSA 70605
32  * 54518 VANDOEUVRE-LES-NANCY
33  * FRANCE
34  */
35 
36 #include <ringmesh/ringmesh_tests_config.h>
37 
38 #include <geogram/basic/string.h>
39 #include <ringmesh/basic/matrix.h>
40 
45 using namespace RINGMesh;
46 
48 {
49  matrix.set_element( 0, 0, 1. );
50  matrix.set_element( 1, 1, 1. );
51  matrix.set_element( 2, 2, 1. );
52  for( index_t i = 0; i < matrix.ni(); ++i )
53  {
54  bool exists = false;
55  double elt;
56  std::tie( exists, elt ) = matrix.get_element( i, i );
57  if( !exists || std::abs( elt - 1. ) > global_epsilon )
58  {
59  throw RINGMeshException( "TEST", "Matrix element at (", i, ",", i,
60  ") should exist and be correct!" );
61  }
62  for( index_t j = 0; j < matrix.nj(); ++j )
63  {
64  if( i != j )
65  {
66  std::tie( exists, std::ignore ) = matrix.get_element( i, j );
67  if( exists )
68  {
69  throw RINGMeshException( "TEST", "Matrix element at (", i,
70  ",", i, ") should not exist!" );
71  }
72  }
73  }
74  }
75 }
76 
78 {
79  matrix.set_element( 0, 1, 3. );
80  bool exists = false;
81  double elt;
82  std::tie( exists, elt ) = matrix.get_element( 0, 1 );
83  if( !exists || std::abs( elt - 3. ) > global_epsilon )
84  {
85  throw RINGMeshException(
86  "TEST", "Matrix element at (0,1) should exist and be correct!" );
87  }
88  if( matrix.is_symmetrical() )
89  {
90  std::tie( exists, elt ) = matrix.get_element( 1, 0 );
91  if( !exists || std::abs( elt - 3. ) > global_epsilon )
92  {
93  throw RINGMeshException( "TEST",
94  "Matrix element at (1,0) should exist and be correct!" );
95  }
96  }
97  else
98  {
99  std::tie( exists, elt ) = matrix.get_element( 1, 0 );
100  if( exists )
101  {
102  throw RINGMeshException(
103  "TEST", "Matrix element at (1,0) should not exist!" );
104  }
105  }
106 }
107 
109  const std::vector< double >& product_result )
110 {
111  std::vector< double > vect_to_multiply = { 5., 2., 9. };
112  std::vector< double > result =
113  product_matrix_by_vector< double >( matrix, vect_to_multiply );
114  if( std::abs( result[0] - product_result[0] ) > global_epsilon
115  || std::abs( result[1] - product_result[1] ) > global_epsilon
116  || std::abs( result[2] - product_result[2] ) > global_epsilon )
117  {
118  throw RINGMeshException( "TEST", "Product matrix by vector failed!" );
119  }
120 }
121 
123  const std::vector< double >& product_result )
124 {
125  matrix.build_matrix( 3, 3 );
126  set_get_test( matrix );
127  symmetry_test( matrix );
128  product_by_vector_test( matrix, product_result );
129 }
130 
131 void run_tests()
132 {
133  SparseMatrix< double, light > symmetrical_matrix( true );
134  std::vector< double > product_result = { 11., 17., 9. };
135  one_test_sequence( symmetrical_matrix, product_result );
136 
137  SparseMatrix< double, light > asymmetrical_matrix( false );
138  product_result[1] = 2.;
139  one_test_sequence( asymmetrical_matrix, product_result );
140 }
141 
142 int main()
143 {
144  using namespace RINGMesh;
145 
146  try
147  {
149 
150  Logger::out( "TEST", "Test Matrix" );
151  run_tests();
152  }
153  catch( const RINGMeshException& e )
154  {
155  Logger::err( e.category(), e.what() );
156  return 1;
157  }
158  catch( const std::exception& e )
159  {
160  Logger::err( "Exception", e.what() );
161  return 1;
162  }
163  Logger::out( "TEST", "SUCCESS" );
164  return 0;
165 }
void set_get_test(SparseMatrix< double, light > &matrix)
Definition: test-matrix.cpp:47
static void err(const std::string &feature, const Args &... args)
Definition: logger.h:68
static void out(const std::string &feature, const Args &... args)
Definition: logger.h:61
void symmetry_test(SparseMatrix< double, light > &matrix)
Definition: test-matrix.cpp:77
void one_test_sequence(SparseMatrix< double, light > &matrix, const std::vector< double > &product_result)
const std::string & category() const
Definition: common.h:165
void run_tests()
void build_matrix(index_t ni, index_t nj)
Definition: matrix.h:296
Classes to build GeoModel from various inputs.
Definition: algorithm.h:48
int main()
void product_by_vector_test(SparseMatrix< double, light > &matrix, const std::vector< double > &product_result)
void RINGMESH_API default_configure()
Definition: common.cpp:99