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
|
/*
* Normaliz
* Copyright (C) 2007-2014 Winfried Bruns, Bogdan Ichim, Christof Soeger
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* As an exception, when this program is distributed through (i) the App Store
* by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or (iii) Google Play
* by Google Inc., then that store may impose any digital rights management,
* device limits and/or redistribution restrictions that are required by its
* terms of service.
*/
/**
* The class Sublattice_Representation represents a sublattice of Z^n as Z^r.
* To transform vectors of the sublattice use:
* Z^r --> Z^n and Z^n --> Z^r
* v |-> vA u |-> (uB)/c
* A r x n matrix
* B n x r matrix
* c Number
*
* We have kept c though it is always 1 for coefficients over a field
*/
#include "libQnormaliz/Qsublattice_representation.h"
#include "libQnormaliz/Qvector_operations.h"
//---------------------------------------------------------------------------
namespace libQnormaliz {
using namespace std;
/**
* creates a representation of Z^n as a sublattice of itself
*/
template<typename Number>
Sublattice_Representation<Number>::Sublattice_Representation(size_t n) {
dim = n;
rank = n;
external_index = 1;
A = Matrix<Number>(n);
B = Matrix<Number>(n);
c = 1;
Equations_computed=false;
is_identity=true;
}
//---------------------------------------------------------------------------
/**
* Main Constructor
* creates a representation of a sublattice of Z^n
* if direct_summand is false the sublattice is generated by the rows of M
* otherwise it is a direct summand of Z^n containing the rows of M
*/
template<typename Number>
Sublattice_Representation<Number>::Sublattice_Representation(const Matrix<Number>& M, bool take_saturation) {
initialize(M); // take saturation is complewtely irrelevant for coefficients in a field
}
template<typename Number>
void Sublattice_Representation<Number>::initialize(const Matrix<Number>& M) {
Equations_computed=false;
is_identity=false;
dim=M.nr_of_columns();
Matrix<Number> N=M;
bool success; // dummy for field coefficients
rank=N.row_echelon_reduce(success); // cleans corner columns and makes corner elements positive
if(rank==dim){
A = B = Matrix<Number>(dim);
c=1;
is_identity=true;
return;
}
vector<key_t> col(rank);
vector<bool> col_is_corner(dim,false);
for(size_t k=0;k<rank;++k){
size_t j=0;
for(;j<dim;++j)
if(N[k][j]!=0)
break;
col_is_corner[j]=true;
col[k]=j;
if(N[k][j]<0)
v_scalar_multiplication<Number>(N[k],-1);
}
A=Matrix<Number>(rank, dim);
B=Matrix<Number>(dim,rank);
for(size_t k=0;k<rank;++k)
A[k]=N[k];
size_t j=0;
for(size_t k=0;k<dim;++k){
if(col_is_corner[k]){
B[k][j]=1/A[j][k]; //to make the inverse of the diagonal matrix that we get
j++; // by extracting the corner columns
}
};
c=1;
return;
}
//---------------------------------------------------------------------------
// Constructor by conversion
//---------------------------------------------------------------------------
template<typename Number>
template<typename NumberFC>
Sublattice_Representation<Number>::Sublattice_Representation(const
Sublattice_Representation<NumberFC>& Original) {
convert(A,Original.A);
convert(B,Original.B);
dim=Original.dim;
rank=Original.rank;
convert(c,Original.c);
is_identity=Original.is_identity;
Equations_computed=Original.Equations_computed;
convert(Equations,Original.Equations);
external_index=Original.external_index;
}
//---------------------------------------------------------------------------
// Manipulation operations
//---------------------------------------------------------------------------
/* first this then SR when going from Z^n to Z^r */
template<typename Number>
void Sublattice_Representation<Number>::compose(const Sublattice_Representation& SR) {
assert(rank == SR.dim); //TODO vielleicht doch exception?
if(SR.is_identity)
return;
if(is_identity){
*this=SR;
return;
}
Equations_computed=false;
rank = SR.rank;
// A = SR.A * A
A = SR.A.multiplication(A);
// B = B * SR.B
B = B.multiplication(SR.B);
c = c * SR.c;
is_identity&=SR.is_identity;
}
template<typename Number>
void Sublattice_Representation<Number>::compose_dual(const Sublattice_Representation& SR) {
assert(rank == SR.dim); //
assert(SR.c==1);
if(SR.is_identity)
return;
Equations_computed=false;
rank = SR.rank;
if(is_identity){
A=SR.B.transpose();
B=SR.A.transpose();
is_identity=false;
return;
}
// Now we compose with the dual of SR
A = SR.B.transpose().multiplication(A);
// B = B * SR.B
B = B.multiplication(SR.A.transpose());
//check if a factor can be extraced from B //TODO necessary?
Number g=1; // = B.matrix_gcd();
is_identity&=SR.is_identity;
}
//---------------------------------------------------------------------------
// Transformations
//---------------------------------------------------------------------------
template<typename Number>
Matrix<Number> Sublattice_Representation<Number>::to_sublattice (const Matrix<Number>& M) const {
Matrix<Number> N;
if(is_identity)
N=M;
else
N = M.multiplication(B);
if (c!=1) N.scalar_division(c);
return N;
}
template<typename Number>
Matrix<Number> Sublattice_Representation<Number>::from_sublattice (const Matrix<Number>& M) const {
Matrix<Number> N;
if(is_identity)
N=M;
else
N = M.multiplication(A);
return N;
}
template<typename Number>
Matrix<Number> Sublattice_Representation<Number>::to_sublattice_dual (const Matrix<Number>& M) const {
Matrix<Number> N;
if(is_identity)
N=M;
else
N = M.multiplication(A.transpose());
N.simplify_rows();
return N;
}
template<typename Number>
Matrix<Number> Sublattice_Representation<Number>::from_sublattice_dual (const Matrix<Number>& M) const {
Matrix<Number> N;
if(is_identity)
N=M;
else
N = M.multiplication(B.transpose());
N.simplify_rows();
return N;
}
template<typename Number>
vector<Number> Sublattice_Representation<Number>::to_sublattice (const vector<Number>& V) const {
if(is_identity)
return V;
vector<Number> N = B.VxM(V);
if (c!=1) v_scalar_division(N,c);
return N;
}
template<typename Number>
vector<Number> Sublattice_Representation<Number>::from_sublattice (const vector<Number>& V) const {
if(is_identity)
return V;
vector<Number> N = A.VxM(V);
return N;
}
template<typename Number>
vector<Number> Sublattice_Representation<Number>::to_sublattice_dual (const vector<Number>& V) const {
vector<Number> N;
vector<Number> dummy;
if(is_identity)
N=V;
else
N = A.MxV(V);
v_simplify(N,dummy);
return N;
}
template<typename Number>
vector<Number> Sublattice_Representation<Number>::from_sublattice_dual (const vector<Number>& V) const {
vector<Number> N;
vector<Number> dummy;
if(is_identity)
N=V;
else
N = B.MxV(V);
v_simplify(N,dummy);
return N;
}
template<typename Number>
vector<Number> Sublattice_Representation<Number>::to_sublattice_dual_no_div (const vector<Number>& V) const {
if(is_identity)
return V;
vector<Number> N = A.MxV(V);
return N;
}
//---------------------------------------------------------------------------
// Data access
//---------------------------------------------------------------------------
/* returns the dimension of the ambient space */
template<typename Number>
size_t Sublattice_Representation<Number>::getDim() const {
return dim;
}
//---------------------------------------------------------------------------
/* returns the rank of the sublattice */
template<typename Number>
size_t Sublattice_Representation<Number>::getRank() const {
return rank;
}
//---------------------------------------------------------------------------
template<typename Number>
const Matrix<Number>& Sublattice_Representation<Number>::getEmbeddingMatrix() const {
return A;
}
template<typename Number>
const vector<vector<Number> >& Sublattice_Representation<Number>::getEmbedding() const{
return getEmbeddingMatrix().get_elements();
}
//---------------------------------------------------------------------------
template<typename Number>
const Matrix<Number>& Sublattice_Representation<Number>::getProjectionMatrix() const {
return B;
}
template<typename Number>
const vector<vector<Number> >& Sublattice_Representation<Number>::getProjection() const{
return getProjectionMatrix().get_elements();
}
//---------------------------------------------------------------------------
template<typename Number>
Number Sublattice_Representation<Number>::getAnnihilator() const {
return c;
}
//---------------------------------------------------------------------------
template<typename Number>
bool Sublattice_Representation<Number>::IsIdentity() const{
return is_identity;
}
//---------------------------------------------------------------------------
template<typename Number>
const Matrix<Number>& Sublattice_Representation<Number>::getEquationsMatrix() const{
if(!Equations_computed)
make_equations();
return Equations;
}
template<typename Number>
const vector<vector<Number> >& Sublattice_Representation<Number>::getEquations() const{
return getEquationsMatrix().get_elements();
}
template<typename Number>
void Sublattice_Representation<Number>::make_equations() const{
if(rank==dim)
Equations=Matrix<Number>(0,dim);
else
Equations=A.kernel();
Equations.simplify_rows();
Equations_computed=true;
}
}
|