| 1 | #ifndef BOOST_QVM_DETAIL_COFACTOR_IMPL_HPP_INCLUDED |
|---|---|
| 2 | #define BOOST_QVM_DETAIL_COFACTOR_IMPL_HPP_INCLUDED |
| 3 | |
| 4 | // Copyright 2008-2022 Emil Dotchevski and Reverge Studios, Inc. |
| 5 | |
| 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 8 | |
| 9 | #include <boost/qvm/detail/determinant_impl.hpp> |
| 10 | #include <boost/qvm/mat_traits.hpp> |
| 11 | #include <boost/qvm/static_assert.hpp> |
| 12 | |
| 13 | namespace boost { namespace qvm { |
| 14 | |
| 15 | namespace |
| 16 | qvm_detail |
| 17 | { |
| 18 | template <class A> |
| 19 | BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS |
| 20 | typename deduce_mat<A>::type |
| 21 | cofactor_impl( A const & a ) |
| 22 | { |
| 23 | BOOST_QVM_STATIC_ASSERT(mat_traits<A>::rows==mat_traits<A>::cols); |
| 24 | int const N=mat_traits<A>::rows; |
| 25 | typedef typename mat_traits<A>::scalar_type T; |
| 26 | T c[N-1][N-1]; |
| 27 | typedef typename deduce_mat<A>::type R; |
| 28 | R b; |
| 29 | for( int j=0; j!=N; ++j ) |
| 30 | { |
| 31 | for( int i=0; i!=N; ++i ) |
| 32 | { |
| 33 | int i1=0; |
| 34 | for( int ii=0; ii!=N; ++ii ) |
| 35 | { |
| 36 | if( ii==i ) |
| 37 | continue; |
| 38 | int j1=0; |
| 39 | for( int jj=0; jj!=N; ++jj ) |
| 40 | { |
| 41 | if( jj==j ) |
| 42 | continue; |
| 43 | c[i1][j1] = mat_traits<A>::read_element_idx(ii,jj,a); |
| 44 | ++j1; |
| 45 | } |
| 46 | ++i1; |
| 47 | } |
| 48 | T det = determinant_impl(c); |
| 49 | if( (i+j)&1 ) |
| 50 | det=-det; |
| 51 | write_mat_element_idx(i,j,b,det); |
| 52 | } |
| 53 | } |
| 54 | return b; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | } } |
| 59 | |
| 60 | #endif |
| 61 |
