/* * Advanced Simulation Library * * Copyright 2015 Avtech Scientific * * * This file is part of Advanced Simulation Library (ASL). * * ASL is free software: you can redistribute it and/or modify it * under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, version 3 of the License. * * ASL 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with ASL. If not, see . * */ /// \file aslVectors.h definition of class АVec #ifndef ASLVECTORSDYNAMICLENGTHOPERATIONS_H #define ASLVECTORSDYNAMICLENGTHOPERATIONS_H #include "../aslUtilities.h" #include namespace asl { /// The function checks whether the sizes are equal \relates AVec template inline bool compatibleSizes(AVec a, AVec b); /// \relates AVec template inline const T l2(const AVec & a); /// \relates AVec template inline const AVec<> normalize(const AVec & a); /// \relates AVec template inline const AVec operator+(const AVec & a, const AVec & b); /// \relates AVec template inline const AVec operator-(const AVec & a, const AVec & b); /// \relates AVec template inline const AVec operator-(const AVec & a); /// \relates AVec template inline const T operator*(const AVec & a, const AVec & b); /// \relates AVec template inline const AVec operator*(const T & a, const AVec & b); /// \relates AVec template inline const AVec operator*(const AVec & a, const T & b); /// \relates AVec template inline const AVec operator/(const AVec & a, const T & b); /// \relates AVec template inline const AVec & operator+=(AVec & a, const AVec & b); /// \relates AVec template inline const AVec & operator-=(AVec & a, const AVec & b); /// \relates AVec template inline const AVec & operator*=(AVec & a, const T & b); /// \relates AVec template inline const bool operator==(const AVec & a, const AVec & b); /// \relates AVec template inline const bool operator!=(const AVec & a, const AVec & b); /// \relates AVec template inline const AVec crossProduct(const AVec & a, const AVec & b); /// returns minimal component \relates AVec template inline const T minComponent(const AVec & a); /// returns maximal component \relates AVec template inline const T maxComponent(const AVec & a); /// returns summ of all components \relates AVec template inline T sumOfElements(const AVec & a); /// returns product of all components \relates AVec template inline T productOfElements(const AVec & a); /// returns vector which elements are product of corresponding elements of \p a and \p b \relates AVec template inline const AVec productOfElements(const AVec & a, const AVec & b); /// returns vector which elements are division of corresponding elements of \p a and \p b \relates AVec template inline const AVec divisionOfElements(const AVec & a, const AVec & b); /// returns \p true in case when all components of \p a more or then 0 \relates AVec template inline const bool positive(const AVec & a); /// returns \p true in case when all components of \p a more or equal 0 \relates AVec template inline const bool nonNegative(const AVec & a); /// returns \p true in case when all components of \p a more then 0 \relates AVec inline const AVec floor(const AVec<> & a); /// returns \p true in case when all components of \p a more then 0 \relates AVec inline const AVec round(const AVec<> & a); /// computes polynom for \p x with \p coefs /** \relates AVec The polinom is \f$ x^{n-1}*coefs_0+x^{n-2}coefs_1+...+coefs_{n-1} \f$ */ inline double computePolynom(double x, AVec<> &coefs); /// \relates AVec template inline std::ostream & operator<<(std::ostream & output, const AVec & a); /// returns \p true in case when all components of \p a more then 0 \relates AVec inline const AVec<> swapXZ(const AVec<> & a); //---------------- Implementation ---------------- template inline bool compatibleSizes(AVec a, AVec b) { return a.getSize() == b.getSize(); } template inline const T l2(const AVec & a) { return a * a; } template inline const AVec<> normalize(const AVec & a) { return AVec<>(a) / sqrt(l2(a)); } template inline const AVec operator+(const AVec & a, const AVec & b){ if (!compatibleSizes(a, b)) errorMessage("(AVec; operator+) Vector sizes are incompatible"); AVec c(a.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) c[i] = a[i] + b[i]; return c; } template inline const AVec operator-(const AVec & a, const AVec & b) { if (!compatibleSizes (a,b)) errorMessage("(AVec; operator-) Vector sizes are incompatible"); AVec c(a.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) c[i] =a[i]-b[i]; return c; } template inline const AVec operator-(const AVec & a) { AVec c(a.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) c[i] =-a[i]; return c; } template inline const T operator*(const AVec & a, const AVec & b){ if (!compatibleSizes (a,b)) errorMessage("(AVec; operator*) Vector sizes are incompatible"); T s(0); for (unsigned int i(0); i < a.getSize(); ++i) s+=a[i]*b[i]; return s; } template inline const AVec operator*(const AVec & a, const T & b){ AVec c(a.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) c[i] =a[i]*b; return c; } template inline const AVec operator*(const T & a, const AVec & b){ return b*a; } template inline const AVec operator/(const AVec & a, const T & b){ AVec c(a.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) c[i] =a[i]/b; return c; } template inline const AVec & operator+=(AVec & a, const AVec & b){ if (!compatibleSizes (a,b)) errorMessage("Vector sizes are incompatible"); for (unsigned int i(0); i < a.getSize(); ++i) a[i]+=b[i]; return a; } template inline const AVec & operator-=(AVec & a, const AVec & b){ if (!compatibleSizes (a,b)) errorMessage("Vector sizes are incompatible"); for (unsigned int i(0); i < a.size; ++i) a[i]-=b[i]; return a; } template inline const AVec & operator*=(AVec & a, const T & b){ for (unsigned int i(0); i < a.getSize(); ++i) a[i] *= b; return a; } template inline const bool operator==(const AVec & a, const AVec & b) { if (!compatibleSizes (a, b)) return false; bool c(true); for (unsigned int i(0); i < a.getSize(); ++i) c = c && (a[i] == b[i]); return c; } template inline const bool operator!=(const AVec & a, const AVec & b) { return !(a == b); } template inline const AVec crossProduct(const AVec & a, const AVec & b) { if (!compatibleSizes (a,b)) errorMessage("(AVec; crossProduct) Vector sizes are incompatible"); if (a.getSize()>3) errorMessage("(AVec; crossProduct) number of components is more than 3"); if (a.getSize()<2) errorMessage("(AVec; crossProduct) number of components is less than 2"); AVec res(1); if(a.getSize() == 2) { res[0] = a[0]*b[1]-a[1]*b[0]; } if(a.getSize() == 3) { res.resize(3); res[0] = a[1]*b[2]-a[2]*b[1]; res[1] = a[2]*b[0]-a[0]*b[2]; res[2] = a[0]*b[1]-a[1]*b[0]; } return res; } template inline const T minComponent(const AVec & a) { T ma(a[0]); for (unsigned int i(1); i < a.getSize(); ++i) ma = std::min(ma, a[i]); return ma; } template inline const T maxComponent(const AVec & a) { if (a.getSize()<1) errorMessage("Vector size less than 1"); T ma(a[0]); for (unsigned int i(1); i < a.getSize(); ++i) ma=std::max(ma,a[i]); return ma; } template inline AVec subAVec(const AVec & source, unsigned int start, unsigned int end) { if (source.getSize() <= end ) errorMessage("subAVec: attempt to copy besides the vector range"); AVec destination(1 + end - start); for (unsigned int i(start); i <= end; ++i) destination[i - start] = source[i]; return destination; } template inline T sumOfElements(const AVec & a) { T s(0); for (unsigned int i(0); i < a.getSize(); ++i) s += a[i]; return s; } template inline T productOfElements(const AVec & a) { T p(1); for (unsigned int i(0); i < a.getSize(); ++i) p *= a[i]; return p; } template inline const AVec productOfElements(const AVec & a, const AVec & b) { if (!compatibleSizes (a, b)) errorMessage("(AVec; productOfElements) Vector sizes are incompatible"); AVec c(a.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) c[i] = a[i] * b[i]; return c; } template inline const AVec divisionOfElements(const AVec & a, const AVec & b) { if (!compatibleSizes (a, b)) errorMessage("(AVec; divisionOfElements) Vector sizes are incompatible"); AVec c(a.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) c[i] = a[i] / b[i]; return c; } template inline const bool positive(const AVec & a) { if (!a.getSize()) errorMessage("(AVec; positive) Vector size is zero"); bool res(a[0]>0); for (unsigned int i(1); i < a.getSize(); ++i) res &= a[i]>0; return res; } template inline const bool nonNegative(const AVec & a) { if (!a.getSize()) errorMessage("(AVec; positive) Vector size is zero"); bool res(a[0]>=0); for (unsigned int i(1); i < a.getSize(); ++i) res &= a[i]>=0; return res; } inline const AVec floor(const AVec<> & a) { if (!a.getSize()) errorMessage("(AVec; floor) Vector size is zero"); AVec res(a.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) res[i] = std::floor(a[i]); return res; } inline const AVec round(const AVec<> & a) { if (!a.getSize()) errorMessage("(AVec; round) Vector size is zero"); AVec res(a.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) res[i] = std::round(a[i]); return res; } inline double computePolynom (double x, AVec<> &coefs) { if (coefs.getSize() < 1) errorMessage("Error: (asl::computePolynom) size of \"coefs\" less than 1"); double p; p=coefs[0]; for (unsigned int i(1); i < coefs.getSize(); ++i) p=p*x+coefs[i]; return p; } /// returns \p true in case when all components of \p a more then 0 \relates AVec inline const AVec<> swapXZ(const AVec<> & a) { if (a.getSize()<3) errorMessage("(AVec; swapXZ) Vector size less than 3"); AVec<> res(a); std::swap(res[0],res[2]); return res; } } // asl #endif //ASLVECTORSDYNAMICLENGTH_H