| 1 |
/* |
| 2 |
* Advanced Simulation Library <http://asl.org.il> |
| 3 |
* |
| 4 |
* Copyright 2015 Avtech Scientific <http://avtechscientific.com> |
| 5 |
* |
| 6 |
* |
| 7 |
* This file is part of Advanced Simulation Library (ASL). |
| 8 |
* |
| 9 |
* ASL is free software: you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU Affero General Public License as |
| 11 |
* published by the Free Software Foundation, version 3 of the License. |
| 12 |
* |
| 13 |
* ASL is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Affero General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Affero General Public License |
| 19 |
* along with ASL. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
/// \file aslVectors.h definition of class АVec<T> |
| 25 |
|
| 26 |
#ifndef ASLVECTORSDYNAMICLENGTHOPERATIONS_H |
| 27 |
#define ASLVECTORSDYNAMICLENGTHOPERATIONS_H |
| 28 |
|
| 29 |
|
| 30 |
#include "../aslUtilities.h" |
| 31 |
#include <cmath> |
| 32 |
|
| 33 |
|
| 34 |
namespace asl |
| 35 |
{ |
| 36 |
|
| 37 |
/// The function checks whether the sizes are equal \relates AVec |
| 38 |
template <typename T1, typename T2> |
| 39 |
inline bool compatibleSizes(AVec<T1> a, AVec<T2> b); |
| 40 |
|
| 41 |
/// \relates AVec |
| 42 |
template <typename T> |
| 43 |
inline const T l2(const AVec<T> & a); |
| 44 |
|
| 45 |
/// \relates AVec |
| 46 |
template <typename T> |
| 47 |
inline const AVec<> normalize(const AVec<T> & a); |
| 48 |
|
| 49 |
/// \relates AVec |
| 50 |
template <typename T> |
| 51 |
inline const AVec<T> operator+(const AVec<T> & a, const AVec<T> & b); |
| 52 |
/// \relates AVec |
| 53 |
template <typename T> |
| 54 |
inline const AVec<T> operator-(const AVec<T> & a, const AVec<T> & b); |
| 55 |
/// \relates AVec |
| 56 |
template <typename T> |
| 57 |
inline const AVec<T> operator-(const AVec<T> & a); |
| 58 |
/// \relates AVec |
| 59 |
template <typename T> |
| 60 |
inline const T operator*(const AVec<T> & a, const AVec<T> & b); |
| 61 |
/// \relates AVec |
| 62 |
template <typename T> |
| 63 |
inline const AVec<T> operator*(const T & a, const AVec<T> & b); |
| 64 |
/// \relates AVec |
| 65 |
template <typename T> |
| 66 |
inline const AVec<T> operator*(const AVec<T> & a, const T & b); |
| 67 |
/// \relates AVec |
| 68 |
template <typename T> |
| 69 |
inline const AVec<T> operator/(const AVec<T> & a, const T & b); |
| 70 |
|
| 71 |
/// \relates AVec |
| 72 |
template <typename T> inline const AVec<T> & operator+=(AVec<T> & a, |
| 73 |
const AVec<T> & b); |
| 74 |
/// \relates AVec |
| 75 |
template <typename T> inline const AVec<T> & operator-=(AVec<T> & a, |
| 76 |
const AVec<T> & b); |
| 77 |
/// \relates AVec |
| 78 |
template <typename T> inline const AVec<T> & operator*=(AVec<T> & a, |
| 79 |
const T & b); |
| 80 |
|
| 81 |
/// \relates AVec |
| 82 |
template <typename T> inline const bool operator==(const AVec<T> & a, |
| 83 |
const AVec<T> & b); |
| 84 |
|
| 85 |
|
| 86 |
/// \relates AVec |
| 87 |
template <typename T> inline const bool operator!=(const AVec<T> & a, |
| 88 |
const AVec<T> & b); |
| 89 |
|
| 90 |
|
| 91 |
/// \relates AVec |
| 92 |
template <typename T> |
| 93 |
inline const AVec<T> crossProduct(const AVec<T> & a, const AVec<T> & b); |
| 94 |
|
| 95 |
/// returns minimal component \relates AVec |
| 96 |
template <typename T> inline const T minComponent(const AVec<T> & a); |
| 97 |
/// returns maximal component \relates AVec |
| 98 |
template <typename T> inline const T maxComponent(const AVec<T> & a); |
| 99 |
|
| 100 |
/// returns summ of all components \relates AVec |
| 101 |
template <typename T> inline T sumOfElements(const AVec<T> & a); |
| 102 |
/// returns product of all components \relates AVec |
| 103 |
template <typename T> inline T productOfElements(const AVec<T> & a); |
| 104 |
|
| 105 |
/// returns vector which elements are product of corresponding elements of \p a and \p b \relates AVec |
| 106 |
template <typename T> inline const AVec<T> productOfElements(const AVec<T> & a, |
| 107 |
const AVec<T> & b); |
| 108 |
/// returns vector which elements are division of corresponding elements of \p a and \p b \relates AVec |
| 109 |
template <typename T> inline const AVec<T> divisionOfElements(const AVec<T> & a, |
| 110 |
const AVec<T> & b); |
| 111 |
|
| 112 |
/// returns \p true in case when all components of \p a more or then 0 \relates AVec |
| 113 |
template <typename T> inline const bool positive(const AVec<T> & a); |
| 114 |
|
| 115 |
/// returns \p true in case when all components of \p a more or equal 0 \relates AVec |
| 116 |
template <typename T> inline const bool nonNegative(const AVec<T> & a); |
| 117 |
|
| 118 |
/// returns \p true in case when all components of \p a more then 0 \relates AVec |
| 119 |
inline const AVec<int> floor(const AVec<> & a); |
| 120 |
|
| 121 |
/// returns \p true in case when all components of \p a more then 0 \relates AVec |
| 122 |
inline const AVec<int> round(const AVec<> & a); |
| 123 |
|
| 124 |
/// computes polynom for \p x with \p coefs |
| 125 |
/** |
| 126 |
\relates AVec |
| 127 |
The polinom is \f$ x^{n-1}*coefs_0+x^{n-2}coefs_1+...+coefs_{n-1} \f$ |
| 128 |
*/ |
| 129 |
inline double computePolynom(double x, AVec<> &coefs); |
| 130 |
|
| 131 |
/// \relates AVec |
| 132 |
template <typename T> |
| 133 |
inline std::ostream & operator<<(std::ostream & output, const AVec<T> & a); |
| 134 |
|
| 135 |
/// returns \p true in case when all components of \p a more then 0 \relates AVec |
| 136 |
inline const AVec<> swapXZ(const AVec<> & a); |
| 137 |
|
| 138 |
//---------------- Implementation ---------------- |
| 139 |
|
| 140 |
template <typename T1, typename T2> |
| 141 |
inline bool compatibleSizes(AVec<T1> a, AVec<T2> b) |
| 142 |
{ |
| 143 |
return a.getSize() == b.getSize(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
template <typename T> inline const T l2(const AVec<T> & a) |
| 148 |
{ |
| 149 |
return a * a; |
| 150 |
} |
| 151 |
|
| 152 |
template <typename T> inline const AVec<> normalize(const AVec<T> & a) |
| 153 |
{ |
| 154 |
return AVec<>(a) / sqrt(l2(a)); |
| 155 |
} |
| 156 |
|
| 157 |
template <typename T> inline const AVec<T> operator+(const AVec<T> & a, |
| 158 |
const AVec<T> & b){ |
| 159 |
if (!compatibleSizes(a, b)) |
| 160 |
errorMessage("(AVec; operator+) Vector sizes are incompatible"); |
| 161 |
AVec<T> c(a.getSize()); |
| 162 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 163 |
c[i] = a[i] + b[i]; |
| 164 |
return c; |
| 165 |
} |
| 166 |
|
| 167 |
template <typename T> inline const AVec<T> operator-(const AVec<T> & a, const AVec<T> & b) |
| 168 |
{ |
| 169 |
if (!compatibleSizes (a,b)) |
| 170 |
errorMessage("(AVec; operator-) Vector sizes are incompatible"); |
| 171 |
AVec<T> c(a.getSize()); |
| 172 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 173 |
c[i] =a[i]-b[i]; |
| 174 |
return c; |
| 175 |
} |
| 176 |
|
| 177 |
template <typename T> inline const AVec<T> operator-(const AVec<T> & a) |
| 178 |
{ |
| 179 |
AVec<T> c(a.getSize()); |
| 180 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 181 |
c[i] =-a[i]; |
| 182 |
return c; |
| 183 |
} |
| 184 |
|
| 185 |
template <typename T> inline const T operator*(const AVec<T> & a, const AVec<T> & b){ |
| 186 |
if (!compatibleSizes (a,b)) |
| 187 |
errorMessage("(AVec; operator*) Vector sizes are incompatible"); |
| 188 |
T s(0); |
| 189 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 190 |
s+=a[i]*b[i]; |
| 191 |
return s; |
| 192 |
} |
| 193 |
|
| 194 |
template <typename T> inline const AVec<T> operator*(const AVec<T> & a, const T & b){ |
| 195 |
AVec<T> c(a.getSize()); |
| 196 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 197 |
c[i] =a[i]*b; |
| 198 |
return c; |
| 199 |
} |
| 200 |
|
| 201 |
template <typename T> inline const AVec<T> operator*(const T & a, const AVec<T> & b){ |
| 202 |
return b*a; |
| 203 |
} |
| 204 |
|
| 205 |
template <typename T> inline const AVec<T> operator/(const AVec<T> & a, const T & b){ |
| 206 |
AVec<T> c(a.getSize()); |
| 207 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 208 |
c[i] =a[i]/b; |
| 209 |
return c; |
| 210 |
} |
| 211 |
|
| 212 |
template <typename T> inline const AVec<T> & operator+=(AVec<T> & a, const AVec<T> & b){ |
| 213 |
if (!compatibleSizes (a,b)) |
| 214 |
errorMessage("Vector sizes are incompatible"); |
| 215 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 216 |
a[i]+=b[i]; |
| 217 |
return a; |
| 218 |
} |
| 219 |
|
| 220 |
template <typename T> inline const AVec<T> & operator-=(AVec<T> & a, const AVec<T> & b){ |
| 221 |
if (!compatibleSizes (a,b)) |
| 222 |
errorMessage("Vector sizes are incompatible"); |
| 223 |
for (unsigned int i(0); i < a.size; ++i) |
| 224 |
a[i]-=b[i]; |
| 225 |
return a; |
| 226 |
} |
| 227 |
|
| 228 |
template <typename T> inline const AVec<T> & operator*=(AVec<T> & a, const T & b){ |
| 229 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 230 |
a[i] *= b; |
| 231 |
return a; |
| 232 |
} |
| 233 |
|
| 234 |
template <typename T> inline const bool operator==(const AVec<T> & a, |
| 235 |
const AVec<T> & b) |
| 236 |
{ |
| 237 |
if (!compatibleSizes (a, b)) |
| 238 |
return false; |
| 239 |
bool c(true); |
| 240 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 241 |
c = c && (a[i] == b[i]); |
| 242 |
return c; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
template <typename T> inline const bool operator!=(const AVec<T> & a, |
| 247 |
const AVec<T> & b) |
| 248 |
{ |
| 249 |
return !(a == b); |
| 250 |
} |
| 251 |
|
| 252 |
template <typename T> |
| 253 |
inline const AVec<T> crossProduct(const AVec<T> & a, const AVec<T> & b) |
| 254 |
{ |
| 255 |
if (!compatibleSizes (a,b)) |
| 256 |
errorMessage("(AVec; crossProduct) Vector sizes are incompatible"); |
| 257 |
if (a.getSize()>3) |
| 258 |
errorMessage("(AVec; crossProduct) number of components is more than 3"); |
| 259 |
if (a.getSize()<2) |
| 260 |
errorMessage("(AVec; crossProduct) number of components is less than 2"); |
| 261 |
|
| 262 |
AVec<T> res(1); |
| 263 |
if(a.getSize() == 2) |
| 264 |
{ |
| 265 |
res[0] = a[0]*b[1]-a[1]*b[0]; |
| 266 |
} |
| 267 |
if(a.getSize() == 3) |
| 268 |
{ |
| 269 |
res.resize(3); |
| 270 |
res[0] = a[1]*b[2]-a[2]*b[1]; |
| 271 |
res[1] = a[2]*b[0]-a[0]*b[2]; |
| 272 |
res[2] = a[0]*b[1]-a[1]*b[0]; |
| 273 |
} |
| 274 |
return res; |
| 275 |
} |
| 276 |
|
| 277 |
template <typename T> inline const T minComponent(const AVec<T> & a) |
| 278 |
{ |
| 279 |
T ma(a[0]); |
| 280 |
for (unsigned int i(1); i < a.getSize(); ++i) |
| 281 |
ma = std::min(ma, a[i]); |
| 282 |
return ma; |
| 283 |
} |
| 284 |
|
| 285 |
template <typename T> inline const T maxComponent(const AVec<T> & a) |
| 286 |
{ |
| 287 |
if (a.getSize()<1) errorMessage("Vector size less than 1"); |
| 288 |
T ma(a[0]); |
| 289 |
for (unsigned int i(1); i < a.getSize(); ++i) |
| 290 |
ma=std::max(ma,a[i]); |
| 291 |
return ma; |
| 292 |
} |
| 293 |
|
| 294 |
template <typename T> inline AVec<T> subAVec(const AVec<T> & source, |
| 295 |
unsigned int start, unsigned int end) |
| 296 |
{ |
| 297 |
if (source.getSize() <= end ) |
| 298 |
errorMessage("subAVec: attempt to copy besides the vector range"); |
| 299 |
|
| 300 |
AVec<T> destination(1 + end - start); |
| 301 |
for (unsigned int i(start); i <= end; ++i) |
| 302 |
destination[i - start] = source[i]; |
| 303 |
return destination; |
| 304 |
} |
| 305 |
|
| 306 |
template <typename T> inline T sumOfElements(const AVec<T> & a) |
| 307 |
{ |
| 308 |
T s(0); |
| 309 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 310 |
s += a[i]; |
| 311 |
return s; |
| 312 |
} |
| 313 |
|
| 314 |
template <typename T> inline T productOfElements(const AVec<T> & a) |
| 315 |
{ |
| 316 |
T p(1); |
| 317 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 318 |
p *= a[i]; |
| 319 |
return p; |
| 320 |
} |
| 321 |
|
| 322 |
template <typename T> inline const AVec<T> productOfElements(const AVec<T> & a, |
| 323 |
const AVec<T> & b) |
| 324 |
{ |
| 325 |
if (!compatibleSizes (a, b)) |
| 326 |
errorMessage("(AVec; productOfElements) Vector sizes are incompatible"); |
| 327 |
AVec<T> c(a.getSize()); |
| 328 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 329 |
c[i] = a[i] * b[i]; |
| 330 |
return c; |
| 331 |
} |
| 332 |
|
| 333 |
template <typename T> inline const AVec<T> divisionOfElements(const AVec<T> & a, const AVec<T> & b) |
| 334 |
{ |
| 335 |
if (!compatibleSizes (a, b)) |
| 336 |
errorMessage("(AVec; divisionOfElements) Vector sizes are incompatible"); |
| 337 |
|
| 338 |
AVec<T> c(a.getSize()); |
| 339 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 340 |
c[i] = a[i] / b[i]; |
| 341 |
|
| 342 |
return c; |
| 343 |
} |
| 344 |
|
| 345 |
template <typename T> inline const bool positive(const AVec<T> & a) |
| 346 |
{ |
| 347 |
if (!a.getSize()) |
| 348 |
errorMessage("(AVec; positive) Vector size is zero"); |
| 349 |
bool res(a[0]>0); |
| 350 |
for (unsigned int i(1); i < a.getSize(); ++i) |
| 351 |
res &= a[i]>0; |
| 352 |
return res; |
| 353 |
} |
| 354 |
|
| 355 |
template <typename T> inline const bool nonNegative(const AVec<T> & a) |
| 356 |
{ |
| 357 |
if (!a.getSize()) |
| 358 |
errorMessage("(AVec; positive) Vector size is zero"); |
| 359 |
bool res(a[0]>=0); |
| 360 |
for (unsigned int i(1); i < a.getSize(); ++i) |
| 361 |
res &= a[i]>=0; |
| 362 |
return res; |
| 363 |
} |
| 364 |
|
| 365 |
inline const AVec<int> floor(const AVec<> & a) |
| 366 |
{ |
| 367 |
if (!a.getSize()) |
| 368 |
errorMessage("(AVec; floor) Vector size is zero"); |
| 369 |
AVec<int> res(a.getSize()); |
| 370 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 371 |
res[i] = std::floor(a[i]); |
| 372 |
return res; |
| 373 |
} |
| 374 |
|
| 375 |
inline const AVec<int> round(const AVec<> & a) |
| 376 |
{ |
| 377 |
if (!a.getSize()) |
| 378 |
errorMessage("(AVec; round) Vector size is zero"); |
| 379 |
AVec<int> res(a.getSize()); |
| 380 |
for (unsigned int i(0); i < a.getSize(); ++i) |
| 381 |
res[i] = std::round(a[i]); |
| 382 |
return res; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
inline double computePolynom (double x, AVec<> &coefs) |
| 387 |
{ |
| 388 |
if (coefs.getSize() < 1) |
| 389 |
errorMessage("Error: (asl::computePolynom) size of \"coefs\" less than 1"); |
| 390 |
double p; |
| 391 |
p=coefs[0]; |
| 392 |
for (unsigned int i(1); i < coefs.getSize(); ++i) |
| 393 |
p=p*x+coefs[i]; |
| 394 |
return p; |
| 395 |
} |
| 396 |
|
| 397 |
/// returns \p true in case when all components of \p a more then 0 \relates AVec |
| 398 |
inline const AVec<> swapXZ(const AVec<> & a) |
| 399 |
{ |
| 400 |
if (a.getSize()<3) |
| 401 |
errorMessage("(AVec; swapXZ) Vector size less than 3"); |
| 402 |
AVec<> res(a); |
| 403 |
std::swap(res[0],res[2]); |
| 404 |
return res; |
| 405 |
} |
| 406 |
|
| 407 |
} // asl |
| 408 |
|
| 409 |
#endif //ASLVECTORSDYNAMICLENGTH_H |