annotate DEPENDENCIES/generic/include/boost/units/detail/linear_algebra.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
Chris@16 2 // unit/quantity manipulation and conversion
Chris@16 3 //
Chris@16 4 // Copyright (C) 2003-2008 Matthias Christian Schabel
Chris@16 5 // Copyright (C) 2008 Steven Watanabe
Chris@16 6 //
Chris@16 7 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 8 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 9 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 10
Chris@16 11 #ifndef BOOST_UNITS_DETAIL_LINEAR_ALGEBRA_HPP
Chris@16 12 #define BOOST_UNITS_DETAIL_LINEAR_ALGEBRA_HPP
Chris@16 13
Chris@16 14 #include <boost/units/static_rational.hpp>
Chris@16 15 #include <boost/mpl/next.hpp>
Chris@16 16 #include <boost/mpl/arithmetic.hpp>
Chris@16 17 #include <boost/mpl/and.hpp>
Chris@16 18 #include <boost/mpl/assert.hpp>
Chris@16 19
Chris@16 20 #include <boost/units/dim.hpp>
Chris@16 21 #include <boost/units/dimensionless_type.hpp>
Chris@16 22 #include <boost/units/static_rational.hpp>
Chris@16 23 #include <boost/units/detail/dimension_list.hpp>
Chris@16 24 #include <boost/units/detail/sort.hpp>
Chris@16 25
Chris@16 26 namespace boost {
Chris@16 27
Chris@16 28 namespace units {
Chris@16 29
Chris@16 30 namespace detail {
Chris@16 31
Chris@16 32 // typedef list<rational> equation;
Chris@16 33
Chris@16 34 template<int N>
Chris@16 35 struct eliminate_from_pair_of_equations_impl;
Chris@16 36
Chris@16 37 template<class E1, class E2>
Chris@16 38 struct eliminate_from_pair_of_equations;
Chris@16 39
Chris@16 40 template<int N>
Chris@16 41 struct elimination_impl;
Chris@16 42
Chris@16 43 template<bool is_zero, bool element_is_last>
Chris@16 44 struct elimination_skip_leading_zeros_impl;
Chris@16 45
Chris@16 46 template<class Equation, class Vars>
Chris@16 47 struct substitute;
Chris@16 48
Chris@16 49 template<int N>
Chris@16 50 struct substitute_impl;
Chris@16 51
Chris@16 52 template<bool is_end>
Chris@16 53 struct solve_impl;
Chris@16 54
Chris@16 55 template<class T>
Chris@16 56 struct solve;
Chris@16 57
Chris@16 58 template<int N>
Chris@16 59 struct check_extra_equations_impl;
Chris@16 60
Chris@16 61 template<int N>
Chris@16 62 struct normalize_units_impl;
Chris@16 63
Chris@16 64 struct inconsistent {};
Chris@16 65
Chris@16 66 // generally useful utilies.
Chris@16 67
Chris@16 68 template<int N>
Chris@16 69 struct divide_equation {
Chris@16 70 template<class Begin, class Divisor>
Chris@16 71 struct apply {
Chris@16 72 typedef list<typename mpl::divides<typename Begin::item, Divisor>::type, typename divide_equation<N - 1>::template apply<typename Begin::next, Divisor>::type> type;
Chris@16 73 };
Chris@16 74 };
Chris@16 75
Chris@16 76 template<>
Chris@16 77 struct divide_equation<0> {
Chris@16 78 template<class Begin, class Divisor>
Chris@16 79 struct apply {
Chris@16 80 typedef dimensionless_type type;
Chris@16 81 };
Chris@16 82 };
Chris@16 83
Chris@16 84 // eliminate_from_pair_of_equations takes a pair of
Chris@16 85 // equations and eliminates the first variable.
Chris@16 86 //
Chris@16 87 // equation eliminate_from_pair_of_equations(equation l1, equation l2) {
Chris@16 88 // rational x1 = l1.front();
Chris@16 89 // rational x2 = l2.front();
Chris@16 90 // return(transform(pop_front(l1), pop_front(l2), _1 * x2 - _2 * x1));
Chris@16 91 // }
Chris@16 92
Chris@16 93 template<int N>
Chris@16 94 struct eliminate_from_pair_of_equations_impl {
Chris@16 95 template<class Begin1, class Begin2, class X1, class X2>
Chris@16 96 struct apply {
Chris@16 97 typedef list<
Chris@16 98 typename mpl::minus<
Chris@16 99 typename mpl::times<typename Begin1::item, X2>::type,
Chris@16 100 typename mpl::times<typename Begin2::item, X1>::type
Chris@16 101 >::type,
Chris@16 102 typename eliminate_from_pair_of_equations_impl<N - 1>::template apply<
Chris@16 103 typename Begin1::next,
Chris@16 104 typename Begin2::next,
Chris@16 105 X1,
Chris@16 106 X2
Chris@16 107 >::type
Chris@16 108 > type;
Chris@16 109 };
Chris@16 110 };
Chris@16 111
Chris@16 112 template<>
Chris@16 113 struct eliminate_from_pair_of_equations_impl<0> {
Chris@16 114 template<class Begin1, class Begin2, class X1, class X2>
Chris@16 115 struct apply {
Chris@16 116 typedef dimensionless_type type;
Chris@16 117 };
Chris@16 118 };
Chris@16 119
Chris@16 120 template<class E1, class E2>
Chris@16 121 struct eliminate_from_pair_of_equations {
Chris@16 122 typedef E1 begin1;
Chris@16 123 typedef E2 begin2;
Chris@16 124 typedef typename eliminate_from_pair_of_equations_impl<(E1::size::value - 1)>::template apply<
Chris@16 125 typename begin1::next,
Chris@16 126 typename begin2::next,
Chris@16 127 typename begin1::item,
Chris@16 128 typename begin2::item
Chris@16 129 >::type type;
Chris@16 130 };
Chris@16 131
Chris@16 132
Chris@16 133
Chris@16 134 // Stage 1. Determine which dimensions should
Chris@16 135 // have dummy base units. For this purpose
Chris@16 136 // row reduce the matrix.
Chris@16 137
Chris@16 138 template<int N>
Chris@16 139 struct make_zero_vector {
Chris@16 140 typedef list<static_rational<0>, typename make_zero_vector<N - 1>::type> type;
Chris@16 141 };
Chris@16 142 template<>
Chris@16 143 struct make_zero_vector<0> {
Chris@16 144 typedef dimensionless_type type;
Chris@16 145 };
Chris@16 146
Chris@16 147 template<int Column, int TotalColumns>
Chris@16 148 struct create_row_of_identity {
Chris@16 149 typedef list<static_rational<0>, typename create_row_of_identity<Column - 1, TotalColumns - 1>::type> type;
Chris@16 150 };
Chris@16 151 template<int TotalColumns>
Chris@16 152 struct create_row_of_identity<0, TotalColumns> {
Chris@16 153 typedef list<static_rational<1>, typename make_zero_vector<TotalColumns - 1>::type> type;
Chris@16 154 };
Chris@16 155 template<int Column>
Chris@16 156 struct create_row_of_identity<Column, 0> {
Chris@16 157 // error
Chris@16 158 };
Chris@16 159
Chris@16 160 template<int RemainingRows>
Chris@16 161 struct determine_extra_equations_impl;
Chris@16 162
Chris@16 163 template<bool first_is_zero, bool is_last>
Chris@16 164 struct determine_extra_equations_skip_zeros_impl;
Chris@16 165
Chris@16 166 // not the last row and not zero.
Chris@16 167 template<>
Chris@16 168 struct determine_extra_equations_skip_zeros_impl<false, false> {
Chris@16 169 template<class RowsBegin, int RemainingRows, int CurrentColumn, int TotalColumns, class Result>
Chris@16 170 struct apply {
Chris@16 171 // remove the equation being eliminated against from the set of equations.
Chris@16 172 typedef typename determine_extra_equations_impl<RemainingRows - 1>::template apply<typename RowsBegin::next, typename RowsBegin::item>::type next_equations;
Chris@16 173 // since this column was present, strip it out.
Chris@16 174 typedef Result type;
Chris@16 175 };
Chris@16 176 };
Chris@16 177
Chris@16 178 // the last row but not zero.
Chris@16 179 template<>
Chris@16 180 struct determine_extra_equations_skip_zeros_impl<false, true> {
Chris@16 181 template<class RowsBegin, int RemainingRows, int CurrentColumn, int TotalColumns, class Result>
Chris@16 182 struct apply {
Chris@16 183 // remove this equation.
Chris@16 184 typedef dimensionless_type next_equations;
Chris@16 185 // since this column was present, strip it out.
Chris@16 186 typedef Result type;
Chris@16 187 };
Chris@16 188 };
Chris@16 189
Chris@16 190
Chris@16 191 // the first columns is zero but it is not the last column.
Chris@16 192 // continue with the same loop.
Chris@16 193 template<>
Chris@16 194 struct determine_extra_equations_skip_zeros_impl<true, false> {
Chris@16 195 template<class RowsBegin, int RemainingRows, int CurrentColumn, int TotalColumns, class Result>
Chris@16 196 struct apply {
Chris@16 197 typedef typename RowsBegin::next::item next_row;
Chris@16 198 typedef typename determine_extra_equations_skip_zeros_impl<
Chris@16 199 next_row::item::Numerator == 0,
Chris@16 200 RemainingRows == 2 // the next one will be the last.
Chris@16 201 >::template apply<
Chris@16 202 typename RowsBegin::next,
Chris@16 203 RemainingRows - 1,
Chris@16 204 CurrentColumn,
Chris@16 205 TotalColumns,
Chris@16 206 Result
Chris@16 207 > next;
Chris@16 208 typedef list<typename RowsBegin::item::next, typename next::next_equations> next_equations;
Chris@16 209 typedef typename next::type type;
Chris@16 210 };
Chris@16 211 };
Chris@16 212
Chris@16 213 // all the elements in this column are zero.
Chris@16 214 template<>
Chris@16 215 struct determine_extra_equations_skip_zeros_impl<true, true> {
Chris@16 216 template<class RowsBegin, int RemainingRows, int CurrentColumn, int TotalColumns, class Result>
Chris@16 217 struct apply {
Chris@16 218 typedef list<typename RowsBegin::item::next, dimensionless_type> next_equations;
Chris@16 219 typedef list<typename create_row_of_identity<CurrentColumn, TotalColumns>::type, Result> type;
Chris@16 220 };
Chris@16 221 };
Chris@16 222
Chris@16 223 template<int RemainingRows>
Chris@16 224 struct determine_extra_equations_impl {
Chris@16 225 template<class RowsBegin, class EliminateAgainst>
Chris@16 226 struct apply {
Chris@16 227 typedef list<
Chris@16 228 typename eliminate_from_pair_of_equations<typename RowsBegin::item, EliminateAgainst>::type,
Chris@16 229 typename determine_extra_equations_impl<RemainingRows-1>::template apply<typename RowsBegin::next, EliminateAgainst>::type
Chris@16 230 > type;
Chris@16 231 };
Chris@16 232 };
Chris@16 233
Chris@16 234 template<>
Chris@16 235 struct determine_extra_equations_impl<0> {
Chris@16 236 template<class RowsBegin, class EliminateAgainst>
Chris@16 237 struct apply {
Chris@16 238 typedef dimensionless_type type;
Chris@16 239 };
Chris@16 240 };
Chris@16 241
Chris@16 242 template<int RemainingColumns, bool is_done>
Chris@16 243 struct determine_extra_equations {
Chris@16 244 template<class RowsBegin, int TotalColumns, class Result>
Chris@16 245 struct apply {
Chris@16 246 typedef typename RowsBegin::item top_row;
Chris@16 247 typedef typename determine_extra_equations_skip_zeros_impl<
Chris@16 248 top_row::item::Numerator == 0,
Chris@101 249 RowsBegin::size::value == 1
Chris@16 250 >::template apply<
Chris@16 251 RowsBegin,
Chris@16 252 RowsBegin::size::value,
Chris@16 253 TotalColumns - RemainingColumns,
Chris@16 254 TotalColumns,
Chris@16 255 Result
Chris@16 256 > column_info;
Chris@16 257 typedef typename determine_extra_equations<
Chris@16 258 RemainingColumns - 1,
Chris@16 259 column_info::next_equations::size::value == 0
Chris@16 260 >::template apply<
Chris@16 261 typename column_info::next_equations,
Chris@16 262 TotalColumns,
Chris@16 263 typename column_info::type
Chris@16 264 >::type type;
Chris@16 265 };
Chris@16 266 };
Chris@16 267
Chris@16 268 template<int RemainingColumns>
Chris@16 269 struct determine_extra_equations<RemainingColumns, true> {
Chris@16 270 template<class RowsBegin, int TotalColumns, class Result>
Chris@16 271 struct apply {
Chris@16 272 typedef typename determine_extra_equations<RemainingColumns - 1, true>::template apply<
Chris@16 273 RowsBegin,
Chris@16 274 TotalColumns,
Chris@16 275 list<typename create_row_of_identity<TotalColumns - RemainingColumns, TotalColumns>::type, Result>
Chris@16 276 >::type type;
Chris@16 277 };
Chris@16 278 };
Chris@16 279
Chris@16 280 template<>
Chris@16 281 struct determine_extra_equations<0, true> {
Chris@16 282 template<class RowsBegin, int TotalColumns, class Result>
Chris@16 283 struct apply {
Chris@16 284 typedef Result type;
Chris@16 285 };
Chris@16 286 };
Chris@16 287
Chris@16 288 // Stage 2
Chris@16 289 // invert the matrix using Gauss-Jordan elimination
Chris@16 290
Chris@16 291
Chris@16 292 template<bool is_zero, bool is_last>
Chris@16 293 struct invert_strip_leading_zeroes;
Chris@16 294
Chris@16 295 template<int N>
Chris@16 296 struct invert_handle_after_pivot_row;
Chris@16 297
Chris@16 298 // When processing column N, none of the first N rows
Chris@16 299 // can be the pivot column.
Chris@16 300 template<int N>
Chris@16 301 struct invert_handle_inital_rows {
Chris@16 302 template<class RowsBegin, class IdentityBegin>
Chris@16 303 struct apply {
Chris@16 304 typedef typename invert_handle_inital_rows<N - 1>::template apply<
Chris@16 305 typename RowsBegin::next,
Chris@16 306 typename IdentityBegin::next
Chris@16 307 > next;
Chris@16 308 typedef typename RowsBegin::item current_row;
Chris@16 309 typedef typename IdentityBegin::item current_identity_row;
Chris@16 310 typedef typename next::pivot_row pivot_row;
Chris@16 311 typedef typename next::identity_pivot_row identity_pivot_row;
Chris@16 312 typedef list<
Chris@16 313 typename eliminate_from_pair_of_equations_impl<(current_row::size::value) - 1>::template apply<
Chris@16 314 typename current_row::next,
Chris@16 315 pivot_row,
Chris@16 316 typename current_row::item,
Chris@16 317 static_rational<1>
Chris@16 318 >::type,
Chris@16 319 typename next::new_matrix
Chris@16 320 > new_matrix;
Chris@16 321 typedef list<
Chris@16 322 typename eliminate_from_pair_of_equations_impl<(current_identity_row::size::value)>::template apply<
Chris@16 323 current_identity_row,
Chris@16 324 identity_pivot_row,
Chris@16 325 typename current_row::item,
Chris@16 326 static_rational<1>
Chris@16 327 >::type,
Chris@16 328 typename next::identity_result
Chris@16 329 > identity_result;
Chris@16 330 };
Chris@16 331 };
Chris@16 332
Chris@16 333 // This handles the switch to searching for a pivot column.
Chris@16 334 // The pivot row will be propagated up in the typedefs
Chris@16 335 // pivot_row and identity_pivot_row. It is inserted here.
Chris@16 336 template<>
Chris@16 337 struct invert_handle_inital_rows<0> {
Chris@16 338 template<class RowsBegin, class IdentityBegin>
Chris@16 339 struct apply {
Chris@16 340 typedef typename RowsBegin::item current_row;
Chris@16 341 typedef typename invert_strip_leading_zeroes<
Chris@16 342 (current_row::item::Numerator == 0),
Chris@16 343 (RowsBegin::size::value == 1)
Chris@16 344 >::template apply<
Chris@16 345 RowsBegin,
Chris@16 346 IdentityBegin
Chris@16 347 > next;
Chris@16 348 // results
Chris@16 349 typedef list<typename next::pivot_row, typename next::new_matrix> new_matrix;
Chris@16 350 typedef list<typename next::identity_pivot_row, typename next::identity_result> identity_result;
Chris@16 351 typedef typename next::pivot_row pivot_row;
Chris@16 352 typedef typename next::identity_pivot_row identity_pivot_row;
Chris@16 353 };
Chris@16 354 };
Chris@16 355
Chris@16 356 // The first internal element which is not zero.
Chris@16 357 template<>
Chris@16 358 struct invert_strip_leading_zeroes<false, false> {
Chris@16 359 template<class RowsBegin, class IdentityBegin>
Chris@16 360 struct apply {
Chris@16 361 typedef typename RowsBegin::item current_row;
Chris@16 362 typedef typename current_row::item current_value;
Chris@16 363 typedef typename divide_equation<(current_row::size::value - 1)>::template apply<typename current_row::next, current_value>::type new_equation;
Chris@16 364 typedef typename divide_equation<(IdentityBegin::item::size::value)>::template apply<typename IdentityBegin::item, current_value>::type transformed_identity_equation;
Chris@16 365 typedef typename invert_handle_after_pivot_row<(RowsBegin::size::value - 1)>::template apply<
Chris@16 366 typename RowsBegin::next,
Chris@16 367 typename IdentityBegin::next,
Chris@16 368 new_equation,
Chris@16 369 transformed_identity_equation
Chris@16 370 > next;
Chris@16 371
Chris@16 372 // results
Chris@16 373 // Note that we don't add the pivot row to the
Chris@16 374 // results here, because it needs to propagated up
Chris@16 375 // to the diagonal.
Chris@16 376 typedef typename next::new_matrix new_matrix;
Chris@16 377 typedef typename next::identity_result identity_result;
Chris@16 378 typedef new_equation pivot_row;
Chris@16 379 typedef transformed_identity_equation identity_pivot_row;
Chris@16 380 };
Chris@16 381 };
Chris@16 382
Chris@16 383 // The one and only non-zero element--at the end
Chris@16 384 template<>
Chris@16 385 struct invert_strip_leading_zeroes<false, true> {
Chris@16 386 template<class RowsBegin, class IdentityBegin>
Chris@16 387 struct apply {
Chris@16 388 typedef typename RowsBegin::item current_row;
Chris@16 389 typedef typename current_row::item current_value;
Chris@16 390 typedef typename divide_equation<(current_row::size::value - 1)>::template apply<typename current_row::next, current_value>::type new_equation;
Chris@16 391 typedef typename divide_equation<(IdentityBegin::item::size::value)>::template apply<typename IdentityBegin::item, current_value>::type transformed_identity_equation;
Chris@16 392
Chris@16 393 // results
Chris@16 394 // Note that we don't add the pivot row to the
Chris@16 395 // results here, because it needs to propagated up
Chris@16 396 // to the diagonal.
Chris@16 397 typedef dimensionless_type identity_result;
Chris@16 398 typedef dimensionless_type new_matrix;
Chris@16 399 typedef new_equation pivot_row;
Chris@16 400 typedef transformed_identity_equation identity_pivot_row;
Chris@16 401 };
Chris@16 402 };
Chris@16 403
Chris@16 404 // One of the initial zeroes
Chris@16 405 template<>
Chris@16 406 struct invert_strip_leading_zeroes<true, false> {
Chris@16 407 template<class RowsBegin, class IdentityBegin>
Chris@16 408 struct apply {
Chris@16 409 typedef typename RowsBegin::item current_row;
Chris@16 410 typedef typename RowsBegin::next::item next_row;
Chris@16 411 typedef typename invert_strip_leading_zeroes<
Chris@16 412 next_row::item::Numerator == 0,
Chris@16 413 RowsBegin::size::value == 2
Chris@16 414 >::template apply<
Chris@16 415 typename RowsBegin::next,
Chris@16 416 typename IdentityBegin::next
Chris@16 417 > next;
Chris@16 418 typedef typename IdentityBegin::item current_identity_row;
Chris@16 419 // these are propagated up.
Chris@16 420 typedef typename next::pivot_row pivot_row;
Chris@16 421 typedef typename next::identity_pivot_row identity_pivot_row;
Chris@16 422 typedef list<
Chris@16 423 typename eliminate_from_pair_of_equations_impl<(current_row::size::value - 1)>::template apply<
Chris@16 424 typename current_row::next,
Chris@16 425 pivot_row,
Chris@16 426 typename current_row::item,
Chris@16 427 static_rational<1>
Chris@16 428 >::type,
Chris@16 429 typename next::new_matrix
Chris@16 430 > new_matrix;
Chris@16 431 typedef list<
Chris@16 432 typename eliminate_from_pair_of_equations_impl<(current_identity_row::size::value)>::template apply<
Chris@16 433 current_identity_row,
Chris@16 434 identity_pivot_row,
Chris@16 435 typename current_row::item,
Chris@16 436 static_rational<1>
Chris@16 437 >::type,
Chris@16 438 typename next::identity_result
Chris@16 439 > identity_result;
Chris@16 440 };
Chris@16 441 };
Chris@16 442
Chris@16 443 // the last element, and is zero.
Chris@16 444 // Should never happen.
Chris@16 445 template<>
Chris@16 446 struct invert_strip_leading_zeroes<true, true> {
Chris@16 447 };
Chris@16 448
Chris@16 449 template<int N>
Chris@16 450 struct invert_handle_after_pivot_row {
Chris@16 451 template<class RowsBegin, class IdentityBegin, class MatrixPivot, class IdentityPivot>
Chris@16 452 struct apply {
Chris@16 453 typedef typename invert_handle_after_pivot_row<N - 1>::template apply<
Chris@16 454 typename RowsBegin::next,
Chris@16 455 typename IdentityBegin::next,
Chris@16 456 MatrixPivot,
Chris@16 457 IdentityPivot
Chris@16 458 > next;
Chris@16 459 typedef typename RowsBegin::item current_row;
Chris@16 460 typedef typename IdentityBegin::item current_identity_row;
Chris@16 461 typedef MatrixPivot pivot_row;
Chris@16 462 typedef IdentityPivot identity_pivot_row;
Chris@16 463
Chris@16 464 // results
Chris@16 465 typedef list<
Chris@16 466 typename eliminate_from_pair_of_equations_impl<(current_row::size::value - 1)>::template apply<
Chris@16 467 typename current_row::next,
Chris@16 468 pivot_row,
Chris@16 469 typename current_row::item,
Chris@16 470 static_rational<1>
Chris@16 471 >::type,
Chris@16 472 typename next::new_matrix
Chris@16 473 > new_matrix;
Chris@16 474 typedef list<
Chris@16 475 typename eliminate_from_pair_of_equations_impl<(current_identity_row::size::value)>::template apply<
Chris@16 476 current_identity_row,
Chris@16 477 identity_pivot_row,
Chris@16 478 typename current_row::item,
Chris@16 479 static_rational<1>
Chris@16 480 >::type,
Chris@16 481 typename next::identity_result
Chris@16 482 > identity_result;
Chris@16 483 };
Chris@16 484 };
Chris@16 485
Chris@16 486 template<>
Chris@16 487 struct invert_handle_after_pivot_row<0> {
Chris@16 488 template<class RowsBegin, class IdentityBegin, class MatrixPivot, class IdentityPivot>
Chris@16 489 struct apply {
Chris@16 490 typedef dimensionless_type new_matrix;
Chris@16 491 typedef dimensionless_type identity_result;
Chris@16 492 };
Chris@16 493 };
Chris@16 494
Chris@16 495 template<int N>
Chris@16 496 struct invert_impl {
Chris@16 497 template<class RowsBegin, class IdentityBegin>
Chris@16 498 struct apply {
Chris@16 499 typedef typename invert_handle_inital_rows<RowsBegin::size::value - N>::template apply<RowsBegin, IdentityBegin> process_column;
Chris@16 500 typedef typename invert_impl<N - 1>::template apply<
Chris@16 501 typename process_column::new_matrix,
Chris@16 502 typename process_column::identity_result
Chris@16 503 >::type type;
Chris@16 504 };
Chris@16 505 };
Chris@16 506
Chris@16 507 template<>
Chris@16 508 struct invert_impl<0> {
Chris@16 509 template<class RowsBegin, class IdentityBegin>
Chris@16 510 struct apply {
Chris@16 511 typedef IdentityBegin type;
Chris@16 512 };
Chris@16 513 };
Chris@16 514
Chris@16 515 template<int N>
Chris@16 516 struct make_identity {
Chris@16 517 template<int Size>
Chris@16 518 struct apply {
Chris@16 519 typedef list<typename create_row_of_identity<Size - N, Size>::type, typename make_identity<N - 1>::template apply<Size>::type> type;
Chris@16 520 };
Chris@16 521 };
Chris@16 522
Chris@16 523 template<>
Chris@16 524 struct make_identity<0> {
Chris@16 525 template<int Size>
Chris@16 526 struct apply {
Chris@16 527 typedef dimensionless_type type;
Chris@16 528 };
Chris@16 529 };
Chris@16 530
Chris@16 531 template<class Matrix>
Chris@16 532 struct make_square_and_invert {
Chris@16 533 typedef typename Matrix::item top_row;
Chris@16 534 typedef typename determine_extra_equations<(top_row::size::value), false>::template apply<
Chris@16 535 Matrix, // RowsBegin
Chris@16 536 top_row::size::value, // TotalColumns
Chris@16 537 Matrix // Result
Chris@16 538 >::type invertible;
Chris@16 539 typedef typename invert_impl<invertible::size::value>::template apply<
Chris@16 540 invertible,
Chris@16 541 typename make_identity<invertible::size::value>::template apply<invertible::size::value>::type
Chris@16 542 >::type type;
Chris@16 543 };
Chris@16 544
Chris@16 545
Chris@16 546 // find_base_dimensions takes a list of
Chris@16 547 // base_units and returns a sorted list
Chris@16 548 // of all the base_dimensions they use.
Chris@16 549 //
Chris@16 550 // list<base_dimension> find_base_dimensions(list<base_unit> l) {
Chris@16 551 // set<base_dimension> dimensions;
Chris@16 552 // for_each(base_unit unit : l) {
Chris@16 553 // for_each(dim d : unit.dimension_type) {
Chris@16 554 // dimensions = insert(dimensions, d.tag_type);
Chris@16 555 // }
Chris@16 556 // }
Chris@16 557 // return(sort(dimensions, _1 > _2, front_inserter(list<base_dimension>())));
Chris@16 558 // }
Chris@16 559
Chris@16 560 typedef char set_no;
Chris@16 561 struct set_yes { set_no dummy[2]; };
Chris@16 562
Chris@16 563 template<class T>
Chris@16 564 struct wrap {};
Chris@16 565
Chris@16 566 struct set_end {
Chris@16 567 static set_no lookup(...);
Chris@16 568 typedef mpl::long_<0> size;
Chris@16 569 };
Chris@16 570
Chris@16 571 template<class T, class Next>
Chris@16 572 struct set : Next {
Chris@16 573 using Next::lookup;
Chris@16 574 static set_yes lookup(wrap<T>*);
Chris@16 575 typedef T item;
Chris@16 576 typedef Next next;
Chris@16 577 typedef typename mpl::next<typename Next::size>::type size;
Chris@16 578 };
Chris@16 579
Chris@16 580 template<bool has_key>
Chris@16 581 struct set_insert;
Chris@16 582
Chris@16 583 template<>
Chris@16 584 struct set_insert<true> {
Chris@16 585 template<class Set, class T>
Chris@16 586 struct apply {
Chris@16 587 typedef Set type;
Chris@16 588 };
Chris@16 589 };
Chris@16 590
Chris@16 591 template<>
Chris@16 592 struct set_insert<false> {
Chris@16 593 template<class Set, class T>
Chris@16 594 struct apply {
Chris@16 595 typedef set<T, Set> type;
Chris@16 596 };
Chris@16 597 };
Chris@16 598
Chris@16 599 template<class Set, class T>
Chris@16 600 struct has_key {
Chris@16 601 static const long size = sizeof(Set::lookup((wrap<T>*)0));
Chris@16 602 static const bool value = (size == sizeof(set_yes));
Chris@16 603 };
Chris@16 604
Chris@16 605 template<int N>
Chris@16 606 struct find_base_dimensions_impl_impl {
Chris@16 607 template<class Begin, class S>
Chris@16 608 struct apply {
Chris@16 609 typedef typename find_base_dimensions_impl_impl<N-1>::template apply<
Chris@16 610 typename Begin::next,
Chris@16 611 S
Chris@16 612 >::type next;
Chris@16 613
Chris@16 614 typedef typename set_insert<
Chris@16 615 (has_key<next, typename Begin::item::tag_type>::value)
Chris@16 616 >::template apply<
Chris@16 617 next,
Chris@16 618 typename Begin::item::tag_type
Chris@16 619 >::type type;
Chris@16 620 };
Chris@16 621 };
Chris@16 622
Chris@16 623 template<>
Chris@16 624 struct find_base_dimensions_impl_impl<0> {
Chris@16 625 template<class Begin, class S>
Chris@16 626 struct apply {
Chris@16 627 typedef S type;
Chris@16 628 };
Chris@16 629 };
Chris@16 630
Chris@16 631 template<int N>
Chris@16 632 struct find_base_dimensions_impl {
Chris@16 633 template<class Begin>
Chris@16 634 struct apply {
Chris@16 635 typedef typename find_base_dimensions_impl_impl<(Begin::item::dimension_type::size::value)>::template apply<
Chris@16 636 typename Begin::item::dimension_type,
Chris@16 637 typename find_base_dimensions_impl<N-1>::template apply<typename Begin::next>::type
Chris@16 638 >::type type;
Chris@16 639 };
Chris@16 640 };
Chris@16 641
Chris@16 642 template<>
Chris@16 643 struct find_base_dimensions_impl<0> {
Chris@16 644 template<class Begin>
Chris@16 645 struct apply {
Chris@16 646 typedef set_end type;
Chris@16 647 };
Chris@16 648 };
Chris@16 649
Chris@16 650 template<class T>
Chris@16 651 struct find_base_dimensions {
Chris@16 652 typedef typename insertion_sort<
Chris@16 653 typename find_base_dimensions_impl<
Chris@16 654 (T::size::value)
Chris@16 655 >::template apply<T>::type
Chris@16 656 >::type type;
Chris@16 657 };
Chris@16 658
Chris@16 659 // calculate_base_dimension_coefficients finds
Chris@16 660 // the coefficients corresponding to the first
Chris@16 661 // base_dimension in each of the dimension_lists.
Chris@16 662 // It returns two values. The first result
Chris@16 663 // is a list of the coefficients. The second
Chris@16 664 // is a list with all the incremented iterators.
Chris@16 665 // When we encounter a base_dimension that is
Chris@16 666 // missing from a dimension_list, we do not
Chris@16 667 // increment the iterator and we set the
Chris@16 668 // coefficient to zero.
Chris@16 669
Chris@16 670 template<bool has_dimension>
Chris@16 671 struct calculate_base_dimension_coefficients_func;
Chris@16 672
Chris@16 673 template<>
Chris@16 674 struct calculate_base_dimension_coefficients_func<true> {
Chris@16 675 template<class T>
Chris@16 676 struct apply {
Chris@16 677 typedef typename T::item::value_type type;
Chris@16 678 typedef typename T::next next;
Chris@16 679 };
Chris@16 680 };
Chris@16 681
Chris@16 682 template<>
Chris@16 683 struct calculate_base_dimension_coefficients_func<false> {
Chris@16 684 template<class T>
Chris@16 685 struct apply {
Chris@16 686 typedef static_rational<0> type;
Chris@16 687 typedef T next;
Chris@16 688 };
Chris@16 689 };
Chris@16 690
Chris@16 691 // begins_with_dimension returns true iff its first
Chris@16 692 // parameter is a valid iterator which yields its
Chris@16 693 // second parameter when dereferenced.
Chris@16 694
Chris@16 695 template<class Iterator>
Chris@16 696 struct begins_with_dimension {
Chris@16 697 template<class Dim>
Chris@16 698 struct apply :
Chris@16 699 boost::is_same<
Chris@16 700 Dim,
Chris@16 701 typename Iterator::item::tag_type
Chris@16 702 > {};
Chris@16 703 };
Chris@16 704
Chris@16 705 template<>
Chris@16 706 struct begins_with_dimension<dimensionless_type> {
Chris@16 707 template<class Dim>
Chris@16 708 struct apply : mpl::false_ {};
Chris@16 709 };
Chris@16 710
Chris@16 711 template<int N>
Chris@16 712 struct calculate_base_dimension_coefficients_impl {
Chris@16 713 template<class BaseUnitDimensions,class Dim,class T>
Chris@16 714 struct apply {
Chris@16 715 typedef typename calculate_base_dimension_coefficients_func<
Chris@16 716 begins_with_dimension<typename BaseUnitDimensions::item>::template apply<
Chris@16 717 Dim
Chris@16 718 >::value
Chris@16 719 >::template apply<
Chris@16 720 typename BaseUnitDimensions::item
Chris@16 721 > result;
Chris@16 722 typedef typename calculate_base_dimension_coefficients_impl<N-1>::template apply<
Chris@16 723 typename BaseUnitDimensions::next,
Chris@16 724 Dim,
Chris@16 725 list<typename result::type, T>
Chris@16 726 > next_;
Chris@16 727 typedef typename next_::type type;
Chris@16 728 typedef list<typename result::next, typename next_::next> next;
Chris@16 729 };
Chris@16 730 };
Chris@16 731
Chris@16 732 template<>
Chris@16 733 struct calculate_base_dimension_coefficients_impl<0> {
Chris@16 734 template<class Begin, class BaseUnitDimensions, class T>
Chris@16 735 struct apply {
Chris@16 736 typedef T type;
Chris@16 737 typedef dimensionless_type next;
Chris@16 738 };
Chris@16 739 };
Chris@16 740
Chris@16 741 // add_zeroes pushs N zeroes onto the
Chris@16 742 // front of a list.
Chris@16 743 //
Chris@16 744 // list<rational> add_zeroes(list<rational> l, int N) {
Chris@16 745 // if(N == 0) {
Chris@16 746 // return(l);
Chris@16 747 // } else {
Chris@16 748 // return(push_front(add_zeroes(l, N-1), 0));
Chris@16 749 // }
Chris@16 750 // }
Chris@16 751
Chris@16 752 template<int N>
Chris@16 753 struct add_zeroes_impl {
Chris@16 754 // If you get an error here and your base units are
Chris@16 755 // in fact linearly independent, please report it.
Chris@16 756 BOOST_MPL_ASSERT_MSG((N > 0), base_units_are_probably_not_linearly_independent, (void));
Chris@16 757 template<class T>
Chris@16 758 struct apply {
Chris@16 759 typedef list<
Chris@16 760 static_rational<0>,
Chris@16 761 typename add_zeroes_impl<N-1>::template apply<T>::type
Chris@16 762 > type;
Chris@16 763 };
Chris@16 764 };
Chris@16 765
Chris@16 766 template<>
Chris@16 767 struct add_zeroes_impl<0> {
Chris@16 768 template<class T>
Chris@16 769 struct apply {
Chris@16 770 typedef T type;
Chris@16 771 };
Chris@16 772 };
Chris@16 773
Chris@16 774 // expand_dimensions finds the exponents of
Chris@16 775 // a set of dimensions in a dimension_list.
Chris@16 776 // the second parameter is assumed to be
Chris@16 777 // a superset of the base_dimensions of
Chris@16 778 // the first parameter.
Chris@16 779 //
Chris@16 780 // list<rational> expand_dimensions(dimension_list, list<base_dimension>);
Chris@16 781
Chris@16 782 template<int N>
Chris@16 783 struct expand_dimensions {
Chris@16 784 template<class Begin, class DimensionIterator>
Chris@16 785 struct apply {
Chris@16 786 typedef typename calculate_base_dimension_coefficients_func<
Chris@16 787 begins_with_dimension<DimensionIterator>::template apply<typename Begin::item>::value
Chris@16 788 >::template apply<DimensionIterator> result;
Chris@16 789 typedef list<
Chris@16 790 typename result::type,
Chris@16 791 typename expand_dimensions<N-1>::template apply<typename Begin::next, typename result::next>::type
Chris@16 792 > type;
Chris@16 793 };
Chris@16 794 };
Chris@16 795
Chris@16 796 template<>
Chris@16 797 struct expand_dimensions<0> {
Chris@16 798 template<class Begin, class DimensionIterator>
Chris@16 799 struct apply {
Chris@16 800 typedef dimensionless_type type;
Chris@16 801 };
Chris@16 802 };
Chris@16 803
Chris@16 804 template<int N>
Chris@16 805 struct create_unit_matrix {
Chris@16 806 template<class Begin, class Dimensions>
Chris@16 807 struct apply {
Chris@16 808 typedef typename create_unit_matrix<N - 1>::template apply<typename Begin::next, Dimensions>::type next;
Chris@16 809 typedef list<typename expand_dimensions<Dimensions::size::value>::template apply<Dimensions, typename Begin::item::dimension_type>::type, next> type;
Chris@16 810 };
Chris@16 811 };
Chris@16 812
Chris@16 813 template<>
Chris@16 814 struct create_unit_matrix<0> {
Chris@16 815 template<class Begin, class Dimensions>
Chris@16 816 struct apply {
Chris@16 817 typedef dimensionless_type type;
Chris@16 818 };
Chris@16 819 };
Chris@16 820
Chris@16 821 template<class T>
Chris@16 822 struct normalize_units {
Chris@16 823 typedef typename find_base_dimensions<T>::type dimensions;
Chris@16 824 typedef typename create_unit_matrix<(T::size::value)>::template apply<
Chris@16 825 T,
Chris@16 826 dimensions
Chris@16 827 >::type matrix;
Chris@16 828 typedef typename make_square_and_invert<matrix>::type type;
Chris@16 829 static const long extra = (type::size::value) - (T::size::value);
Chris@16 830 };
Chris@16 831
Chris@16 832 // multiply_add_units computes M x V
Chris@16 833 // where M is a matrix and V is a horizontal
Chris@16 834 // vector
Chris@16 835 //
Chris@16 836 // list<rational> multiply_add_units(list<list<rational> >, list<rational>);
Chris@16 837
Chris@16 838 template<int N>
Chris@16 839 struct multiply_add_units_impl {
Chris@16 840 template<class Begin1, class Begin2 ,class X>
Chris@16 841 struct apply {
Chris@16 842 typedef list<
Chris@16 843 typename mpl::plus<
Chris@16 844 typename mpl::times<
Chris@16 845 typename Begin2::item,
Chris@16 846 X
Chris@16 847 >::type,
Chris@16 848 typename Begin1::item
Chris@16 849 >::type,
Chris@16 850 typename multiply_add_units_impl<N-1>::template apply<
Chris@16 851 typename Begin1::next,
Chris@16 852 typename Begin2::next,
Chris@16 853 X
Chris@16 854 >::type
Chris@16 855 > type;
Chris@16 856 };
Chris@16 857 };
Chris@16 858
Chris@16 859 template<>
Chris@16 860 struct multiply_add_units_impl<0> {
Chris@16 861 template<class Begin1, class Begin2 ,class X>
Chris@16 862 struct apply {
Chris@16 863 typedef dimensionless_type type;
Chris@16 864 };
Chris@16 865 };
Chris@16 866
Chris@16 867 template<int N>
Chris@16 868 struct multiply_add_units {
Chris@16 869 template<class Begin1, class Begin2>
Chris@16 870 struct apply {
Chris@16 871 typedef typename multiply_add_units_impl<
Chris@16 872 (Begin2::item::size::value)
Chris@16 873 >::template apply<
Chris@16 874 typename multiply_add_units<N-1>::template apply<
Chris@16 875 typename Begin1::next,
Chris@16 876 typename Begin2::next
Chris@16 877 >::type,
Chris@16 878 typename Begin2::item,
Chris@16 879 typename Begin1::item
Chris@16 880 >::type type;
Chris@16 881 };
Chris@16 882 };
Chris@16 883
Chris@16 884 template<>
Chris@16 885 struct multiply_add_units<1> {
Chris@16 886 template<class Begin1, class Begin2>
Chris@16 887 struct apply {
Chris@16 888 typedef typename add_zeroes_impl<
Chris@16 889 (Begin2::item::size::value)
Chris@16 890 >::template apply<dimensionless_type>::type type1;
Chris@16 891 typedef typename multiply_add_units_impl<
Chris@16 892 (Begin2::item::size::value)
Chris@16 893 >::template apply<
Chris@16 894 type1,
Chris@16 895 typename Begin2::item,
Chris@16 896 typename Begin1::item
Chris@16 897 >::type type;
Chris@16 898 };
Chris@16 899 };
Chris@16 900
Chris@16 901
Chris@16 902 // strip_zeroes erases the first N elements of a list if
Chris@16 903 // they are all zero, otherwise returns inconsistent
Chris@16 904 //
Chris@16 905 // list strip_zeroes(list l, int N) {
Chris@16 906 // if(N == 0) {
Chris@16 907 // return(l);
Chris@16 908 // } else if(l.front == 0) {
Chris@16 909 // return(strip_zeroes(pop_front(l), N-1));
Chris@16 910 // } else {
Chris@16 911 // return(inconsistent);
Chris@16 912 // }
Chris@16 913 // }
Chris@16 914
Chris@16 915 template<int N>
Chris@16 916 struct strip_zeroes_impl;
Chris@16 917
Chris@16 918 template<class T>
Chris@16 919 struct strip_zeroes_func {
Chris@16 920 template<class L, int N>
Chris@16 921 struct apply {
Chris@16 922 typedef inconsistent type;
Chris@16 923 };
Chris@16 924 };
Chris@16 925
Chris@16 926 template<>
Chris@16 927 struct strip_zeroes_func<static_rational<0> > {
Chris@16 928 template<class L, int N>
Chris@16 929 struct apply {
Chris@16 930 typedef typename strip_zeroes_impl<N-1>::template apply<typename L::next>::type type;
Chris@16 931 };
Chris@16 932 };
Chris@16 933
Chris@16 934 template<int N>
Chris@16 935 struct strip_zeroes_impl {
Chris@16 936 template<class T>
Chris@16 937 struct apply {
Chris@16 938 typedef typename strip_zeroes_func<typename T::item>::template apply<T, N>::type type;
Chris@16 939 };
Chris@16 940 };
Chris@16 941
Chris@16 942 template<>
Chris@16 943 struct strip_zeroes_impl<0> {
Chris@16 944 template<class T>
Chris@16 945 struct apply {
Chris@16 946 typedef T type;
Chris@16 947 };
Chris@16 948 };
Chris@16 949
Chris@16 950 // Given a list of base_units, computes the
Chris@16 951 // exponents of each base unit for a given
Chris@16 952 // dimension.
Chris@16 953 //
Chris@16 954 // list<rational> calculate_base_unit_exponents(list<base_unit> units, dimension_list dimensions);
Chris@16 955
Chris@16 956 template<class T>
Chris@16 957 struct is_base_dimension_unit {
Chris@16 958 typedef mpl::false_ type;
Chris@16 959 typedef void base_dimension_type;
Chris@16 960 };
Chris@16 961 template<class T>
Chris@16 962 struct is_base_dimension_unit<list<dim<T, static_rational<1> >, dimensionless_type> > {
Chris@16 963 typedef mpl::true_ type;
Chris@16 964 typedef T base_dimension_type;
Chris@16 965 };
Chris@16 966
Chris@16 967 template<int N>
Chris@16 968 struct is_simple_system_impl {
Chris@16 969 template<class Begin, class Prev>
Chris@16 970 struct apply {
Chris@16 971 typedef is_base_dimension_unit<typename Begin::item::dimension_type> test;
Chris@16 972 typedef mpl::and_<
Chris@16 973 typename test::type,
Chris@16 974 mpl::less<Prev, typename test::base_dimension_type>,
Chris@16 975 typename is_simple_system_impl<N-1>::template apply<
Chris@16 976 typename Begin::next,
Chris@16 977 typename test::base_dimension_type
Chris@16 978 >
Chris@16 979 > type;
Chris@16 980 static const bool value = (type::value);
Chris@16 981 };
Chris@16 982 };
Chris@16 983
Chris@16 984 template<>
Chris@16 985 struct is_simple_system_impl<0> {
Chris@16 986 template<class Begin, class Prev>
Chris@16 987 struct apply : mpl::true_ {
Chris@16 988 };
Chris@16 989 };
Chris@16 990
Chris@16 991 template<class T>
Chris@16 992 struct is_simple_system {
Chris@16 993 typedef T Begin;
Chris@16 994 typedef is_base_dimension_unit<typename Begin::item::dimension_type> test;
Chris@16 995 typedef typename mpl::and_<
Chris@16 996 typename test::type,
Chris@16 997 typename is_simple_system_impl<
Chris@16 998 T::size::value - 1
Chris@16 999 >::template apply<
Chris@16 1000 typename Begin::next::type,
Chris@16 1001 typename test::base_dimension_type
Chris@16 1002 >
Chris@16 1003 >::type type;
Chris@16 1004 static const bool value = type::value;
Chris@16 1005 };
Chris@16 1006
Chris@16 1007 template<bool>
Chris@16 1008 struct calculate_base_unit_exponents_impl;
Chris@16 1009
Chris@16 1010 template<>
Chris@16 1011 struct calculate_base_unit_exponents_impl<true> {
Chris@16 1012 template<class T, class Dimensions>
Chris@16 1013 struct apply {
Chris@16 1014 typedef typename expand_dimensions<(T::size::value)>::template apply<
Chris@16 1015 typename find_base_dimensions<T>::type,
Chris@16 1016 Dimensions
Chris@16 1017 >::type type;
Chris@16 1018 };
Chris@16 1019 };
Chris@16 1020
Chris@16 1021 template<>
Chris@16 1022 struct calculate_base_unit_exponents_impl<false> {
Chris@16 1023 template<class T, class Dimensions>
Chris@16 1024 struct apply {
Chris@16 1025 // find the units that correspond to each base dimension
Chris@16 1026 typedef normalize_units<T> base_solutions;
Chris@16 1027 // pad the dimension with zeroes so it can just be a
Chris@16 1028 // list of numbers, making the multiplication easy
Chris@16 1029 // e.g. if the arguments are list<pound, foot> and
Chris@16 1030 // list<mass,time^-2> then this step will
Chris@16 1031 // yield list<0,1,-2>
Chris@16 1032 typedef typename expand_dimensions<(base_solutions::dimensions::size::value)>::template apply<
Chris@16 1033 typename base_solutions::dimensions,
Chris@16 1034 Dimensions
Chris@16 1035 >::type dimensions;
Chris@16 1036 // take the unit corresponding to each base unit
Chris@16 1037 // multiply each of its exponents by the exponent
Chris@16 1038 // of the base_dimension in the result and sum.
Chris@16 1039 typedef typename multiply_add_units<dimensions::size::value>::template apply<
Chris@16 1040 dimensions,
Chris@16 1041 typename base_solutions::type
Chris@16 1042 >::type units;
Chris@16 1043 // Now, verify that the dummy units really
Chris@16 1044 // cancel out and remove them.
Chris@16 1045 typedef typename strip_zeroes_impl<base_solutions::extra>::template apply<units>::type type;
Chris@16 1046 };
Chris@16 1047 };
Chris@16 1048
Chris@16 1049 template<class T, class Dimensions>
Chris@16 1050 struct calculate_base_unit_exponents {
Chris@16 1051 typedef typename calculate_base_unit_exponents_impl<is_simple_system<T>::value>::template apply<T, Dimensions>::type type;
Chris@16 1052 };
Chris@16 1053
Chris@16 1054 } // namespace detail
Chris@16 1055
Chris@16 1056 } // namespace units
Chris@16 1057
Chris@16 1058 } // namespace boost
Chris@16 1059
Chris@16 1060 #endif