NumCpp  1.0
A C++ implementation of the Python Numpy library
cross.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/NdArray.hpp"
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
36 
37 #include <string>
38 
39 namespace nc
40 {
41  //============================================================================
42  // Method Description:
53  template<typename dtype>
54  NdArray<dtype> cross(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2, Axis inAxis = Axis::NONE)
55  {
57 
58  if (inArray1.shape() != inArray2.shape())
59  {
60  THROW_INVALID_ARGUMENT_ERROR("the input array dimensions are not consistant.");
61  }
62 
63  switch (inAxis)
64  {
65  case Axis::NONE:
66  {
67  const uint32 arraySize = inArray1.size();
68  if (arraySize != inArray2.size() || arraySize < 2 || arraySize > 3)
69  {
70  THROW_INVALID_ARGUMENT_ERROR("incompatible dimensions for cross product (dimension must be 2 or 3)");
71  }
72 
73  NdArray<dtype> in1 = inArray1.flatten();
74  NdArray<dtype> in2 = inArray2.flatten();
75 
76  switch (arraySize)
77  {
78  case 2:
79  {
80  NdArray<dtype> returnArray = { in1[0] * in2[1] - in1[1] * in2[0] };
81  return returnArray;
82  }
83  case 3:
84  {
85  dtype i = in1[1] * in2[2] - in1[2] * in2[1];
86  dtype j = -(in1[0] * in2[2] - in1[2] * in2[0]);
87  dtype k = in1[0] * in2[1] - in1[1] * in2[0];
88 
89  NdArray<dtype> returnArray = { i, j, k };
90  return returnArray;
91  }
92  default:
93  {
94  // this isn't actually possible, just putting this here to get rid
95  // of the compiler warning.
96  return NdArray<dtype>(0);
97  }
98  }
99  }
100  case Axis::ROW:
101  {
102  const Shape arrayShape = inArray1.shape();
103  if (arrayShape != inArray2.shape() || arrayShape.rows < 2 || arrayShape.rows > 3)
104  {
105  THROW_INVALID_ARGUMENT_ERROR("incompatible dimensions for cross product (dimension must be 2 or 3)");
106  }
107 
108  Shape returnArrayShape;
109  returnArrayShape.cols = arrayShape.cols;
110  if (arrayShape.rows == 2)
111  {
112  returnArrayShape.rows = 1;
113  }
114  else
115  {
116  returnArrayShape.rows = 3;
117  }
118 
119  NdArray<dtype> returnArray(returnArrayShape);
120  for (uint32 col = 0; col < arrayShape.cols; ++col)
121  {
122  const int32 theCol = static_cast<int32>(col);
123  NdArray<dtype> vec1 = inArray1({ 0, static_cast<int32>(arrayShape.rows) }, { theCol, theCol + 1 });
124  NdArray<dtype> vec2 = inArray2({ 0, static_cast<int32>(arrayShape.rows) }, { theCol, theCol + 1 });
125  NdArray<dtype> vecCross = cross(vec1, vec2, Axis::NONE);
126 
127  returnArray.put({ 0, static_cast<int32>(returnArrayShape.rows) }, { theCol, theCol + 1 }, vecCross);
128  }
129 
130  return returnArray;
131  }
132  case Axis::COL:
133  {
134  const Shape arrayShape = inArray1.shape();
135  if (arrayShape != inArray2.shape() || arrayShape.cols < 2 || arrayShape.cols > 3)
136  {
137  THROW_INVALID_ARGUMENT_ERROR("incompatible dimensions for cross product (dimension must be 2 or 3)");
138  }
139 
140  Shape returnArrayShape;
141  returnArrayShape.rows = arrayShape.rows;
142  if (arrayShape.cols == 2)
143  {
144  returnArrayShape.cols = 1;
145  }
146  else
147  {
148  returnArrayShape.cols = 3;
149  }
150 
151  NdArray<dtype> returnArray(returnArrayShape);
152  for (uint32 row = 0; row < arrayShape.rows; ++row)
153  {
154  const int32 theRow = static_cast<int32>(row);
155  NdArray<dtype> vec1 = inArray1({ theRow, theRow + 1 }, { 0, static_cast<int32>(arrayShape.cols) });
156  NdArray<dtype> vec2 = inArray2({ theRow, theRow + 1 }, { 0, static_cast<int32>(arrayShape.cols) });
157  NdArray<dtype> vecCross = cross(vec1, vec2, Axis::NONE);
158 
159  returnArray.put({ theRow, theRow + 1 }, { 0, static_cast<int32>(returnArrayShape.cols) }, vecCross);
160  }
161 
162  return returnArray;
163  }
164  default:
165  {
166  // this isn't actually possible, just putting this here to get rid
167  // of the compiler warning.
168  return NdArray<dtype>(0);
169  }
170  }
171  }
172 }
StaticAsserts.hpp
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
nc::Axis::NONE
@ NONE
nc::int32
std::int32_t int32
Definition: Types.hpp:37
Error.hpp
nc::Axis::ROW
@ ROW
STATIC_ASSERT_ARITHMETIC_OR_COMPLEX
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:51
nc::NdArray< dtype >
nc::constants::j
constexpr auto j
Definition: Constants.hpp:46
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::NdArray::put
NdArray< dtype > & put(int32 inIndex, value_type inValue)
Definition: NdArrayCore.hpp:3667
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4310
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::cross
NdArray< dtype > cross(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2, Axis inAxis=Axis::NONE)
Definition: cross.hpp:54
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:47
Shape.hpp
nc::NdArray::flatten
NdArray< dtype > flatten() const
Definition: NdArrayCore.hpp:2768
nc
Definition: Coordinate.hpp:45
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
Types.hpp
nc::Axis::COL
@ COL