NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
det.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <type_traits>
31
35#include "NumCpp/Core/Shape.hpp"
36#include "NumCpp/Core/Types.hpp"
37#include "NumCpp/NdArray.hpp"
38
39namespace nc::linalg
40{
41 namespace detail
42 {
43 //============================================================================
44 // Method Description:
51 template<typename dtype>
52 auto det(const NdArray<dtype>& inArray, uint32 order)
53 -> std::conditional_t<std::is_integral_v<dtype>, int64, double>
54 {
56
57 using ReturnType = std::conditional_t<std::is_integral_v<dtype>, int64, double>;
58
59 if (order == 1)
60 {
61 return static_cast<ReturnType>(inArray.front());
62 }
63
64 if (order == 2)
65 {
66 return static_cast<ReturnType>(inArray(0, 0)) * static_cast<ReturnType>(inArray(1, 1)) -
67 static_cast<ReturnType>(inArray(0, 1)) * static_cast<ReturnType>(inArray(1, 0));
68 }
69
70 if (order == 3)
71 {
72 const auto aei = static_cast<ReturnType>(inArray(0, 0)) * static_cast<ReturnType>(inArray(1, 1)) *
73 static_cast<ReturnType>(inArray(2, 2));
74 const auto bfg = static_cast<ReturnType>(inArray(0, 1)) * static_cast<ReturnType>(inArray(1, 2)) *
75 static_cast<ReturnType>(inArray(2, 0));
76 const auto cdh = static_cast<ReturnType>(inArray(0, 2)) * static_cast<ReturnType>(inArray(1, 0)) *
77 static_cast<ReturnType>(inArray(2, 1));
78 const auto ceg = static_cast<ReturnType>(inArray(0, 2)) * static_cast<ReturnType>(inArray(1, 1)) *
79 static_cast<ReturnType>(inArray(2, 0));
80 const auto bdi = static_cast<ReturnType>(inArray(0, 1)) * static_cast<ReturnType>(inArray(1, 0)) *
81 static_cast<ReturnType>(inArray(2, 2));
82 const auto afh = static_cast<ReturnType>(inArray(0, 0)) * static_cast<ReturnType>(inArray(1, 2)) *
83 static_cast<ReturnType>(inArray(2, 1));
84
85 return aei + bfg + cdh - ceg - bdi - afh;
86 }
87
88 ReturnType determinant = 0;
89 ReturnType sign = 1;
90 NdArray<dtype> submat(order - 1);
91
92 for (uint32 c = 0; c < order; ++c)
93 {
94 uint32 subi = 0;
95 for (uint32 i = 1; i < order; ++i)
96 {
97 uint32 subj = 0;
98 for (uint32 j = 0; j < order; ++j)
99 {
100 if (j == c)
101 {
102 continue;
103 }
104
105 submat(subi, subj++) = inArray(i, j);
106 }
107 ++subi;
108 }
109
110 determinant += (sign * static_cast<ReturnType>(inArray(0, c)) * det(submat, order - 1));
111 sign *= -1;
112 }
113
114 return determinant;
115 }
116 } // namespace detail
117
118 //============================================================================
119 // Method Description:
128 template<typename dtype>
129 auto det(const NdArray<dtype>& inArray)
130 {
132
133 const Shape inShape = inArray.shape();
134 if (!inShape.issquare())
135 {
136 THROW_INVALID_ARGUMENT_ERROR("input array must be square.");
137 }
138
139 return detail::det(inArray, inShape.rows);
140 }
141} // namespace nc::linalg
#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:135
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
uint32 rows
Definition Core/shape.hpp:44
bool issquare() const noexcept
Definition Core/shape.hpp:125
Definition det.hpp:42
auto det(const NdArray< dtype > &inArray, uint32 order) -> std::conditional_t< std::is_integral_v< dtype >, int64, double >
Definition det.hpp:52
Definition cholesky.hpp:41
auto det(const NdArray< dtype > &inArray)
Definition det.hpp:129
int8 sign(dtype inValue) noexcept
Definition sign.hpp:49
std::int64_t int64
Definition Types.hpp:35
std::uint32_t uint32
Definition Types.hpp:40