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