Macaulay2 Engine
Loading...
Searching...
No Matches

◆ convertToM2()

const RingElement * convertToM2 ( const PolynomialRing * R,
CanonicalForm h )
static

Definition at line 214 of file factory.cpp.

215{
216 // this seems not to handle polynomials with rational coefficients at all!!
217 const int n = R->n_vars();
218 if (h.inCoeffDomain())
219 {
220 if (h.inZ())
221 {
222 mpz_t x = {toInteger(h)};
223 ring_elem ret = R->from_int(x);
224 mpz_clear(x);
225 return RingElement::make_raw(R, ret);
226 }
227 else if (h.inQ())
228 {
229 struct enter_factory c;
230 __mpq_struct z = {toInteger(h.num()), toInteger(h.den())};
231 ring_elem val;
232 bool ok = R->from_rational(&z, val);
233 if (not ok)
234 {
235 std::cout << "internal error: unexpected failure to lift "
236 "rational number to ring"
237 << std::endl;
238 val = R->from_long(0);
239 }
240 RingElement *ret = RingElement::make_raw(R, val);
241 mpq_clear(&z);
242 return ret;
243 }
244 else if (h.inFF())
245 return RingElement::make_raw(R, R->from_long(h.intval()));
246 else if (h.inExtension())
247 {
248 assert(algebraicElement_M2 != NULL);
249 ring_elem result = R->from_long(0);
250 for (int j = h.taildegree(); j <= h.degree(); j++)
251 {
252 const RingElement *r = convertToM2(R, h[j]);
253 if (error()) return RingElement::make_raw(R, R->one());
254 ring_elem r1 = r->get_value();
255 ring_elem v = algebraicElement_M2->get_value();
256 v = R->power(v, j);
257 r1 = R->mult(r1, v);
258 R->add_to(result, r1);
259 }
260 return RingElement::make_raw(R, result);
261 }
262 else
263 {
264 ERROR("conversion from factory over unknown type");
265 return RingElement::make_raw(R, R->one());
266 }
267 }
268 ring_elem result = R->from_long(0);
269 for (int j = h.taildegree(); j <= h.degree(); j++)
270 {
271 const RingElement *r = convertToM2(R, h[j]);
272 if (error()) return RingElement::make_raw(R, R->one());
273 ring_elem r1 = r->get_value();
274 int var =
275#if REVERSE_VARIABLES
276 (n - 1) -
277#endif
278 (h.level() - 1);
279 ring_elem v = R->var(var);
280 v = R->power(v, j);
281 r1 = R->mult(r1, v);
282 R->add_to(result, r1);
283 }
284 return RingElement::make_raw(R, result);
285}
virtual ring_elem var(int v) const =0
int n_vars() const
Definition polyring.hpp:196
ring_elem one() const
Definition ring.hpp:357
virtual ring_elem from_long(long n) const =0
void add_to(ring_elem &f, const ring_elem &g) const
Definition ring.cpp:205
virtual ring_elem from_int(mpz_srcptr n) const =0
virtual ring_elem power(const ring_elem f, mpz_srcptr n) const
Exponentiation. This is the default function, if a class doesn't define this.
Definition ring.cpp:109
virtual ring_elem mult(const ring_elem f, const ring_elem g) const =0
virtual bool from_rational(const mpq_srcptr q, ring_elem &result) const =0
ring_elem get_value() const
Definition relem.hpp:79
static RingElement * make_raw(const Ring *R, ring_elem f)
Definition relem.cpp:20
Front-end-visible "ring element" value: an engine ring_elem paired with the Ring* that gives it meani...
Definition relem.hpp:67
int error()
Definition error.c:48
static const RingElement * convertToM2(const PolynomialRing *R, CanonicalForm h)
Definition factory.cpp:214
static __mpz_struct toInteger(CanonicalForm h)
Definition factory.cpp:170
const RingElement * algebraicElement_M2
Definition factory.cpp:81
const int ERROR
Definition m2-mem.cpp:55
VALGRIND_MAKE_MEM_DEFINED & result(result)
volatile int x

References Ring::add_to(), algebraicElement_M2, convertToM2(), ERROR, error(), Ring::from_int(), Ring::from_long(), Ring::from_rational(), RingElement::get_value(), RingElement::make_raw(), Ring::mult(), PolynomialRing::n_vars(), Ring::one(), Ring::power(), result(), toInteger(), PolynomialRing::var(), and x.

Referenced by convertToM2(), displayCF(), rawCharSeries(), rawExtendedGCDRingElement(), rawFactorBase(), rawGCDRingElement(), and rawPseudoRemainder().