/usr/include/linbox/blackbox/scalar-matrix.h is in liblinbox-dev 1.4.2-5build1.
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 | /* linbox/blackbox/scalar.h
* Copyright (C) 2002 by -bds
* evolved from diagonal.h written by William J Turner and Bradford Hovinen
*
* -------------------------------
* Modified by Dmitriy Morozov <linbox@foxcub.org>. May 28, 2002.
*
* Added parametrization of VectorCategory tags by VectorTraits. See
* vector-traits.h for more details.
*
* ========LICENCE========
* This file is part of the library LinBox.
*
* LinBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ========LICENCE========
* -------------------------------
*/
#ifndef __LINBOX_scalar_H
#define __LINBOX_scalar_H
#include <algorithm>
#include <iostream>
#include "linbox/field/hom.h"
#include "linbox/vector/vector-traits.h"
#include "linbox/linbox-config.h"
#include "linbox/blackbox/blackbox-interface.h"
#include "linbox/solutions/solution-tags.h"
#include "linbox/util/matrix-stream.h"
#include "linbox/util/write-mm.h"
namespace LinBox
{
/** \brief Blackbox for <tt>aI</tt>. Use particularly for representing <tt>0</tt> and <tt>I</tt>.
* This is a class of blackbox square scalar matrices.
* Each scalar matrix occupies O(scalar-size) memory.
* The matrix itself is not stored in memory, just the scalar and the dimensions.
* \ingroup blackbox
*/
template <class Field_>
class ScalarMatrix : public BlackboxInterface {
public:
typedef Field_ Field;
typedef typename Field::Element Element;
typedef ScalarMatrix<Field> Self_t;
/* In each specialization, I must define suitable constructor(s) and
* BlackboxArchetype<Vector> * clone() const;
* Vector& apply(Vector& y, Vector& x) const;
* Vector& applyTranspose(Vector& y, Vector& x) const;
* size_t rowdim(void) const;
* size_t coldim(void) const;
* Field& field() const;
* ...rebind...
*/
/// Constructs an initially 0 by 0 matrix.
//! @bug this should not be allowed (unknown field)
ScalarMatrix () :
field_(NULL),
n_(0)
{}
ScalarMatrix( MatrixStream<Field> & ms) :
field_(&ms.field())
,n_(0)
{
size_t c, i, j;
if( !ms.getDimensions(n_, c) || c != n_ )
throw ms.reportError(__FUNCTION__,__LINE__);
ms.nextTriple(i, j, v_);
if (i != j) throw ms.reportError(__FUNCTION__,__LINE__);
// finalize();
}
void changeField(const Field &F)
{
field_ = &F ;
}
/** Constructor of readable scalar matrix.
* @param F field in which to do arithmetic.
*/
ScalarMatrix (const Field &F) :
field_(&F),
n_(0)
{}
#if 0
/** Scalar matrix Constructor from an element.
* @param F field in which to do arithmetic.
* @param n size of the matrix.
* @param s scalar, a field element, to be used as the diagonal of the matrix.
* @bug this is a wrong constructor, should be the following...
*/
ScalarMatrix (const Field &F, const size_t n, const Element &s) :
field_(&F), n_(n), v_(s)
{}
#endif
ScalarMatrix (const Field &F, const size_t n, const size_t m, const Element &s) :
field_(&F), n_(n), v_(s)
{
linbox_check(n ==m);
}
ScalarMatrix (const Field &F, const size_t n, const size_t m) :
field_(&F), n_(n), v_(0)
{
linbox_check(m==n);
}
/** Constructor from a random element.
* @param F field in which to do arithmetic.
* @param n size of the matrix.
* @param iter Random iterator from which to get the diagonal scalar element.
*/
ScalarMatrix (const Field &F, const size_t n, const typename Field::RandIter& iter) :
field_(&F), n_(n)
{ iter.random(v_); }
ScalarMatrix(const ScalarMatrix<Field> &Mat) :
field_(Mat.field_)
, n_(Mat.n_), v_(Mat.v_)
{
//n_ = Mat.n_;
//v_ = Mat.v_;
}
void setScalar(Element & x)
{
field().assign(v_, x) ;
}
/** Application of BlackBox matrix.
* y= A*x.
* Requires time linear in n, the size of the matrix.
*/
template<class OutVector, class InVector>
OutVector& apply(OutVector &y, InVector &x) const
{
//typename VectorTraits<InVector>::VectorCategory t;
//return _app (y, x, t);
return _app (y, x, VectorCategories::DenseVectorTag());
}
/** Application of BlackBox matrix transpose.
* y= transpose(A)*x.
* Requires time linear in n, the size of the matrix.
*/
template<class OutVector, class InVector>
OutVector& applyTranspose(OutVector &y, InVector &x) const
{ return apply(y, x); } // symmetric matrix.
template<typename _Tp1>
struct rebind {
typedef ScalarMatrix<_Tp1> other;
void operator() (other & Ap, const Self_t& A)
{
Hom<typename Self_t::Field, _Tp1> hom(A.field(), Ap.field());
typename _Tp1::Element e;
Ap.field().assign(e,Ap.field().zero);
hom.image (e, A.v_);
Ap.setScalar(e);
}
};
template<typename _Tp1>
ScalarMatrix (const ScalarMatrix<_Tp1>& S, const Field &F) :
field_(&F), n_(S.rowdim())
{
typename ScalarMatrix<_Tp1>::template rebind<Field>() (*this, S);
}
size_t rowdim(void) const { return n_; }
size_t coldim(void) const { return n_; }
const Field& field() const {return *field_;}
// for specialized solutions
Element& trace(Element& t) const
{ Element n; field().init(n, n_);
return field().mul(t, v_, n);
}
Element& getEntry(Element& x, const size_t i, const size_t j) const
{
// return (i==j ? field().assign(x,v_) : field().assign(x,field().zero));
return (i==j ? field().assign(x,v_) : field().assign(x,field().zero));
}
Element& det(Element& d) const
{
return pow(field(), d, v_, n_);
}
long int& rank(long int& r) const
{
return r = (field().isZero(v_) ? 0 : n_);
}
Element& getScalar(Element& x) const { return this->field().assign(x,this->v_); }
Element& setScalar(const Element& x) { return this->field().assign(this->v_,x); }
std::ostream& write(std::ostream& os) const {
writeMMCoordHeader(os, *this, 1, "ScalarMatrix");
field().write(os << "1 1 ", v_) << std::endl;
return os;
}
std::istream& read(std::istream& is) {
MatrixStream<Field> ms(field(), is);
size_t c, i, j;
if( !ms.getDimensions(n_, c) || c != n_ )
throw ms.reportError(__FUNCTION__,__LINE__);
ms.nextTriple(i, j, v_);
if (i != j) throw ms.reportError(__FUNCTION__,__LINE__);
return is;
}
protected:
const Field *field_; // Field for arithmetic
size_t n_; // Number of rows and columns of square matrix.
Element v_; // the scalar used in applying matrix.
// dense vector _app for apply
template<class OutVector, class InVector>
OutVector& _app (OutVector &y, const InVector &x, VectorCategories::DenseVectorTag) const;
// The third argument is just a device to let overloading determine the method.
// sparse sequence vector _app for apply
template <class OutVector, class InVector>
OutVector& _app (OutVector &y, const InVector &x, VectorCategories::SparseSequenceVectorTag) const;
// sparse associative vector _app for apply
template<class OutVector, class InVector>
OutVector& _app (OutVector &y, const InVector &x, VectorCategories::SparseAssociativeVectorTag) const;
// p <- a^e. Really should be a field op
Element& pow(Field& F, Element& p, const Element& a, const size_t e) {
Element x; F.init(x);
if (e == 0) return F.assign(p, F.one);
if (e%2 == 0) return pow(F, p, F.mul(x, a, a), e/2);
else /* (e%2 == 1)*/ return F.mul(p, a, pow(F, p, a, e-1));
}
}; // template <Field> class ScalarMatrix
// dense vector _app
template <class Field>
template <class OutVector, class InVector>
inline OutVector &ScalarMatrix<Field>::
_app(OutVector& y, const InVector& x, VectorCategories::DenseVectorTag t) const
{
linbox_check (x.size() >= n_);
linbox_check (y.size() >= n_);
typename OutVector::iterator y_iter = y.begin ();
if (field().isZero(v_)) // just write zeroes
for ( ; y_iter != y.end (); ++y_iter) *y_iter = v_;
else if (field().isOne(v_) ) // just copy
std::copy(x.begin(), x.end(), y.begin());
else // use actual muls
{ typename InVector::const_iterator x_iter = x.begin ();
for ( ; y_iter != y.end () ; ++y_iter, ++x_iter )
field().mul (*y_iter, v_, *x_iter);
}
return y;
} // dense vector _app
// sparse sequence vector _app
template <class Field>
template <class OutVector, class InVector>
inline OutVector &ScalarMatrix<Field>::
_app(OutVector& y, const InVector& x, VectorCategories::SparseSequenceVectorTag t) const
{
//linbox_check ((!x.empty ()) && (n_ < x.back ().first));
// neither is required of x ?
y.clear (); // we'll overwrite using push_backs.
// field element to be used in calculations
Element entry;
field().assign(entry, field().zero);
// For each element, multiply input element with corresponding element
// of stored scalar and insert non-zero elements into output vector
for ( typename InVector::const_iterator x_iter = x.begin (); x_iter != x.end (); ++x_iter)
{ field().mul (entry, v_, x_iter->second);
if (!field().isZero (entry)) y.push_back (make_pair (x_iter->first, entry));
}
return y;
} // sparse sequence vector _app
// sparse associative vector _app
template <class Field>
template <class OutVector, class InVector>
inline OutVector& ScalarMatrix<Field> ::
_app(OutVector& y, const InVector& x, VectorCategories::SparseAssociativeVectorTag t) const
{
y.clear (); // we'll overwrite using inserts
// create field elements and size_t to be used in calculations
Element entry;
field().assign(entry, field().zero);
// Iterator over indices of input vector.
// For each element, multiply input element with
// stored scalar and insert non-zero elements into output vector
for ( typename InVector::const_iterator x_iter = x.begin (); x_iter != x.end (); ++x_iter)
{ field().mul (entry, v_, x_iter->second);
if (!field().isZero (entry)) y.insert (y.end (), make_pair (x_iter->first, entry));
}
return y;
} // sparse associative vector _app
// let solutions know we have getEntry() and trace().
template <class Field>
struct GetEntryCategory<ScalarMatrix<Field> >
{ typedef SolutionTags::Local Tag; };
template <class Field>
struct TraceCategory<ScalarMatrix<Field> >
{ typedef SolutionTags::Local Tag; };
template <class Field>
struct DetCategory<ScalarMatrix<Field> >
{ typedef SolutionTags::Local Tag; };
template <class Field>
struct RankCategory<ScalarMatrix<Field> >
{ typedef SolutionTags::Local Tag; };
} // namespace LinBox
#endif // __LINBOX_scalar_H
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End:
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
|