aslVectorsDynamicLengthOperations.h
Go to the documentation of this file.
/*
* Advanced Simulation Library <http://asl.org.il>
*
* Copyright 2015 Avtech Scientific <http://avtechscientific.com>
*
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#ifndef ASLVECTORSDYNAMICLENGTHOPERATIONS_H
#define ASLVECTORSDYNAMICLENGTHOPERATIONS_H
#include "../aslUtilities.h"
#include <cmath>
namespace asl
{
template <typename T1, typename T2>
inline bool compatibleSizes(AVec<T1> a, AVec<T2> b);
template <typename T>
inline const T l2(const AVec<T> & a);
template <typename T>
inline const AVec<> normalize(const AVec<T> & a);
template <typename T>
inline const AVec<T> operator+(const AVec<T> & a, const AVec<T> & b);
template <typename T>
inline const AVec<T> operator-(const AVec<T> & a, const AVec<T> & b);
template <typename T>
inline const AVec<T> operator-(const AVec<T> & a);
template <typename T>
inline const T operator*(const AVec<T> & a, const AVec<T> & b);
template <typename T>
inline const AVec<T> operator*(const T & a, const AVec<T> & b);
template <typename T>
inline const AVec<T> operator*(const AVec<T> & a, const T & b);
template <typename T>
inline const AVec<T> operator/(const AVec<T> & a, const T & b);
template <typename T> inline const AVec<T> & operator+=(AVec<T> & a,
const AVec<T> & b);
template <typename T> inline const AVec<T> & operator-=(AVec<T> & a,
const AVec<T> & b);
template <typename T> inline const AVec<T> & operator*=(AVec<T> & a,
const T & b);
template <typename T> inline const bool operator==(const AVec<T> & a,
const AVec<T> & b);
template <typename T> inline const bool operator!=(const AVec<T> & a,
const AVec<T> & b);
template <typename T>
inline const AVec<T> crossProduct(const AVec<T> & a, const AVec<T> & b);
template <typename T> inline const T minComponent(const AVec<T> & a);
template <typename T> inline const T maxComponent(const AVec<T> & a);
template <typename T> inline T sumOfElements(const AVec<T> & a);
template <typename T> inline T productOfElements(const AVec<T> & a);
template <typename T> inline const AVec<T> productOfElements(const AVec<T> & a,
const AVec<T> & b);
template <typename T> inline const AVec<T> divisionOfElements(const AVec<T> & a,
const AVec<T> & b);
template <typename T> inline const bool positive(const AVec<T> & a);
template <typename T> inline const bool nonNegative(const AVec<T> & a);
inline const AVec<int> floor(const AVec<> & a);
inline const AVec<int> round(const AVec<> & a);
inline double computePolynom(double x, AVec<> &coefs);
template <typename T>
inline std::ostream & operator<<(std::ostream & output, const AVec<T> & a);
inline const AVec<> swapXZ(const AVec<> & a);
//---------------- Implementation ----------------
template <typename T1, typename T2>
inline bool compatibleSizes(AVec<T1> a, AVec<T2> b)
{
return a.getSize() == b.getSize();
}
template <typename T> inline const T l2(const AVec<T> & a)
{
return a * a;
}
template <typename T> inline const AVec<> normalize(const AVec<T> & a)
{
return AVec<>(a) / sqrt(l2(a));
}
template <typename T> inline const AVec<T> operator+(const AVec<T> & a,
const AVec<T> & b){
if (!compatibleSizes(a, b))
errorMessage("(AVec; operator+) Vector sizes are incompatible");
AVec<T> c(a.getSize());
for (unsigned int i(0); i < a.getSize(); ++i)
c[i] = a[i] + b[i];
return c;
}
template <typename T> inline const AVec<T> operator-(const AVec<T> & a, const AVec<T> & b)
{
if (!compatibleSizes (a,b))
errorMessage("(AVec; operator-) Vector sizes are incompatible");
AVec<T> c(a.getSize());
for (unsigned int i(0); i < a.getSize(); ++i)
c[i] =a[i]-b[i];
return c;
}
template <typename T> inline const AVec<T> operator-(const AVec<T> & a)
{
AVec<T> c(a.getSize());
for (unsigned int i(0); i < a.getSize(); ++i)
c[i] =-a[i];
return c;
}
template <typename T> inline const T operator*(const AVec<T> & a, const AVec<T> & 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 <typename T> inline const AVec<T> operator*(const AVec<T> & a, const T & b){
AVec<T> c(a.getSize());
for (unsigned int i(0); i < a.getSize(); ++i)
c[i] =a[i]*b;
return c;
}
template <typename T> inline const AVec<T> operator*(const T & a, const AVec<T> & b){
return b*a;
}
template <typename T> inline const AVec<T> operator/(const AVec<T> & a, const T & b){
AVec<T> c(a.getSize());
for (unsigned int i(0); i < a.getSize(); ++i)
c[i] =a[i]/b;
return c;
}
template <typename T> inline const AVec<T> & operator+=(AVec<T> & a, const AVec<T> & 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 <typename T> inline const AVec<T> & operator-=(AVec<T> & a, const AVec<T> & 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 <typename T> inline const AVec<T> & operator*=(AVec<T> & a, const T & b){
for (unsigned int i(0); i < a.getSize(); ++i)
a[i] *= b;
return a;
}
template <typename T> inline const bool operator==(const AVec<T> & a,
const AVec<T> & 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 <typename T> inline const bool operator!=(const AVec<T> & a,
const AVec<T> & b)
{
return !(a == b);
}
template <typename T>
inline const AVec<T> crossProduct(const AVec<T> & a, const AVec<T> & 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<T> 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 <typename T> inline const T minComponent(const AVec<T> & a)
{
T ma(a[0]);
for (unsigned int i(1); i < a.getSize(); ++i)
ma = std::min(ma, a[i]);
return ma;
}
template <typename T> inline const T maxComponent(const AVec<T> & 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 <typename T> inline AVec<T> subAVec(const AVec<T> & source,
unsigned int start, unsigned int end)
{
if (source.getSize() <= end )
errorMessage("subAVec: attempt to copy besides the vector range");
AVec<T> destination(1 + end - start);
for (unsigned int i(start); i <= end; ++i)
destination[i - start] = source[i];
return destination;
}
template <typename T> inline T sumOfElements(const AVec<T> & a)
{
T s(0);
for (unsigned int i(0); i < a.getSize(); ++i)
s += a[i];
return s;
}
template <typename T> inline T productOfElements(const AVec<T> & a)
{
T p(1);
for (unsigned int i(0); i < a.getSize(); ++i)
p *= a[i];
return p;
}
template <typename T> inline const AVec<T> productOfElements(const AVec<T> & a,
const AVec<T> & b)
{
if (!compatibleSizes (a, b))
errorMessage("(AVec; productOfElements) Vector sizes are incompatible");
AVec<T> c(a.getSize());
for (unsigned int i(0); i < a.getSize(); ++i)
c[i] = a[i] * b[i];
return c;
}
template <typename T> inline const AVec<T> divisionOfElements(const AVec<T> & a, const AVec<T> & b)
{
if (!compatibleSizes (a, b))
errorMessage("(AVec; divisionOfElements) Vector sizes are incompatible");
AVec<T> c(a.getSize());
for (unsigned int i(0); i < a.getSize(); ++i)
c[i] = a[i] / b[i];
return c;
}
template <typename T> inline const bool positive(const AVec<T> & 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 <typename T> inline const bool nonNegative(const AVec<T> & 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<int> floor(const AVec<> & a)
{
if (!a.getSize())
errorMessage("(AVec; floor) Vector size is zero");
AVec<int> res(a.getSize());
for (unsigned int i(0); i < a.getSize(); ++i)
res[i] = std::floor(a[i]);
return res;
}
inline const AVec<int> round(const AVec<> & a)
{
if (!a.getSize())
errorMessage("(AVec; round) Vector size is zero");
AVec<int> 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;
}
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
asl::AVec
Definition: aslVectorsDynamicLength.h:40
asl::AVec::swapXZ
const AVec swapXZ(const AVec<> &a)
returns true in case when all components of a more then 0
asl::AVec::getSize
const unsigned int & getSize() const
Definition: aslVectorsDynamicLength.h:171
asl::errorMessage
void errorMessage(cl_int status, const char *errorMessage)
Prints errorMessage and exits depending on the status.
asl::normalize
SPDistanceFunction normalize(SPDistanceFunction a, double dx)
asl::operator-
SPDistanceFunction operator-(SPDistanceFunction a)
acl::elementOperators::max
Element max(Element a, Element b)
acl::elementOperators::sqrt
Element sqrt(Element e)
acl::elementOperators::min
Element min(Element a, Element b)
asl::operator*
SPPositionFunction operator*(SPPositionFunction a, SPPositionFunction b)
Definition: aslPositionFunction.h:215
asl
Advanced Simulation Library.
Definition: aslDataInc.h:31
asl::minComponent
const T minComponent(const AVec< T > &a)
Definition: aslVectorsDynamicLengthOperations.h:277
asl::computePolynom
double computePolynom(double x, AVec<> &coefs)
Definition: aslVectorsDynamicLengthOperations.h:386
asl::divisionOfElements
const AVec< T > divisionOfElements(const AVec< T > &a, const AVec< T > &b)
Definition: aslVectorsDynamicLengthOperations.h:333
asl::productOfElements
T productOfElements(const AVec< T > &a)
Definition: aslVectorsDynamicLengthOperations.h:314
asl::operator<<
std::ostream & operator<<(std::ostream &output, const std::vector< T > &vector)
Prints elements of the vector separated by space.
Definition: aslUtilities.h:173
asl::operator*=
const AVec< T > & operator*=(AVec< T > &a, const T &b)
Definition: aslVectorsDynamicLengthOperations.h:228
asl::round
const AVec< int > round(const AVec<> &a)
Definition: aslVectorsDynamicLengthOperations.h:375
asl::subAVec
AVec< T > subAVec(const AVec< T > &source, unsigned int start, unsigned int end)
Definition: aslVectorsDynamicLengthOperations.h:294
asl::operator+
const AMatr< T > operator+(const AMatr< T > &a, const AMatr< T > &b)
Definition: aslMatrices.h:252
asl::operator==
bool operator==(const std::vector< T > &vector1, const std::vector< T > &vector2)
Compares two vectors.
Definition: aslUtilities.h:185
asl::sumOfElements
T sumOfElements(const AVec< T > &a)
Definition: aslVectorsDynamicLengthOperations.h:306
asl::operator-=
const AVec< T > & operator-=(AVec< T > &a, const AVec< T > &b)
Definition: aslVectorsDynamicLengthOperations.h:220
asl::maxComponent
const T maxComponent(const AVec< T > &a)
Definition: aslVectorsDynamicLengthOperations.h:285
asl::operator+=
const AMatr< T > & operator+=(AMatr< T > &a, const AMatr< T > &b)
Definition: aslMatrices.h:245
asl::l2
const T l2(const AVec< T > &a)
Definition: aslVectorsDynamicLengthOperations.h:147
asl::crossProduct
const AVec< T > crossProduct(const AVec< T > &a, const AVec< T > &b)
Definition: aslVectorsDynamicLengthOperations.h:253
asl::positive
const bool positive(const AVec< T > &a)
Definition: aslVectorsDynamicLengthOperations.h:345
asl::compatibleSizes
bool compatibleSizes(AVec< T1 > a, AVec< T2 > b)
Definition: aslVectorsDynamicLengthOperations.h:141
asl::floor
const AVec< int > floor(const AVec<> &a)
Definition: aslVectorsDynamicLengthOperations.h:365
asl::operator/
const AMatr< T > operator/(const AMatr< T > &b, const T &a)
Definition: aslMatrices.h:264
asl::operator!=
const bool operator!=(const AVec< T > &a, const AVec< T > &b)
Definition: aslVectorsDynamicLengthOperations.h:246
asl::nonNegative
const bool nonNegative(const AVec< T > &a)
Definition: aslVectorsDynamicLengthOperations.h:355