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 / DST / include / libnormaliz / vector_operations.h
Views: 418425/*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*/22//---------------------------------------------------------------------------23#ifndef VECTOR_OPERATIONS_H24#define VECTOR_OPERATIONS_H25//---------------------------------------------------------------------------2627#include <vector>28#include <ostream>29#include <list>3031#include <libnormaliz/libnormaliz.h>32#include <libnormaliz/integer.h>33#include <libnormaliz/convert.h>3435namespace libnormaliz {36using std::vector;3738//---------------------------------------------------------------------------39// Data access40//---------------------------------------------------------------------------4142template <typename T>43std::ostream& operator<< (std::ostream& out, const vector<T>& vec) {44for (size_t i=0; i<vec.size(); ++i) {45out << vec[i] << " ";46}47out << std::endl;48return out;49}5051//---------------------------------------------------------------------------52// Vector operations53//---------------------------------------------------------------------------54template<typename Integer>55Integer v_scalar_product(const vector<Integer>& a,const vector<Integer>& b);5657template<typename Integer>58bool v_scalar_product_nonnegative(const vector<Integer>& a,const vector<Integer>& b);5960template<typename Integer>61bool v_scalar_product_positive(const vector<Integer>& a,const vector<Integer>& b);6263//returns the scalar product of the truncations of vectors a and b to minimum of lengths64template<typename Integer>65Integer v_scalar_product_vectors_unequal_lungth(const vector<Integer>& a,const vector<Integer>& b);6667//returns the addition a + b, vectors must be of equal size68template<typename Integer>69vector<Integer> v_add(const vector<Integer>& a,const vector<Integer>& b);70template<typename Integer>71vector<Integer> v_add_overflow_check(const vector<Integer>& a,const vector<Integer>& b);72template<typename Integer>73void v_add_result(vector<Integer>& result, const size_t length, const vector<Integer>& a,const vector<Integer>& b);7475//adds b to a reduces the result modulo m, a and b must be reduced modulo m!76template<typename Integer>77vector<Integer>& v_add_to_mod(vector<Integer>& a, const vector<Integer>& b, const Integer& m);7879//---------------------------------------------------------------------------80// abs, gcd and lcm81//---------------------------------------------------------------------------8283// takes the absolute value of the elements and returns a reference to the changed vector84template<typename Integer>85vector<Integer>& v_abs(vector<Integer>& v);8687// returns the vector of absolute values, does not change the argument88template<typename Integer>89vector<Integer> v_abs_value(vector<Integer>& v);9091//returns gcd of the elements of v92template<typename Integer>93Integer v_gcd(const vector<Integer>& v);9495//returns lcm of the elements of v96template<typename Integer>97Integer v_lcm(const vector<Integer>& v);9899//returns lcm of the elements of v from index k up to index j100template<typename Integer>101Integer v_lcm_to(const vector<Integer>& v,const size_t k, const size_t j);102103//divides the elements by their gcd and returns the gcd104template<typename Integer>105Integer v_make_prime(vector<Integer>& v);106107nmz_float l1norm(vector<nmz_float>& v);108109template<>110nmz_float v_make_prime<>(vector<nmz_float>& v);111112//---------------------------------------------------------------------------113// Scalar operations114//---------------------------------------------------------------------------115116//v = v * scalar117template<typename Integer>118void v_scalar_multiplication(vector<Integer>& v, const Integer scalar){119size_t i,size=v.size();120for (i = 0; i <size; i++) {121v[i] *= scalar;122}123}124125//---------------------------------------------------------------------------126127template<typename Integer>128void v_scalar_division(vector<Integer>& v, const Integer scalar){129size_t i,size=v.size();130for (i = 0; i <size; i++) {131assert(v[i]%scalar == 0);132v[i] /= scalar;133}134}135136//returns v * scalar mod modulus137template<typename Integer>138vector<Integer> v_scalar_mult_mod(const vector<Integer>& v, const Integer& scalar, const Integer& modulus, bool& success);139140template<typename Integer>141void v_reduction_modulo(vector<Integer>& v, const Integer& modulo);142//v = v mod modulo143144//---------------------------------------------------------------------------145// Test146//---------------------------------------------------------------------------147148template<typename Integer>149bool v_test_scalar_product(const vector<Integer>& a,const vector<Integer>& b, const Integer& result, const long& m);150// test the main computation for arithmetic overflow151// uses multiplication mod m152153//---------------------------------------------------------------------------154// General vector operations155//---------------------------------------------------------------------------156157//returns a new vector with the content of a extended by b158template<typename T>159vector<T> v_merge(const vector<T>& a, const T& b);160161//returns a new vector with the content of a and b162template<typename T>163vector<T> v_merge(const vector<T>& a, const vector<T>& b);164165//returns a new vector with the last size entries of v166template<typename T>167vector<T> v_cut_front(const vector<T>& v, size_t size);168169//the input vectors must be ordered of equal size170//if u is different from v by just one element, it returns that element171//else returns 0 (the elements of u and v are >0)172//int v_difference_ordered_fast(const vector<size_t>& u,const vector<size_t>& v);173174175template<typename Integer>176bool compare_last (const vector<Integer>& a, const vector<Integer>& b)177{178return a.back() < b.back();179}180181//returns a key vector containing the positions of non-zero entrys of v182template<typename Integer>183vector<key_t> v_non_zero_pos(const vector<Integer>& v);184185// counts the number of positive entries186template<typename Integer>187size_t v_nr_positive(const vector<Integer>& v);188189template<typename Integer>190size_t v_nr_negative(const vector<Integer>& v);191192template<typename Integer>193bool v_non_negative(const vector<Integer>& v);194195// check whether the vector only contains 0196template<typename Integer>197bool v_is_zero(const vector<Integer>& v);198199template<typename Integer>200bool v_is_symmetric(const vector<Integer>& v);201202template<typename Integer>203bool v_is_nonnegative(const vector<Integer>& v);204205template<typename Integer>206Integer v_max_abs(const vector<Integer>& v){207Integer tmp = 0;208for (size_t i=0; i<v.size(); i++){209if (Iabs(v[i])>tmp) tmp=Iabs(v[i]);210}211return tmp;212}213214//---------------------------------------------------------------------------215// bool vector operations216//---------------------------------------------------------------------------217218vector<bool> v_bool_andnot(const vector<bool>& a, const vector<bool>& b);219220// swaps entry i and j of the vector<bool> v221void v_bool_entry_swap(vector<bool>& v, size_t i, size_t j);222223//---------------------------------------------------------------------------224// Special225//---------------------------------------------------------------------------226227// computes integral simplex containing a rational vector228template<typename Integer>229void approx_simplex(const vector<Integer>& q, std::list<vector<Integer> >& approx,const long k);230231vector<key_t> identity_key(size_t n);232233// compute the degree vector of a hsop234template<typename Integer>235vector<Integer> degrees_hsop(const vector<Integer> gen_degrees,const vector<size_t> heights);236237//---------------------------------------------------------------------------238// Sorting239//---------------------------------------------------------------------------240241template <typename T>242void order_by_perm(vector<T>& v, const vector<key_t>& permfix);243244// compare sizes of v_scalar_product_unequal_vectors_begin245246247} // namespace248249//---------------------------------------------------------------------------250#endif251//---------------------------------------------------------------------------252253254