Macaulay2 Engine
Loading...
Searching...
No Matches
MonomialView.cpp
Go to the documentation of this file.
1#include "MonomialView.hpp"
2#include "../exceptions.hpp"
3namespace newf4 {
4
5// a general splice command which handles lcm, product and quotient
7 const MonomialView& right,
8 bool useLeft,
9 bool useRight,
10 std::function<MonomialInt(MonomialInt,MonomialInt)> bothFunc,
11 MemoryBlock &block)
12{
13 auto rng = block.allocateArray<MonomialInt>(left.size() + right.size() - 1);
14 MonomialView result(rng.first);
15
16 auto leftIt = left.begin();
17 auto rightIt = right.begin();
18 auto leftEnd = left.end();
19 auto rightEnd = right.end();
20 auto resultIt = result.begin();
21
22 while (true)
23 {
24 if (leftIt == leftEnd)
25 {
26 // take from right and increment right and result
27 if (useRight)
28 {
29 std::copy(rightIt.loc(),rightEnd.loc(),resultIt.loc());
30 resultIt += ((rightEnd.loc() - rightIt.loc()) >> 1);
31 }
32 break;
33 }
34 if (rightIt == rightEnd)
35 {
36 // take from left and increment left and result
37 if (useLeft)
38 {
39 std::copy(leftIt.loc(),leftEnd.loc(),resultIt.loc());
40 resultIt += ((leftEnd.loc() - leftIt.loc()) >> 1);
41 }
42 break;
43 }
44 if (leftIt.var() < rightIt.var())
45 {
46 // take from left and increment left and result
47 std::copy(leftIt.loc(), leftIt.loc() + 2, resultIt.loc());
48 ++resultIt;
49 ++leftIt;
50 }
51 else if (leftIt.var() > rightIt.var())
52 {
53 // take from right and increment right and result
54 std::copy(rightIt.loc(), rightIt.loc() + 2, resultIt.loc());
55 ++resultIt;
56 ++rightIt;
57 }
58 else
59 {
60 // take maximum and increment both
61 resultIt.loc()[0] = leftIt.var();
62 auto temp = bothFunc(leftIt.power(), rightIt.power());
63 if (temp > 0) // if we actually need this variable, store power and increment result
64 {
65 resultIt.loc()[1] = bothFunc(leftIt.power(), rightIt.power());
66 ++resultIt;
67 }
68 ++leftIt;
69 ++rightIt;
70 }
71 }
72 block.shrinkLastAllocate(rng.first, rng.second, resultIt.loc());
73 *rng.first = resultIt.loc() - rng.first;
74 return result;
75}
76
78 const MonomialView& right,
79 MemoryBlock &block)
80{
81 return MonomialView::combine(left,
82 right,
83 true,
84 true,
85 [](MonomialInt l, MonomialInt r) -> MonomialInt { return std::max(l,r); },
86 block);
87}
88
90 const MonomialView& right,
91 MemoryBlock &block)
92{
93 return MonomialView::combine(left,
94 right,
95 true,
96 true,
97 [](MonomialInt l, MonomialInt r) -> MonomialInt { return l+r; },
98 block);
99}
100
101
102// returns monomial m such that right*m is a multiple of left.
104 const MonomialView& right,
105 MemoryBlock &block)
106{
107 return MonomialView::combine(left,
108 right,
109 true,
110 false,
111 [](MonomialInt l, MonomialInt r) -> MonomialInt { return std::max(0,l-r); },
112 block);
113}
114
115void MonomialView::display(std::ostream& o,
116 const std::vector<std::string>& varnames,
117 const newf4::MonomialView& m
118 )
119{
120 // Note: if the monomial is '1', nothing is printed.
121 // TODO: possibly allow print_one boolean argument, instead of handling it at calling point?
122 bool first = true;
123 for (auto t = m.begin(); t != m.end(); ++t)
124 {
125 auto v = t.var();
126 auto e = t.power();
127 if (v >= varnames.size() or v < 0)
128 {
129 throw exc::engine_error("variable out of range");
130 }
131 if (not first)
132 {
133 o << '*';
134 }
135 first = false;
136 o << varnames[v];
137 if (e != 1)
138 o << '^' << e;
139 }
140}
141
142} // end namespace f4
143
newf4::MonomialView — non-owning view over a [length, var_1, e_1, ...]-encoded monomial.
std::pair< T *, T * > shrinkLastAllocate(T *begin, T *end, T *newtop)
std::pair< T *, T * > allocateArray(size_t nelems)
Thin RAII wrapper around memtailor::Arena providing bump-pointer array allocation with optional mutex...
MonomialView(MonomialInt *data)
static MonomialView quotient(const MonomialView &left, const MonomialView &right, MemoryBlock &block)
static void display(std::ostream &o, const std::vector< std::string > &varnames, const newf4::MonomialView &m)
static MonomialView combine(const MonomialView &left, const MonomialView &right, bool copyLeft, bool copyRight, std::function< int(int, int)>, MemoryBlock &block)
auto begin() const -> MonomialIterator< true >
auto end() const -> MonomialIterator< true >
static MonomialView lcm(const MonomialView &left, const MonomialView &right, MemoryBlock &block)
static MonomialView product(const MonomialView &left, const MonomialView &right, MemoryBlock &block)
size_t size() const
namespace exc — internal C++ exception types and the TRY / CATCH macro pair.
VALGRIND_MAKE_MEM_DEFINED & result(result)
int32_t MonomialInt
std::mt19937 rng(rd())