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 / Qvector_operations.cpp
Views: 418386/*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//---------------------------------------------------------------------------2425#include <iostream>26#include <string>27#include <algorithm>28#include <list>2930#include "libQnormaliz/Qinteger.h"31#include "libQnormaliz/Qvector_operations.h"32#include "libQnormaliz/Qmatrix.h"3334//---------------------------------------------------------------------------3536namespace libQnormaliz {37using namespace std;3839//---------------------------------------------------------------------------4041template<typename Number>42Number v_scalar_product(const vector<Number>& av,const vector<Number>& bv){43//loop stretching ; brings some small speed improvement4445Number ans = 0;46size_t i,n=av.size();4748typename vector<Number>::const_iterator a=av.begin(), b=bv.begin();4950if( n >= 16 )51{52for( i = 0; i < ( n >> 4 ); ++i, a += 16, b +=16 ){53ans += a[0] * b[0];54ans += a[1] * b[1];55ans += a[2] * b[2];56ans += a[3] * b[3];57ans += a[4] * b[4];58ans += a[5] * b[5];59ans += a[6] * b[6];60ans += a[7] * b[7];61ans += a[8] * b[8];62ans += a[9] * b[9];63ans += a[10] * b[10];64ans += a[11] * b[11];65ans += a[12] * b[12];66ans += a[13] * b[13];67ans += a[14] * b[14];68ans += a[15] * b[15];69}7071n -= i<<4;72}7374if( n >= 8)75{76ans += a[0] * b[0];77ans += a[1] * b[1];78ans += a[2] * b[2];79ans += a[3] * b[3];80ans += a[4] * b[4];81ans += a[5] * b[5];82ans += a[6] * b[6];83ans += a[7] * b[7];8485n -= 8;86a += 8;87b += 8;88}8990if( n >= 4)91{92ans += a[0] * b[0];93ans += a[1] * b[1];94ans += a[2] * b[2];95ans += a[3] * b[3];9697n -= 4;98a += 4;99b += 4;100}101102if( n >= 2)103{104ans += a[0] * b[0];105ans += a[1] * b[1];106107n -= 2;108a += 2;109b += 2;110}111112if(n>0)113ans += a[0]*b[0];114115return ans;116}117118//---------------------------------------------------------------------------119120template<typename Number>121Number v_scalar_product_unequal_vectors_end(const vector<Number>& a,const vector<Number>& b){122Number ans = 0;123size_t i,n=a.size(),m=b.size();124for (i = 1; i <= n; i++) {125ans+=a[n-i]*b[m-i];126}127return ans;128}129130//---------------------------------------------------------------------------131132template<typename Number>133vector<Number> v_add(const vector<Number>& a,const vector<Number>& b){134assert(a.size() == b.size());135size_t i,s=a.size();136vector<Number> d(s);137for (i = 0; i <s; i++) {138d[i]=a[i]+b[i];139}140return d;141}142143//---------------------------------------------------------------------------144145template<typename Number>146void v_add_result(vector<Number>& result, const size_t s, const vector<Number>& a,const vector<Number>& b){147assert(a.size() == b.size() && a.size() == result.size());148size_t i;149// vector<Number> d(s);150for (i = 0; i <s; i++) {151result[i]=a[i]+b[i];152}153// return d;154}155156//---------------------------------------------------------------------------157158template<typename Number>159vector<Number>& v_abs(vector<Number>& v){160size_t i, size=v.size();161for (i = 0; i < size; i++) {162if (v[i]<0) v[i] = Iabs(v[i]);163}164return v;165}166167//---------------------------------------------------------------------------168169template<typename Number>170vector<Number> v_abs_value(vector<Number>& v){171size_t i, size=v.size();172vector<Number> w=v;173for (i = 0; i < size; i++) {174if (v[i]<0) w[i] = Iabs(v[i]);175}176return w;177}178179//---------------------------------------------------------------------------180181// the following function removes the denominators and then extracts the Gcd of the numerators182mpq_class v_simplify(vector<mpq_class>& v){183size_t size=v.size();184mpz_class d=1;185for (size_t i = 0; i < size; i++)186//d=lcm(d,v[i].get_den()); // GMP C++ function only available in GMP >= 6.1187mpz_lcm(d.get_mpz_t(), d.get_mpz_t(), v[i].get_den().get_mpz_t());188for (size_t i = 0; i < size; i++)189v[i]*=d;190mpz_class g=0;191for (size_t i = 0; i < size; i++)192//g=gcd(g,v[i].get_num()); // GMP C++ function only available in GMP >= 6.1193mpz_gcd(g.get_mpz_t(), g.get_mpz_t(), v[i].get_num().get_mpz_t());194if (g==0)195return 0;196for (size_t i = 0; i < size; i++)197v[i]/=g;198return 1;199}200201//---------------------------------------------------------------------------202203template<typename T>204vector<T> v_merge(const vector<T>& a, const T& b) {205size_t s=a.size();206vector<T> c(s+1);207for (size_t i = 0; i < s; i++) {208c[i]=a[i];209}210c[s] = b;211return c;212}213214//---------------------------------------------------------------------------215216template<typename T>217vector<T> v_merge(const vector<T>& a,const vector<T>& b){218size_t s1=a.size(), s2=b.size(), i;219vector<T> c(s1+s2);220for (i = 0; i < s1; i++) {221c[i]=a[i];222}223for (i = 0; i < s2; i++) {224c[s1+i]=b[i];225}226return c;227}228//---------------------------------------------------------------------------229230template<typename T>231vector<T> v_cut_front(const vector<T>& v, size_t size){232size_t s,k;233vector<T> tmp(size);234s=v.size()-size;235for (k = 0; k < size; k++) {236tmp[k]=v[s+k];237}238return tmp;239}240241//---------------------------------------------------------------------------242243template<typename Number>244vector<key_t> v_non_zero_pos(const vector<Number>& v){245vector<key_t> key;246size_t size=v.size();247key.reserve(size);248for (key_t i = 0; i <size; i++) {249if (v[i]!=0) {250key.push_back(i);251}252}253return key;254}255256//---------------------------------------------------------------------------257258template<typename Number>259bool v_is_zero(const vector<Number>& v) {260for (size_t i = 0; i < v.size(); ++i) {261if (v[i] != 0) return false;262}263return true;264}265266//---------------------------------------------------------------------------267268template<typename Number>269bool v_is_symmetric(const vector<Number>& v) {270for (size_t i = 0; i < v.size()/2; ++i) {271if (v[i] != v[v.size()-1-i]) return false;272}273return true;274}275276//---------------------------------------------------------------------------277278template<typename Number>279bool v_is_nonnegative(const vector<Number>& v) {280for (size_t i = 0; i < v.size(); ++i) {281if (v[i] <0) return false;282}283return true;284}285286287//---------------------------------------------------------------------------288289template<typename Number>290void v_el_trans(const vector<Number>& av,vector<Number>& bv, const Number& F, const size_t& start){291292size_t i,n=av.size();293294typename vector<Number>::const_iterator a=av.begin();295typename vector<Number>::iterator b=bv.begin();296297a += start;298b += start;299n -= start;300301302if( n >= 8 )303{304for( i = 0; i < ( n >> 3 ); ++i, a += 8, b += 8 ){305b[0] += F*a[0];306b[1] += F*a[1];307b[2] += F*a[2];308b[3] += F*a[3];309b[4] += F*a[4];310b[5] += F*a[5];311b[6] += F*a[6];312b[7] += F*a[7];313}314n -= i << 3;315}316317if( n >= 4)318{319b[0] += F*a[0];320b[1] += F*a[1];321b[2] += F*a[2];322b[3] += F*a[3];323324n -=4;325a +=4;326b +=4;327}328329if( n >= 2)330{331b[0] += F*a[0];332b[1] += F*a[1];333334n -=2;335a +=2;336b +=2;337}338339if(n>0)340b[0] += F*a[0];341}342343//---------------------------------------------------------------344345vector<bool> v_bool_andnot(const vector<bool>& a, const vector<bool>& b) {346assert(a.size() == b.size());347vector<bool> result(a);348for (size_t i=0; i<b.size(); ++i) {349if (b[i])350result[i]=false;351}352return result;353}354355// swaps entry i and j of the vector<bool> v356void v_bool_entry_swap(vector<bool>& v, size_t i, size_t j) {357if (v[i] != v[j]) {358v[i].flip();359v[j].flip();360}361}362363364vector<key_t> identity_key(size_t n){365vector<key_t> key(n);366for(size_t k=0;k<n;++k)367key[k]=k;368return key;369}370371//---------------------------------------------------------------372// Sorting373374template <typename T>375void order_by_perm(vector<T>& v, const vector<key_t>& permfix){376377vector<key_t> perm=permfix; // we may want to use permfix a second time378vector<key_t> inv(perm.size());379for(key_t i=0;i<perm.size();++i)380inv[perm[i]]=i;381for(key_t i=0;i<perm.size();++i){382key_t j=perm[i];383swap(v[i],v[perm[i]]);384swap(perm[i],perm[inv[i]]);385swap(inv[i],inv[j]);386}387}388389// vector<bool> is special390template <>391void order_by_perm(vector<bool>& v, const vector<key_t>& permfix){392393vector<key_t> perm=permfix; // we may want to use permfix a second time394vector<key_t> inv(perm.size());395for(key_t i=0;i<perm.size();++i)396inv[perm[i]]=i;397for(key_t i=0;i<perm.size();++i){398key_t j=perm[i];399// v.swap(v[i],v[perm[i]]);400v_bool_entry_swap(v,i,perm[i]);401swap(perm[i],perm[inv[i]]);402swap(inv[i],inv[j]);403}404}405406// make random vector of length n with entries between -m and m407template <typename Number>408vector<Number> v_random(size_t n, long m){409vector<Number> result(n);410for(size_t i=0;i<n;++i)411result[i]=rand()%(2*m+1)-m;412return result;413}414415template bool v_is_nonnegative<long>(const vector<long>&);416template bool v_is_nonnegative<long long>(const vector<long long>&);417template bool v_is_nonnegative<mpz_class>(const vector<mpz_class>&);418419template bool v_is_symmetric<long>(const vector<long>&);420template bool v_is_symmetric<long long>(const vector<long long>&);421template bool v_is_symmetric<mpz_class>(const vector<mpz_class>&);422//423424template void v_add_result<long >(vector<long >&, size_t, const vector<long >&, const vector<long >&);425template void v_add_result<long long>(vector<long long>&, size_t, const vector<long long>&, const vector<long long>&);426template void v_add_result<mpz_class>(vector<mpz_class>&, size_t, const vector<mpz_class>&, const vector<mpz_class>&);427428template long v_scalar_product(const vector<long>& a,const vector<long>& b);429template long long v_scalar_product(const vector<long long>& a,const vector<long long>& b);430template mpz_class v_scalar_product(const vector<mpz_class>& a,const vector<mpz_class>& b);431432} // end namespace libQnormaliz433434435