Macaulay2 Engine
Loading...
Searching...
No Matches
BasicPoly.cpp
Go to the documentation of this file.
1#include "BasicPoly.hpp"
3#include <sstream>
4
6{
7 // for (auto& num : mCoefficients)
8 // mpz_clear(num);
9 mCoefficients.clear();
10 mComponents.clear();
11 mMonomials.clear();
12}
13std::string BasicPoly::toString(const std::vector<std::string> & varnames,
14 bool print_one,
15 bool print_plus,
16 bool print_parens) const
17{
18 std::ostringstream o;
19 display(o, varnames, print_one, print_plus, print_parens);
20 return o.str();
21}
22
24{
25 return 3 * sizeof(std::vector<int>)
26 + sizeof(int) * mCoefficients.size()
27 + sizeof(int) * mComponents.size()
28 + sizeof(int) * mMonomials.size();
29}
30
31void BasicPoly::debug_display(std::ostream& o) const
32 {
33 o << "Poly([";
34 bool first_term = true;
35 for (auto a : mCoefficients)
36 {
37 if (first_term)
38 first_term = false;
39 else
40 o << ", ";
41 o << a;
42 }
43 o << "]" << std::endl;
44 int nextloc = 0;
45 o << " monomials[";
46 for (int i=0; i<mMonomials.size(); ++i)
47 {
48 if (i == nextloc) o << " .";
49 nextloc += mMonomials[i];
50 o << " " << mMonomials[i];
51 }
52 o << "])" << std::endl;
53 }
54
55template<typename T>
56void BasicPoly::displayCoefficient(std::ostream& o, T val, bool print_plus, bool print_one)
57{
58 // print_one is true: if value is +1 or -1, then print the "1", else don't.
59 bool is_negative = (val < 0);
60 bool is_one = (val == 1 or val == -1);
61
62 if (not is_negative and print_plus) o << '+';
63 if (is_one)
64 {
65 if (is_negative) o << '-';
66 if (print_one) o << '1';
67 }
68 else
69 {
70 o << val;
71 }
72
73}
74
75// TODO: need BasicPolyIterator. For now, we do it by hand
76// TODO: how to deal with components?
77void BasicPoly::display(std::ostream& o,
78 const std::vector<std::string> & varnames,
79 bool print_one,
80 bool print_plus,
81 bool print_parens) const
82{
83 if (termCount() == 0)
84 {
85 o << '0';
86 return;
87 }
88
89 bool more_than_one_term = (termCount() >= 2);
90 int needs_parens = print_parens && more_than_one_term;
91
92 if (needs_parens)
93 {
94 if (print_plus) o << '+';
95 o << '(';
96 print_plus = false;
97 }
98
99 const int* monom_loc = mMonomials.data();
100 for (int i=0; i<termCount(); ++i)
101 {
102 bool monom_is_one = (*monom_loc == 1); // TODO: do *not* hand code this determination in!
103 print_parens = !monom_is_one;
104 bool p_one_this = (monom_is_one && needs_parens) || (monom_is_one && print_one);
105 displayCoefficient(o, mCoefficients[i], print_plus, p_one_this);
106 if (!monom_is_one)
107 {
108 if (mCoefficients[i] != 1 and mCoefficients[i] != -1)
109 o << '*';
110 newf4::MonomialView::display(o, varnames, newf4::MonomialView(const_cast<int*>(monom_loc)));
111 }
112 print_plus = true;
113 monom_loc += *monom_loc;
114 }
115 if (needs_parens) o << ')';
116}
117
118// Local Variables:
119// indent-tabs-mode: nil
120// End:
Minimal, portable polynomial value type used where heavier engine types would be overkill.
newf4::MonomialView — non-owning view over a [length, var_1, e_1, ...]-encoded monomial.
static void displayCoefficient(std::ostream &o, T val, bool print_plus, bool print_one)
Definition BasicPoly.cpp:56
long bytesUsed() const
Definition BasicPoly.cpp:23
std::vector< int > mMonomials
Definition BasicPoly.hpp:68
void clear()
Definition BasicPoly.cpp:5
void display(std::ostream &o, const std::vector< std::string > &varnames, bool print_one, bool print_plus, bool print_parens) const
Definition BasicPoly.cpp:77
std::vector< int > mComponents
Definition BasicPoly.hpp:67
size_t termCount() const
Definition BasicPoly.hpp:73
std::vector< mpz_class > mCoefficients
Definition BasicPoly.hpp:66
std::string toString(const std::vector< std::string > &varnames, bool print_one, bool print_plus, bool print_parens) const
Definition BasicPoly.cpp:13
void debug_display(std::ostream &o) const
Definition BasicPoly.cpp:31
static void display(std::ostream &o, const std::vector< std::string > &varnames, const newf4::MonomialView &m)
#define T
Definition table.c:13