NumCpp  2.5.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
complex.hpp
Go to the documentation of this file.
1 #pragma once
29 
33 #include "NumCpp/NdArray.hpp"
34 
35 #include <complex>
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
47  template<typename dtype>
48  auto complex(dtype inReal)
49  {
51 
52  return std::complex<dtype>(inReal);
53  }
54 
55  //============================================================================
56  // Method Description:
64  template<typename dtype>
65  auto complex(dtype inReal, dtype inImag)
66  {
68 
69  return std::complex<dtype>(inReal, inImag);
70  }
71 
72  //============================================================================
73  // Method Description:
80  template<typename dtype>
81  auto complex(const NdArray<dtype>& inReal)
82  {
83  NdArray<decltype(nc::complex(dtype{0}))> returnArray(inReal.shape());
84  stl_algorithms::transform(inReal.cbegin(), inReal.cend(), returnArray.begin(),
85  [](dtype real) -> auto
86  {
87  return nc::complex(real);
88  });
89 
90  return returnArray;
91  }
92 
93  //============================================================================
94  // Method Description:
102  template<typename dtype>
103  auto complex(const NdArray<dtype>& inReal, const NdArray<dtype>& inImag)
104  {
105  if (inReal.shape() != inImag.shape())
106  {
107  THROW_INVALID_ARGUMENT_ERROR("Input real array must be the same shape as input imag array");
108  }
109 
110  NdArray<decltype(nc::complex(dtype{0}, dtype{0}))> returnArray(inReal.shape());
111  stl_algorithms::transform(inReal.cbegin(), inReal.cend(), inImag.cbegin(), returnArray.begin(),
112  [](dtype real, dtype imag) -> auto
113  {
114  return nc::complex(real, imag);
115  });
116 
117  return returnArray;
118  }
119 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1270
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1614
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:702
Definition: Coordinate.hpp:45
auto imag(const std::complex< dtype > &inValue)
Definition: imag.hpp:49
auto complex(dtype inReal)
Definition: complex.hpp:48
auto real(const std::complex< dtype > &inValue)
Definition: real.hpp:50