10 #define MCL_VECTOROP_H
27 return (
UInt) input.size();
35 return std::vector<T>(length);
40 return std::vector<T>();
49 std::vector<T>
ZeroPad(
const std::vector<T>& input,
50 const Int total_length) noexcept {
51 std::vector<T> output = Zeros<T>(total_length);
52 Int M = ((
Int)input.size() < total_length) ? input.size() : total_length;
53 for (
Int i=0; i<M; ++i) { output[i] = input[i]; }
64 std::vector<T>
Multiply(
const std::vector<T>& vector,
65 const T gain) noexcept {
66 std::vector<T> output(vector.size());
67 for (
Int i=0; i<(
Int)vector.size(); ++i) {
68 output[i] = vector[i]*gain;
75 const Int num_samples,
77 Real* output_data) noexcept;
80 inline std::vector<Real>
Multiply(
const std::vector<Real>& input,
81 const Real gain) noexcept {
82 std::vector<Real> output(input.size());
83 Multiply(input.data(), input.size(), gain, output.data());
92 const Real* input_data_add,
93 const Int num_samples,
94 Real* output_data) noexcept;
102 std::vector<T>
Add(
const std::vector<T>& vector_a,
103 const T scalar) noexcept {
104 std::vector<T> output((
Int)vector_a.size());
105 for (
Int i=0; i<(
Int)vector_a.size(); ++i) {
106 output[i] = vector_a[i]+scalar;
121 std::vector<T>
Subset(
const std::vector<T>& vector,
122 const Int from_index,
const Int to_index) noexcept {
125 ASSERT(from_index <= to_index);
127 return std::vector<T>(vector.begin() + from_index,
128 vector.begin() + to_index + 1);
138 const std::vector<T>& vector_b) noexcept {
139 std::vector<T> output = Zeros<T>((
Int)vector_a.size()+(
Int)vector_b.size());
140 vector_a.insert(vector_a.end(), vector_b.begin(), vector_b.end());
148 std::vector<T> output(1, element);
154 std::vector<T>
BinaryVector(
const T& element_a,
const T& element_b) noexcept {
155 std::vector<T> output(2);
156 output[0] = element_a;
157 output[1] = element_b;
166 std::vector<T>
Flip(std::vector<T> vector) noexcept {
167 if (vector.size() <= 1) {
return vector; }
169 for (
Int i=0; i<=((
Int) (floor(N/2)-1)); ++i) {
170 T temp_value = vector[i];
171 vector[i] = vector[N-i-1];
172 vector[N-i-1] = temp_value;
183 Int num_positions) noexcept {
184 Int N = vector.size();
185 std::vector<T> output(N);
186 for (
Int i=0; i<
N; ++i) {
188 output[i] = vector[index];
196 std::vector<T>
Conv(
const std::vector<T>& vector_a,
197 const std::vector<T>& vector_b) noexcept {
198 Int N_a = (
Int)vector_a.size();
199 Int N_b = (
Int)vector_b.size();
200 Int out_length = N_a+N_b-1;
202 std::vector<T> moving_vector_temp =
Concatenate(Zeros<T>(N_b-1),
204 std::vector<T> moving_vector_a =
Concatenate(moving_vector_temp,
207 std::vector<T> output = Zeros<T>(out_length);
208 for (
Int n=0; n<out_length; ++n) {
209 for (
Int m=0; m<N_b; ++m) {
210 output[out_length-n-1] += moving_vector_a[n+m]*vector_b[m];
223 AddVectors(
const std::vector<std::vector<T> >& vectors) noexcept {
225 std::vector<Int> vector_lengths(vectors.size());
226 for (
Int i=0; i<(
Int)vectors.size(); ++i) {
227 vector_lengths[i] = (
Int)vectors[i].size();
229 Int max_length(
Max(vector_lengths));
231 std::vector<T> output = Zeros<T>(max_length);
232 for (
Int i=0; i<(
Int)vectors.size(); ++i) {
233 output =
Add(output,
ZeroPad(vectors[i], max_length));
245 const std::vector<T>& vector_b) noexcept {
247 Int max_length(
Max((
Int)vector_a.size(), (
Int)vector_b.size()));
249 std::vector<T> output = Zeros<T>(max_length);
250 output =
Add(output,
ZeroPad(vector_a, max_length));
251 output =
Add(output,
ZeroPad(vector_b, max_length));
261 const std::vector<T>& vector_b) noexcept {
262 ASSERT(vector_a.size() == vector_b.size());
264 std::vector<T> output;
265 for (
Int i=0; i<(
Int)vector_a.size(); ++i) {
266 output.push_back(vector_a[i]);
267 output.push_back(vector_b[i]);
276 const Int downsampling_factor) noexcept {
277 ASSERT(downsampling_factor >= 1);
278 std::vector<T> output;
279 for (
Int i=0; i<(
Int)vector.size(); i += downsampling_factor) {
280 output.push_back(vector[i]);
291 if ((to-from) < 0) {
return EmptyVector<T>(); }
292 const Int vector_length = (
UInt) (to-from+1);
293 std::vector<T> output(vector_length);
294 for (
Int i=0; i<vector_length; ++i) {
295 output[i] = ((T) i) + ((T) from);
314 std::vector<T>
Elements(
const std::vector<T>& vector,
316 const Int to_id) noexcept {
317 return std::vector<T>(vector.begin() + ((
Int)from_id),
318 vector.begin() + ((
Int)to_id)+1);
325 const Int subset_length,
326 bool zeropad_if_shorter =
false) noexcept {
327 const Int size = vector.size();
329 const Int from_sample = subset_id * subset_length;
330 if (from_sample >= size) {
331 if (zeropad_if_shorter) {
332 return Zeros<T>(subset_length);
334 return std::vector<T>();
338 const Int to_sample =
Min(from_sample + subset_length - 1,
341 const Int actual_length = to_sample - from_sample + 1;
342 if (zeropad_if_shorter && actual_length < subset_length) {
343 return ZeroPad(
Elements(vector, from_sample, to_sample), subset_length);
345 return Elements(vector, from_sample, to_sample);
354 T
Prod(
const std::vector<T>& vector) noexcept {
355 const Int num_elements = vector.size();
357 for (
Int i=0; i<num_elements; ++i) { output *= vector[i]; }
363 T
Dot(
const std::vector<T>& vector_a,
364 const std::vector<T>& vector_b) noexcept {
365 const Int num_elements = (
Int)vector_a.size();
366 ASSERT(num_elements == (
Int)vector_b.size());
369 for (
Int i=0; i<num_elements; ++i) {
370 output += vector_a[i]*vector_b[i];
375 Real Norm(
const std::vector<Real>& vector,
376 Real l_norm = 2.0) noexcept;
379 void Print(const T* vector, const
Int num_elements) noexcept {
380 std::cout<<
"\n------------\n";
381 for (
Int i=0; i<num_elements; ++i) {
382 std::cout<<vector[i]<<std::endl;
384 std::cout<<
"------------\n";
388 void Print(
const std::vector<T>& vector) noexcept {
389 Print(vector.data(), vector.size());
393 std::vector<Real>
Ones(
Int length) noexcept;
396 std::vector<Real>
Hann(
const Int length) noexcept;
399 std::vector<Real>
Hamming(
const Int length) noexcept;
402 const Real ratio) noexcept;
407 Int num_elements) noexcept;
410 Real Sum(
const std::vector<Real>& input) noexcept;
413 Real Mean(
const std::vector<Real>& input) noexcept;
419 Real Geomean(
const std::vector<Real>& input) noexcept;
426 Real Mean(
const std::vector<Real>& input,
427 const std::vector<Real>& weigths) noexcept;
433 Real Std(
const std::vector<Real>& input) noexcept;
436 Real Var(
const std::vector<Real>& input) noexcept;
439 Real Var(
const std::vector<Real>& input,
440 const std::vector<Real>& weights) noexcept;
443 std::vector<std::string>
Split(
const std::string&
string,
444 char delim) noexcept;
447 std::vector<Complex>
Poly(
const std::vector<Complex> roots) noexcept;
448 std::vector<Complex>
Poly(
const std::vector<Real> roots) noexcept;
456 Matrix<Real>
Cov(
const std::vector<Real>& x,
457 const std::vector<Real>& y) noexcept;
459 Matrix<Real>
Cov(
const std::vector<std::vector<Real> >& input) noexcept;
462 const std::vector<
Real>& y) noexcept;
468 std::vector<
Real>
CumSum(const std::vector<
Real>& input) noexcept;
471 std::vector<std::vector<
Real> >
Enframe(const std::vector<
Real>& input,
472 const std::vector<
Real>& window,
473 const
Int frame_increment) noexcept;
476 const std::vector<
Real>& window,
477 const
Int frame_increment) noexcept;