tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
tensor_scale.cc
1 /*
2  Copyright (c) 2010 Juan Jose Garcia Ripoll
3 
4  Tensor is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published
6  by the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #define TENSOR_LOAD_IMPL
20 #include <tensor/tensor.h>
21 
22 namespace tensor {
23 
25 // SCALE A TENSOR
26 //
27 // 1) IN PLACE
28 //
29 
30 template<typename t1, typename t2>
31 void doscale(t1 *p1, const t2 *p20, index d1, index d2, index d3) {
32  index i, j;
33  const t2 *p2;
34  if (d1 == 1) {
35  for (; d3; d3--) {
36  for (i = d2, p2 = p20; i; i--, p1++, p2++) {
37  *p1 *= *p2;
38  }
39  }
40  } else {
41  for (; d3; d3--) {
42  for (i = d2, p2 = p20; i; i--, p2++) {
43  t2 r = *p2;
44  for (j = d1; j; j--, p1++) {
45  *p1 *= r;
46  }
47  }
48  }
49  }
50 }
51 
52 template<typename elt_t>
53 void scale_inplace(Tensor<elt_t> &t, int ndx, const Vector<double> &v)
54 {
55  index d1, d2, d3;
56  surrounding_dimensions(t.get_dims(), t.normal_index(ndx), &d1, &d2, &d3);
57  if (d2 != v.size()) {
58  std::cerr << "In scale() the dimension " << ndx <<
59  " of the tensor does not match the length " <<
60  v.size() << " of the scale vector" << std::endl;
61  abort();
62  }
63  doscale(t.begin(), v.begin_const(), d1, d2, d3);
64 }
65 
66 //
67 // 2) OUT OF PLACE
68 //
69 
70 template <class t1, class t2, class t3>
71 void doscale(t1 *p1, const t2 *p2, const t3 *p30,
72  index d1, index d2, index d3)
73 {
74  index i, j;
75  const t3 *p3;
76  if (d1 == 1) {
77  for (; d3; d3--) {
78  for (i = d2, p3 = p30; i; i--, p1++, p2++, p3++) {
79  *p1 = *p2 * *p3;
80  }
81  }
82  } else {
83  for (; d3; d3--) {
84  for (i = d2, p3 = p30; i; i--, p3++) {
85  const t3 r = *p3;
86  for (j = d1; j; j--, p1++, p2++) {
87  *p1 = r * *p2;
88  }
89  }
90  }
91  }
92 }
93 
94 } // namespace tensor