// Boost.Units - A C++ library for zero-overhead dimensional analysis and // unit/quantity manipulation and conversion // // Copyright (C) 2003-2008 Matthias Christian Schabel // Copyright (C) 2008 Steven Watanabe // // 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_UNITS_DETAIL_LINEAR_ALGEBRA_HPP #define BOOST_UNITS_DETAIL_LINEAR_ALGEBRA_HPP #include #include #include #include #include #include #include #include #include #include namespace boost { namespace units { namespace detail { // typedef list equation; template struct eliminate_from_pair_of_equations_impl; template struct eliminate_from_pair_of_equations; template struct elimination_impl; template struct elimination_skip_leading_zeros_impl; template struct substitute; template struct substitute_impl; template struct solve_impl; template struct solve; template struct check_extra_equations_impl; template struct normalize_units_impl; struct inconsistent {}; // generally useful utilies. template struct divide_equation { template struct apply { typedef list::type, typename divide_equation::template apply::type> type; }; }; template<> struct divide_equation<0> { template struct apply { typedef dimensionless_type type; }; }; // eliminate_from_pair_of_equations takes a pair of // equations and eliminates the first variable. // // equation eliminate_from_pair_of_equations(equation l1, equation l2) { // rational x1 = l1.front(); // rational x2 = l2.front(); // return(transform(pop_front(l1), pop_front(l2), _1 * x2 - _2 * x1)); // } template struct eliminate_from_pair_of_equations_impl { template struct apply { typedef list< typename mpl::minus< typename mpl::times::type, typename mpl::times::type >::type, typename eliminate_from_pair_of_equations_impl::template apply< typename Begin1::next, typename Begin2::next, X1, X2 >::type > type; }; }; template<> struct eliminate_from_pair_of_equations_impl<0> { template struct apply { typedef dimensionless_type type; }; }; template struct eliminate_from_pair_of_equations { typedef E1 begin1; typedef E2 begin2; typedef typename eliminate_from_pair_of_equations_impl<(E1::size::value - 1)>::template apply< typename begin1::next, typename begin2::next, typename begin1::item, typename begin2::item >::type type; }; // Stage 1. Determine which dimensions should // have dummy base units. For this purpose // row reduce the matrix. template struct make_zero_vector { typedef list, typename make_zero_vector::type> type; }; template<> struct make_zero_vector<0> { typedef dimensionless_type type; }; template struct create_row_of_identity { typedef list, typename create_row_of_identity::type> type; }; template struct create_row_of_identity<0, TotalColumns> { typedef list, typename make_zero_vector::type> type; }; template struct create_row_of_identity { // error }; template struct determine_extra_equations_impl; template struct determine_extra_equations_skip_zeros_impl; // not the last row and not zero. template<> struct determine_extra_equations_skip_zeros_impl { template struct apply { // remove the equation being eliminated against from the set of equations. typedef typename determine_extra_equations_impl::template apply::type next_equations; // since this column was present, strip it out. typedef Result type; }; }; // the last row but not zero. template<> struct determine_extra_equations_skip_zeros_impl { template struct apply { // remove this equation. typedef dimensionless_type next_equations; // since this column was present, strip it out. typedef Result type; }; }; // the first columns is zero but it is not the last column. // continue with the same loop. template<> struct determine_extra_equations_skip_zeros_impl { template struct apply { typedef typename RowsBegin::next::item next_row; typedef typename determine_extra_equations_skip_zeros_impl< next_row::item::Numerator == 0, RemainingRows == 2 // the next one will be the last. >::template apply< typename RowsBegin::next, RemainingRows - 1, CurrentColumn, TotalColumns, Result > next; typedef list next_equations; typedef typename next::type type; }; }; // all the elements in this column are zero. template<> struct determine_extra_equations_skip_zeros_impl { template struct apply { typedef list next_equations; typedef list::type, Result> type; }; }; template struct determine_extra_equations_impl { template struct apply { typedef list< typename eliminate_from_pair_of_equations::type, typename determine_extra_equations_impl::template apply::type > type; }; }; template<> struct determine_extra_equations_impl<0> { template struct apply { typedef dimensionless_type type; }; }; template struct determine_extra_equations { template struct apply { typedef typename RowsBegin::item top_row; typedef typename determine_extra_equations_skip_zeros_impl< top_row::item::Numerator == 0, RowsBegin::item::size::value == 1 >::template apply< RowsBegin, RowsBegin::size::value, TotalColumns - RemainingColumns, TotalColumns, Result > column_info; typedef typename determine_extra_equations< RemainingColumns - 1, column_info::next_equations::size::value == 0 >::template apply< typename column_info::next_equations, TotalColumns, typename column_info::type >::type type; }; }; template struct determine_extra_equations { template struct apply { typedef typename determine_extra_equations::template apply< RowsBegin, TotalColumns, list::type, Result> >::type type; }; }; template<> struct determine_extra_equations<0, true> { template struct apply { typedef Result type; }; }; // Stage 2 // invert the matrix using Gauss-Jordan elimination template struct invert_strip_leading_zeroes; template struct invert_handle_after_pivot_row; // When processing column N, none of the first N rows // can be the pivot column. template struct invert_handle_inital_rows { template struct apply { typedef typename invert_handle_inital_rows::template apply< typename RowsBegin::next, typename IdentityBegin::next > next; typedef typename RowsBegin::item current_row; typedef typename IdentityBegin::item current_identity_row; typedef typename next::pivot_row pivot_row; typedef typename next::identity_pivot_row identity_pivot_row; typedef list< typename eliminate_from_pair_of_equations_impl<(current_row::size::value) - 1>::template apply< typename current_row::next, pivot_row, typename current_row::item, static_rational<1> >::type, typename next::new_matrix > new_matrix; typedef list< typename eliminate_from_pair_of_equations_impl<(current_identity_row::size::value)>::template apply< current_identity_row, identity_pivot_row, typename current_row::item, static_rational<1> >::type, typename next::identity_result > identity_result; }; }; // This handles the switch to searching for a pivot column. // The pivot row will be propagated up in the typedefs // pivot_row and identity_pivot_row. It is inserted here. template<> struct invert_handle_inital_rows<0> { template struct apply { typedef typename RowsBegin::item current_row; typedef typename invert_strip_leading_zeroes< (current_row::item::Numerator == 0), (RowsBegin::size::value == 1) >::template apply< RowsBegin, IdentityBegin > next; // results typedef list new_matrix; typedef list identity_result; typedef typename next::pivot_row pivot_row; typedef typename next::identity_pivot_row identity_pivot_row; }; }; // The first internal element which is not zero. template<> struct invert_strip_leading_zeroes { template struct apply { typedef typename RowsBegin::item current_row; typedef typename current_row::item current_value; typedef typename divide_equation<(current_row::size::value - 1)>::template apply::type new_equation; typedef typename divide_equation<(IdentityBegin::item::size::value)>::template apply::type transformed_identity_equation; typedef typename invert_handle_after_pivot_row<(RowsBegin::size::value - 1)>::template apply< typename RowsBegin::next, typename IdentityBegin::next, new_equation, transformed_identity_equation > next; // results // Note that we don't add the pivot row to the // results here, because it needs to propagated up // to the diagonal. typedef typename next::new_matrix new_matrix; typedef typename next::identity_result identity_result; typedef new_equation pivot_row; typedef transformed_identity_equation identity_pivot_row; }; }; // The one and only non-zero element--at the end template<> struct invert_strip_leading_zeroes { template struct apply { typedef typename RowsBegin::item current_row; typedef typename current_row::item current_value; typedef typename divide_equation<(current_row::size::value - 1)>::template apply::type new_equation; typedef typename divide_equation<(IdentityBegin::item::size::value)>::template apply::type transformed_identity_equation; // results // Note that we don't add the pivot row to the // results here, because it needs to propagated up // to the diagonal. typedef dimensionless_type identity_result; typedef dimensionless_type new_matrix; typedef new_equation pivot_row; typedef transformed_identity_equation identity_pivot_row; }; }; // One of the initial zeroes template<> struct invert_strip_leading_zeroes { template struct apply { typedef typename RowsBegin::item current_row; typedef typename RowsBegin::next::item next_row; typedef typename invert_strip_leading_zeroes< next_row::item::Numerator == 0, RowsBegin::size::value == 2 >::template apply< typename RowsBegin::next, typename IdentityBegin::next > next; typedef typename IdentityBegin::item current_identity_row; // these are propagated up. typedef typename next::pivot_row pivot_row; typedef typename next::identity_pivot_row identity_pivot_row; typedef list< typename eliminate_from_pair_of_equations_impl<(current_row::size::value - 1)>::template apply< typename current_row::next, pivot_row, typename current_row::item, static_rational<1> >::type, typename next::new_matrix > new_matrix; typedef list< typename eliminate_from_pair_of_equations_impl<(current_identity_row::size::value)>::template apply< current_identity_row, identity_pivot_row, typename current_row::item, static_rational<1> >::type, typename next::identity_result > identity_result; }; }; // the last element, and is zero. // Should never happen. template<> struct invert_strip_leading_zeroes { }; template struct invert_handle_after_pivot_row { template struct apply { typedef typename invert_handle_after_pivot_row::template apply< typename RowsBegin::next, typename IdentityBegin::next, MatrixPivot, IdentityPivot > next; typedef typename RowsBegin::item current_row; typedef typename IdentityBegin::item current_identity_row; typedef MatrixPivot pivot_row; typedef IdentityPivot identity_pivot_row; // results typedef list< typename eliminate_from_pair_of_equations_impl<(current_row::size::value - 1)>::template apply< typename current_row::next, pivot_row, typename current_row::item, static_rational<1> >::type, typename next::new_matrix > new_matrix; typedef list< typename eliminate_from_pair_of_equations_impl<(current_identity_row::size::value)>::template apply< current_identity_row, identity_pivot_row, typename current_row::item, static_rational<1> >::type, typename next::identity_result > identity_result; }; }; template<> struct invert_handle_after_pivot_row<0> { template struct apply { typedef dimensionless_type new_matrix; typedef dimensionless_type identity_result; }; }; template struct invert_impl { template struct apply { typedef typename invert_handle_inital_rows::template apply process_column; typedef typename invert_impl::template apply< typename process_column::new_matrix, typename process_column::identity_result >::type type; }; }; template<> struct invert_impl<0> { template struct apply { typedef IdentityBegin type; }; }; template struct make_identity { template struct apply { typedef list::type, typename make_identity::template apply::type> type; }; }; template<> struct make_identity<0> { template struct apply { typedef dimensionless_type type; }; }; template struct make_square_and_invert { typedef typename Matrix::item top_row; typedef typename determine_extra_equations<(top_row::size::value), false>::template apply< Matrix, // RowsBegin top_row::size::value, // TotalColumns Matrix // Result >::type invertible; typedef typename invert_impl::template apply< invertible, typename make_identity::template apply::type >::type type; }; // find_base_dimensions takes a list of // base_units and returns a sorted list // of all the base_dimensions they use. // // list find_base_dimensions(list l) { // set dimensions; // for_each(base_unit unit : l) { // for_each(dim d : unit.dimension_type) { // dimensions = insert(dimensions, d.tag_type); // } // } // return(sort(dimensions, _1 > _2, front_inserter(list()))); // } typedef char set_no; struct set_yes { set_no dummy[2]; }; template struct wrap {}; struct set_end { static set_no lookup(...); typedef mpl::long_<0> size; }; template struct set : Next { using Next::lookup; static set_yes lookup(wrap*); typedef T item; typedef Next next; typedef typename mpl::next::type size; }; template struct set_insert; template<> struct set_insert { template struct apply { typedef Set type; }; }; template<> struct set_insert { template struct apply { typedef set type; }; }; template struct has_key { static const long size = sizeof(Set::lookup((wrap*)0)); static const bool value = (size == sizeof(set_yes)); }; template struct find_base_dimensions_impl_impl { template struct apply { typedef typename find_base_dimensions_impl_impl::template apply< typename Begin::next, S >::type next; typedef typename set_insert< (has_key::value) >::template apply< next, typename Begin::item::tag_type >::type type; }; }; template<> struct find_base_dimensions_impl_impl<0> { template struct apply { typedef S type; }; }; template struct find_base_dimensions_impl { template struct apply { typedef typename find_base_dimensions_impl_impl<(Begin::item::dimension_type::size::value)>::template apply< typename Begin::item::dimension_type, typename find_base_dimensions_impl::template apply::type >::type type; }; }; template<> struct find_base_dimensions_impl<0> { template struct apply { typedef set_end type; }; }; template struct find_base_dimensions { typedef typename insertion_sort< typename find_base_dimensions_impl< (T::size::value) >::template apply::type >::type type; }; // calculate_base_dimension_coefficients finds // the coefficients corresponding to the first // base_dimension in each of the dimension_lists. // It returns two values. The first result // is a list of the coefficients. The second // is a list with all the incremented iterators. // When we encounter a base_dimension that is // missing from a dimension_list, we do not // increment the iterator and we set the // coefficient to zero. template struct calculate_base_dimension_coefficients_func; template<> struct calculate_base_dimension_coefficients_func { template struct apply { typedef typename T::item::value_type type; typedef typename T::next next; }; }; template<> struct calculate_base_dimension_coefficients_func { template struct apply { typedef static_rational<0> type; typedef T next; }; }; // begins_with_dimension returns true iff its first // parameter is a valid iterator which yields its // second parameter when dereferenced. template struct begins_with_dimension { template struct apply : boost::is_same< Dim, typename Iterator::item::tag_type > {}; }; template<> struct begins_with_dimension { template struct apply : mpl::false_ {}; }; template struct calculate_base_dimension_coefficients_impl { template struct apply { typedef typename calculate_base_dimension_coefficients_func< begins_with_dimension::template apply< Dim >::value >::template apply< typename BaseUnitDimensions::item > result; typedef typename calculate_base_dimension_coefficients_impl::template apply< typename BaseUnitDimensions::next, Dim, list > next_; typedef typename next_::type type; typedef list next; }; }; template<> struct calculate_base_dimension_coefficients_impl<0> { template struct apply { typedef T type; typedef dimensionless_type next; }; }; // add_zeroes pushs N zeroes onto the // front of a list. // // list add_zeroes(list l, int N) { // if(N == 0) { // return(l); // } else { // return(push_front(add_zeroes(l, N-1), 0)); // } // } template struct add_zeroes_impl { // If you get an error here and your base units are // in fact linearly independent, please report it. BOOST_MPL_ASSERT_MSG((N > 0), base_units_are_probably_not_linearly_independent, (void)); template struct apply { typedef list< static_rational<0>, typename add_zeroes_impl::template apply::type > type; }; }; template<> struct add_zeroes_impl<0> { template struct apply { typedef T type; }; }; // expand_dimensions finds the exponents of // a set of dimensions in a dimension_list. // the second parameter is assumed to be // a superset of the base_dimensions of // the first parameter. // // list expand_dimensions(dimension_list, list); template struct expand_dimensions { template struct apply { typedef typename calculate_base_dimension_coefficients_func< begins_with_dimension::template apply::value >::template apply result; typedef list< typename result::type, typename expand_dimensions::template apply::type > type; }; }; template<> struct expand_dimensions<0> { template struct apply { typedef dimensionless_type type; }; }; template struct create_unit_matrix { template struct apply { typedef typename create_unit_matrix::template apply::type next; typedef list::template apply::type, next> type; }; }; template<> struct create_unit_matrix<0> { template struct apply { typedef dimensionless_type type; }; }; template struct normalize_units { typedef typename find_base_dimensions::type dimensions; typedef typename create_unit_matrix<(T::size::value)>::template apply< T, dimensions >::type matrix; typedef typename make_square_and_invert::type type; static const long extra = (type::size::value) - (T::size::value); }; // multiply_add_units computes M x V // where M is a matrix and V is a horizontal // vector // // list multiply_add_units(list >, list); template struct multiply_add_units_impl { template struct apply { typedef list< typename mpl::plus< typename mpl::times< typename Begin2::item, X >::type, typename Begin1::item >::type, typename multiply_add_units_impl::template apply< typename Begin1::next, typename Begin2::next, X >::type > type; }; }; template<> struct multiply_add_units_impl<0> { template struct apply { typedef dimensionless_type type; }; }; template struct multiply_add_units { template struct apply { typedef typename multiply_add_units_impl< (Begin2::item::size::value) >::template apply< typename multiply_add_units::template apply< typename Begin1::next, typename Begin2::next >::type, typename Begin2::item, typename Begin1::item >::type type; }; }; template<> struct multiply_add_units<1> { template struct apply { typedef typename add_zeroes_impl< (Begin2::item::size::value) >::template apply::type type1; typedef typename multiply_add_units_impl< (Begin2::item::size::value) >::template apply< type1, typename Begin2::item, typename Begin1::item >::type type; }; }; // strip_zeroes erases the first N elements of a list if // they are all zero, otherwise returns inconsistent // // list strip_zeroes(list l, int N) { // if(N == 0) { // return(l); // } else if(l.front == 0) { // return(strip_zeroes(pop_front(l), N-1)); // } else { // return(inconsistent); // } // } template struct strip_zeroes_impl; template struct strip_zeroes_func { template struct apply { typedef inconsistent type; }; }; template<> struct strip_zeroes_func > { template struct apply { typedef typename strip_zeroes_impl::template apply::type type; }; }; template struct strip_zeroes_impl { template struct apply { typedef typename strip_zeroes_func::template apply::type type; }; }; template<> struct strip_zeroes_impl<0> { template struct apply { typedef T type; }; }; // Given a list of base_units, computes the // exponents of each base unit for a given // dimension. // // list calculate_base_unit_exponents(list units, dimension_list dimensions); template struct is_base_dimension_unit { typedef mpl::false_ type; typedef void base_dimension_type; }; template struct is_base_dimension_unit >, dimensionless_type> > { typedef mpl::true_ type; typedef T base_dimension_type; }; template struct is_simple_system_impl { template struct apply { typedef is_base_dimension_unit test; typedef mpl::and_< typename test::type, mpl::less, typename is_simple_system_impl::template apply< typename Begin::next, typename test::base_dimension_type > > type; static const bool value = (type::value); }; }; template<> struct is_simple_system_impl<0> { template struct apply : mpl::true_ { }; }; template struct is_simple_system { typedef T Begin; typedef is_base_dimension_unit test; typedef typename mpl::and_< typename test::type, typename is_simple_system_impl< T::size::value - 1 >::template apply< typename Begin::next::type, typename test::base_dimension_type > >::type type; static const bool value = type::value; }; template struct calculate_base_unit_exponents_impl; template<> struct calculate_base_unit_exponents_impl { template struct apply { typedef typename expand_dimensions<(T::size::value)>::template apply< typename find_base_dimensions::type, Dimensions >::type type; }; }; template<> struct calculate_base_unit_exponents_impl { template struct apply { // find the units that correspond to each base dimension typedef normalize_units base_solutions; // pad the dimension with zeroes so it can just be a // list of numbers, making the multiplication easy // e.g. if the arguments are list and // list then this step will // yield list<0,1,-2> typedef typename expand_dimensions<(base_solutions::dimensions::size::value)>::template apply< typename base_solutions::dimensions, Dimensions >::type dimensions; // take the unit corresponding to each base unit // multiply each of its exponents by the exponent // of the base_dimension in the result and sum. typedef typename multiply_add_units::template apply< dimensions, typename base_solutions::type >::type units; // Now, verify that the dummy units really // cancel out and remove them. typedef typename strip_zeroes_impl::template apply::type type; }; }; template struct calculate_base_unit_exponents { typedef typename calculate_base_unit_exponents_impl::value>::template apply::type type; }; } // namespace detail } // namespace units } // namespace boost #endif