| 1 |
/* |
| 2 |
* This file is part of MPSolve 3.2.2 |
| 3 |
* |
| 4 |
* Copyright (C) 2001-2020, Dipartimento di Matematica "L. Tonelli", Pisa. |
| 5 |
* License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher |
| 6 |
* |
| 7 |
* Authors: |
| 8 |
* Leonardo Robol <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* @file |
| 13 |
* @brief Implementation in C++ of arithmetic between formal polynomials |
| 14 |
* with rational coefficients. |
| 15 |
* |
| 16 |
* This implementation is used while parsing polynomials with the lexer |
| 17 |
* to construct a polynomial incrementally. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef MPS_FORMAL_MONOMIAL_H_ |
| 21 |
#define MPS_FORMAL_MONOMIAL_H_ |
| 22 |
|
| 23 |
MPS_BEGIN_DECLS |
| 24 |
|
| 25 |
struct mps_formal_monomial; |
| 26 |
|
| 27 |
typedef struct mps_formal_monomial mps_formal_monomial; |
| 28 |
|
| 29 |
mps_formal_monomial * mps_formal_monomial_new_with_string (const char *, long); |
| 30 |
|
| 31 |
mps_formal_monomial * mps_formal_monomial_new_with_strings (const char * real, const char * imag, |
| 32 |
long degree); |
| 33 |
|
| 34 |
void mps_formal_monomial_free (mps_formal_monomial*); |
| 35 |
|
| 36 |
void mps_formal_monomial_print (mps_formal_monomial*); |
| 37 |
|
| 38 |
mps_formal_monomial * mps_formal_monomial_neg (mps_formal_monomial * m); |
| 39 |
|
| 40 |
mps_formal_monomial * mps_formal_monomial_mul_eq (mps_formal_monomial * m, |
| 41 |
mps_formal_monomial * other); |
| 42 |
|
| 43 |
mps_formal_monomial * mps_formal_monomial_mul (mps_formal_monomial * m, |
| 44 |
mps_formal_monomial * other); |
| 45 |
|
| 46 |
const char * mps_formal_monomial_get_str(mps_formal_monomial * m); |
| 47 |
|
| 48 |
int mps_formal_monomial_degree (mps_formal_monomial *m); |
| 49 |
|
| 50 |
MPS_END_DECLS |
| 51 |
|
| 52 |
#ifdef __cplusplus |
| 53 |
|
| 54 |
#include <iostream> |
| 55 |
#include <gmpxx.h> |
| 56 |
|
| 57 |
namespace mps { |
| 58 |
namespace formal { |
| 59 |
|
| 60 |
class Monomial { |
| 61 |
public: |
| 62 |
|
| 63 |
/** |
| 64 |
* @brief Create a default Monomial, consisting of the 0 constant with |
| 65 |
* conventional degree -1. |
| 66 |
*/ |
| 67 |
Monomial(); |
| 68 |
|
| 69 |
/** |
| 70 |
* @brief Create a monomial starting from a string representing the |
| 71 |
* coefficient and an integer representing the degree. |
| 72 |
*/ |
| 73 |
Monomial(const char * coeff_string, long degree); |
| 74 |
|
| 75 |
/** |
| 76 |
* @brief Create a monomial starting from a string representing the |
| 77 |
* coefficient and an integer representing the degree. |
| 78 |
*/ |
| 79 |
Monomial(const char * real_part, const char * imag_part, long degree); |
| 80 |
|
| 81 |
/** |
| 82 |
* @brief Create a monomial with the given coefficient and of the specified |
| 83 |
* degree. |
| 84 |
*/ |
| 85 |
Monomial(const mpq_class coeff, long degree); |
| 86 |
|
| 87 |
/** |
| 88 |
* @brief Create a monomial with the given coefficient and of the specified |
| 89 |
* degree. |
| 90 |
*/ |
| 91 |
Monomial(const mpq_class real, const mpq_class imag, long degree); |
| 92 |
|
| 93 |
/** |
| 94 |
* @brief Copy constructor. |
| 95 |
*/ |
| 96 |
Monomial(const Monomial& rhs); |
| 97 |
|
| 98 |
~Monomial(); |
| 99 |
|
| 100 |
/** |
| 101 |
* @brief Get the degree of the monomial. |
| 102 |
*/ |
| 103 |
const long degree() const { return mDegree; } |
| 104 |
|
| 105 |
/** |
| 106 |
* @brief Access the real GMP coefficient stored inside the Monomial. |
| 107 |
*/ |
| 108 |
const mpq_class coefficientReal() const { return mCoeffR; } |
| 109 |
|
| 110 |
/** |
| 111 |
* @brief Access the imaginary GMP coefficient stored inside the Monomial. |
| 112 |
*/ |
| 113 |
const mpq_class coefficientImag() const { return mCoeffI; } |
| 114 |
|
| 115 |
/** |
| 116 |
* @brief Check if this is the zero monomial. |
| 117 |
*/ |
| 118 |
bool isZero() const; |
| 119 |
|
| 120 |
/** |
| 121 |
* @brief Check whether the monomial has a real coefficient. |
| 122 |
*/ |
| 123 |
bool isReal() const; |
| 124 |
|
| 125 |
/** |
| 126 |
* @brief Check whether the monomial has a real coefficient. |
| 127 |
*/ |
| 128 |
bool isImag() const; |
| 129 |
|
| 130 |
Monomial operator-(); |
| 131 |
|
| 132 |
/** |
| 133 |
* @brief Multiply two monomials together. |
| 134 |
*/ |
| 135 |
Monomial& operator*=(const Monomial& other); |
| 136 |
|
| 137 |
/** |
| 138 |
* @brief Multiply two monomials together. |
| 139 |
*/ |
| 140 |
Monomial operator*(const Monomial& other) const; |
| 141 |
|
| 142 |
/** |
| 143 |
* @brief Print a monomial to a stream. |
| 144 |
*/ |
| 145 |
friend std::ostream& operator<<(std::ostream& os, const Monomial& l); |
| 146 |
|
| 147 |
private: |
| 148 |
mpq_class mCoeffR; |
| 149 |
mpq_class mCoeffI; |
| 150 |
|
| 151 |
long mDegree; |
| 152 |
}; |
| 153 |
|
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
#endif /* ifdef __cplusplus */ |
| 158 |
|
| 159 |
#endif /* MPS_FORMAL_MONOMIAL_H_ */ |
| 160 |
|