row.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file COPYING.txt, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. #include "maptype.h"
  10. #include "symbol.h"
  11. #include "util.h"
  12. namespace kiwi
  13. {
  14. namespace impl
  15. {
  16. class Row
  17. {
  18. public:
  19. typedef MapType<Symbol, double>::Type CellMap;
  20. Row() : m_constant( 0.0 ) {}
  21. Row( double constant ) : m_constant( constant ) {}
  22. Row( const Row& other ) :
  23. m_cells( other.m_cells ), m_constant( other.m_constant ) {}
  24. ~Row() {}
  25. const CellMap& cells() const
  26. {
  27. return m_cells;
  28. }
  29. double constant() const
  30. {
  31. return m_constant;
  32. }
  33. /* Add a constant value to the row constant.
  34. The new value of the constant is returned.
  35. */
  36. double add( double value )
  37. {
  38. return m_constant += value;
  39. }
  40. /* Insert a symbol into the row with a given coefficient.
  41. If the symbol already exists in the row, the coefficient will be
  42. added to the existing coefficient. If the resulting coefficient
  43. is zero, the symbol will be removed from the row.
  44. */
  45. void insert( const Symbol& symbol, double coefficient = 1.0 )
  46. {
  47. if( nearZero( m_cells[ symbol ] += coefficient ) )
  48. m_cells.erase( symbol );
  49. }
  50. /* Insert a row into this row with a given coefficient.
  51. The constant and the cells of the other row will be multiplied by
  52. the coefficient and added to this row. Any cell with a resulting
  53. coefficient of zero will be removed from the row.
  54. */
  55. void insert( const Row& other, double coefficient = 1.0 )
  56. {
  57. typedef CellMap::const_iterator iter_t;
  58. m_constant += other.m_constant * coefficient;
  59. iter_t end = other.m_cells.end();
  60. for( iter_t it = other.m_cells.begin(); it != end; ++it )
  61. {
  62. double coeff = it->second * coefficient;
  63. if( nearZero( m_cells[ it->first ] += coeff ) )
  64. m_cells.erase( it->first );
  65. }
  66. }
  67. /* Remove the given symbol from the row.
  68. */
  69. void remove( const Symbol& symbol )
  70. {
  71. CellMap::iterator it = m_cells.find( symbol );
  72. if( it != m_cells.end() )
  73. m_cells.erase( it );
  74. }
  75. /* Reverse the sign of the constant and all cells in the row.
  76. */
  77. void reverseSign()
  78. {
  79. typedef CellMap::iterator iter_t;
  80. m_constant = -m_constant;
  81. iter_t end = m_cells.end();
  82. for( iter_t it = m_cells.begin(); it != end; ++it )
  83. it->second = -it->second;
  84. }
  85. /* Solve the row for the given symbol.
  86. This method assumes the row is of the form a * x + b * y + c = 0
  87. and (assuming solve for x) will modify the row to represent the
  88. right hand side of x = -b/a * y - c / a. The target symbol will
  89. be removed from the row, and the constant and other cells will
  90. be multiplied by the negative inverse of the target coefficient.
  91. The given symbol *must* exist in the row.
  92. */
  93. void solveFor( const Symbol& symbol )
  94. {
  95. typedef CellMap::iterator iter_t;
  96. double coeff = -1.0 / m_cells[ symbol ];
  97. m_cells.erase( symbol );
  98. m_constant *= coeff;
  99. iter_t end = m_cells.end();
  100. for( iter_t it = m_cells.begin(); it != end; ++it )
  101. it->second *= coeff;
  102. }
  103. /* Solve the row for the given symbols.
  104. This method assumes the row is of the form x = b * y + c and will
  105. solve the row such that y = x / b - c / b. The rhs symbol will be
  106. removed from the row, the lhs added, and the result divided by the
  107. negative inverse of the rhs coefficient.
  108. The lhs symbol *must not* exist in the row, and the rhs symbol
  109. *must* exist in the row.
  110. */
  111. void solveFor( const Symbol& lhs, const Symbol& rhs )
  112. {
  113. insert( lhs, -1.0 );
  114. solveFor( rhs );
  115. }
  116. /* Get the coefficient for the given symbol.
  117. If the symbol does not exist in the row, zero will be returned.
  118. */
  119. double coefficientFor( const Symbol& symbol ) const
  120. {
  121. CellMap::const_iterator it = m_cells.find( symbol );
  122. if( it == m_cells.end() )
  123. return 0.0;
  124. return it->second;
  125. }
  126. /* Substitute a symbol with the data from another row.
  127. Given a row of the form a * x + b and a substitution of the
  128. form x = 3 * y + c the row will be updated to reflect the
  129. expression 3 * a * y + a * c + b.
  130. If the symbol does not exist in the row, this is a no-op.
  131. */
  132. void substitute( const Symbol& symbol, const Row& row )
  133. {
  134. typedef CellMap::iterator iter_t;
  135. iter_t it = m_cells.find( symbol );
  136. if( it != m_cells.end() )
  137. {
  138. double coefficient = it->second;
  139. m_cells.erase( it );
  140. insert( row, coefficient );
  141. }
  142. }
  143. private:
  144. CellMap m_cells;
  145. double m_constant;
  146. };
  147. } // namespace impl
  148. } // namespace