NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
cross.hpp
Go to the documentation of this file.
1
28#pragma once
29
32#include "NumCpp/Core/Shape.hpp"
33#include "NumCpp/Core/Types.hpp"
34#include "NumCpp/NdArray.hpp"
35
36namespace nc
37{
38 //============================================================================
39 // Method Description:
49 template<typename dtype>
50 NdArray<dtype> cross(const NdArray<dtype>& inArray1, const NdArray<dtype>& inArray2, Axis inAxis = Axis::NONE)
51 {
53
54 if (inArray1.shape() != inArray2.shape())
55 {
56 THROW_INVALID_ARGUMENT_ERROR("the input array dimensions are not consistant.");
57 }
58
59 switch (inAxis)
60 {
61 case Axis::NONE:
62 {
63 const uint32 arraySize = inArray1.size();
64 if (arraySize != inArray2.size() || arraySize < 2 || arraySize > 3)
65 {
67 "incompatible dimensions for cross product (dimension must be 2 or 3)");
68 }
69
70 NdArray<dtype> in1 = inArray1.flatten();
71 NdArray<dtype> in2 = inArray2.flatten();
72
73 switch (arraySize)
74 {
75 case 2:
76 {
77 NdArray<dtype> returnArray = { in1[0] * in2[1] - in1[1] * in2[0] };
78 return returnArray;
79 }
80 case 3:
81 {
82 dtype i = in1[1] * in2[2] - in1[2] * in2[1];
83 dtype j = -(in1[0] * in2[2] - in1[2] * in2[0]);
84 dtype k = in1[0] * in2[1] - in1[1] * in2[0];
85
86 NdArray<dtype> returnArray = { i, j, k };
87 return returnArray;
88 }
89 default:
90 {
91 THROW_INVALID_ARGUMENT_ERROR("Unimplemented array size.");
92 return {}; // get rid of compiler warning
93 }
94 }
95 }
96 case Axis::ROW:
97 {
98 const Shape arrayShape = inArray1.shape();
99 if (arrayShape != inArray2.shape() || arrayShape.rows < 2 || arrayShape.rows > 3)
100 {
102 "incompatible dimensions for cross product (dimension must be 2 or 3)");
103 }
104
105 Shape returnArrayShape;
106 returnArrayShape.cols = arrayShape.cols;
107 if (arrayShape.rows == 2)
108 {
109 returnArrayShape.rows = 1;
110 }
111 else
112 {
113 returnArrayShape.rows = 3;
114 }
115
116 NdArray<dtype> returnArray(returnArrayShape);
117 for (uint32 col = 0; col < arrayShape.cols; ++col)
118 {
119 const auto theCol = static_cast<int32>(col);
120 NdArray<dtype> vec1 = inArray1(inArray1.rSlice(), { theCol, theCol + 1 });
121 NdArray<dtype> vec2 = inArray2(inArray2.rSlice(), { theCol, theCol + 1 });
122 NdArray<dtype> vecCross = cross(vec1, vec2, Axis::NONE);
123
124 returnArray.put({ 0, static_cast<int32>(returnArrayShape.rows) }, { theCol, theCol + 1 }, vecCross);
125 }
126
127 return returnArray;
128 }
129 case Axis::COL:
130 {
131 const Shape arrayShape = inArray1.shape();
132 if (arrayShape != inArray2.shape() || arrayShape.cols < 2 || arrayShape.cols > 3)
133 {
135 "incompatible dimensions for cross product (dimension must be 2 or 3)");
136 }
137
138 Shape returnArrayShape;
139 returnArrayShape.rows = arrayShape.rows;
140 if (arrayShape.cols == 2)
141 {
142 returnArrayShape.cols = 1;
143 }
144 else
145 {
146 returnArrayShape.cols = 3;
147 }
148
149 NdArray<dtype> returnArray(returnArrayShape);
150 for (uint32 row = 0; row < arrayShape.rows; ++row)
151 {
152 const auto theRow = static_cast<int32>(row);
153 NdArray<dtype> vec1 = inArray1({ theRow, theRow + 1 }, inArray1.cSlice());
154 NdArray<dtype> vec2 = inArray2({ theRow, theRow + 1 }, inArray2.cSlice());
155 NdArray<dtype> vecCross = cross(vec1, vec2, Axis::NONE);
156
157 returnArray.put({ theRow, theRow + 1 }, { 0, static_cast<int32>(returnArrayShape.cols) }, vecCross);
158 }
159
160 return returnArray;
161 }
162 default:
163 {
164 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
165 return {}; // get rid of compiler warning
166 }
167 }
168 }
169} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition StaticAsserts.hpp:56
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
self_type flatten() const
Definition NdArrayCore.hpp:2923
Slice rSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition NdArrayCore.hpp:1018
Slice cSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition NdArrayCore.hpp:1004
self_type & put(index_type inIndex, const value_type &inValue)
Definition NdArrayCore.hpp:3769
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
uint32 rows
Definition Core/shape.hpp:44
uint32 cols
Definition Core/shape.hpp:45
Definition Cartesian.hpp:39
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
std::int32_t int32
Definition Types.hpp:36
NdArray< dtype > cross(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2, Axis inAxis=Axis::NONE)
Definition cross.hpp:50
std::uint32_t uint32
Definition Types.hpp:40