NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
copySign.hpp
Go to the documentation of this file.
1 #pragma once
29 
33 #include "NumCpp/NdArray.hpp"
34 
35 #include <cmath>
36 #include <string>
37 
38 namespace nc
39 {
40  //============================================================================
41  // Method Description:
50  template<typename dtype>
51  NdArray<dtype> copySign(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2)
52  {
54 
55  if (inArray1.shape() != inArray2.shape())
56  {
57  THROW_INVALID_ARGUMENT_ERROR("input arrays are not consistant.");
58  }
59 
60  NdArray<dtype> returnArray(inArray1.shape());
61  stl_algorithms::transform(inArray1.cbegin(), inArray1.cend(), inArray2.cbegin(), returnArray.begin(),
62  [](dtype inValue1, dtype inValue2) -> dtype
63  {
64  return inValue2 < dtype{ 0 } ? std::abs(inValue1) * -1 : std::abs(inValue1);
65  });
66 
67  return returnArray;
68  }
69 } // 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 abs(dtype inValue) noexcept
Definition: abs.hpp:49
NdArray< dtype > copySign(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition: copySign.hpp:51