Macaulay2 Engine
Loading...
Searching...
No Matches
mat.cpp
Go to the documentation of this file.
1// Copyright 2005 Michael E. Stillman
2
3#include "util.hpp"
4#include "dmat.hpp"
5#include "smat.hpp"
6#include "mat.hpp"
7#include "mutablemat.hpp"
8
9#include "coeffrings.hpp"
10
11#include "matrix-con.hpp"
12#include "matrix.hpp"
13
14#include "aring-RRR.hpp"
15#include "aring-RR.hpp"
16#include "aring-CCC.hpp"
17#include "aring-zz-gmp.hpp"
18#include "aring-zz-flint.hpp"
19#include "aring-zzp.hpp"
20#include "aring-zzp-ffpack.hpp"
21#include "aring-zzp-flint.hpp"
22#include "aring-m2-gf.hpp"
23#include "aring-glue.hpp"
24#include "aring-tower.hpp"
25#include "aring-qq.hpp"
26
27#include "lapack.hpp"
28
29#include "mutablemat.hpp"
30#include "ZZp.hpp"
31
33 size_t ncols,
34 bool dense) const
35{
36 if (dense)
38 this, get_ARing(), nrows, ncols);
39 else
41 this, get_ARing(), nrows, ncols);
42}
43
45 size_t ncols,
46 bool dense) const
47{
48 if (dense)
49 return new MutableMat<DMat<M2::ARingZZp> >(this, get_ARing(), nrows, ncols);
50 else
51 return new MutableMat<SMat<M2::ARingZZp> >(this, get_ARing(), nrows, ncols);
52}
53
55 size_t nrows,
56 size_t ncols,
57 bool dense)
58{
59 MutableMatrix *result = R->makeMutableMatrix(nrows, ncols, dense);
60 if (result != nullptr) return result;
61 // In this case, we just use ring elem arithmetic
63 if (dense)
64 return new MutableMat<DMat<CoefficientRingR> >(R, cR, nrows, ncols);
65 else
66 return new MutableMat<SMat<CoefficientRingR> >(R, cR, nrows, ncols);
67}
68
69MutableMatrix *MutableMatrix::identity(const Ring *R, size_t nrows, bool dense)
70{
71 MutableMatrix *result = MutableMatrix::zero_matrix(R, nrows, nrows, dense);
72 for (size_t i = 0; i < nrows; i++) result->set_entry(i, i, R->from_long(1));
73 return result;
74}
75
76MutableMatrix *MutableMatrix::from_matrix(const Matrix *m, bool prefer_dense)
77{
79 zero_matrix(m->get_ring(), m->n_rows(), m->n_cols(), prefer_dense);
80 Matrix::iterator i(m);
81 for (unsigned int c = 0; c < m->n_cols(); c++)
82 {
83 for (i.set(c); i.valid(); i.next())
84 result->set_entry(i.row(), c, i.entry());
85 }
86 return result;
87}
88
90{
91 const Ring *R = get_ring();
92 size_t nrows = n_rows();
93 size_t ncols = n_cols();
94 buffer *p = new buffer[nrows];
95 size_t r;
96 for (size_t c = 0; c < ncols; c++)
97 {
98 size_t maxcount = 0;
99 for (r = 0; r < nrows; r++)
100 {
101 ring_elem f;
102 get_entry(r, c, f);
103 if (!R->is_zero(f))
104 R->elem_text_out(p[r], f);
105 else
106 p[r] << ".";
107 if (p[r].size() > maxcount) maxcount = p[r].size();
108 }
109 for (r = 0; r < nrows; r++)
110 for (size_t k = maxcount + 1 - p[r].size(); k > 0; k--) p[r] << ' ';
111 }
112 for (r = 0; r < nrows; r++)
113 {
114 p[r] << '\0';
115 char *s = p[r].str();
116 o << s << newline;
117 }
118 delete[] p;
119}
120
122 M2_arrayint cols,
123 engine_RawRingElementArray values)
124{
125 if (rows->len != cols->len || rows->len != values->len) return false;
126 for (size_t i = 0; i < rows->len; i++)
127 {
128 if (!set_entry(
129 rows->array[i], cols->array[i], values->array[i]->get_value()))
130 return false;
131 }
132 return true;
133}
134
135#if 0
136engine_RawArrayIntPairOrNull rawLQUPFactorizationInPlace(MutableMatrix *A, M2_bool transpose)
137{
138 // Suppose A is m x n
139 // then we get A = LQUP = LSP, see e.g. http://www.ens-lyon.fr/LIP/Pub/Rapports/RR/RR2006/RR2006-28.pdf
140 // P and Q are permutation info using LAPACK's convention:, see
141 // http://www.netlib.org/lapack/explore-html/d0/d39/_v_a_r_i_a_n_t_s_2lu_2_r_e_c_2dgetrf_8f.html
142 // P is n element permutation on column: size(P)=min(m,n);
143 // for 1 <= i <= min(m,n), col i of the matrix was interchanged with col P(i).
144 // Qt is m element permutation on rows (inverse permutation)
145 // for 1 <= i <= min(m,n), col i of the matrix was interchanged with col P(i).
146 A->transpose();
147
149 if (mat == 0)
150 {
151 throw exc::engine_error("LUDivine not defined for this ring");
152 // ERROR("LUDivine not defined for this ring");
153 // return 0;
154 }
155 size_t nelems = mat->numColumns();
156 if (mat->numRows() < mat->numColumns()) nelems = mat->numRows();
157
158
159 std::vector<size_t> P(nelems, -1);
160 std::vector<size_t> Qt(nelems, -1);
161
162 // ignore return value (rank) of:
163 LUdivine(mat->ring().field(),
164 FFLAS::FflasNonUnit,
165 (transpose ? FFLAS::FflasTrans : FFLAS::FflasNoTrans),
166 mat->numRows(),
167 mat->numColumns(),
168 mat->array(),
169 mat->numColumns(),
170 &P[0],
171 &Qt[0]);
172
173 engine_RawArrayIntPairOrNull result = new engine_RawArrayIntPair_struct;
176
177 return result;
178}
179#endif
180
181// Local Variables:
182// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
183// indent-tabs-mode: nil
184// End:
Legacy Z_mod — a Ring-derived Z/p with log / exp tables.
M2::ARingCCC — arbitrary-precision complex numbers (pair of MPFR floats).
M2::ARingRR — machine-precision real numbers (IEEE 754 double).
M2::ARingRRR — arbitrary-precision real numbers backed by MPFR.
ConcreteRing<RingType> — the templated bridge between aring and the legacy Ring API.
M2::ARingGFM2 — native engine Galois field, no FLINT dependency.
Tiny dispatcher header that picks the default ARingQQ from among the QQ aring implementations.
M2::ARingTower — iterated finite-field extension tower for very large GF(p^k).
M2::ARingZZ — FLINT-backed arbitrary-precision integers with small-value inlining.
M2::ARingZZGMP — aring integer ring backed straight by GMP mpz_t.
M2::ARingZZpFFPACK — Z/p via FFLAS-FFPACK's Givaro::Modular<double> field.
M2::ARingZZpFlint — Z/p via FLINT's nmod_t precomputed-reciprocal reduction.
M2::ARingZZp — portable Z/p for small primes via log / exp tables.
Generic CoefficientRing adapter that wraps an arbitrary const Ring* and forwards every operation to i...
size_t numRows() const
Definition dmat.hpp:144
const ACoeffRing & ring() const
Definition dmat.hpp:143
size_t numColumns() const
Definition dmat.hpp:145
Definition dmat.hpp:62
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
int n_rows() const
Definition matrix.hpp:146
virtual size_t n_rows() const =0
virtual size_t n_cols() const =0
virtual bool get_entry(size_t r, size_t c, ring_elem &result) const =0
static MutableMatrix * zero_matrix(const Ring *R, size_t nrows, size_t ncols, bool dense)
Definition mat.cpp:54
static MutableMatrix * from_matrix(const Matrix *N, bool is_dense)
Definition mat.cpp:76
void text_out(buffer &o) const
Definition mat.cpp:89
virtual MutableMatrix * transpose() const =0
bool set_values(M2_arrayint rows, M2_arrayint cols, engine_RawRingElementArray values)
Definition mat.cpp:121
virtual const Ring * get_ring() const =0
MatT * coerce()
Definition mat.hpp:151
virtual bool set_entry(size_t r, size_t c, const ring_elem a)=0
MutableMatrix()
Definition mat.hpp:81
static MutableMatrix * identity(const Ring *R, size_t nrows, bool dense)
Definition mat.cpp:69
Abstract base class for mutable matrices over an arbitrary engine Ring, the in-place counterpart of t...
Definition mat.hpp:79
virtual MutableMatrix * makeMutableMatrix(size_t nrows, size_t ncols, bool dense) const
Definition ring.hpp:332
virtual ring_elem from_long(long n) const =0
virtual void elem_text_out(buffer &o, const ring_elem f, bool p_one=true, bool p_plus=false, bool p_parens=false) const =0
virtual bool is_zero(const ring_elem f) const =0
const CoefficientRingR * getCoefficientRingR() const
Definition ring.cpp:24
xxx xxx xxx
Definition ring.hpp:102
virtual MutableMatrix * makeMutableMatrix(size_t nrows, size_t ncols, bool dense) const
Definition mat.cpp:32
M2::ARingZZGMP * get_ARing() const
Definition ZZ.hpp:98
M2::ARingZZp * get_ARing() const
Definition ZZp.hpp:94
virtual MutableMatrix * makeMutableMatrix(size_t nrows, size_t ncols, bool dense) const
Definition mat.cpp:44
Two SimpleARing-style coefficient adapters: CoefficientRingZZp and CoefficientRingR.
DMat<ACoeffRing> — dense-matrix template plus the umbrella that wires in every per-ring specialisatio...
#define Matrix
Definition factory.cpp:14
int p
Engine bridge into LAPACK for RR / CC dense linear algebra.
void size_t s
Definition m2-mem.cpp:271
VALGRIND_MAKE_MEM_DEFINED & result(result)
char newline[]
Definition m2-types.cpp:49
engine_RawArrayIntPair engine_RawArrayIntPairOrNull
Definition m2-types.h:188
char M2_bool
Definition m2-types.h:82
MutableMatrix — abstract base of every mutable matrix the engine hands across the boundary.
MatrixConstructor — the mutable builder that produces an immutable Matrix.
Matrix — the engine's immutable homomorphism F -> G between free modules.
Umbrella header that ties together MutableMat declarations, implementations, and the SLP variant.
SMat<ACoeffRing> — column-oriented sparse matrix template, dual of DMat<R>.
M2_arrayint stdvector_to_M2_arrayint(const std::vector< T > &v)
Definition util.hpp:79
Conversion helpers between M2 boundary types and standard C++ containers.