File: sublattice_representation.cpp

package info (click to toggle)
regina-normal 5.1-6
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 54,488 kB
  • sloc: cpp: 142,029; ansic: 19,218; xml: 9,844; objc: 7,729; perl: 1,190; python: 623; sh: 614; makefile: 34
file content (437 lines) | stat: -rw-r--r-- 13,036 bytes parent folder | download | duplicates (2)
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
/*
 * 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  Integer
 */


#include "libnormaliz/sublattice_representation.h"
#include "libnormaliz/vector_operations.h"

//---------------------------------------------------------------------------

namespace libnormaliz {
using namespace std;

/**
 * creates a representation of Z^n as a sublattice of itself
 */
template<typename Integer>
Sublattice_Representation<Integer>::Sublattice_Representation(size_t n) {
    dim = n;
    rank = n;
    index = 1;
    A = Matrix<Integer>(n);
    B = Matrix<Integer>(n);
    c = 1;
    Equations_computed=false;
    Congruences_computed=false;
}

//---------------------------------------------------------------------------

/**
 * 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 Integer>
Sublattice_Representation<Integer>::Sublattice_Representation(const Matrix<Integer>& M, bool take_saturation) {
    bool success;
    initialize(M,take_saturation,success);
    if(!success){
        Matrix<mpz_class> mpz_M(M.nr,M.nc);
        mat_to_mpz(M,mpz_M);
        Sublattice_Representation<mpz_class> mpz_SLR;
        mpz_SLR.initialize(mpz_M,take_saturation,success);
        A=Matrix<Integer>(mpz_SLR.A.nr,mpz_SLR.A.nc);
        B=Matrix<Integer>(mpz_SLR.B.nr,mpz_SLR.B.nc);
        mat_to_Int(mpz_SLR.A,A);
        mat_to_Int(mpz_SLR.B,B);
        index=mpz_SLR.index;
        convert(c, mpz_SLR.c);
        rank=mpz_SLR.rank;        
    }
}


template<typename Integer>
void Sublattice_Representation<Integer>::initialize(const Matrix<Integer>& M, bool take_saturation, bool& success) {

    Equations_computed=false;
    Congruences_computed=false;

    success=true;

    dim=M.nr_of_columns();
    Matrix<Integer> N=M;    

    rank=N.row_echelon_reduce(success);
    if(!success)
        return;
    if (rank==0) {
        errorOutput()<<"warning: matrix has rank 0. Please check input data."<<endl;
    }
    
    
    if(rank==dim && take_saturation){
        A = B = Matrix<Integer>(dim);
        index=1;
        for(size_t k=0;k<dim;++k)
            index*=convertTo<mpz_class>(N[k][k]);
        index=Iabs(index);
        c=1;
        // cout << "Ausgang 0" << endl;
        return;   
    }
    
    A=Matrix<Integer>(rank, dim);
    B=Matrix<Integer>(dim,rank);
    
    mpz_class row_index=1;
    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<Integer>(N[k],-1);
        row_index*=convertTo<mpz_class>(N[k][j]);
    }
    
    if(row_index==1){  // ==> index=1, sublattice is saturated and we can take a projection
    
        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;
                j++;
            }
        }
        index=1;
        c=1;
        return;               
    }
    
    if(!take_saturation){
        Matrix<Integer> P(dim,dim);  // A augmented by unit vectors to full rank
        for(size_t k=0;k<rank;++k)
            A[k]=P[k]=N[k];
        size_t k=rank;
        for(size_t j=0;j<dim;++j){
            if(col_is_corner[j])
                continue;
            P[k][j]=1;
            k++;        
        }
        Matrix<Integer> Q=P.invert_unprotected(c,success);
        if(!success)
            return;
        index=1;
        
        for(k=0;k<dim;++k)
            for(size_t j=0;j<rank;++j)
                B[k][j]=Q[k][j];
        return;               
    }
    
    // now we must take the saturation
    
    Matrix<Integer> R_inv(dim);
    success=N.column_trigonalize(rank,R_inv);
    Matrix<Integer> R=R_inv.invert_unprotected(c,success);   // yields c=1 as it should be in this case
    if(!success)
        return;
    for (size_t i = 0; i < rank; i++) {
        for (size_t j = 0; j < dim; j++) {
                A[i][j]=R[i][j];
                B[j][i]=R_inv[j][i];
        }
    }
    index=1;
    for(size_t k=0;k<rank;++k)
        index*=convertTo<mpz_class>(N[k][k]);
    return; 
}



//---------------------------------------------------------------------------
//                       Manipulation operations
//---------------------------------------------------------------------------

/* first this then SR when going from Z^n to Z^r */
template<typename Integer>
void Sublattice_Representation<Integer>::compose(const Sublattice_Representation& SR) {
    assert(rank == SR.dim); //TODO vielleicht doch exception?
    
    Equations_computed=false;
    Congruences_computed=false;

    rank = SR.rank;
    index = SR.index;
    // A = SR.A * A
    A = SR.A.multiplication(A);
    // B = B * SR.B
    B = B.multiplication(SR.B);
    c = c * SR.c;
    
    //check if a factor can be extraced from B  //TODO necessary?
    Integer g = B.matrix_gcd();
    g = libnormaliz::gcd(g,c);  //TODO necessary??
    if (g > 1) {
        c /= g;
        B.scalar_division(g);
    }
}

//---------------------------------------------------------------------------
//                       Transformations
//---------------------------------------------------------------------------

template<typename Integer>
Matrix<Integer> Sublattice_Representation<Integer>::to_sublattice (const Matrix<Integer>& M) const {
    Matrix<Integer> N = M.multiplication(B);
    if (c!=1) N.scalar_division(c);
    return N;
}

template<typename Integer>
Matrix<Integer> Sublattice_Representation<Integer>::from_sublattice (const Matrix<Integer>& M) const {
    Matrix<Integer> N = M.multiplication(A);
    return N;
}

template<typename Integer>
Matrix<Integer> Sublattice_Representation<Integer>::to_sublattice_dual (const Matrix<Integer>& M) const {
    Matrix<Integer> N = M.multiplication(A.transpose());
    N.make_prime();
    return N;
}

template<typename Integer>
Matrix<Integer> Sublattice_Representation<Integer>::from_sublattice_dual (const Matrix<Integer>& M) const {
    Matrix<Integer> N = M.multiplication(B.transpose());
    N.make_prime();
    return N;
}


template<typename Integer>
vector<Integer> Sublattice_Representation<Integer>::to_sublattice (const vector<Integer>& V) const {
    vector<Integer> N = B.VxM(V);
    if (c!=1) v_scalar_division(N,c);
    return N;
}

template<typename Integer>
vector<Integer> Sublattice_Representation<Integer>::from_sublattice (const vector<Integer>& V) const {
    vector<Integer> N = A.VxM(V);
    return N;
}

template<typename Integer>
vector<Integer> Sublattice_Representation<Integer>::to_sublattice_dual (const vector<Integer>& V) const {
    vector<Integer> N = A.MxV(V);
    v_make_prime(N);
    return N;
}

template<typename Integer>
vector<Integer> Sublattice_Representation<Integer>::from_sublattice_dual (const vector<Integer>& V) const {
    vector<Integer> N = B.MxV(V);
    v_make_prime(N);
    return N;
}

template<typename Integer>
vector<Integer> Sublattice_Representation<Integer>::to_sublattice_dual_no_div (const vector<Integer>& V) const {
    vector<Integer> N = A.MxV(V);
    return N;
}

//---------------------------------------------------------------------------
//                       Data access
//---------------------------------------------------------------------------

/* returns the dimension of the ambient space */
template<typename Integer>
size_t Sublattice_Representation<Integer>::getDim() const {
    return dim;
}

//---------------------------------------------------------------------------

/* returns the rank of the sublattice */
template<typename Integer>
size_t Sublattice_Representation<Integer>::getRank() const {
    return rank;
}

//---------------------------------------------------------------------------

// returns the index of the sublattice */

/*
template<typename Integer>
mpz_class Sublattice_Representation<Integer>::get_index() const {
    return index;
}
*/
//---------------------------------------------------------------------------

template<typename Integer>
const Matrix<Integer>& Sublattice_Representation<Integer>::getEmbeddingMatrix() const {
    return A;
} 

template<typename Integer>
const vector<vector<Integer> >& Sublattice_Representation<Integer>::getEmbedding() const{
    return getEmbeddingMatrix().get_elements();
}

//---------------------------------------------------------------------------

template<typename Integer>
const Matrix<Integer>& Sublattice_Representation<Integer>::getProjectionMatrix() const {
    return B;
}

template<typename Integer>
const vector<vector<Integer> >& Sublattice_Representation<Integer>::getProjection() const{
    return getProjectionMatrix().get_elements();
}


//---------------------------------------------------------------------------

template<typename Integer>
Integer Sublattice_Representation<Integer>::getAnnihilator() const {
    return c;
}

//---------------------------------------------------------------------------

/* returns the congruences defining the sublattice */

template<typename Integer>
const Matrix<Integer>& Sublattice_Representation<Integer>::getEquationsMatrix() const{

    if(!Equations_computed)
        make_equations();
    return Equations;
}

template<typename Integer>
const vector<vector<Integer> >& Sublattice_Representation<Integer>::getEquations() const{
        return getEquationsMatrix().get_elements();
}

template<typename Integer>
void Sublattice_Representation<Integer>::make_equations() const{

    if(rank==dim)
        Equations=Matrix<Integer>(0,dim);
    else
        Equations=A.kernel();    
    Equations_computed=true;
}

template<typename Integer>
const Matrix<Integer>& Sublattice_Representation<Integer>::getCongruencesMatrix() const{

    if(!Congruences_computed)
        make_congruences();
    return Congruences;
}

template<typename Integer>
const vector<vector<Integer> >& Sublattice_Representation<Integer>::getCongruences() const{
    return getCongruencesMatrix().get_elements();
}

template<typename Integer>
mpz_class Sublattice_Representation<Integer>::getExternalIndex() const{

    if(!Congruences_computed)
        make_congruences();
    return external_index;
}

template<typename Integer>
void Sublattice_Representation<Integer>::make_congruences() const {

    if ( c == 1) { // no congruences then
        Congruences=Matrix<Integer>(0,dim+1);
        Congruences_computed=true;
        external_index=1;
        return;
    }
    
    size_t dummy;
    Matrix<Integer> A_Copy=A;
    Matrix<Integer> Transf=A_Copy.SmithNormalForm(dummy);

    // Congruences given by first rank columns of Transf transposed and with an extra column for the modulus m
    // The moduli are the diagonal elements of the Smith normal form
    
    // Transf.pretty_print(cout);

    Transf.append(Matrix<Integer>(1,dim));
    Transf = Transf.transpose();
    Matrix<Integer> Transf2(0,dim+1); //only the relavant congruences
    for(size_t k=0;k<rank;++k){
        if(A_Copy[k][k]!=1){
            Transf2.append(Transf[k]);
            Transf2[Transf2.nr-1][dim]=A_Copy[k][k];
            for(size_t j=0;j<dim;++j){
                Transf2[Transf2.nr-1][j]%=A_Copy[k][k];
                if(Transf2[Transf2.nr-1][j]<0)
                    Transf2[Transf2.nr-1][j]+=A_Copy[k][k];
            }
        
        }   
    }
    Congruences=Transf2;
    Congruences_computed=true;
    external_index=1;
    for(size_t i=0;i<Transf2.nr;++i)
        external_index*=convertTo<mpz_class>(Transf2[i][dim]);
}

}