Macaulay2 Engine
Loading...
Searching...
No Matches
mutable-matrix.h
Go to the documentation of this file.
1#ifndef _mutable_matrix_h_
2# define _mutable_matrix_h_
3
47
48# include "engine-includes.hpp"
49
50// TODO: fix this
51# if defined(__cplusplus)
52class Matrix;
53class MutableMatrix;
54class Ring;
55class RingElement;
56# else
57typedef struct Matrix Matrix;
58typedef struct MutableMatrix MutableMatrix;
59typedef struct Ring Ring;
60typedef struct RingElement RingElement;
61# endif
62
73
74# if defined(__cplusplus)
75extern "C" {
76# endif
77
78/**************************************************/
79/**** MutableMatrix routines **********************/
80/**************************************************/
81
83 int nrows,
84 M2_bool prefer_dense);
85/* drg: connected rawMutableIdentity, OK*/
86
88 int nrows,
89 int ncols,
90 M2_bool prefer_dense);
91/* drg: connected rawMutableMatrix, OK */
92
94 M2_bool prefer_dense);
95/* drg: connected rawMutableMatrix, OK*/
96
98/* drg: connected rawMatrix, OK*/
99
101/* drg: connected toString, OK */
102
103unsigned int rawMutableMatrixHash(const MutableMatrix *M);
104/* drg: connected to "hash" */
105
107/* drg: connected rawNumberOfRows, OK */
108
110/* drg: connected rawNumberOfColumns, OK */
111
113 double density,
114 int special_type);
115/* drg: connected rawMutableMatrixFillRandom */
116/* special_type: 0 is general, 1 is (strictly) upper triangular. */
117
118void rawMutableMatrixFillRandom(MutableMatrix *M, long nelems);
119/* drg: connected rawMutableMatrixFillRandom */
120
121MutableMatrix /* or null */ *rawMutableMatrixPromote(const Ring *R,
122 const MutableMatrix *f);
123/* connected to rawPromote*/
124
125MutableMatrix /* or null */ *rawMutableMatrixLift(int *success_return,
126 const Ring *R,
127 const MutableMatrix *f);
128/* connected to rawLift */
129// returns null if lifting not possible
130
131const RingElement /* or null */ *
132IM2_MutableMatrix_get_entry(const MutableMatrix *M, int r, int c);
133/* drg: connected rawMatrixEntry, OK*/
134
137
138/* Each of these routines returns false if there was an error. */
139
141 int r,
142 int c,
143 const RingElement *a);
144/* drg: connected rawSetMatrixEntry, OK */
145
147/* drg: connected rawMatrixRowSwap, OK */
148
150/* drg: connected rawMatrixColSwap, OK*/
151
153 int i,
154 const RingElement *r,
155 int j,
156 M2_bool opposite_mult);
157/* drg: connected rawMatrixRowChange, OK */
158/* row(i) <- row(i) + r * row(j), returns false if matrix is
159 immutable, or rows are out of bounds */
160
162 int i,
163 const RingElement *r,
164 int j,
165 M2_bool opposite_mult);
166/* drg: connected rawMatrixColChange, OK*/
167/* column(i) <- column(i) + r * column(j), returns false if matrix is
168 immutable, or columns are out of bounds */
169
171 const RingElement *r,
172 int i,
173 M2_bool opposite_mult);
174/* drg: connected rawMatrixRowScale, OK*/
175/* row(i) <- r * row(i), returns false if matrix is immutable
176 or row is out of bounds */
177
179 const RingElement *r,
180 int i,
181 M2_bool opposite_mult);
182/* drg: connected rawMatrixColumnScale, OK */
183/* column(i) <- r * column(i), returns false if matrix is immutable
184 or row is out of bounds */
185
187/* connected to rawInsertColumns, OK */
188/* Insert n_to_add columns directly BEFORE column i. */
189
191/* connected to rawInsertRows, OK */
192/* Insert n_to_add rows directly BEFORE row i. */
193
195/* connected to rawDeleteColumns, OK */
196/* Delete columns i .. j from M */
197
199/* connected to rawDeleteRows, OK */
200/* Delete rows i .. j from M */
201
203 int c1,
204 int c2,
205 const RingElement *a1,
206 const RingElement *a2,
207 const RingElement *b1,
208 const RingElement *b2,
209 M2_bool opposite_mult);
210/* connected to rawMatrixColumnOperation2, OK */
211/* column(c1) <- a1 * column(c1) + a2 * column(c2)
212 column(c2) <- b1 * column(c1) + b2 * column(c2)
213*/
214
216 int r1,
217 int r2,
218 const RingElement *a1,
219 const RingElement *a2,
220 const RingElement *b1,
221 const RingElement *b2,
222 M2_bool opposite_mult);
223/* connected to rawMatrixRowOperation2, OK */
224/* row(r1) <- a1 * row(r1) + a2 * row(r2)
225 row(r2) <- b1 * row(r1) + b2 * row(r2)
226*/
227
229/* connected to rawSortColumns2, OK */
230/* Returns false if M is not mutable, or lo, or hi are out of range */
231
233 int start,
234 M2_arrayint perm);
235/* connected to rawPermuteRows, OK */
236/* if perm = [p0 .. pr], then row(start + i) --> row(start + pi), and
237 all other rows are unchanged. p0 .. pr should be a permutation of 0..r */
238
240 int start,
241 M2_arrayint perm);
242/* connected to rawPermuteColumns, OK */
243/* if perm = [p0 .. pr], then column(start + i) --> column(start + pi), and
244 all other rows are unchanged. p0 .. pr should be a permutation of 0..r */
245
247 int c1,
248 int c2);
249/* connected to rawColumnDotProduct */
250/* Return the dot product of columns c1 and c2 of the matrix M. If either c1 or
251 c2 is out of range, 0 is returned. */
252
257
259/* drg: connected rawIsZero, OK */
260
262 const MutableMatrix *N);
263/* drg: connected to rawIsEqual for use with ==, not connected to '===', OK */
264/* This checks that the entries of M,N are the same */
265
267/* connected to rawMutableMatrix, OK */
268
270 M2_arrayint rows,
271 M2_arrayint cols,
272 engine_RawRingElementArray values);
273/* connected to rawSetMatrixValues, OK */
274/* Given three arrays of the same length, 'rows', 'cols', 'values', set the
275 corresponding values of M. If any elements are out of range, ignore those
276 triples. If the type of ring element is not valid, or the sizes of the
277 matrices do not match, return false. */
278
280 M2_arrayint rows,
281 M2_arrayint cols);
282/* drg: connected rawSubmatrix, OK */
283
285 const MutableMatrix *M,
286 M2_arrayint cols);
287/* drg: connected rawSubmatrix, OK */
288
290/* connected rawReduceByPivots */
291/* Using row and column operations, use unit pivots to reduce the matrix */
292/* A return value of false means that the computation was interrupted */
293
296
297/**************************************************/
298/**** Fraction free LU decomposition **************/
299/**************************************************/
300
302/* connected to rawFFLU */
303/* Replace M by a column echelon form. No fractions are generated, but the
304 base ring should be a domain.
305 If M has a column change of basis matrix attached, it will be modified
306 accordingly.
307*/
308
309/**************************************************/
310/**** LLL bases ***********************************/
311/**************************************************/
312
314 MutableMatrix /* or null */ *U,
315 gmp_QQ threshold,
316 int strategy);
317/* DAN: connected to rawLLL */
318/* Given a mutable matrix M over ZZ, and a rational number threshold, 1/4 <
319 threshold <= 1, modify M so that the columns form a Lenstra-Lenstra-Lovasz
320 basis of the image of (the original) M. ASSUMPTION: (strategy=0 case)
321 the columns of M are already a a basis for the lattice.
322 The algorithm used is that in Cohen's book on computational algebraic
323 number theory, BUT: beware of the typos in the algorithm! If there is any
324 error (interrupted, M or threshold not the correct kind), then false is
325 returned, and LLL is set to 0. If M has a column change of basis matrix
326 attached, it will be modified accordingly.
327
328 strategy: 0 means use original Macaulay2 engine routine.
329 2 means use NTL LLL
330 (strategy%3) == 3 means use one of the real number variants:
331 GramSchmidt or Givens: 0 or 4 (respectively)
332 LLL or BKZ: 0 or 8 (respectively)
333 FP, QP1, QP, XD, RR (respectively: 0, 16, 2*16, 3*16, 4*16
334 Thus: strategy 3+4+8+16 means use NTL's GBKZ_QP1.
335
336 For the RR variants, the suggested value of the threshold is 99/100.
337
338 If U is not NULL, then it should be an m by m matrix over ZZ, where m is the
339 number of columns of the matrix M. In this case, it is set to be the
340 invertible transform matrix such that Mold * U = Mnew.
341*/
342
344/* connected rawSmithNormalForm */
345/* Given a mutable matrix over ZZ, compute the Smith normal form for M.
346 (replaces M with this normal form. Currently the algorithm used makes
347 computing the change of basis matrices difficult (we use mod D arithmetic,
348 where D is the determinant). If there is an error, then an error is flagged
349 and false is returned.
350*/
351
353/* connect rawHermiteNormalForm */
354/* Given a mutable matrix over ZZ, compute the Hermite normal form for M.
355 (replaces M with this normal form. Currently the algorithm used makes
356 computing the change of basis matrices difficult (we use mod D arithmetic,
357 where D is the determinant). If there is an error, then an error is flagged
358 and false is returned.
359*/
360
361/***************************************************
362 ***** Lapack routines for dense mutable matrices **
363 ***************************************************/
364
365/* Each of the following routines accepts honest MutableMatrix arguments,
366 and returns false if there is an error. The return values are placed into
367 some of the (already existing) parameters of the routine */
368
370 MutableMatrix *L,
371 MutableMatrix *U);
372/* connected */
373/* Returns the permutation array: we need to be more precise which one.
374 A encodes both the L and the U part, as in lapack.
375 */
376
389 MutableMatrix *LU, /* modified in routine */
390 const MutableMatrix *v, /* constant */
391 int m);
392
393void rawTriangularSolve(MutableMatrix *Lv, /* modified in routine */
394 MutableMatrix *x, /* modified in routine */
395 int m,
396 int strategy);
397
399 MutableMatrix *eigenvalues,
400 M2_bool isHermitian); /* connected */
401/*
402 */
403
405 MutableMatrix *eigenvalues,
406 MutableMatrix *eigenvectors,
407 M2_bool isHermitian); /* connected */
408/*
409 */
410
412 MutableMatrix *Sigma,
413 MutableMatrix *U,
414 MutableMatrix *VT,
415 M2_bool use_divide_and_conquer); /* connected */
416/*
417 */
418
420 MutableMatrix *b,
421 MutableMatrix *x, /* return value: argument modified */
422 M2_bool assume_full_rank); /* connected */
423/* Case 1: A is a dense matrix over RR. Then so are b,x.
424 Case 2: A is a dense matrix over CC. Then so are b,x. */
425
426M2_bool rawQR(const MutableMatrix *A, /* input m x n matrix */
427 MutableMatrix *Q, /* output m x n orthonormal columns matrix */
428 MutableMatrix *R, /* output R matrix: upper triangular,
429 nonsingular if A has ker A = 0 */
430 M2_bool return_QR); /* if false, the output is instead the lapack
431 encoded Householder transformations */
432/* if return_QR is false, then Q will contain the encoded Householder
433 reflections and the multipliers tau_i will appear in R. MES TODO: be more
434 specific here, once we know the exact format!
435*/
436
437/********************************/
438/* Fast linear algebra routines */
439/********************************/
440
446
452
454
462
464
466
475 const MutableMatrix *B,
476 int *success);
477
486 const MutableMatrix *B,
487 int *success);
488
495
500 const MutableMatrix *A,
501 const MutableMatrix *B);
502
507 const MutableMatrix *A,
508 const MutableMatrix *B);
509
510/* return A*B, where A,B are mutable matrices, over same ring, same density
511 * type.
512 */
513MutableMatrix * /* or null */ rawLinAlgMult(const MutableMatrix *A,
514 const MutableMatrix *B);
515
517// returns an array whose coefficients give the characteristic polynomial of the
518// square matrix A
519
521// returns an array whose coefficients give the minimal polynomial of the square
522// matrix A
523
524# if 0
525RingElement *rawFFPackDeterminant(MutableMatrix *M);
526/* connected to rawFFPackDeterminant, MES */
527/* requires: M should be a square matrix over a prime finite field */
528
529size_t rawFFPackRank(MutableMatrix *M);
530/* connected to rawFFPackRank, MES */
531/* requires: M should be a matrix over a prime finite field */
532
533MutableMatrix /* Or Null */ *rawFFPackNullSpace(MutableMatrix *M,
534 M2_bool right_side);
535/* connected to rawFFPackNullSpace, MES */
536/* requires: M should be a matrix over a prime finite field */
537/* computes either left or right nullspace */
538
539MutableMatrix /* or null */ *rawFFPackSolve(MutableMatrix *A,
540 MutableMatrix *B,
541 M2_bool right_side);
542/* connected to rawFFPackSolve, MES */
543/* requires: M should be a matrix over a prime finite field */
544/* returns solution of AX=B or XA=B, depending on right_side */
545
546MutableMatrix /* or null */ *rawFFPackInvert(MutableMatrix *M);
547/* connected to rawFFPackInvert, MES */
548/* requires: M should be a square matrix over a prime finite field */
549
550MutableMatrix /* or null */ *rawFFPackAddMultipleTo(MutableMatrix *C,
551 const MutableMatrix *A,
552 const MutableMatrix *B,
553 M2_bool transposeA,
554 M2_bool transposeB,
555 const RingElement *a,
556 const RingElement *b);
557/* A,B,C should be mutable matrices over a finite prime field, and a,b
558 elements of this field.
559 C = b*C + a * op(A)*op(B),
560 where op(A) = A or transpose(A), depending on transposeA
561 where op(B) = B or transpose(B), depending on transposeB
562 connected to rawFFPackAddMultipleTo, MES
563*/
564
565M2_arrayintOrNull rawFFPackRowRankProfile(MutableMatrix *A);
566/* connected, MES */
567
568M2_arrayintOrNull rawFFPackColumnRankProfile(MutableMatrix *A);
569/* connected, MES */
570# endif
571
573
574/**************************************************/
575/**** Special routines for objects over RRR,CCC ***/
576/**************************************************/
577
578// FIXME: move these elsewhere
579
580/* The code for these is in x-mutablemat.cpp */
581
582/* These routines set any real or complex numbers whose absolute value is less
583 than epsilon. If the ring is not over RRR or CCC, then an error message is
584 given, and NULL is returned. */
585
586const Matrix /* or null */ *rawMatrixClean(gmp_RR epsilon, const Matrix *M);
587const RingElement /* or null */ *rawRingElementClean(gmp_RR epsilon,
588 const RingElement *f);
589MutableMatrix /* or null */ *rawMutableMatrixClean(gmp_RR epsilon,
590 MutableMatrix *M);
591/* modifies M in place */
592
593/* p is currently limited to infinity (with a given precision), and this routine
594 returns the maximum norm of any RRR or CCC coefficient in the object.
595 If the ring is not over RRR or CCC, then an error message is given, and NULL
596 is returned */
597
601
602# if defined(__cplusplus)
603}
604# endif
605
606#endif /* _mutable-matrix_h_ */
607
608// Local Variables:
609// indent-tabs-mode: nil
610// End:
Abstract base class for mutable matrices over an arbitrary engine Ring, the in-place counterpart of t...
Definition mat.hpp:79
const Ring * R
Definition relem.hpp:68
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-wide include prelude — a single point of truth for portability shims.
#define Matrix
Definition factory.cpp:14
int p
mpfr_srcptr gmp_RR
Definition m2-types.h:148
M2_arrayint M2_arrayintOrNull
Definition m2-types.h:99
engine_RawRingElementArray engine_RawRingElementArrayOrNull
Definition m2-types.h:176
engine_RawArrayIntPair engine_RawArrayIntPairOrNull
Definition m2-types.h:188
mpfr_srcptr gmp_RRorNull
Definition m2-types.h:149
char M2_bool
Definition m2-types.h:82
mpq_srcptr gmp_QQ
Definition m2-types.h:145
engine_RawRingElementArrayArray engine_RawRingElementArrayArrayOrNull
Definition m2-types.h:180
M2_bool rawMutableMatrixIsDense(const MutableMatrix *M)
MutableMatrix * IM2_MutableMatrix_from_matrix(const Matrix *M, M2_bool prefer_dense)
const RingElement * IM2_MutableMatrix_get_entry(const MutableMatrix *M, int r, int c)
engine_RawRingElementArrayOrNull rawLinAlgMinPoly(MutableMatrix *A)
M2_bool IM2_MutableMatrix_insert_columns(MutableMatrix *M, int i, int n_to_add)
M2_string IM2_MutableMatrix_to_string(const MutableMatrix *M)
MutableMatrix * rawMutableMatrixClean(gmp_RR epsilon, MutableMatrix *M)
const RingElement * rawRingElementClean(gmp_RR epsilon, const RingElement *f)
M2_bool IM2_MutableMatrix_set_entry(MutableMatrix *M, int r, int c, const RingElement *a)
long rawLinAlgRank(MutableMatrix *M)
M2_bool IM2_MutableMatrix_sort_columns(MutableMatrix *M, int lo, int hi)
M2_bool IM2_MutableMatrix_column_scale(MutableMatrix *M, const RingElement *r, int i, M2_bool opposite_mult)
M2_bool IM2_SmithNormalForm(MutableMatrix *M)
M2_bool IM2_MutableMatrix_column_swap(MutableMatrix *M, int i, int j)
M2_bool IM2_MutableMatrix_delete_columns(MutableMatrix *M, int i, int j)
MutableMatrix * IM2_MutableMatrix_identity(const Ring *R, int nrows, M2_bool prefer_dense)
M2_bool rawLLL(MutableMatrix *M, MutableMatrix *U, gmp_QQ threshold, int strategy)
M2_bool IM2_MutableMatrix_insert_rows(MutableMatrix *M, int i, int n_to_add)
M2_bool IM2_MutableMatrix_row_operation(MutableMatrix *M, int i, const RingElement *r, int j, M2_bool opposite_mult)
M2_bool IM2_MutableMatrix_is_zero(const MutableMatrix *M)
MutableMatrix * rawMutableMatrixLift(int *success_return, const Ring *R, const MutableMatrix *f)
M2_arrayintOrNull rawLUincremental(M2_arrayintOrNull P, MutableMatrix *LU, const MutableMatrix *v, int m)
const Matrix * IM2_MutableMatrix_to_matrix(const MutableMatrix *M)
const Matrix * rawMatrixClean(gmp_RR epsilon, const Matrix *M)
M2_bool rawLinAlgSubMult(MutableMatrix *C, const MutableMatrix *A, const MutableMatrix *B)
void rawTriangularSolve(MutableMatrix *Lv, MutableMatrix *x, int m, int strategy)
M2_bool rawEigenvectors(MutableMatrix *A, MutableMatrix *eigenvalues, MutableMatrix *eigenvectors, M2_bool isHermitian)
M2_bool rawQR(const MutableMatrix *A, MutableMatrix *Q, MutableMatrix *R, M2_bool return_QR)
MutableMatrix * rawLinAlgNullSpace(MutableMatrix *A)
engine_RawRingElementArrayOrNull rawLinAlgCharPoly(MutableMatrix *A)
MutableMatrix * rawMutableMatrixPromote(const Ring *R, const MutableMatrix *f)
MutableMatrix * rawMutableMatrixTranspose(MutableMatrix *A)
M2_bool IM2_HermiteNormalForm(MutableMatrix *M)
M2_bool rawLeastSquares(MutableMatrix *A, MutableMatrix *b, MutableMatrix *x, M2_bool assume_full_rank)
engine_RawArrayIntPairOrNull rawLQUPFactorization(MutableMatrix *A)
MutableMatrix * rawLinAlgMult(const MutableMatrix *A, const MutableMatrix *B)
gmp_RRorNull rawMutableMatrixNorm(gmp_RR p, const MutableMatrix *M)
MutableMatrix * IM2_MutableMatrix_submatrix(const MutableMatrix *M, M2_arrayint rows, M2_arrayint cols)
M2_bool IM2_MutableMatrix_column_permute(MutableMatrix *M, int start, M2_arrayint perm)
int IM2_MutableMatrix_n_rows(const MutableMatrix *M)
MutableMatrix * IM2_MutableMatrix_copy(MutableMatrix *M, M2_bool prefer_dense)
gmp_RRorNull rawMatrixNorm(gmp_RR p, const Matrix *M)
M2_bool IM2_MutableMatrix_reduce_by_pivots(MutableMatrix *M)
M2_bool IM2_MutableMatrix_row_scale(MutableMatrix *M, const RingElement *r, int i, M2_bool opposite_mult)
M2_bool IM2_MutableMatrix_set_values(MutableMatrix *M, M2_arrayint rows, M2_arrayint cols, engine_RawRingElementArray values)
M2_bool rawLinAlgAddMult(MutableMatrix *C, const MutableMatrix *A, const MutableMatrix *B)
MutableMatrix * rawLinAlgInverse(MutableMatrix *A)
const RingElement * rawLinAlgDeterminant(MutableMatrix *A)
M2_bool IM2_MutableMatrix_row_2by2(MutableMatrix *M, int r1, int r2, const RingElement *a1, const RingElement *a2, const RingElement *b1, const RingElement *b2, M2_bool opposite_mult)
MutableMatrix * IM2_MutableMatrix_submatrix1(const MutableMatrix *M, M2_arrayint cols)
MutableMatrix * rawLinAlgSolveInvertible(const MutableMatrix *A, const MutableMatrix *B, int *success)
MutableMatrix * rawLinAlgRREF(MutableMatrix *A)
M2_arrayintOrNull rawLinAlgRankProfile(MutableMatrix *A, M2_bool row_profile)
void rawMutableMatrixFillRandom(MutableMatrix *M, long nelems)
M2_bool IM2_MutableMatrix_is_equal(const MutableMatrix *M, const MutableMatrix *N)
M2_bool rawSVD(MutableMatrix *A, MutableMatrix *Sigma, MutableMatrix *U, MutableMatrix *VT, M2_bool use_divide_and_conquer)
M2_bool IM2_MutableMatrix_column_operation(MutableMatrix *M, int i, const RingElement *r, int j, M2_bool opposite_mult)
M2_bool IM2_MutableMatrix_row_swap(MutableMatrix *M, int i, int j)
M2_bool rawEigenvalues(MutableMatrix *A, MutableMatrix *eigenvalues, M2_bool isHermitian)
engine_RawRingElementArrayArrayOrNull IM2_MutableMatrix_get_entries(const MutableMatrix *M)
unsigned int rawMutableMatrixHash(const MutableMatrix *M)
M2_arrayintOrNull rawLU(const MutableMatrix *A, MutableMatrix *L, MutableMatrix *U)
M2_bool IM2_MutableMatrix_row_permute(MutableMatrix *M, int start, M2_arrayint perm)
M2_bool IM2_MutableMatrix_column_2by2(MutableMatrix *M, int c1, int c2, const RingElement *a1, const RingElement *a2, const RingElement *b1, const RingElement *b2, M2_bool opposite_mult)
void rawMutableMatrixFillRandomDensity(MutableMatrix *M, double density, int special_type)
M2_arrayintOrNull IM2_FF_LU(MutableMatrix *M)
gmp_RRorNull rawRingElementNorm(gmp_RR p, const RingElement *f)
int IM2_MutableMatrix_n_cols(const MutableMatrix *M)
M2_bool IM2_MutableMatrix_delete_rows(MutableMatrix *M, int i, int j)
MutableMatrix * IM2_MutableMatrix_make(const Ring *R, int nrows, int ncols, M2_bool prefer_dense)
const RingElement * IM2_Matrix_dot_product(const MutableMatrix *M, int c1, int c2)
MutableMatrix * rawLinAlgSolve(const MutableMatrix *A, const MutableMatrix *B, int *success)
volatile int x