/usr/include/flint/flintxx/matrix.h is in libflint-dev 2.4.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | /*=============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2013 Tom Bachmann
******************************************************************************/
// Common code shared among matrix classes
#ifndef FLINTXX_MATRIX_H
#define FLINTXX_MATRIX_H
#include "flint_classes.h"
#include "mp.h"
#include "rules.h"
#include "stdmath.h"
#include "ltuple.h"
#include "traits.h"
#include "tuple.h"
#include "../permxx.h"
namespace flint {
FLINT_DEFINE_BINOP(solve)
FLINT_DEFINE_BINOP(solve_fflu)
FLINT_DEFINE_THREEARY(mat_at)
FLINT_DEFINE_THREEARY(solve_fflu_precomp)
FLINT_DEFINE_UNOP(charpoly)
FLINT_DEFINE_UNOP(det)
FLINT_DEFINE_UNOP(det_fflu)
FLINT_DEFINE_UNOP(det_interpolate)
FLINT_DEFINE_UNOP(nullspace)
FLINT_DEFINE_UNOP(rref)
FLINT_DEFINE_UNOP(trace)
FLINT_DEFINE_UNOP(transpose)
FLINT_DEFINE_THREEARY(fflu)
FLINT_DEFINE_THREEARY_HERE_2DEFAULT(fflu, permxx*, 0, bool, false)
template<class T> struct matrix_traits : rules::UNIMPLEMENTED { };
// override this for the non-reference type of your choice
// {
// template<class M> static slong rows(const M&);
// template<class M> static slong cols(const M&);
// template<class M> static ??? at(const M&, slong, slong);
// template<class M> static ??? at(M&, slong, slong);
// };
namespace traits {
template <class T, class Enable = void> struct is_mat : is_implemented<
matrix_traits<typename flint_classes::to_nonref<T>::type> > { };
template<class T> struct is_mat<T,
typename mp::disable_if<flint_classes::is_flint_class<T> >::type>
: mp::false_ { };
} // traits
namespace matrices {
namespace mdetail {
// Some helper traits used below.
template<class Data> struct second_is_mat_data : mp::false_ { };
template<class Data1, class Data2>
struct second_is_mat_data<tuple<Data1, tuple<Data2, empty_tuple> > >
: traits::is_mat<typename traits::basetype<Data2>::type> { };
template<class Expr> struct second_is_mat
: second_is_mat_data<typename Expr::data_t> { };
template<class Mat>
struct both_mat : mp::and_<
traits::is_mat<
typename traits::basetype<typename Mat::data_t::head_t>::type>,
traits::is_mat<
typename traits::basetype<typename Mat::data_t::tail_t::head_t>::type>
> { };
// A more convenient way to obtain the traits associated to a non-immediate
// or non-nonref expression.
template<class Expr> struct immediate_traits
: matrix_traits<typename flint_classes::to_nonref<Expr>::type> { };
} // mdetail
// For matrix expressions to create temporaries, it is necessary to know the
// dimensions of the result of a computation. This is a generic implementation,
// which assumes that the output dimensions are the same as the dimensions of
// the first argument, which is assumed to be a matrix, except if there are
// precisely two arguments only the second of which is a matrix, in which case
// we assume its the dimension of that.
// This implementation works correctly in many cases, e.g. matrix-addition or
// matrix-scalar multiplication.
template<class Operation>
struct outsize_generic
{
template<class Mat>
static slong rows(const Mat& m,
typename mp::disable_if<mdetail::second_is_mat<Mat> >::type* = 0)
{
return m._data().head.rows();
}
template<class Mat>
static slong cols(const Mat& m,
typename mp::disable_if<mdetail::second_is_mat<Mat> >::type* = 0)
{
return m._data().head.cols();
}
template<class Mat>
static slong rows(const Mat& m,
typename mp::enable_if<mdetail::second_is_mat<Mat> >::type* = 0)
{
return m._data().tail.head.rows();
}
template<class Mat>
static slong cols(const Mat& m,
typename mp::enable_if<mdetail::second_is_mat<Mat> >::type* = 0)
{
return m._data().tail.head.cols();
}
};
// This is the expression template used for computing the dimensions of an
// operation. Without further specialisation, it is just the generic
// implementation described above.
// If you introduce a new operation where the generic implementation is
// incorrect, you must specialise this template.
template<class Operation>
struct outsize : outsize_generic<Operation> { };
// Specialise immediates, where the dimensions are stored with the object.
template<>
struct outsize<operations::immediate>
{
template<class Mat>
static slong rows(const Mat& m)
{
return mdetail::immediate_traits<Mat>::rows(m);
}
template<class Mat>
static slong cols(const Mat& m)
{
return mdetail::immediate_traits<Mat>::cols(m);
}
};
// Specialise multiplication. For matrix-matrix multiplication, use
// the usual formula. For matrix-scalar multiplication, use the generic
// implementation.
template<>
struct outsize<operations::times>
{
template<class Mat>
static slong rows(const Mat& m,
typename mp::enable_if<mdetail::both_mat<Mat> >::type* = 0)
{
return m._data().head.rows();
}
template<class Mat>
static slong cols(const Mat& m,
typename mp::enable_if<mdetail::both_mat<Mat> >::type* = 0)
{
return m._data().tail.head.cols();
}
template<class Mat>
static slong rows(const Mat& m,
typename mp::disable_if<mdetail::both_mat<Mat> >::type* = 0)
{
return outsize_generic<operations::times>::rows(m);
}
template<class Mat>
static slong cols(const Mat& m,
typename mp::disable_if<mdetail::both_mat<Mat> >::type* = 0)
{
return outsize_generic<operations::times>::cols(m);
}
};
// Any particular multipication algorithm also has to be specialised.
template<>
struct outsize<operations::mul_classical_op>
: outsize<operations::times> { };
// Specialise transpose.
template<>
struct outsize<operations::transpose_op>
{
template<class Mat>
static slong rows(const Mat& m)
{
return m._data().head.cols();
}
template<class Mat>
static slong cols(const Mat& m)
{
return m._data().head.rows();
}
};
// Specialise nullspace. Note that the nullspace computation functions in
// flint return a matrix the columns of which span the nullspace. Since the
// nullity is not known in advance in general, we have to allocate a square
// matrix.
template<>
struct outsize<operations::nullspace_op>
{
template<class Mat>
static slong rows(const Mat& m)
{
return m._data().head.cols();
}
template<class Mat>
static slong cols(const Mat& m)
{
return m._data().head.cols();
}
};
// This is a bit of a hack. Matrix operations returning a tuple typically
// only return one matrix. We key outsize on the inner operation to find out
// the dimensions. So e.g. solve(A, X).get<1>() (say) will invoke outsize
// with ltuple_get_op<1> as argument, which then invokes outsize with solve_op
// and (A, X) as argument.
template<unsigned n>
struct outsize<operations::ltuple_get_op<n> >
{
template<class Mat>
static slong rows(const Mat& m)
{
return outsize<
typename Mat::data_t::head_t::operation_t>::rows(m._data().head);
}
template<class Mat>
static slong cols(const Mat& m)
{
return outsize<
typename Mat::data_t::head_t::operation_t>::cols(m._data().head);
}
};
// This is not actually a matrix expression, but called by the above ...
template<>
struct outsize<operations::solve_op>
{
template<class Mat>
static slong rows(const Mat& m)
{
return m._data().second().rows();
}
template<class Mat>
static slong cols(const Mat& m)
{
return m._data().second().cols();
}
};
template<> struct outsize<operations::solve_fflu_op>
: outsize<operations::solve_op> { };
template<>
struct outsize<operations::solve_fflu_precomp_op>
{
template<class Mat>
static slong rows(const Mat& m)
{
return m._data().tail.second().rows();
}
template<class Mat>
static slong cols(const Mat& m)
{
return m._data().tail.second().cols();
}
};
namespace mdetail {
struct base_traits
{
template<class M>
static slong rows(const M& m)
{
return matrices::outsize<typename M::operation_t>::rows(m);
}
template<class M>
static slong cols(const M& m)
{
return matrices::outsize<typename M::operation_t>::cols(m);
}
};
} // mdetail
// These traits classes are useful for implementing unified coefficient access.
// See fmpz_matxx etc for example usage.
template<class Mat>
struct generic_traits : mdetail::base_traits
{
template<class T, class U>
struct at
{
typedef FLINT_THREEARY_ENABLE_RETTYPE(mat_at, Mat, T, U)
entry_ref_t;
typedef entry_ref_t entry_srcref_t;
static entry_srcref_t get(const Mat& m, T i, U j)
{
return mat_at(m, i, j);
}
};
};
template<class Srcref>
struct generic_traits_srcref : mdetail::base_traits
{
template<class T, class U>
struct at
{
typedef Srcref entry_ref_t;
typedef Srcref entry_srcref_t;
template<class M>
static Srcref get(M m, T i, U j)
{
return mdetail::immediate_traits<M>::at(m, i, j);
}
};
};
template<class Ref>
struct generic_traits_ref : mdetail::base_traits
{
template<class T, class U>
struct at
{
typedef Ref entry_ref_t;
typedef Ref entry_srcref_t;
template<class M>
static Ref get(M m, T i, U j)
{
return mdetail::immediate_traits<M>::at(m, i, j);
}
};
};
template<class Ref, class Srcref>
struct generic_traits_nonref : mdetail::base_traits
{
template<class T, class U>
struct at
{
typedef Ref entry_ref_t;
typedef Srcref entry_srcref_t;
template<class M>
static Ref get(M& m, T i, U j)
{
return mdetail::immediate_traits<M>::at(m, i, j);
}
template<class M>
static Srcref get(const M& m, T i, U j)
{
return mdetail::immediate_traits<M>::at(m, i, j);
}
};
};
} // matrices
// immediate functions
template<class Mat>
inline typename mp::enable_if<traits::is_mat<Mat>, slong>::type
rank(const Mat& m)
{
return m.rank();
}
template<class Mat>
inline typename mp::enable_if<traits::is_mat<Mat>, slong>::type
find_pivot_any(const Mat& m, slong start, slong end, slong c)
{
return m.find_pivot_any(start, end, c);
}
template<class Mat>
inline typename mp::enable_if<traits::is_mat<Mat>, slong>::type
find_pivot_partial(const Mat& m, slong start, slong end, slong c)
{
return m.find_pivot_partial(start, end, c);
}
} // flint
// Define rows(), cols(), create_temporary() and at() methods.
// For this to work, Traits must behave like the above generic traits.
// Also your matrix class must have a static create_temporary_rowscols function.
// See fmpz_mat for an example.
#define FLINTXX_DEFINE_MATRIX_METHODS(Traits) \
template<class T, class U> \
typename Traits::template at<T, U>::entry_ref_t at(T i, U j) \
{return Traits::template at<T, U>::get(*this, i, j);} \
template<class T, class U> \
typename Traits::template at<T, U>::entry_srcref_t at(T i, U j) const \
{return Traits::template at<T, U>::get(*this, i, j);} \
\
slong rows() const {return Traits::rows(*this);} \
slong cols() const {return Traits::cols(*this);} \
evaluated_t create_temporary() const \
{ \
return create_temporary_rowscols(*this, rows(), cols()); \
}
// Disable temporary merging. Requires create_temporary_rowscols.
// TODO do we really need the ltuple code everywhere?
#define FLINTXX_DEFINE_TEMPORARY_RULES(Matrix) \
namespace traits { \
template<> struct use_temporary_merging<Matrix> : mp::false_ { }; \
} /* traits */ \
namespace rules { \
template<class Expr> \
struct use_default_temporary_instantiation<Expr, Matrix> : mp::false_ { }; \
template<class Expr> \
struct instantiate_temporaries<Expr, Matrix, \
typename mp::disable_if<flint_classes::is_Base<Matrix, Expr> >::type> \
{ \
/* The only case where this should ever happen is if Expr is an ltuple */ \
/* TODO static assert this */ \
static Matrix get(const Expr& e) \
{ \
typedef typename Expr::operation_t op_t; \
return Matrix::create_temporary_rowscols(e, \
matrices::outsize<op_t>::rows(e), \
matrices::outsize<op_t>::cols(e)); \
} \
}; \
template<class Expr> \
struct instantiate_temporaries<Expr, Matrix, \
typename mp::enable_if<flint_classes::is_Base<Matrix, Expr> >::type> \
{ \
static Matrix get(const Expr& e) \
{ \
return e.create_temporary(); \
} \
}; \
} /* rules */
// Add a fflu() member function to the matrix class.
#define FLINTXX_DEFINE_MEMBER_FFLU \
template<class T> typename detail::nary_op_helper2<operations::fflu_op, \
typename base_t::derived_t, permxx*, T>::enable::type \
fflu(permxx* p, const T& t) const \
{ \
return flint::fflu(*this, p, t); \
}
#endif
|