Macaulay2 Engine
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1// Copyright 1995 Michael E. Stillman
2
3#ifndef _matrix_hh_
4#define _matrix_hh_
5
40
41#include "monoid.hpp"
42#include "freemod.hpp"
43#include "monideal.hpp"
44
45#include <vector>
46
48
66class Matrix : public EngineObject
67{
68 FreeModule * mTarget; // _rows;
69 FreeModule * mSource; // _cols;
70 monomial mDegreeShift; // an element of the degree monoid
72
73 friend class FreeModule;
74
75 private:
76 friend class MatrixConstructor;
77 Matrix(const FreeModule *rows,
78 const FreeModule *cols,
80 VECTOR(vec) & entries);
81
82 static bool make_sparse_vecs(MatrixConstructor &mat,
83 const FreeModule *target,
84 int ncols,
87 const engine_RawRingElementArray entries);
88 // returns false if an error, true otherwise.
89 // Places the elements into 'mat'.
90
91 // These two routines are private to 'coeffs'
92 // vec strip_vector(vec &f,
93 // const int *vars,
94 // const FreeModule *F,
95 // vec &vmonom) const;
96 // int moneq(const int *exp, int *m, const int *vars, int *exp2) const;
97
98 protected:
99 virtual unsigned int computeHashValue() const;
100
101 public:
102 static const Matrix /* or null */ *make(const FreeModule *target,
103 int ncols,
104 const engine_RawRingElementArray M);
105
106 static const Matrix /* or null */ *make(const FreeModule *target,
107 const FreeModule *source,
108 M2_arrayint deg,
109 const engine_RawRingElementArray M);
110
111 static const Matrix /* or null */ *make_sparse(
112 const FreeModule *target,
113 int ncols,
116 const engine_RawRingElementArray entries);
117
118 static const Matrix /* or null */ *make_sparse(
119 const FreeModule *target,
120 const FreeModule *source,
121 M2_arrayint deg,
124 const engine_RawRingElementArray entries);
125
126 const Matrix /* or null */ *remake(const FreeModule *target,
127 const FreeModule *source,
128 M2_arrayint deg) const;
129
130 const Matrix /* or null */ *remake(const FreeModule *target) const;
131
132 static const Matrix *make(const MonomialIdeal *mi);
133
134 const Ring *get_ring() const { return rows()->get_ring(); }
135 /* The following 5 routines will go away, or change name */
136 vec &operator[](int i) { return mEntries[i]; }
137 const vec &operator[](int i) const { return mEntries[i]; }
138 ring_elem elem(int i, int j) const;
139 vec &elem(int i) { return mEntries[i]; }
140 const vec &elem(int i) const { return mEntries[i]; }
141 /*****************************************/
142
143 /* The non-const versions of these will go away */
144 const FreeModule *rows() const { return mTarget; }
145 const FreeModule *cols() const { return mSource; }
146 int n_rows() const { return rows()->rank(); }
147 int n_cols() const { return cols()->rank(); }
148 // The degree shift
150 // to/from monideals
151 MonomialIdeal *make_monideal(
152 int n,
153 bool use_only_monomials_with_unit_coeffs = false) const;
154
155 // matrices over RRR, CCC
156 Matrix /* or null */ *clean(gmp_RR epsilon) const;
157 gmp_RRorNull norm(gmp_RR p) const;
158
159 // Matrix operations
160 Matrix /* or null */ *sub_matrix(M2_arrayint r, M2_arrayint c) const;
161 Matrix /* or null */ *sub_matrix(M2_arrayint c) const;
162 Matrix *transpose() const;
163 Matrix *operator+(const Matrix &m) const;
164 Matrix *operator-() const;
165 Matrix *operator-(const Matrix &m) const;
166 Matrix *scalar_mult(const ring_elem r, bool opposite_mult) const;
167 Matrix *mult(const Matrix *m, bool opposite_mult) const;
168 Matrix *concat(const Matrix &m) const;
169
170 static Matrix *identity(const FreeModule *F);
171 static Matrix /* or null */ *zero(const FreeModule *F, const FreeModule *G);
172
173 Matrix /* or null */ *koszul(int p) const;
174 static Matrix /* or null */ *koszul(const Matrix *rows, const Matrix *cols);
175 static Matrix /* or null */ *koszul_monomials(int nskew,
176 const Matrix *rows,
177 const Matrix *cols);
178
179 Matrix /* or null */ *reshape(const FreeModule *G, const FreeModule *H) const;
180 static Matrix /* or null */ *flip(const FreeModule *G, const FreeModule *H);
181
182 Matrix /* or null */ *direct_sum(const Matrix *m) const;
183 Matrix /* or null */ *module_tensor(const Matrix *m) const;
184 Matrix /* or null */ *tensor(const Matrix *m) const;
185 Matrix /* or null */ *diff(const Matrix *m, int use_coef) const;
186
187 Matrix /* or null */ *symm(int n) const; // in symm.cpp
188
189 // Matrix /* or null */ *coeffs(const int *vars, Matrix *&result_monoms) const;
190
191 Matrix /* or null */ *coeffs(M2_arrayint vars, const Matrix *monoms) const;
192
193 Matrix /* or null */ *monomials(M2_arrayint vars) const;
194
195 M2_arrayintOrNull support() const;
196 // gives error if not a polynomial ring, other wise the array of indices
197 // appearing in this.
198
199 Matrix *top_coefficients(Matrix *&monoms) const;
200
201 const Matrix /* or null */ *basis(M2_arrayint lo_degree,
202 M2_arrayint hi_degree,
203 M2_arrayint wt,
204 M2_arrayint vars,
205 bool do_truncation,
206 int limit) const;
207
208 Matrix *exterior(int p, int strategy) const;
209 Matrix *minors(int p, int strategy) const;
210 Matrix /* or null */ *minors(int p,
211 int strategy,
212 int n_to_compute, // -1 means all
213 M2_arrayintOrNull first_row, // possibly NULL
214 M2_arrayintOrNull first_col // possibly NULL
215 ) const;
216
217 // in pfaff.cpp
218 Matrix *pfaffians(int p) const;
219 ring_elem pfaffian() const;
220
221 static Matrix *wedge_product(int p, int q, const FreeModule *F);
222 // static Matrix wedge_dual(int p, const FreeModule *F);
223
224 // equality, zero
225 bool is_equal(const Matrix &m) const;
226 bool is_zero() const;
227
228 // degrees
229 bool is_homogeneous() const;
230 Matrix *homogenize(int v, const std::vector<int> &wts) const;
231
232 // Simplification of column set
233 Matrix *simplify(int n) const;
235 const; // An error is given, if there are two lead terms
236 // one which divides another.
237
238 // Sorting the columns of the matrix (new positions into 'result')
239 // void sort(int degorder, int monorder, intarray &result) const;
240 M2_arrayint sort(int degorder, int monorder) const;
241
242 // Matrix selection
243 Matrix *lead_term(int n = -1) const; // Select those monomials in each column
244 // which are maximal in the order under
245 // the first n weight vectors
246 // If n is -1, then the flat lead terms are returned,
247 // If n is > 0, then the first n parts of the monomial order are used.
248 // HOWEVER: in this case, the Schreyer order is not used: the usual order
249 // of the ring (for free modules) is used.
250
251 // Module operations
252 int dimension1() const; // Compute the dimension of the quotient of the
253 // submodule generated by the lead terms of the
254 // columns of the matrix, modulo any lead terms of the
255 // presentation ideal of the ring of m.
256 // Over ZZ, this gives the dimension over QQ.
257
258 // See engine.h for the definition of 'content' here
259 const Matrix /* or null */ *content() const;
260 const Matrix /* or null */ *remove_content() const;
261 const Matrix /* or null */ *split_off_content(
262 const Matrix /* or null */ *&result) const;
263
264 private:
266
267 public:
269
270 M2_arrayint elim_vars(int nparts) const;
271 M2_arrayint elim_keep(int nparts) const;
272 Matrix *divide_by_var(int n, int maxd, int &maxdivided)
273 const; // maxd<0 means divide by as much as possible
274
275 Matrix *compress() const; // Remove zero columns
276 Matrix *remove_monomial_factors(bool make_squarefree_only) const;
278
279 static Matrix *random(const Ring *R, int r, int c);
280
281 static Matrix *random(
282 const Ring *R,
283 int r,
284 int c,
285 double fraction_non_zero,
286 int special_type); // 0: general, 1:upper triangular, others?
287
288 void text_out(buffer &o) const;
289
303 {
304 const Matrix *M; // all matrices are immutable
305 int col;
306 vec v;
307
308 public:
309 // iterator(const Matrix *M0, int col0=0) : M(M0), col(col0),
310 // v(M0->elem(col0)) {}
311 iterator(const Matrix *M0) : M(M0), col(-1), v(nullptr) {}
312 void set(int newcol)
313 {
314 col = newcol;
315 v = M->elem(col);
316 }
317 void next() { v = v->next; }
318 bool valid() { return v != nullptr; }
319 int row() { return v->comp; }
320 ring_elem entry() { return v->coeff; }
321 };
322
336 {
337 const vecterm * v;
338 public:
339 column_iterator(const Matrix *M, int c) : v(M->elem(c)) {}
340 column_iterator(const Matrix *M) : v(nullptr) { (void) M; }
341
342 column_iterator& operator++() { v = v->next; return *this; }
343 const vecterm* operator *() { return v; }
344 bool operator!=(const column_iterator b) { return v != b.v; }
345 };
346};
347
348#endif
349
350// Local Variables:
351// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
352// indent-tabs-mode: nil
353// End:
EngineObject()
Definition hash.hpp:66
Engine-side free module R^n over a Ring.
Definition freemod.hpp:66
column_iterator(const Matrix *M, int c)
Definition matrix.hpp:339
column_iterator(const Matrix *M)
Definition matrix.hpp:340
column_iterator & operator++()
Definition matrix.hpp:342
const vecterm * v
Definition matrix.hpp:337
bool operator!=(const column_iterator b)
Definition matrix.hpp:344
const Matrix * M
Definition matrix.hpp:304
void set(int newcol)
Definition matrix.hpp:312
ring_elem entry()
Definition matrix.hpp:320
iterator(const Matrix *M0)
Definition matrix.hpp:311
friend class FreeModule
Definition matrix.hpp:73
static const Matrix * make_sparse(const FreeModule *target, int ncols, M2_arrayint rows, M2_arrayint cols, const engine_RawRingElementArray entries)
Definition matrix.cpp:191
const_monomial degree_shift() const
Definition matrix.hpp:149
const Ring * get_ring() const
Definition matrix.hpp:134
ring_elem elem(int i, int j) const
Definition matrix.cpp:307
Matrix * remove_monomial_factors(bool make_squarefree_only) const
Definition matrix.cpp:1462
monomial mDegreeShift
Definition matrix.hpp:70
const vec & operator[](int i) const
Definition matrix.hpp:137
gc_vector< vec > mEntries
Definition matrix.hpp:71
Matrix * simplify(int n) const
Matrix * auto_reduce() const
static const Matrix * make(const FreeModule *target, int ncols, const engine_RawRingElementArray M)
Definition matrix.cpp:60
const Matrix * remake(const FreeModule *target, const FreeModule *source, M2_arrayint deg) const
Definition matrix.cpp:228
const Matrix * remove_content() const
Definition matrix.cpp:2034
friend class MatrixConstructor
Definition matrix.hpp:76
FreeModule * mSource
Definition matrix.hpp:69
int n_cols() const
Definition matrix.hpp:147
Matrix(const FreeModule *rows, const FreeModule *cols, const_monomial degree_shift, VECTOR(vec) &entries)
Definition matrix.cpp:32
M2_arrayint elim_keep(int nparts) const
Definition matrix.cpp:1047
const vec & elem(int i) const
Definition matrix.hpp:140
virtual unsigned int computeHashValue() const
Definition matrix.cpp:43
int n_rows() const
Definition matrix.hpp:146
const Matrix * split_off_content(const Matrix *&result) const
Definition matrix.cpp:2043
static Matrix * random(const Ring *R, int r, int c)
const FreeModule * rows() const
Definition matrix.hpp:144
void minimal_lead_terms_ZZ(gc_vector< int > &result) const
void minimal_lead_terms(gc_vector< int > &result) const
Matrix * lead_term(int n=-1) const
Definition matrix.cpp:840
M2_arrayint sort(int degorder, int monorder) const
void text_out(buffer &o) const
Definition matrix.cpp:1316
const FreeModule * cols() const
Definition matrix.hpp:145
vec & operator[](int i)
Definition matrix.hpp:136
Matrix * compress() const
Definition matrix.cpp:1347
Matrix * divide_by_var(int n, int maxd, int &maxdivided) const
Definition matrix.cpp:1062
int dimension1() const
Definition matrix.cpp:1978
M2_arrayint elim_vars(int nparts) const
Definition matrix.cpp:1032
const Matrix * content() const
Definition matrix.cpp:2021
FreeModule * mTarget
Definition matrix.hpp:68
Matrix * remove_scalar_multiples() const
Definition matrix.cpp:1437
static bool make_sparse_vecs(MatrixConstructor &mat, const FreeModule *target, int ncols, M2_arrayint rows, M2_arrayint cols, const engine_RawRingElementArray entries)
Definition matrix.cpp:143
vec & elem(int i)
Definition matrix.hpp:139
Mutable builder used to assemble an immutable Matrix one column (or one term) at a time.
Engine-side monomial ideal: a decision tree of Nmi_nodes storing the (typically minimal) generators b...
Definition monideal.hpp:136
xxx xxx xxx
Definition ring.hpp:102
#define Matrix
Definition factory.cpp:14
std::list< brMonomial > monomials
FreeModule — finite-rank free module R^n, the type-level anchor for every Matrix.
#define monomial
Definition gb-toric.cpp:11
int p
int zero
const int * const_monomial
Definition imonorder.hpp:45
VALGRIND_MAKE_MEM_DEFINED & result(result)
mpfr_srcptr gmp_RR
Definition m2-types.h:148
M2_arrayint M2_arrayintOrNull
Definition m2-types.h:99
mpfr_srcptr gmp_RRorNull
Definition m2-types.h:149
MonomialIdeal — exponent-vector-only representation of an ideal generated by monomials.
Monoid — variable count, naming, grading, and monomial order of a polynomial ring.
typename std::vector< T, gc_allocator< T > > gc_vector
a version of the STL vector, which allocates its backing memory with gc.
Definition newdelete.hpp:76
#define VECTOR(T)
Definition newdelete.hpp:78
tbb::flow::graph G