queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
MultiDimensionalIndexing.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2017 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 // This class
26 #include <queso/MultiDimensionalIndexing.h>
27 
28 // QUESO
29 #include <queso/asserts.h>
30 
31 namespace QUESO
32 {
33 
34  unsigned int MultiDimensionalIndexing::coordToGlobal( const std::vector<unsigned int>& coord_indices,
35  const std::vector<unsigned int>& n_points )
36  {
37  // Make sure the dimension is consisent
38  queso_assert_equal_to( coord_indices.size(), n_points.size() );
39 
40  // The input objects implicitly carry the dimension with them
41  unsigned int dim = coord_indices.size();
42 
43  /* Mapping is: i + j*n_i + k*n_i*n_j + l*n_i*n_j*n_k + ...
44  Initialize global_index to "i".
45  Then loop and build up each term.*/
46  queso_assert_less( coord_indices[0], n_points[0] );
47 
48  unsigned int global_index = coord_indices[0];
49 
50  for( unsigned int d = 1; d < dim; d++ )
51  {
52  queso_assert_less( coord_indices[d], n_points[d] );
53 
54  // Accumulate the current term
55  unsigned int idx = coord_indices[d];
56 
57  for( int local_d = d-1; local_d >=0; local_d -= 1)
58  {
59  idx *= n_points[local_d];
60  }
61 
62  global_index += idx;
63  }
64 
65  return global_index;
66  }
67 
68  void MultiDimensionalIndexing::globalToCoord( unsigned int global,
69  const std::vector<unsigned int>& n_points,
70  std::vector<unsigned int>& coord_indices )
71  {
72  // The input object implicitly carry's the dimension
73  unsigned int dim = n_points.size();
74 
75  coord_indices.resize(dim);
76 
77  /* coordToGlobal computes: i + j*n_points[0] + k*n_points[0]*n_points[1] + ...
78  so we use integer arithmetic and work backwards. */
79  unsigned int tmp = global;
80 
81  for( int d = dim-1; d > 0; d -= 1 )
82  {
83  unsigned int np = compute_npoints_factor( n_points, d );
84  coord_indices[d] = tmp/np;
85  tmp -= coord_indices[d]*np;
86  }
87 
88  // What's left should be our starting index
89  coord_indices[0] = tmp;
90 
91  // Sanity check: the indices we determined should map back to the global
92  queso_assert_equal_to( global, coordToGlobal(coord_indices,n_points) );
93  }
94 
95  unsigned int MultiDimensionalIndexing::compute_npoints_factor( const std::vector<unsigned int>& n_points,
96  unsigned int term )
97  {
98  unsigned int value = 1;
99 
100  for( unsigned int d = 0; d < term; d++ )
101  {
102  value *= n_points[d];
103  }
104 
105  return value;
106  }
107 
108 } // end namespace QUESO
static void globalToCoord(unsigned int global, const std::vector< unsigned int > &n_points, std::vector< unsigned int > &coord_indices)
Inverse of coordToGlobal map.
static unsigned int coordToGlobal(const std::vector< unsigned int > &coord_indices, const std::vector< unsigned int > &n_points)
Map coordinate indices to a singal global index.
int dim
Definition: ann_test.cpp:472
static unsigned int compute_npoints_factor(const std::vector< unsigned int > &n_points, unsigned int term)
Helper function.

Generated on Tue Jun 5 2018 19:48:55 for queso-0.57.1 by  doxygen 1.8.5