Macaulay2 Engine
Loading...
Searching...
No Matches
BasicPoly.hpp
Go to the documentation of this file.
1
35
36// Current restriction: the coefficients must be an integral type.
37// TODO: allow infinite precision integers too.
38// TODO: how should we handle coefficients which are: GF(p^n), QQ, fraction fields? or even polynomials?
39#pragma once
40
41#include "exceptions.hpp"
42#include <vector>
43#include <iosfwd>
44#include <string>
45#include <unordered_map>
46#include <gmpxx.h>
47
64{
65public:
66 std::vector<mpz_class> mCoefficients;
67 std::vector<int> mComponents; // if zero length: all components are 0.
68 std::vector<int> mMonomials; // a concatenated list of varpower monomials. Each first entry is its length.
69
70 void clear(); // resets all data to represent the zero polynomial
72
73 size_t termCount() const { return mCoefficients.size(); }
74 void debug_display(std::ostream& o) const;
75
76 template<typename T>
77 static void displayCoefficient(std::ostream& o, T val, bool print_plus, bool print_one);
78
79 void display(std::ostream& o,
80 const std::vector<std::string> & varnames,
81 bool print_one,
82 bool print_plus,
83 bool print_parens) const;
84 void display(std::ostream& o, const std::vector<std::string> & varnames) const
85 {
86 display(o, varnames, true, false, false);
87 }
88 std::string toString(const std::vector<std::string> & varnames,
89 bool print_one,
90 bool print_plus,
91 bool print_parens) const;
92 std::string toString(const std::vector<std::string> & varnames) const
93 {
94 return toString(varnames, true, false, false);
95 }
96
97 long bytesUsed() const;
98};
99
101// Simple parsing of polynomials //
103
105{
106 explicit parsing_error(const std::string &msg) : exc::engine_error(msg) {}
107};
108
111{
112private:
113 std::vector<std::string> mAllocatedStrings;
114 std::unordered_map<std::string_view, int> mMap;
115public:
116 IdentifierHash() = default;
117 IdentifierHash(std::vector<std::string>& idens)
118 : mAllocatedStrings(idens),
119 mMap()
120 {
121 for (int i=0; i<mAllocatedStrings.size(); ++i)
122 {
123 mMap[std::string_view(mAllocatedStrings[i])] = i;
124 }
125 }
126
127 auto find(std::string_view s) const -> int
128 {
129 auto foundloc = mMap.find(s);
130 return (foundloc != mMap.end() ? foundloc->second : -1); // TODO: throw an error if not found?
131 }
132};
133
134// These will throw a parsing_error if there is a parsing error. The
135// plan is that that will include the location in the string of the
136// error.
137
138BasicPoly parseBasicPoly(std::string poly, std::vector<std::string> varnames);
139
141void parseBasicPoly(const std::string_view& str, const IdentifierHash& idenHash, BasicPoly& result);
142
143
144// TODO: we want an iterator type here.
145// TODO:
146//
147
148// Local Variables:
149// indent-tabs-mode: nil
150// End:
BasicPoly parseBasicPoly(std::string poly, std::vector< std::string > varnames)
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::string toString(const std::vector< std::string > &varnames) const
Definition BasicPoly.hpp:92
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
void display(std::ostream &o, const std::vector< std::string > &varnames) const
Definition BasicPoly.hpp:84
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
Standalone, self-contained polynomial representation independent of any engine Ring — coefficients ar...
Definition BasicPoly.hpp:64
IdentifierHash(std::vector< std::string > &idens)
auto find(std::string_view s) const -> int
IdentifierHash()=default
std::vector< std::string > mAllocatedStrings
std::unordered_map< std::string_view, int > mMap
IdentifierHash: used to facilitate parsing of polynomials from strings and files.
namespace exc — internal C++ exception types and the TRY / CATCH macro pair.
void size_t s
Definition m2-mem.cpp:271
VALGRIND_MAKE_MEM_DEFINED & result(result)
engine_error(const std::string &msg)
parsing_error(const std::string &msg)
#define T
Definition table.c:13