NumCpp  2.16.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
complex.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31
35#include "NumCpp/NdArray.hpp"
36
37namespace nc
38{
39 //============================================================================
40 // Method Description:
46 template<typename dtype, typename dtypeOut = dtype>
47 auto complex(dtype inReal)
48 {
51
52 return std::complex<dtypeOut>(inReal);
53 }
54
55 //============================================================================
56 // Method Description:
63 template<typename dtype, typename dtypeOut = dtype>
64 auto complex(dtype inReal, dtype inImag)
65 {
68
69 return std::complex<dtypeOut>(inReal, inImag);
70 }
71
72 //============================================================================
73 // Method Description:
79 template<typename dtype, typename dtypeOut = dtype, std::enable_if_t<std::is_arithmetic_v<dtype>, int> = 0>
80 auto complex(const NdArray<dtype>& inReal)
81 {
82 NdArray<decltype(nc::complex(dtypeOut{ 0 }))> returnArray(inReal.shape());
84 inReal.cend(),
85 returnArray.begin(),
86 [](dtype real) -> auto { return nc::complex<dtype, dtypeOut>(real); });
87
88 return returnArray;
89 }
90
91 //============================================================================
92 // Method Description:
99 template<typename dtype, typename dtypeOut = dtype>
100 auto complex(const NdArray<dtype>& inReal, const NdArray<dtype>& inImag)
101 {
102 if (inReal.shape() != inImag.shape())
103 {
104 THROW_INVALID_ARGUMENT_ERROR("Input real array must be the same shape as input imag array");
105 }
106
107 NdArray<decltype(nc::complex(dtypeOut{ 0 }, dtypeOut{ 0 }))> returnArray(inReal.shape());
109 inReal.cend(),
110 inImag.cbegin(),
111 returnArray.begin(),
112 [](dtype real, dtype imag) -> auto
113 { return nc::complex<dtype, dtypeOut>(real, imag); });
114
115 return returnArray;
116 }
117
118 //============================================================================
119 // Method Description:
125 template<typename dtype, typename dtypeOut = dtype>
126 auto complex(const NdArray<std::complex<dtype>>& inArray)
127 {
129 STATIC_ASSERT_ARITHMETIC(dtypeOut);
130
131 return inArray.template astype<std::complex<dtypeOut>>();
132 }
133} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1365
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4591
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1673
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:776
Definition: Cartesian.hpp:40
auto imag(const std::complex< dtype > &inValue)
Definition: imag.hpp:47
auto real(const std::complex< dtype > &inValue)
Definition: real.hpp:48
auto complex(dtype inReal)
Definition: complex.hpp:47