Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
polynomial_reference_probe.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
37# include <cstdlib>
38# include <iostream>
39# include <sstream>
40# include <string>
41# include <type_traits>
42
43# include <tpl_polynomial.H>
44# include <tpl_multi_polynomial.H>
45
46using namespace Aleph;
47
48namespace
49{
53
54 template <typename Coeff>
55 void
57 {
58 std::cout << "[";
59 for (size_t exp = 0; exp <= p.degree(); ++exp)
60 {
61 std::cout << p.get_coeff(exp);
62 if (exp + 1 <= p.degree())
63 std::cout << ", ";
64 }
65 std::cout << "]";
66 }
67
68 template <typename Poly>
69 void
70 print_term_array(const Poly &p)
71 {
72 bool first = true;
73 std::cout << "[";
74 p.for_each_term([&first](const Array<size_t> &idx, const typename Poly::Coeff &coeff)
75 {
76 if (not first)
77 std::cout << ", ";
78 first = false;
79 std::cout << "{\"coeff\": " << coeff << ", \"exponents\": [";
80 for (size_t i = 0; i < idx.size(); ++i)
81 {
82 std::cout << idx(i);
83 if (i + 1 < idx.size())
84 std::cout << ", ";
85 }
86 std::cout << "]}";
87 });
88 std::cout << "]";
89 }
90
91 template <typename TermList>
92 void
93 print_factor_terms(const TermList &terms)
94 {
95 bool first = true;
96 std::cout << "[";
97 for (auto it = terms.get_it(); it.has_curr(); it.next_ne())
98 {
99 if (not first)
100 std::cout << ", ";
101 first = false;
102 const auto &term = it.get_curr();
103 std::cout << "{\"multiplicity\": " << term.multiplicity << ", \"factor\": ";
104 using FactorType = std::remove_cvref_t<decltype(term.factor)>;
105 if constexpr (std::is_same_v<FactorType, IntPoly>)
106 print_coeff_array(term.factor);
107 else
108 print_term_array(term.factor);
109 std::cout << "}";
110 }
111 std::cout << "]";
112 }
113
114 void
116 {
117 IntPoly a({-5, 2, -3, 0, 1});
118 IntPoly b({2, -1, 1});
119 IntPoly prod = a * b;
120 auto [q, r] = prod.divmod(b);
121
122 IntPoly outer({1, -2, 0, 0, 1});
123 IntPoly inner({1, 1, 2});
125
126 IntPoly f1({1, 1, 1});
127 IntPoly f2({-3, 1});
128 IntPoly f3({1, 2});
129 IntPoly factorable = f3 * f2 * f1 * f1;
131
132 std::cout << " \"univariate\": [\n"
133 << " {\n"
134 << " \"name\": \"sum_mul_divmod\",\n"
135 << " \"a\": ";
137 std::cout << ",\n"
138 << " \"b\": ";
140 std::cout << ",\n"
141 << " \"sum\": ";
142 print_coeff_array(a + b);
143 std::cout << ",\n"
144 << " \"product\": ";
146 std::cout << ",\n"
147 << " \"quotient\": ";
149 std::cout << ",\n"
150 << " \"remainder\": ";
152 std::cout << "\n"
153 << " },\n"
154 << " {\n"
155 << " \"name\": \"compose_sparse\",\n"
156 << " \"outer\": ";
158 std::cout << ",\n"
159 << " \"inner\": ";
161 std::cout << ",\n"
162 << " \"composed\": ";
164 std::cout << "\n"
165 << " },\n"
166 << " {\n"
167 << " \"name\": \"factorize_integer\",\n"
168 << " \"poly\": ";
170 std::cout << ",\n"
171 << " \"factors\": ";
173 std::cout << "\n"
174 << " }\n"
175 << " ]";
176 }
177
178 void
180 {
181 auto x = IntMultiPoly::variable(2, 0);
182 auto y = IntMultiPoly::variable(2, 1);
183
184 IntMultiPoly diff_sq = x * x - y * y;
185 auto diff_sq_factors = diff_sq.factorize();
186
187 IntMultiPoly q1 = 2LL * x * x + 3LL * y + IntMultiPoly(2, 1LL);
188 IntMultiPoly q2 = 3LL * x * x + 5LL * y + IntMultiPoly(2, 2LL);
190 auto same_degree_factors = same_degree.factorize();
191
192 auto xlex = LexMultiPoly::variable(2, 0);
193 auto ylex = LexMultiPoly::variable(2, 1);
195 gens(0) = xlex * xlex - ylex;
196 gens(1) = xlex * ylex - LexMultiPoly(2, 1.0);
197 auto gb = LexMultiPoly::reduced_groebner_basis(gens);
198
199 std::cout << ",\n"
200 << " \"multivariate\": [\n"
201 << " {\n"
202 << " \"name\": \"factorize_difference_of_squares\",\n"
203 << " \"nvars\": 2,\n"
204 << " \"poly\": ";
206 std::cout << ",\n"
207 << " \"factors\": ";
209 std::cout << "\n"
210 << " },\n"
211 << " {\n"
212 << " \"name\": \"factorize_same_degree_non_monic\",\n"
213 << " \"nvars\": 2,\n"
214 << " \"poly\": ";
216 std::cout << ",\n"
217 << " \"factors\": ";
219 std::cout << "\n"
220 << " },\n"
221 << " {\n"
222 << " \"name\": \"groebner_reduced_lex\",\n"
223 << " \"nvars\": 2,\n"
224 << " \"generators\": [";
225 for (size_t i = 0; i < gens.size(); ++i)
226 {
228 if (i + 1 < gens.size())
229 std::cout << ", ";
230 }
231 std::cout << "],\n"
232 << " \"basis\": [";
233 for (size_t i = 0; i < gb.size(); ++i)
234 {
236 if (i + 1 < gb.size())
237 std::cout << ", ";
238 }
239 std::cout << "]\n"
240 << " }\n"
241 << " ]";
242 }
243}
244
245int
246main(int argc, char **argv)
247{
248 for (int i = 1; i < argc; ++i)
249 {
250 const std::string arg = argv[i];
251 if (arg == "--help")
252 {
253 std::cout << "Usage: polynomial_reference_probe [--json]\n";
254 return 0;
255 }
256 if (arg != "--json")
257 {
258 std::cerr << "Unknown option: " << arg << "\n";
259 return 1;
260 }
261 }
262
263 std::cout << "{\n"
264 << " \"mode\": \"reference\",\n";
267 std::cout << "\n}\n";
268 return 0;
269}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
Sparse multivariate polynomial.
Univariate polynomial over a generic coefficient ring.
Gen_Polynomial compose(const Gen_Polynomial &q) const
Composition .
size_t degree() const noexcept
Degree of the polynomial.
DynList< SfdTerm > factorize() const
Main factorization over integers.
std::pair< Gen_Polynomial, Gen_Polynomial > divmod(const Gen_Polynomial &d) const
Polynomial long division: returns (quotient, remainder).
Coefficient get_coeff(size_t exp) const noexcept
Coefficient accessor (read-only at exponent).
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4066
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
gsl_rng * r
Sparse multivariate polynomial over an arbitrary coefficient ring.
Univariate polynomial ring arithmetic over generic coefficients.