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