Macaulay2 Engine
Loading...
Searching...
No Matches
cra.cpp
Go to the documentation of this file.
1#include "interface/cra.h"
2
3#include "cra.hpp"
4#include "error.h"
5#include "freemod.hpp"
6#include "matrix-con.hpp"
7#include "matrix.hpp"
8#include "monoid.hpp"
9#include "poly.hpp"
10#include "relem.hpp"
11#include "ring.hpp"
12#include "ringelem.hpp"
13
15 const RingElement *g,
16 mpz_srcptr m,
17 mpz_srcptr n)
18{
19 // Assumption: f and g are either in ZZ, or in a polynomial ring whose coeff
20 // ring is ZZ. The output is a ring element in the same ring.
21
22 const Ring *Rf = f->get_ring();
23 const Ring *Rg = g->get_ring();
24 if (Rf != Rg)
25 {
26 ERROR("expected same ring");
27 return nullptr;
28 }
29 const PolyRing *P = Rf->cast_to_PolyRing();
30 if (P == nullptr)
31 {
32 // check whether Rf is ZZ. If not, error.
33 if (!Rf->is_ZZ())
34 {
35 ERROR("expected ZZ, or polynomial ring over ZZ");
36 return nullptr;
37 }
38 ERROR("not implemented yet");
39 return nullptr;
40 }
41 else
42 {
43 const Ring *K = P->getCoefficientRing();
44 if (K->is_ZZ())
45 {
46 ring_elem rf = f->get_value();
47 ring_elem rg = g->get_value();
48 ring_elem result = ChineseRemainder::CRA(P, rf, rg, m, n);
49 return RingElement::make_raw(Rf, result);
50 }
51 else
52 {
53 ERROR("expected coefficient ring to be ZZ");
54 return nullptr;
55 }
56 }
57 ERROR("not written yet");
58 return nullptr;
59}
60
61const Matrix *rawMatrixCRA(const Matrix *f, const Matrix *g, mpz_srcptr m, mpz_srcptr n)
62{
63 // Error handling:
64 if (f->get_ring() != g->get_ring())
65 {
66 ERROR("matrices have different base rings");
67 return nullptr;
68 }
69 if (f->rows()->rank() != g->rows()->rank() ||
70 f->cols()->rank() != g->cols()->rank())
71 {
72 ERROR("matrices have different shapes");
73 return nullptr;
74 }
75
76 // Assumption: f and g are either matrices over ZZ, or over a polynomial ring
77 // whose coeff
78 // ring is ZZ. The output is a matrix in the same ring.
79
80 mpz_t um, vn, mn;
81 mpz_init(um);
82 mpz_init(vn);
83 mpz_init(mn);
85 mpz_t result_coeff;
86 mpz_init(result_coeff);
87 Matrix *result = ChineseRemainder::CRA(f, g, um, vn, mn);
88 mpz_clear(um);
89 mpz_clear(vn);
90 mpz_clear(mn);
91 return result;
92}
93
95 mpz_srcptr m,
96 const Ring *RQ)
97{
98 const Ring *Rf = f->get_ring();
99 const PolyRing *P = Rf->cast_to_PolyRing();
100 const PolyRing *PQ = RQ->cast_to_PolyRing();
101
102 if (P == nullptr)
103 {
104 // check whether Rf is ZZ. If not, error.
105 if (!Rf->is_ZZ())
106 {
107 ERROR("expected ZZ, or polynomial ring over ZZ");
108 return nullptr;
109 }
110 ERROR("not implemented yet");
111 return nullptr;
112 }
113 else
114 {
115 const Ring *K = P->getCoefficientRing();
116 if (K->is_ZZ())
117 {
118 ring_elem rf = f->get_value();
120 return RingElement::make_raw(PQ, result); // debug this line!
121 }
122 else
123 {
124 ERROR("expected coefficient ring to be ZZ");
125 return nullptr;
126 }
127 }
128 ERROR("not written yet");
129 return nullptr;
130}
131
132// f should be an element in the polynomial ring R (over ZZ).
133// RQ should be the same ring as R, but with rational coefficients
134
135const Matrix *rawMatrixRatConversion(const Matrix *f, mpz_srcptr m, const Ring *RQ)
136{
137 const Ring *Rf = f->get_ring();
138 const PolyRing *P = Rf->cast_to_PolyRing();
139 const PolyRing *PQ = RQ->cast_to_PolyRing();
140
141 if (P == nullptr)
142 {
143 ERROR("expected polynomial ring over ZZ");
144 return nullptr;
145 }
146
147 const FreeModule *F = f->rows();
148 const FreeModule *G = f->cols();
149 const FreeModule *FQ = PQ->make_FreeModule(F->rank());
150 const FreeModule *GQ = PQ->make_FreeModule(G->rank());
151
152 const int *deg;
153
154 deg = Rf->degree_monoid()->make_one();
155
156 MatrixConstructor mat(FQ, GQ, deg);
157 for (int i = 0; i < f->n_cols(); i++)
158 {
159 vec u = ChineseRemainder::ratConversion(f->elem(i), m, PQ);
160 mat.set_column(i, u);
161 }
162 return mat.to_matrix();
163}
164
165// Local Variables:
166// indent-tabs-mode: nil
167// End:
static bool computeMultipliers(mpz_srcptr m, mpz_srcptr n, mpz_t result_um, mpz_t result_vn, mpz_t result_mn)
Definition cra.cpp:36
static bool ratConversion(mpz_srcptr a, mpz_srcptr m, mpq_t result)
Definition cra.cpp:300
static ring_elem CRA(const PolyRing *R, const ring_elem f, const ring_elem g, mpz_srcptr um, mpz_srcptr vn, mpz_srcptr mn)
Definition cra.cpp:53
int rank() const
Definition freemod.hpp:105
Engine-side free module R^n over a Ring.
Definition freemod.hpp:66
const Ring * get_ring() const
Definition matrix.hpp:134
ring_elem elem(int i, int j) const
Definition matrix.cpp:307
int n_cols() const
Definition matrix.hpp:147
const FreeModule * rows() const
Definition matrix.hpp:144
const FreeModule * cols() const
Definition matrix.hpp:145
Matrix * to_matrix()
void set_column(int c, vec v)
Mutable builder used to assemble an immutable Matrix one column (or one term) at a time.
monomial make_one() const
Definition monoid.cpp:455
Concrete PolyRingFlat subclass implementing ordinary commutative polynomial rings K[x_1,...
Definition poly.hpp:64
const Ring * getCoefficientRing() const
Definition polyring.hpp:200
virtual bool is_ZZ() const
Definition ring.hpp:171
virtual FreeModule * make_FreeModule() const
Definition ring.cpp:53
virtual const PolyRing * cast_to_PolyRing() const
Definition ring.hpp:245
const Monoid * degree_monoid() const
Definition ring.cpp:13
ring_elem get_value() const
Definition relem.hpp:79
static RingElement * make_raw(const Ring *R, ring_elem f)
Definition relem.cpp:20
const Ring * get_ring() const
Definition relem.hpp:81
Front-end-visible "ring element" value: an engine ring_elem paired with the Ring* that gives it meani...
Definition relem.hpp:67
xxx xxx xxx
Definition ring.hpp:102
Engine-boundary C API for Chinese-remainder lifting and rational reconstruction.
ChineseRemainder — CRT lifting and rational reconstruction primitives.
Engine error-reporting primitives: ERROR, INTERNAL_ERROR, error, error_message.
#define Matrix
Definition factory.cpp:14
FreeModule — finite-rank free module R^n, the type-level anchor for every Matrix.
const RingElement * rawRingElementCRA(const RingElement *f, const RingElement *g, mpz_srcptr m, mpz_srcptr n)
Definition cra.cpp:14
const RingElement * rawRingElementRatConversion(const RingElement *f, mpz_srcptr m, const Ring *RQ)
Definition cra.cpp:94
const Matrix * rawMatrixRatConversion(const Matrix *f, mpz_srcptr m, const Ring *RQ)
Definition cra.cpp:135
const Matrix * rawMatrixCRA(const Matrix *f, const Matrix *g, mpz_srcptr m, mpz_srcptr n)
Definition cra.cpp:61
const int ERROR
Definition m2-mem.cpp:55
VALGRIND_MAKE_MEM_DEFINED & result(result)
MatrixConstructor — the mutable builder that produces an immutable Matrix.
Matrix — the engine's immutable homomorphism F -> G between free modules.
Monoid — variable count, naming, grading, and monomial order of a polynomial ring.
Concrete commutative PolyRing — standard polynomial ring inheriting from PolyRingFlat.
RingElement — tagged (Ring*, ring_elem) pair, the engine's universal element type.
tbb::flow::graph G
Ring — the legacy abstract base class for every coefficient and polynomial ring.
ring_elem — the universal value type carried by every Ring* in the engine.