Macaulay2 Engine
Loading...
Searching...
No Matches
dmat.hpp
Go to the documentation of this file.
1// Copyright 2005-2012 Michael E. Stillman
2
3#ifndef _dmat_hpp_
4#define _dmat_hpp_
5
42
43#include "engine-includes.hpp"
44#include "mat-util.hpp"
45
46#include <algorithm>
47#include <utility>
48#include <vector>
49
50template <typename ACoeffRing>
51class DMat;
52
53// Special instantiations of DMat class
54#include "dmat-zz-flint.hpp"
55#include "dmat-qq-flint.hpp"
56#include "dmat-zzp-flint.hpp"
57#include "dmat-gf-flint-big.hpp"
58#include "dmat-gf-flint.hpp"
59
60template <typename ACoeffRing>
61class DMat
62{
63 public:
64 typedef ACoeffRing CoeffRing;
65 typedef typename ACoeffRing::ElementType ElementType;
66 typedef typename ACoeffRing::Element Element;
67
68 DMat() : mRing(nullptr), mNumRows(0), mNumColumns(0), mArray(nullptr) {}
69 DMat(const ACoeffRing& R,
70 size_t nrows,
71 size_t ncols)
72 : mRing(&R),
73 mNumRows(nrows),
74 mNumColumns(ncols)
75 {
76 size_t len = mNumRows * mNumColumns;
77 if (len == 0)
78 {
79 mArray = nullptr;
80 }
81 else
82 {
84 for (size_t i = 0; i < len; i++)
85 {
86 ring().init(mArray[i]);
87 ring().set_zero(mArray[i]);
88 }
89 ElementType* next = mArray;
90 for (size_t r=0; r < mNumRows; ++r)
91 {
92 mRowPointers.push_back(next);
93 next += mNumColumns;
94 }
95 }
96 }
98 : mRing(&M.ring()),
99 mNumRows(M.numRows()),
101 {
102 size_t len = mNumRows * mNumColumns;
103 if (len == 0)
104 mArray = nullptr;
105 else
106 {
108 for (size_t i = 0; i < len; i++)
109 {
110 ring().init(mArray[i]);
111 ring().set_zero(mArray[i]);
112 }
113 ElementType* next = mArray;
114 for (size_t r=0; r < mNumRows; ++r)
115 {
116 mRowPointers.push_back(next);
117 next += mNumColumns;
118 }
119 for (size_t r = 0; r < mNumRows; ++r)
120 for (size_t c = 0; c < mNumColumns; ++c)
121 ring().init_set(entry(r,c), M.entry(r,c));
122 }
123 }
124
126 {
127 size_t len = mNumRows * mNumColumns;
128 for (size_t i = 0; i < len; i++) ring().clear(mArray[i]);
129 if (mArray != nullptr) freemem(mArray);
130 // don't need to free mRowPointers (they are pointers into mArray...)
131 }
132
133 // swap the actual matrices of 'this' and 'M'.
142
143 const ACoeffRing& ring() const { return *mRing; }
144 size_t numRows() const { return mNumRows; }
145 size_t numColumns() const { return mNumColumns; }
146
147 // default placement: elements in row-major order
148 ElementType& entry(size_t row, size_t column)
149 {
150 return *(mRowPointers[row] + column);
151 }
152 const ElementType& entry(size_t row, size_t column) const
153 {
154 return *(mRowPointers[row] + column);
155 }
156
157 void resize(size_t new_nrows, size_t new_ncols)
158 {
159 DMat newMatrix(ring(), new_nrows, new_ncols);
160 swap(newMatrix);
161 }
162
163 // Get rid of these too!
164 const ElementType* rowMajorArray() const { return mArray; }
166
167 // These are labelled 'unsafe', as it s possible the rows
168 // are out of order (which happens in particular if
169 // certain flint functions created this.
170 const ElementType* unsafeArray() const { return mArray; }
172
173 private:
174 const ACoeffRing* mRing;
175 size_t mNumRows;
178 std::vector<ElementType*> mRowPointers;
179};
180
181
182
183#endif
184
185// Local Variables:
186// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
187// indent-tabs-mode: nil
188// End:
RingType CoeffRing
Definition dmat.hpp:64
DMat(const DMat< ACoeffRing > &M)
Definition dmat.hpp:97
size_t numRows() const
Definition dmat.hpp:144
ElementType *& unsafeArray()
Definition dmat.hpp:171
size_t mNumColumns
Definition dmat.hpp:176
const RingType * mRing
Definition dmat.hpp:174
std::vector< ElementType * > mRowPointers
Definition dmat.hpp:178
ElementType * mArray
Definition dmat.hpp:177
ElementType & entry(size_t row, size_t column)
Definition dmat.hpp:148
size_t mNumRows
Definition dmat.hpp:175
void resize(size_t new_nrows, size_t new_ncols)
Definition dmat.hpp:157
const ACoeffRing & ring() const
Definition dmat.hpp:143
void swap(DMat< ACoeffRing > &M)
Definition dmat.hpp:134
RingType::ElementType ElementType
Definition dmat.hpp:65
~DMat()
Definition dmat.hpp:125
DMat(const ACoeffRing &R, size_t nrows, size_t ncols)
Definition dmat.hpp:69
RingType::Element Element
Definition dmat.hpp:66
const ElementType & entry(size_t row, size_t column) const
Definition dmat.hpp:152
const ElementType * unsafeArray() const
Definition dmat.hpp:170
ElementType *& rowMajorArray()
Definition dmat.hpp:165
DMat()
Definition dmat.hpp:68
const ElementType * rowMajorArray() const
Definition dmat.hpp:164
size_t numColumns() const
Definition dmat.hpp:145
Definition dmat.hpp:62
DMat<M2::ARingGFFlintBig> — dense GF matrices stored in a FLINT fq_nmod_mat_t.
DMat<M2::ARingGFFlint> — dense GF matrices stored in a FLINT fq_zech_mat_t.
DMat<M2::ARingQQFlint> — dense rational matrices stored in a FLINT fmpq_mat_t.
DMat<M2::ARingZZ> — dense integer matrices stored in a FLINT fmpz_mat_t.
DMat<M2::ARingZZpFlint> — dense Z/p matrices stored in a FLINT nmod_mat_t.
Engine-wide include prelude — a single point of truth for portability shims.
void freemem(void *s)
Definition m2-mem.cpp:103
Generic helpers (displayMat, concatenateMatrices) for DMat / SMat matrices.
#define swap(a, b, t)
Definition monsort.hpp:127
void swap(mpfr::mpreal &x, mpfr::mpreal &y)
Definition mpreal.h:3244
#define newarray(T, len)
Definition newdelete.hpp:82