34 template<
typename CoeffRing >
35 template<
class RingType>
36 size_t DMat < CoeffRing >::rank(
typename enable_if<is_givaro_or_ffpack<RingType>::value >::type* dummy )
const
40 std::cout <<
"Calling rankGF_or_FFPACK" << std::endl;
41 ElementType *N =
newarray(ElementType, n_rows() * n_cols() );
45 copy_elems( n_rows()*n_cols(), N, 1, get_array(), 1);
60 size_t result = FFPACK::Rank(ring().field(), n_cols(), n_rows(), N, n_rows() );
66 template<
typename CoeffRing>
69 typedef typename CoeffRing::ElementType ElementType;
70 std::cout <<
"Calling FFpackRank" << std::endl;
71 assert(
typeid(CoeffRing) ==
typeid(
M2::ARingZZpFFPACK) ||
typeid(CoeffRing) ==
typeid(M2::ARingGFGivaro ));
72 ElementType* N =
newarray( ElementType, mat.n_rows() * mat.n_cols());
73 mat.copy_elems(mat.n_rows()*mat.n_cols(), N, 1, mat.get_array(), 1);
76 size_t result = FFPACK::Rank(mat.
ring().field(), mat.n_cols(), mat.n_rows(), N, mat.n_rows());
81 template<
typename CoeffRing>
83 typename CoeffRing::ElementType&
result )
85 typedef typename CoeffRing::ElementType ElementType;
86 std::cout <<
"Calling FFpackDeterminant" << std::endl;
87 assert(
typeid(CoeffRing) ==
typeid(
M2::ARingZZpFFPACK) ||
typeid(CoeffRing) ==
typeid(M2::ARingGFGivaro ));
88 ElementType* N =
newarray( ElementType, mat.n_rows() * mat.n_cols());
89 mat.copy_elems(mat.n_rows()*mat.n_cols(), N, 1, mat.get_array(), 1);
92 result = FFPACK::Det(mat.
ring().field(), mat.n_cols(), mat.n_rows(), N, mat.n_rows());
96 template<
typename CoeffRing>
99 typedef typename CoeffRing::ElementType ElementType;
100 assert(mat.n_rows() == mat.n_cols());
101 ElementType* N =
newarray( ElementType, mat.n_rows() * mat.n_cols());
102 mat.copy_elems(mat.n_rows()*mat.n_cols(), N, 1, mat.get_array(), 1);
104 size_t n = mat.n_rows();
106 FFPACK::Invert2(mat.
ring().field(), n, N, n,
inverse.get_array(), n, nullspacedim);
112 template<
typename CoeffRing>
117 right_side = !right_side;
119 typedef typename CoeffRing::ElementType ElementType;
120 ElementType* N =
newarray( ElementType, mat.n_rows() * mat.n_cols());
121 mat.copy_elems(mat.n_rows()*mat.n_cols(), N, 1, mat.get_array(), 1);
123 size_t nr = mat.n_rows();
124 size_t nc = mat.n_cols();
126 ElementType *nullspaceFFPACK = 0;
128 size_t nullspace_dim;
129 size_t nullspace_leading_dim;
131 FFPACK::NullSpaceBasis(mat.
ring().field(),
132 (right_side ? FFLAS::FflasRight : FFLAS::FflasLeft),
133 nc, nr, N, nr, nullspaceFFPACK, nullspace_leading_dim, nullspace_dim);
135 std::cerr <<
"leading dim = " << nullspace_leading_dim <<
" and dim = " << nullspace_dim << std::endl;
137 if (right_side && nullspace_dim != nullspace_leading_dim)
139 std::cerr <<
"error: this should not happen!" << std::endl;
141 else if (!right_side && nullspace_leading_dim != nc)
143 std::cerr <<
"error: this should not happen either!" << std::endl;
147 nullspace.
resize(nullspace_dim,nr);
149 nullspace.
resize(nc,nullspace_dim);
151 mat.copy_elems(nullspace.n_rows() * nullspace.n_cols(), nullspace.get_array(), 1, nullspaceFFPACK, 1);
153 delete [] nullspaceFFPACK;
156 template<
typename CoeffRing>
163 typedef typename CoeffRing::ElementType ElementType;
164 ElementType* N =
newarray( ElementType, mat.n_rows() * mat.n_cols());
165 mat.copy_elems(mat.n_rows()*mat.n_cols(), N, 1, mat.get_array(), 1);
171 rk = FFPACK::RowRankProfile(mat.
ring().field(),
172 mat.n_cols(),mat.n_rows(),
176 rk = FFPACK::ColumnRankProfile(mat.
ring().field(),
177 mat.n_cols(),mat.n_rows(),
182 for (
size_t i=0; i<rk; i++)
183 profile->array[i] =
static_cast<int>(prof[i]);
191 template<
typename CoeffRing>
197 std::cerr <<
"inside FFpackSolveLinear" << std::endl;
199 typedef typename CoeffRing::ElementType ElementType;
200 size_t a_rows = mat.n_rows();
201 size_t a_cols = mat.n_cols();
203 size_t b_rows = B.n_rows();
204 size_t b_cols = B.n_cols();
206 ElementType* ffpackA =
newarray(ElementType, mat.n_rows() * mat.n_cols());
207 mat.copy_elems(mat.n_rows()*mat.n_cols(), ffpackA, 1, mat.get_array(), 1);
209 ElementType* ffpackB =
newarray(ElementType, b_rows * b_cols);
210 B.copy_elems(b_rows * b_cols, ffpackB, 1, B.get_array(), 1);
213 size_t x_rows = (right_side ? a_cols : b_rows);
214 size_t x_cols = (right_side ? b_cols : a_rows);
217 ElementType *ffpackX =
newarray_clear(ElementType, x_rows * x_cols);
221 FFPACK::fgesv(mat.
ring().field(),
222 (!right_side ? FFLAS::FflasLeft : FFLAS::FflasRight),
224 (!right_side ? b_cols : b_rows),
234 ERROR(
"the system is inconsistent");
239 X.copy_elems(x_rows * x_cols, X.get_array(), 1, ffpackX, 1);
249 template<
typename CoeffRing>
252 const typename CoeffRing::ElementType& scalar)
255 FFLAS::fgemm( A.
ring().field(), A.n_rows()*A.n_cols(), scalar, A.get_array(), incx);
259 template<
typename CoeffRing>
265 FFLAS::fadd ( C.
ring().field(),
266 C.n_cols(), C.n_rows(),
267 A.get_array(), A.n_rows(),
268 B.get_array(), B.n_rows(),
269 C.get_array(), C.n_rows() );
272 template<
typename CoeffRing>
277 FFLAS::faddin ( C.
ring().field(),
278 C.n_cols(), C.n_rows(),
279 A.get_array(), A.n_rows(),
280 C.get_array(), C.n_rows() );
283 template<
typename CoeffRing>
289 FFLAS::fsub ( C.
ring().field(),
290 C.n_cols(), C.n_rows(),
291 A.get_array(), A.n_rows(),
292 B.get_array(), B.n_rows(),
293 C.get_array(), C.n_rows() );
298 template<
typename CoeffRing>
304 const typename CoeffRing::ElementType& a,
305 const typename CoeffRing::ElementType& b)
314 FFLAS::FFLAS_TRANSPOSE tA = (transposeA ? FFLAS::FflasTrans : FFLAS::FflasNoTrans);
315 FFLAS::FFLAS_TRANSPOSE tB = (transposeB ? FFLAS::FflasTrans : FFLAS::FflasNoTrans);
317 size_t m = (transposeB ? B.n_rows() : B.n_cols());
318 size_t n = (transposeA ? A.n_cols() : A.n_rows());
320 size_t k = (transposeA ? A.n_rows() : A.n_cols());
321 size_t k2 = (transposeB ? B.n_cols() : B.n_rows());
326 ERROR(
"matrices are not composable. this error is handled in Macaulay2 code and this message should never appear! ");
331 FFLAS::fgemm( C.
ring().field(),
351 std::cout <<
"DMat<M2::ARingZZpFFPACK>::addInPlace()" << std::endl;
352 FFpackMatrixAddInPlace<M2::ARingZZpFFPACK>(*
this,B);
359 std::cout <<
"DMat<M2::ARingZZpFFPACK>::rank()" << std::endl;
360 return FFpackRank<M2::ARingZZpFFPACK>(*
this);
366 std::cout <<
"Calling DMat<M2::ARingZZpFFPACK>::determinant" << std::endl;
367 FFpackDeterminant<M2::ARingZZpFFPACK>(*
this,
result);
373 std::cout <<
"Calling DMat<M2::ARingZZpFFPACK>::inverse" << std::endl;
374 return FFpackInvert<M2::ARingZZpFFPACK>(*
this, inverse);
380 std::cout <<
"Calling DMat<M2::ARingZZpFFPACK>::nullspace" << std::endl;
381 FFpackNullSpace<M2::ARingZZpFFPACK>(*
this, nullspace, right_side);
387 std::cout <<
"Calling DMat<M2::ARingZZpFFPACK>::rankProfile" << std::endl;
388 return FFpackRankProfile(*
this, row_profile);
394 bool right_side)
const
396 std::cout <<
"Calling DMat<M2::ARingZZpFFPACK>::solveLinear" << std::endl;
397 return FFpackSolveLinear(*
this, X, B, right_side);
405 const ElementType& a,
406 const ElementType& b)
408 std::cout <<
"Calling DMat<M2::ARingZZpFFPACK>::addMultipleTo *" << std::endl;
409 FFpackAddMultipleTo(*
this, A, B, transposeA, transposeB, a, b);
421 std::cout <<
"not implemented yet" << std::endl;
427 std::cout <<
"Calling DMat<M2::ARingGFGivaro>::rank()" << std::endl;
428 return FFpackRank<M2::ARingGFGivaro>(*
this);
434 std::cout <<
"Calling DMat<M2::ARingGFGivaro>::determinant" << std::endl;
435 FFpackDeterminant<M2::ARingGFGivaro>(*
this,
result );
441 std::cout <<
"Calling DMat<M2::ARingGFGivaro>::inverse" << std::endl;
442 return FFpackInvert<M2::ARingGFGivaro>(*
this, inverse);
448 std::cout <<
"Calling DMat<M2::ARingGFGivaro>::nullspace" << std::endl;
449 FFpackNullSpace<M2::ARingGFGivaro>(*
this, nullspace, right_side);
455 std::cout <<
"Calling DMat<M2::ARingGFGivaro>::rankProfile" << std::endl;
456 return FFpackRankProfile(*
this, row_profile);
462 bool right_side)
const
464 std::cout <<
"Calling DMat<M2::ARingGFGivaro>::solveLinear" << std::endl;
465 return FFpackSolveLinear(*
this, X, B, right_side);
473 const ElementType& a,
474 const ElementType& b)
476 std::cout <<
"Calling DMat<M2::ARingGFGivaro>::addMultipleTo" << std::endl;
477 FFpackAddMultipleTo(*
this, A, B, transposeA, transposeB, a, b);
void resize(size_t new_nrows, size_t new_ncols)
const ACoeffRing & ring() const
wrapper for the FFPACK::ModularBalanced<double> field implementation
VALGRIND_MAKE_MEM_DEFINED & result(result)
M2_arrayint M2_makearrayint(int n)
M2_arrayint M2_arrayintOrNull
bool inverse(const DMatZZpFFPACK &A, DMatZZpFFPACK &result_inv)
#define newarray_clear(T, len)