cofactor_impl.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //Copyright (c) 2008-2016 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef UUID_995547FAAE0E11DE8CF511E755D89593
  5. #define UUID_995547FAAE0E11DE8CF511E755D89593
  6. #include <boost/qvm/detail/determinant_impl.hpp>
  7. #include <boost/qvm/mat_traits.hpp>
  8. #include <boost/qvm/static_assert.hpp>
  9. namespace
  10. boost
  11. {
  12. namespace
  13. qvm
  14. {
  15. namespace
  16. qvm_detail
  17. {
  18. template <class A>
  19. 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. mat_traits<R>::write_element_idx(i,j,b) = det;
  52. }
  53. }
  54. return b;
  55. }
  56. }
  57. }
  58. }
  59. #endif