How to resolve the algorithm Tropical algebra overloading step by step in the C++ programming language
How to resolve the algorithm Tropical algebra overloading step by step in the C++ programming language
Table of Contents
Problem Statement
In algebra, a max tropical semiring (also called a max-plus algebra) is the semiring (ℝ ∪ -Inf, ⊕, ⊗) containing the ring of real numbers ℝ augmented by negative infinity, the max function (returns the greater of two real numbers), and addition. In max tropical algebra, x ⊕ y = max(x, y) and x ⊗ y = x + y. The identity for ⊕ is -Inf (the max of any number with -infinity is that number), and the identity for ⊗ is 0. Show that 2 ⊗ -2 is 0, -0.001 ⊕ -Inf is -0.001, 0 ⊗ -Inf is -Inf, 1.5 ⊕ -1 is 1.5, and -0.5 ⊗ 0 is -0.5. where ⊗ has precedence over ⊕. Demonstrate that 5 ⊗ (8 ⊕ 7) equals 5 ⊗ 8 ⊕ 5 ⊗ 7.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Tropical algebra overloading step by step in the C++ programming language
The provided C++ code defines a TropicalAlgebra
class and various operators to perform operations on values in a tropical algebra setting. Tropical algebra is a mathematical structure that uses the operations of maximum and addition instead of the usual multiplication and addition operations. In this code, negative infinity (-Inf) is represented using an unset std::optional<double>
.
Here's a breakdown of the code:
-
TropicalAlgebra
Class:- This class represents a point in tropical algebra, where the value can be initialized to a finite double or left unset to represent negative infinity.
- It defines overloaded operators for addition (
+=
), multiplication (*=
), and exponentiation (pow
).
-
Operator Overloads:
operator+
: This operator adds twoTropicalAlgebra
values using the+=
operator defined in theTropicalAlgebra
class.operator*
: This operator multiplies twoTropicalAlgebra
values using the*=
operator defined in theTropicalAlgebra
class.operator<<
: This operator overloads the insertion operator (<<
) to print theTropicalAlgebra
value. It prints "-Inf" if the value is unset or the actual value otherwise.
-
pow
Function:- This function exponentiates a
TropicalAlgebra
value to a given exponent using successive multiplication.
- This function exponentiates a
-
main
Function:- This function demonstrates the use of the
TropicalAlgebra
class and its operators:- It creates several
TropicalAlgebra
objects with different values, including negative infinity. - It performs various operations such as addition, multiplication, and exponentiation on these values.
- It prints the results of these operations, which can be either finite values or negative infinity.
- It creates several
- This function demonstrates the use of the
Overall, this code showcases the use of C++ classes and operator overloading to implement a tropical algebra, where negative infinity is handled using an unset std::optional<double>
. It demonstrates how tropical algebra operations (maximum and addition) can be applied to values, including negative infinity, to obtain meaningful results.
Source code in the cpp programming language
#include <iostream>
#include <optional>
using namespace std;
class TropicalAlgebra
{
// use an unset std::optional to represent -infinity
optional<double> m_value;
public:
friend std::ostream& operator<<(std::ostream&, const TropicalAlgebra&);
friend TropicalAlgebra pow(const TropicalAlgebra& base, unsigned int exponent) noexcept;
// create a point that is initialized to -infinity
TropicalAlgebra() = default;
// construct with a value
explicit TropicalAlgebra(double value) noexcept
: m_value{value} {}
// add a value to this one ( p+=q ). it is common to also overload
// the += operator when overloading +
TropicalAlgebra& operator+=(const TropicalAlgebra& rhs) noexcept
{
if(!m_value)
{
// this point is -infinity so the other point is max
*this = rhs;
}
else if (!rhs.m_value)
{
// since rhs is -infinity this point is max
}
else
{
// both values are valid, find the max
*m_value = max(*rhs.m_value, *m_value);
}
return *this;
}
// multiply this value by another (p *= q)
TropicalAlgebra& operator*=(const TropicalAlgebra& rhs) noexcept
{
if(!m_value)
{
// since this value is -infinity this point does not need to be
// modified
}
else if (!rhs.m_value)
{
// the other point is -infinity, make this -infinity too
*this = rhs;
}
else
{
*m_value += *rhs.m_value;
}
return *this;
}
};
// add values (p + q)
inline TropicalAlgebra operator+(TropicalAlgebra lhs, const TropicalAlgebra& rhs) noexcept
{
// implemented using the += operator defined above
lhs += rhs;
return lhs;
}
// multiply values (p * q)
inline TropicalAlgebra operator*(TropicalAlgebra lhs, const TropicalAlgebra& rhs) noexcept
{
lhs *= rhs;
return lhs;
}
// pow is the idomatic way for exponentiation in C++
inline TropicalAlgebra pow(const TropicalAlgebra& base, unsigned int exponent) noexcept
{
auto result = base;
for(unsigned int i = 1; i < exponent; i++)
{
// compute the power by successive multiplication
result *= base;
}
return result;
}
// print the point
ostream& operator<<(ostream& os, const TropicalAlgebra& pt)
{
if(!pt.m_value) cout << "-Inf\n";
else cout << *pt.m_value << "\n";
return os;
}
int main(void) {
const TropicalAlgebra a(-2);
const TropicalAlgebra b(-1);
const TropicalAlgebra c(-0.5);
const TropicalAlgebra d(-0.001);
const TropicalAlgebra e(0);
const TropicalAlgebra h(1.5);
const TropicalAlgebra i(2);
const TropicalAlgebra j(5);
const TropicalAlgebra k(7);
const TropicalAlgebra l(8);
const TropicalAlgebra m; // -Inf
cout << "2 * -2 == " << i * a;
cout << "-0.001 + -Inf == " << d + m;
cout << "0 * -Inf == " << e * m;
cout << "1.5 + -1 == " << h + b;
cout << "-0.5 * 0 == " << c * e;
cout << "pow(5, 7) == " << pow(j, 7);
cout << "5 * (8 + 7)) == " << j * (l + k);
cout << "5 * 8 + 5 * 7 == " << j * l + j * k;
}
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Logical operations step by step in the OCaml programming language
You may also check:How to resolve the algorithm String case step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Y combinator step by step in the BlitzMax programming language