NumCpp  2.7.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 
33 #include "NumCpp/NdArray.hpp"
34 
35 #include <complex>
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());
81  stl_algorithms::transform(inReal.cbegin(), inReal.cend(), returnArray.begin(),
82  [](dtype real) -> auto
83  {
84  return nc::complex(real);
85  });
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());
107  stl_algorithms::transform(inReal.cbegin(), inReal.cend(), inImag.cbegin(), returnArray.begin(),
108  [](dtype real, dtype imag) -> auto
109  {
110  return nc::complex(real, imag);
111  });
112 
113  return returnArray;
114  }
115 } // 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:1216
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1524
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
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:47
auto complex(dtype inReal)
Definition: complex.hpp:47
auto real(const std::complex< dtype > &inValue)
Definition: real.hpp:48