tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
tensor_common.cc
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include <iostream>
21 #include <tensor/tensor.h>
22 #include <tensor/io.h>
23 
24 namespace tensor {
25 
26 bool verify_tensor_dimensions(const Indices &d, index total_size) {
27  index aux = total_size;
28  if (aux == 0) {
29  if (d.size() == 0)
30  return true;
31  for (Indices::const_iterator it = d.begin_const(); it != d.end_const();
32  ++it) {
33  if (*it == 0)
34  return true;
35  }
36  std::cerr << "Product of tensor dimensions exceeds data size."
37  << std::endl
38  << "All dimensions: " << d << std::endl
39  << "Expected size: " << total_size << std::endl;
40  return false;
41  } else {
42  for (Indices::const_iterator it = d.begin_const(); it != d.end_const();
43  ++it) {
44  if (*it < 0) {
45  std::cerr << "Negative dimension in tensor's dimension #"
46  << (it - d.begin()) << std::endl
47  << "All dimensions:" << std::endl
48  << d << std::endl;
49  return false;
50  }
51  aux /= *it;
52  if (aux <= 0) {
53  std::cerr << "Product of tensor dimensions exceeds index range."
54  << std::endl
55  << "All dimensions: " << d << std::endl
56  << "Expected size: " << total_size << std::endl;
57  return false;
58  }
59  }
60  return true;
61  }
62 }
63 
64 bool verify_tensor_dimensions_match(const Indices &d1, const Indices &d2) {
65  if ((d1.size() != d2.size()) || some_unequal(d1, d2)) {
66  std::cerr << "A binary operation was attempted among two tensors" << std::endl
67  << "with different dimensions:" << std::endl
68  << "d1 = " << d1 << std::endl
69  << "d2 = " << d2 << std::endl;
70  return false;
71  }
72  return true;
73 }
74 
75 } // namespace