NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
complex.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <complex>
31 
35 #include "NumCpp/NdArray.hpp"
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
46  template<typename dtype>
47  auto complex(dtype inReal)
48  {
50 
51  return std::complex<dtype>(inReal);
52  }
53 
54  //============================================================================
55  // Method Description:
62  template<typename dtype>
63  auto complex(dtype inReal, dtype inImag)
64  {
66 
67  return std::complex<dtype>(inReal, inImag);
68  }
69 
70  //============================================================================
71  // Method Description:
77  template<typename dtype>
78  auto complex(const NdArray<dtype>& inReal)
79  {
80  NdArray<decltype(nc::complex(dtype{ 0 }))> returnArray(inReal.shape());
82  inReal.cbegin(),
83  inReal.cend(),
84  returnArray.begin(),
85  [](dtype real) -> auto{ return nc::complex(real); });
86 
87  return returnArray;
88  }
89 
90  //============================================================================
91  // Method Description:
98  template<typename dtype>
99  auto complex(const NdArray<dtype>& inReal, const NdArray<dtype>& inImag)
100  {
101  if (inReal.shape() != inImag.shape())
102  {
103  THROW_INVALID_ARGUMENT_ERROR("Input real array must be the same shape as input imag array");
104  }
105 
106  NdArray<decltype(nc::complex(dtype{ 0 }, dtype{ 0 }))> returnArray(inReal.shape());
108  inReal.cbegin(),
109  inReal.cend(),
110  inImag.cbegin(),
111  returnArray.begin(),
112  [](dtype real, dtype imag) -> auto{ return nc::complex(real, imag); });
113 
114  return returnArray;
115  }
116 } // 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:1221
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4092
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1529
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:784
Definition: Coordinate.hpp:45
auto imag(const std::complex< dtype > &inValue)
Definition: imag.hpp:47
auto complex(dtype inReal)
Definition: complex.hpp:47
auto real(const std::complex< dtype > &inValue)
Definition: real.hpp:48