Macaulay2 Engine
Loading...
Searching...
No Matches
matrix-stream.hpp
Go to the documentation of this file.
1// Copyright 2013 Michael E. Stillman
2
3#ifndef __matrix_stream_hhp__
4#define __matrix_stream_hhp__
5
44
45#include "poly.hpp"
46#include "matrix.hpp"
47#include "matrix-con.hpp"
48#include <gmpxx.h>
49
50#if 0
51// Example of the calls used to create an ideal (one rowed matrix), over a poly ring
52// over a prime finite field.
53// After this. s.value() returns the Matrix with these elements as columns.
54 s.idealBegin(2);
55 s.appendPolynomialBegin(2); // x^2 - y
56 s.appendTermBegin(0);
57 s.appendExponent(0,2);
58 s.appendTermDone(1);
59 s.appendTermBegin(0);
60 s.appendExponent(1,1);
61 s.appendTermDone(s.modulus() - 1);
62 s.appendPolynomialDone();
63 s.appendPolynomialBegin(2); // x^3-z
64 s.appendTermBegin(0);
65 s.appendExponent(0,3);
66 s.appendTermDone(1);
67 s.appendTermBegin(0);
68 s.appendExponent(2,1);
69 s.appendTermDone(s.modulus() - 1);
70 s.appendPolynomialDone();
71 s.idealDone();
72#endif
73
89{
90 public:
91 MatrixStream(const FreeModule* F);
93
94 const PolyRing& ring() const { return *mPolyRing; }
95 const Matrix* value() const
96 {
97 return mValue;
98 } // This will return null before idealDone() is called.
99
100 // Fields required for the general stream interface (see mathicgb::mathicgb.h)
101 using Coefficient = mpz_class;
102 // typedef long Coefficient;
103 // typedef size_t VarIndex;
104 typedef int VarIndex;
105 typedef int Exponent;
106 //typedef unsigned int Component;
107 typedef int Component;
108
110 {
111 return static_cast<int>(mPolyRing->characteristic());
112 }
113 VarIndex varCount() const { return mPolyRing->n_vars(); }
114 Component comCount() const { return mFreeModule->rank(); }
115
116 void idealBegin(size_t polyCount);
117 void appendPolynomialBegin(size_t termCount);
118 void appendTermBegin(Component com);
119
120 void appendExponent(VarIndex index, Exponent exponent);
121 void appendTermDone(Coefficient coefficient);
123 void idealDone();
124
125 private:
128
133
134 Nterm** mCurrentColumn; // array 0..numcomps-1
136};
137
138template <typename T>
139void matrixToStream(const Matrix* M, T& stream)
140{
141 const Ring* R = M->get_ring();
142 const PolyRing* P = R->cast_to_PolyRing();
143 assert(P != 0);
144 const Ring* KK = P->getCoefficientRing();
145 size_t nvars = P->n_vars();
146 size_t ncols = M->n_cols();
147 int charac = static_cast<int>(P->characteristic());
148 assert(charac > 0);
149 exponents_t exp =
150 ALLOCATE_EXPONENTS(EXPONENT_BYTE_SIZE(nvars)); // allocated on stack
151 stream.idealBegin(ncols);
152 Matrix::iterator i(M);
153 for (int c = 0; c < ncols; c++)
154 {
155 i.set(c);
156 // We need the length of this column, in number of monomials
157 size_t nterms = 0;
158 for (; i.valid(); i.next())
159 {
160 Nterm* t = i.entry();
161 for ([[maybe_unused]] Nterm& s : t) nterms++;
162 }
163 stream.appendPolynomialBegin(nterms);
164
165 i.set(c);
166 // Now we process each column, sending it to the stream
167 for (; i.valid(); i.next())
168 {
169 Nterm* t = i.entry();
170 for (Nterm& s : t)
171 {
172 P->getMonoid()->to_expvector(s.monom, exp);
173 stream.appendTermBegin(i.row());
174 for (size_t j = 0; j < nvars; j++)
175 if (exp[j] != 0) stream.appendExponent(j, exp[j]);
176 std::pair<bool, long> b = KK->coerceToLongInteger(s.coeff);
177 assert(b.first);
178 int a = static_cast<int>(
179 b.second); // This will fit, as the charac fits into an int
180 if (a < 0) a += charac;
181 stream.appendTermDone(a);
182 }
183 }
184 stream.appendPolynomialDone();
185 }
186 stream.idealDone();
187}
188
189#endif
190
191// Local Variables:
192// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
193// indent-tabs-mode: nil
194// End:
exponents::Exponents exponents_t
Engine-side free module R^n over a Ring.
Definition freemod.hpp:66
void set(int newcol)
Definition matrix.hpp:312
ring_elem entry()
Definition matrix.hpp:320
const Ring * get_ring() const
Definition matrix.hpp:134
int n_cols() const
Definition matrix.hpp:147
Mutable builder used to assemble an immutable Matrix one column (or one term) at a time.
Coefficient modulus() const
void idealBegin(size_t polyCount)
VarIndex varCount() const
void appendTermDone(Coefficient coefficient)
Exponent * mCurrentExponents
mpz_class Coefficient
MatrixConstructor mMatrixConstructor
Component comCount() const
const Matrix * mValue
void appendTermBegin(Component com)
void appendExponent(VarIndex index, Exponent exponent)
const PolyRing * mPolyRing
const FreeModule * mFreeModule
Component mCurrentComponent
const PolyRing & ring() const
Nterm ** mCurrentColumn
void appendPolynomialBegin(size_t termCount)
MatrixStream(const FreeModule *F)
Nterm ** mLastTerms
void appendPolynomialDone()
const Matrix * value() const
void to_expvector(const_monomial m, exponents_t result_exp) const
Definition monoid.cpp:747
Concrete PolyRingFlat subclass implementing ordinary commutative polynomial rings K[x_1,...
Definition poly.hpp:64
const Ring * getCoefficientRing() const
Definition polyring.hpp:200
virtual const Monoid * getMonoid() const
Definition polyring.hpp:282
int n_vars() const
Definition polyring.hpp:196
virtual std::pair< bool, long > coerceToLongInteger(ring_elem a) const
Definition ring.cpp:236
virtual const PolyRing * cast_to_PolyRing() const
Definition ring.hpp:245
long characteristic() const
Definition ring.hpp:159
xxx xxx xxx
Definition ring.hpp:102
int * exponent
Definition exptable.h:34
#define Matrix
Definition factory.cpp:14
void size_t s
Definition m2-mem.cpp:271
MatrixConstructor — the mutable builder that produces an immutable Matrix.
void matrixToStream(const Matrix *M, T &stream)
Matrix — the engine's immutable homomorphism F -> G between free modules.
#define ALLOCATE_EXPONENTS(byte_len)
Definition monoid.hpp:62
#define EXPONENT_BYTE_SIZE(nvars)
Definition monoid.hpp:63
Concrete commutative PolyRing — standard polynomial ring inheriting from PolyRingFlat.
Singly linked-list node carrying one term of a polynomial-ring element.
Definition ringelem.hpp:156
#define T
Definition table.c:13