MCL
A C++ library mirroring some of the most common Matlab functions.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pointwiseop.h
Go to the documentation of this file.
1 /*
2  MCL
3  Copyright (c) 2012-18, Enzo De Sena
4  All rights reserved.
5 
6  Authors: Enzo De Sena, enzodesena@gmail.com
7  */
8 
9 #ifndef MCL_POINTWISE_H
10 #define MCL_POINTWISE_H
11 
12 #include <cassert>
13 #include "mcltypes.h"
14 #include <vector>
15 #include <limits>
16 
17 namespace mcl {
18 
23 template<class T>
24 std::vector<T> Add(const std::vector<T>& vector_a,
25  const std::vector<T>& vector_b) noexcept {
26  ASSERT(vector_a.size() == vector_b.size());
27 
28  std::vector<T> output((Int)vector_a.size());
29  for (Int i=0; i<(Int)vector_a.size(); ++i) {
30  output[i] = vector_a[i]+vector_b[i];
31  }
32  return output;
33 }
34 
35 void Add(const Real* input_data_a,
36  const Real* input_data_b,
37  Int num_samples,
38  Real* output_data) noexcept;
39 
40 
41 template<>
42 inline std::vector<Real> Add(const std::vector<Real>& vector_a,
43  const std::vector<Real>& vector_b) noexcept {
44  ASSERT(vector_a.size() == vector_b.size());
45 
46  std::vector<Real> output((Int)vector_a.size());
47  Add(vector_a.data(), vector_b.data(), (Int)vector_a.size(),
48  output.data());
49  return output;
50 }
51 
53 template<class T>
54 std::vector<T> Opposite(const std::vector<T>& vector) noexcept {
55  // Checking we are not dealing with unsigned types.
56  // The assert below responds false to complex. TODO: fix this
57  //ASSERT(std::numeric_limits<T>::is_signed);
58 
59  std::vector<T> output(vector.size());
60  for (Int i=0; i<(Int)vector.size(); ++i) { output[i] = -vector[i]; }
61  return output;
62 }
63 
64 
66 std::vector<Real> Inverse(const std::vector<Real>& vector) noexcept;
67 
72 template<class T>
73 std::vector<T> Subtract(const std::vector<T>& vector_a,
74  const std::vector<T>& vector_b) noexcept {
75  return Add(vector_a, Opposite(vector_b));
76 }
77 
78 
83 template<class T>
84 std::vector<T> Multiply(const std::vector<T>& vector_a,
85  const std::vector<T>& vector_b) noexcept {
86  ASSERT(vector_a.size() == vector_b.size());
87 
88  std::vector<T> output((Int)vector_a.size());
89  for (Int i=0; i<(Int)vector_a.size(); ++i) {
90  output[i] = vector_a[i]*vector_b[i];
91  }
92  return output;
93 }
94 
95 void Multiply(const Real* input_data_a, const Real* input_data_b,
96  Int num_samples, Real* output_data) noexcept;
97 
98 
99 template<>
100 inline std::vector<Real> Multiply(const std::vector<Real>& vector_a,
101  const std::vector<Real>& vector_b) noexcept {
102  ASSERT(vector_a.size() == vector_b.size());
103 
104  std::vector<Real> output((Int)vector_a.size());
105  Multiply(vector_a.data(), vector_b.data(), (Int)vector_a.size(),
106  output.data());
107  return output;
108 }
109 
114 template<class T>
115 std::vector<T> Divide(const std::vector<T>& vector_a,
116  const std::vector<T>& vector_b) noexcept {
117  ASSERT(vector_a.size() == vector_b.size());
118  std::vector<T> output((Int)vector_a.size());
119  for (Int i=0; i<(Int)vector_a.size(); ++i) {
120  output[i] = vector_a[i]/vector_b[i];
121  }
122  return output;
123 }
124 
125 
127 template<class T>
128 std::vector<T> Exp(const std::vector<T>& vector) noexcept {
129  Int n(vector.size());
130  std::vector<T> output(vector.size());
131  for (Int i=0; i<n; ++i) { output[i] = exp(vector[i]); }
132  return output;
133 }
134 
135 
136 
141 std::vector<Complex> Conj(const std::vector<Complex>& vector) noexcept;
142 
144 std::vector<Complex>
145 ComplexVector(const std::vector<Real>& input) noexcept;
146 
148 std::vector<Real> RealPart(const std::vector<Complex>& input) noexcept;
149 
151 std::vector<Real> Imag(const std::vector<Complex>& input) noexcept;
152 
153 
158 std::vector<Real> Pow(const std::vector<Real>& vector,
159  Real exponent) noexcept;
160 
161 
163 std::vector<Real> Abs(const std::vector<Real>& input) noexcept;
164 
166 std::vector<Real> Abs(const std::vector<Complex>& input) noexcept;
167 
169 std::vector<Real> HalfWave(const std::vector<Real>& vector) noexcept;
170 
172 std::vector<Real> Cos(const std::vector<Real>& vector) noexcept;
173 
175 std::vector<Real> Sin(const std::vector<Real>& vector) noexcept;
176 
181 std::vector<Real> Log(const std::vector<Real>& vector) noexcept;
182 
187 std::vector<Real> Log10(const std::vector<Real>& vector) noexcept;
188 
189 std::vector<Int> ConvertToInt(const std::vector<UInt>& vector) noexcept;
190 
191 bool PointWiseOpTest();
192 
193 }
195 #endif