1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
// Copyright 2005-2012 Michael E. Stillman
#ifndef _dmat_hpp_
#define _dmat_hpp_
#include "engine-includes.hpp"
#include "mat-util.hpp"
#include <algorithm>
#include <utility>
template <typename ACoeffRing>
class DMat;
template <typename ACoeffRing>
class DMatConstIterator
{
public:
typedef DMat<ACoeffRing> Mat;
typedef typename Mat::ElementType ElementType;
DMatConstIterator(const ElementType* start, size_t stride)
: mCurrent(start), mStride(stride)
{
}
void operator++() { mCurrent += mStride; }
const ElementType& operator*() { return *mCurrent; }
bool operator==(const DMatConstIterator& i) const { return (&(*i.mCurrent) == mCurrent); }
bool operator!=(const DMatConstIterator& i) const { return (&(*i.mCurrent) != mCurrent); }
private:
const ElementType* mCurrent;
size_t mStride;
};
template <typename ACoeffRing>
class DMatIterator
{
public:
typedef DMat<ACoeffRing> Mat;
typedef typename Mat::ElementType ElementType;
DMatIterator(ElementType* start, size_t stride)
: mCurrent(start), mStride(stride)
{
}
void operator++() { mCurrent += mStride; }
ElementType& operator*() { return *mCurrent; }
bool operator==(DMatConstIterator<ACoeffRing>& i) const
{
return (&(*i) == mCurrent);
}
bool operator!=(DMatConstIterator<ACoeffRing>& i) const
{
return (&(*i) != mCurrent);
}
private:
ElementType* mCurrent;
size_t mStride;
};
// Special instantiations of DMat class
#include "dmat-zz-flint.hpp"
#include "dmat-qq-flint.hpp"
#include "dmat-zzp-flint.hpp"
#include "dmat-gf-flint-big.hpp"
#include "dmat-gf-flint.hpp"
template <typename ACoeffRing>
class DMat
{
public:
typedef ACoeffRing CoeffRing;
typedef typename ACoeffRing::ElementType ElementType;
typedef ElementType elem;
typedef DMatIterator<ACoeffRing> Iterator;
typedef DMatConstIterator<ACoeffRing> ConstIterator;
DMat() : mRing(nullptr), mNumRows(0), mNumColumns(0), mArray(nullptr) {}
DMat(const ACoeffRing& R, size_t nrows, size_t ncols)
: mRing(&R), mNumRows(nrows), mNumColumns(ncols)
{
size_t len = mNumRows * mNumColumns;
if (len == 0)
mArray = nullptr;
else
{
mArray = newarray(ElementType,len);
// mArray = new ElementType[len];
for (size_t i = 0; i < len; i++)
{
ring().init(mArray[i]);
ring().set_zero(mArray[i]);
}
}
}
DMat(const DMat<ACoeffRing>& M)
: mRing(&M.ring()), mNumRows(M.numRows()), mNumColumns(M.numColumns())
{
size_t len = mNumRows * mNumColumns;
if (len == 0)
mArray = nullptr;
else
{
mArray = newarray(ElementType,len);
// mArray = new ElementType[len];
for (size_t i = 0; i < len; i++)
ring().init_set(mArray[i], M.array()[i]);
}
}
~DMat()
{
size_t len = mNumRows * mNumColumns;
for (size_t i = 0; i < len; i++) ring().clear(mArray[i]);
// if (mArray != 0) delete[] mArray;
if (mArray != nullptr) freemem(mArray);
}
// swap the actual matrices of 'this' and 'M'.
void swap(DMat<ACoeffRing>& M)
{
std::swap(mRing, M.mRing);
std::swap(mNumRows, M.mNumRows);
std::swap(mNumColumns, M.mNumColumns);
std::swap(mArray, M.mArray);
}
const ACoeffRing& ring() const { return *mRing; }
size_t numRows() const { return mNumRows; }
size_t numColumns() const { return mNumColumns; }
// column-major order (old)
// Iterator rowBegin(size_t row) { return Iterator(array() + row, numRows());
// }
// ConstIterator rowBegin(size_t row) const { return ConstIterator(array() +
// row, numRows()); }
// ConstIterator rowEnd(size_t row) const { return ConstIterator(array() + row
// + numRows() * numColumns(), numRows()); }
// Iterator columnBegin(size_t col) { return Iterator(array() + col *
// numRows(), 1); }
// ConstIterator columnBegin(size_t col) const { return ConstIterator(array()
// + col * numRows(), 1); }
// ConstIterator columnEnd(size_t col) const { return ConstIterator(array() +
// (col+1) * numRows(), 1); }
// row-major order
Iterator rowBegin(size_t row)
{
return Iterator(array() + row * numColumns(), 1);
}
ConstIterator rowBegin(size_t row) const
{
return ConstIterator(array() + row * numColumns(), 1);
}
ConstIterator rowEnd(size_t row) const
{
return ConstIterator(array() + (row + 1) * numColumns(), 1);
}
Iterator columnBegin(size_t col)
{
return Iterator(array() + col, numColumns());
}
ConstIterator columnBegin(size_t col) const
{
return ConstIterator(array() + col, numColumns());
}
ConstIterator columnEnd(size_t col) const
{
return ConstIterator(array() + col + numRows() * numColumns(),
numColumns());
}
// column-major order (old)
// ElementType& entry(size_t row, size_t column) { return mArray[mNumRows *
// column + row]; }
// const ElementType& entry(size_t row, size_t column) const { return
// mArray[mNumRows * column + row]; }
// row-major order
ElementType& entry(size_t row, size_t column)
{
return mArray[mNumColumns * row + column];
}
const ElementType& entry(size_t row, size_t column) const
{
return mArray[mNumColumns * row + column];
}
void resize(size_t new_nrows, size_t new_ncols)
{
DMat newMatrix(ring(), new_nrows, new_ncols);
swap(newMatrix);
}
const ElementType* array() const { return mArray; }
ElementType*& array() { return mArray; }
const ElementType* rowMajorArray() const { return mArray; }
ElementType*& rowMajorArray() { return mArray; }
private:
const ACoeffRing* mRing;
size_t mNumRows;
size_t mNumColumns;
ElementType* mArray;
};
#endif
// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
// indent-tabs-mode: nil
// End:
|