term.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 LICENSE, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. #include <utility>
  10. #include "variable.h"
  11. namespace kiwi
  12. {
  13. class Term
  14. {
  15. public:
  16. Term( Variable variable, double coefficient = 1.0 ) :
  17. m_variable( std::move(variable) ), m_coefficient( coefficient ) {}
  18. // to facilitate efficient map -> vector copies
  19. Term( const std::pair<const Variable, double>& pair ) :
  20. m_variable( pair.first ), m_coefficient( pair.second ) {}
  21. Term(const Term&) = default;
  22. Term(Term&&) noexcept = default;
  23. ~Term() = default;
  24. const Variable& variable() const
  25. {
  26. return m_variable;
  27. }
  28. double coefficient() const
  29. {
  30. return m_coefficient;
  31. }
  32. double value() const
  33. {
  34. return m_coefficient * m_variable.value();
  35. }
  36. Term& operator=(const Term&) = default;
  37. Term& operator=(Term&&) noexcept = default;
  38. private:
  39. Variable m_variable;
  40. double m_coefficient;
  41. };
  42. } // namespace kiwi