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
|
// Copyright 2014 Michael E. Stillman
#ifndef _flintqq_mat_hpp_
#define _flintqq_mat_hpp_
// This class is designed to use DMat<M2::ARingQQ>, which stores elements as gmp
// ints
// This sets up flint fmpq_mat matrices, and provides translation. This is
// significantly faster than doing the operations in a naive manner.
// This will become un-needed once DMat<ARingQQ> starts using flint
// integers/rationals.
class FlintZZMat
{
public:
FlintZZMat(const DMatZZGMP& mat)
{
fmpz_mat_init(mMatrix, mat.numRows(), mat.numColumns());
to_fmpz_mat(mat, mMatrix);
}
FlintZZMat(long numrows, long numcolumns)
{
fmpz_mat_init(mMatrix, numrows, numcolumns);
}
~FlintZZMat() { fmpz_mat_clear(mMatrix); }
fmpz_mat_struct* value() { return mMatrix; }
void toDMat(DMatZZGMP& result)
{
result.resize(fmpz_mat_nrows(mMatrix), fmpz_mat_ncols(mMatrix));
from_fmpz_mat(mMatrix, result);
}
long numRows() const { return fmpz_mat_nrows(mMatrix); }
long numColumns() const { return fmpz_mat_ncols(mMatrix); }
private:
fmpz_mat_t mMatrix;
static void to_fmpz_mat(const DMatZZGMP& mat1, fmpz_mat_t result_mat)
{
DMatZZGMP& mat = const_cast<DMatZZGMP&>(mat1);
for (long r = 0; r < mat.numRows(); r++)
{
auto end = mat.rowEnd(r);
long c = 0;
for (auto it = mat.rowBegin(r); it != end; ++it, ++c)
fmpz_set_mpz(fmpz_mat_entry(result_mat, r, c), &(*it));
}
}
static void from_fmpz_mat(fmpz_mat_t mat, DMatZZGMP& result_mat)
{
for (long r = 0; r < result_mat.numRows(); r++)
{
auto end = result_mat.rowEnd(r);
long c = 0;
for (auto it = result_mat.rowBegin(r); it != end; ++it, ++c)
fmpz_get_mpz(&(*it), fmpz_mat_entry(mat, r, c));
}
}
};
class FlintQQMat
{
public:
typedef DMat<M2::ARingQQ> DMatQQ;
FlintQQMat(const DMatQQ& mat)
{
fmpq_mat_init(mMatrix, mat.numRows(), mat.numColumns());
to_fmpq_mat(mat, mMatrix);
}
FlintQQMat(long numrows, long numcolumns)
{
fmpq_mat_init(mMatrix, numrows, numcolumns);
}
~FlintQQMat() { fmpq_mat_clear(mMatrix); }
fmpq_mat_struct* value() { return mMatrix; }
void toDMat(DMatQQ& result)
{
result.resize(fmpq_mat_nrows(mMatrix), fmpq_mat_ncols(mMatrix));
from_fmpq_mat(mMatrix, result);
}
long numRows() const { return fmpq_mat_nrows(mMatrix); }
long numColumns() const { return fmpq_mat_ncols(mMatrix); }
void set_from_fmpz(long r, long c, fmpz_t val)
{
fmpz_set(fmpq_numref(fmpq_mat_entry(mMatrix, r, c)), val);
fmpz_set_ui(fmpq_denref(fmpq_mat_entry(mMatrix, r, c)), 1);
}
private:
fmpq_mat_t mMatrix;
static void to_fmpq_mat(const DMatQQ& mat1, fmpq_mat_t result_mat)
{
DMatQQ& mat = const_cast<DMatQQ&>(mat1);
for (long r = 0; r < mat.numRows(); r++)
{
auto end = mat.rowEnd(r);
long c = 0;
for (auto it = mat.rowBegin(r); it != end; ++it, ++c)
fmpq_set_mpq(fmpq_mat_entry(result_mat, r, c), &(*it));
}
}
static void from_fmpq_mat(fmpq_mat_t mat, DMatQQ& result_mat)
{
for (long r = 0; r < result_mat.numRows(); r++)
{
auto end = result_mat.rowEnd(r);
long c = 0;
for (auto it = result_mat.rowBegin(r); it != end; ++it, ++c)
fmpq_get_mpq(&(*it), fmpq_mat_entry(mat, r, c));
}
}
};
#endif
// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
// indent-tabs-mode: nil
// End:
|