Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Path: gap4r8 / pkg / NormalizInterface-1.0.2 / Normaliz.git / Qsource / libQnormaliz / Qsublattice_representation.cpp
Views: 418384/*1* Normaliz2* Copyright (C) 2007-2014 Winfried Bruns, Bogdan Ichim, Christof Soeger3* This program is free software: you can redistribute it and/or modify4* it under the terms of the GNU General Public License as published by5* the Free Software Foundation, either version 3 of the License, or6* (at your option) any later version.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License14* along with this program. If not, see <http://www.gnu.org/licenses/>.15*16* As an exception, when this program is distributed through (i) the App Store17* by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or (iii) Google Play18* by Google Inc., then that store may impose any digital rights management,19* device limits and/or redistribution restrictions that are required by its20* terms of service.21*/2223/**24* The class Sublattice_Representation represents a sublattice of Z^n as Z^r.25* To transform vectors of the sublattice use:26* Z^r --> Z^n and Z^n --> Z^r27* v |-> vA u |-> (uB)/c28* A r x n matrix29* B n x r matrix30* c Number31*32* We have kept c though it is always 1 for coefficients over a field33*/343536#include "libQnormaliz/Qsublattice_representation.h"37#include "libQnormaliz/Qvector_operations.h"3839//---------------------------------------------------------------------------4041namespace libQnormaliz {42using namespace std;4344/**45* creates a representation of Z^n as a sublattice of itself46*/47template<typename Number>48Sublattice_Representation<Number>::Sublattice_Representation(size_t n) {49dim = n;50rank = n;51external_index = 1;52A = Matrix<Number>(n);53B = Matrix<Number>(n);54c = 1;55Equations_computed=false;56is_identity=true;57}5859//---------------------------------------------------------------------------6061/**62* Main Constructor63* creates a representation of a sublattice of Z^n64* if direct_summand is false the sublattice is generated by the rows of M65* otherwise it is a direct summand of Z^n containing the rows of M66*/6768template<typename Number>69Sublattice_Representation<Number>::Sublattice_Representation(const Matrix<Number>& M, bool take_saturation) {70initialize(M); // take saturation is complewtely irrelevant for coefficients in a field71}727374template<typename Number>75void Sublattice_Representation<Number>::initialize(const Matrix<Number>& M) {7677Equations_computed=false;78is_identity=false;7980dim=M.nr_of_columns();81Matrix<Number> N=M;8283bool success; // dummy for field coefficients84rank=N.row_echelon_reduce(success); // cleans corner columns and makes corner elements positive8586if(rank==dim){87A = B = Matrix<Number>(dim);88c=1;89is_identity=true;90return;91}9293vector<key_t> col(rank);94vector<bool> col_is_corner(dim,false);95for(size_t k=0;k<rank;++k){96size_t j=0;97for(;j<dim;++j)98if(N[k][j]!=0)99break;100col_is_corner[j]=true;101col[k]=j;102if(N[k][j]<0)103v_scalar_multiplication<Number>(N[k],-1);104}105106A=Matrix<Number>(rank, dim);107B=Matrix<Number>(dim,rank);108109for(size_t k=0;k<rank;++k)110A[k]=N[k];111size_t j=0;112for(size_t k=0;k<dim;++k){113if(col_is_corner[k]){114B[k][j]=1/A[j][k]; //to make the inverse of the diagonal matrix that we get115j++; // by extracting the corner columns116}117};118c=1;119return;120121}122123//---------------------------------------------------------------------------124// Constructor by conversion125//---------------------------------------------------------------------------126127template<typename Number>128template<typename NumberFC>129Sublattice_Representation<Number>::Sublattice_Representation(const130Sublattice_Representation<NumberFC>& Original) {131132convert(A,Original.A);133convert(B,Original.B);134dim=Original.dim;135rank=Original.rank;136convert(c,Original.c);137is_identity=Original.is_identity;138Equations_computed=Original.Equations_computed;139convert(Equations,Original.Equations);140external_index=Original.external_index;141}142143144//---------------------------------------------------------------------------145// Manipulation operations146//---------------------------------------------------------------------------147148/* first this then SR when going from Z^n to Z^r */149template<typename Number>150void Sublattice_Representation<Number>::compose(const Sublattice_Representation& SR) {151assert(rank == SR.dim); //TODO vielleicht doch exception?152153if(SR.is_identity)154return;155156if(is_identity){157*this=SR;158return;159}160161Equations_computed=false;162163164rank = SR.rank;165// A = SR.A * A166A = SR.A.multiplication(A);167// B = B * SR.B168B = B.multiplication(SR.B);169c = c * SR.c;170171is_identity&=SR.is_identity;172}173174template<typename Number>175void Sublattice_Representation<Number>::compose_dual(const Sublattice_Representation& SR) {176177assert(rank == SR.dim); //178assert(SR.c==1);179180if(SR.is_identity)181return;182183Equations_computed=false;184rank = SR.rank;185186if(is_identity){187A=SR.B.transpose();188B=SR.A.transpose();189is_identity=false;190return;191}192193// Now we compose with the dual of SR194A = SR.B.transpose().multiplication(A);195// B = B * SR.B196B = B.multiplication(SR.A.transpose());197198//check if a factor can be extraced from B //TODO necessary?199Number g=1; // = B.matrix_gcd();200is_identity&=SR.is_identity;201}202203//---------------------------------------------------------------------------204// Transformations205//---------------------------------------------------------------------------206207template<typename Number>208Matrix<Number> Sublattice_Representation<Number>::to_sublattice (const Matrix<Number>& M) const {209Matrix<Number> N;210if(is_identity)211N=M;212else213N = M.multiplication(B);214if (c!=1) N.scalar_division(c);215return N;216}217template<typename Number>218Matrix<Number> Sublattice_Representation<Number>::from_sublattice (const Matrix<Number>& M) const {219Matrix<Number> N;220if(is_identity)221N=M;222else223N = M.multiplication(A);224return N;225}226227template<typename Number>228Matrix<Number> Sublattice_Representation<Number>::to_sublattice_dual (const Matrix<Number>& M) const {229Matrix<Number> N;230if(is_identity)231N=M;232else233N = M.multiplication(A.transpose());234N.simplify_rows();235return N;236}237238template<typename Number>239Matrix<Number> Sublattice_Representation<Number>::from_sublattice_dual (const Matrix<Number>& M) const {240Matrix<Number> N;241if(is_identity)242N=M;243else244N = M.multiplication(B.transpose());245N.simplify_rows();246return N;247}248249250template<typename Number>251vector<Number> Sublattice_Representation<Number>::to_sublattice (const vector<Number>& V) const {252if(is_identity)253return V;254vector<Number> N = B.VxM(V);255if (c!=1) v_scalar_division(N,c);256return N;257}258259template<typename Number>260vector<Number> Sublattice_Representation<Number>::from_sublattice (const vector<Number>& V) const {261if(is_identity)262return V;263vector<Number> N = A.VxM(V);264return N;265}266267template<typename Number>268vector<Number> Sublattice_Representation<Number>::to_sublattice_dual (const vector<Number>& V) const {269vector<Number> N;270if(is_identity)271N=V;272else273N = A.MxV(V);274v_simplify(N);275return N;276}277278template<typename Number>279vector<Number> Sublattice_Representation<Number>::from_sublattice_dual (const vector<Number>& V) const {280vector<Number> N;281if(is_identity)282N=V;283else284N = B.MxV(V);285v_simplify(N);286return N;287}288289template<typename Number>290vector<Number> Sublattice_Representation<Number>::to_sublattice_dual_no_div (const vector<Number>& V) const {291if(is_identity)292return V;293vector<Number> N = A.MxV(V);294return N;295}296297//---------------------------------------------------------------------------298// Data access299//---------------------------------------------------------------------------300301/* returns the dimension of the ambient space */302template<typename Number>303size_t Sublattice_Representation<Number>::getDim() const {304return dim;305}306307//---------------------------------------------------------------------------308309/* returns the rank of the sublattice */310template<typename Number>311size_t Sublattice_Representation<Number>::getRank() const {312return rank;313}314315//---------------------------------------------------------------------------316317template<typename Number>318const Matrix<Number>& Sublattice_Representation<Number>::getEmbeddingMatrix() const {319return A;320}321322template<typename Number>323const vector<vector<Number> >& Sublattice_Representation<Number>::getEmbedding() const{324return getEmbeddingMatrix().get_elements();325}326327//---------------------------------------------------------------------------328329template<typename Number>330const Matrix<Number>& Sublattice_Representation<Number>::getProjectionMatrix() const {331return B;332}333334template<typename Number>335const vector<vector<Number> >& Sublattice_Representation<Number>::getProjection() const{336return getProjectionMatrix().get_elements();337}338339340//---------------------------------------------------------------------------341342template<typename Number>343Number Sublattice_Representation<Number>::getAnnihilator() const {344return c;345}346347//---------------------------------------------------------------------------348349template<typename Number>350bool Sublattice_Representation<Number>::IsIdentity() const{351return is_identity;352}353354//---------------------------------------------------------------------------355356357template<typename Number>358const Matrix<Number>& Sublattice_Representation<Number>::getEquationsMatrix() const{359360if(!Equations_computed)361make_equations();362return Equations;363}364365template<typename Number>366const vector<vector<Number> >& Sublattice_Representation<Number>::getEquations() const{367return getEquationsMatrix().get_elements();368}369370template<typename Number>371void Sublattice_Representation<Number>::make_equations() const{372373if(rank==dim)374Equations=Matrix<Number>(0,dim);375else376Equations=A.kernel();377Equations.simplify_rows();378Equations_computed=true;379}380381382}383384385