| 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 ASLVECTORSDYNAMICLENGTH_H |
| 27 |
#define ASLVECTORSDYNAMICLENGTH_H |
| 28 |
|
| 29 |
|
| 30 |
#include "../aslUtilities.h" |
| 31 |
#include <cmath> |
| 32 |
|
| 33 |
|
| 34 |
namespace asl |
| 35 |
{ |
| 36 |
|
| 37 |
/// class algebraic vector. |
| 38 |
/// The class is an implementation of a dynamic array with defined algebraic operations |
| 39 |
template <typename T = double> class AVec |
| 40 |
{ |
| 41 |
private: |
| 42 |
T* x; |
| 43 |
unsigned int size; |
| 44 |
|
| 45 |
public: |
| 46 |
typedef T Type; |
| 47 |
inline AVec(); |
| 48 |
inline explicit AVec(unsigned int s); |
| 49 |
inline AVec(unsigned int s, T a); |
| 50 |
inline ~AVec(); |
| 51 |
template <typename Tv> |
| 52 |
inline explicit AVec(const AVec<Tv> & a); |
| 53 |
inline AVec(const AVec<T> & a); |
| 54 |
template <typename Tv> |
| 55 |
inline const AVec<T> & operator=(const AVec<Tv> &a); |
| 56 |
template <typename Tv> |
| 57 |
inline const AVec<T> & operator=(const std::vector<Tv> &a); |
| 58 |
inline const AVec<T> & operator=(const AVec<T> &a); |
| 59 |
inline T& operator[](unsigned int i); |
| 60 |
inline const T& operator[](unsigned int i) const; |
| 61 |
inline const unsigned int & getSize() const; |
| 62 |
inline void resize(unsigned int newSize); |
| 63 |
}; |
| 64 |
|
| 65 |
/// \relates AVec |
| 66 |
template <typename T> |
| 67 |
inline const unsigned int nD(const AVec<T> a); |
| 68 |
|
| 69 |
/// \relates AVec |
| 70 |
template <typename T> inline AVec<T> makeAVec(T a1); |
| 71 |
/// \relates AVec |
| 72 |
template <typename T> inline AVec<T> makeAVec(T a1, T a2); |
| 73 |
/// \relates AVec |
| 74 |
template <typename T> inline AVec<T> makeAVec(T a1, T a2, T a3); |
| 75 |
/// \relates AVec |
| 76 |
template <typename T> inline AVec<T> makeAVec(T a1, T a2, T a3, T a4); |
| 77 |
|
| 78 |
/// \relates AVec |
| 79 |
template <typename T> |
| 80 |
inline std::ostream & operator<<(std::ostream & output, const AVec<T> & a); |
| 81 |
|
| 82 |
|
| 83 |
//---------------- Implementation ---------------- |
| 84 |
template <typename T> inline AVec<T>::AVec(): |
| 85 |
x(new T[1]), |
| 86 |
size(1) |
| 87 |
{ |
| 88 |
x[0] =0; |
| 89 |
} |
| 90 |
|
| 91 |
template <typename T> inline AVec<T>::AVec(unsigned int s): |
| 92 |
x(new T[s]), |
| 93 |
size(s) |
| 94 |
{ |
| 95 |
if (size<1) errorMessage("Vector size less than 1"); |
| 96 |
memset(x, 0, sizeof(T)*s); |
| 97 |
} |
| 98 |
|
| 99 |
template <typename T> inline AVec<T>::AVec(unsigned int s, T a): |
| 100 |
x(new T[s]), |
| 101 |
size(s) |
| 102 |
{ |
| 103 |
if (size<1) errorMessage("Vector size less than 1"); |
| 104 |
for (unsigned int i(0); i<size; ++i) |
| 105 |
x[i] =a; |
| 106 |
} |
| 107 |
|
| 108 |
template <typename T> inline AVec<T>::~AVec() |
| 109 |
{ |
| 110 |
delete[] x; |
| 111 |
} |
| 112 |
|
| 113 |
template <typename T> template<typename Tv> inline AVec<T>::AVec(const AVec<Tv> & a): |
| 114 |
x(new T[a.getSize()]), |
| 115 |
size(a.getSize()) |
| 116 |
{ |
| 117 |
for (unsigned int i(0); i < size; ++i) |
| 118 |
x[i] = a[i]; |
| 119 |
} |
| 120 |
|
| 121 |
template <typename T> inline AVec<T>::AVec(const AVec<T> & a): |
| 122 |
x(new T[a.getSize()]), |
| 123 |
size(a.getSize()) |
| 124 |
{ |
| 125 |
for (unsigned int i(0); i < size; ++i) |
| 126 |
x[i] = a[i]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
template <typename T> template<typename Tv> |
| 131 |
inline const AVec<T> & AVec<T>::operator=(const AVec<Tv> & a) |
| 132 |
{ |
| 133 |
resize(a.getSize()); |
| 134 |
for (unsigned int i(0); i<size; ++i) |
| 135 |
x[i] =a[i]; |
| 136 |
return *this; |
| 137 |
} |
| 138 |
|
| 139 |
template <typename T> |
| 140 |
inline const AVec<T> & AVec<T>::operator=(const AVec<T> & a) |
| 141 |
{ |
| 142 |
resize(a.getSize()); |
| 143 |
for (unsigned int i(0); i<size; ++i) |
| 144 |
x[i] =a[i]; |
| 145 |
return *this; |
| 146 |
} |
| 147 |
|
| 148 |
template <typename T> template<typename Tv> |
| 149 |
inline const AVec<T> & AVec<T>::operator=(const std::vector<Tv> & a) |
| 150 |
{ |
| 151 |
resize(a.size()); |
| 152 |
for (unsigned int i(0); i<size; ++i) |
| 153 |
x[i] =a[i]; |
| 154 |
return *this; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
template <typename T> |
| 159 |
inline T& AVec<T>::operator[](unsigned int i) |
| 160 |
{ |
| 161 |
return x[i]; |
| 162 |
} |
| 163 |
|
| 164 |
template <typename T> |
| 165 |
inline const T& AVec<T>::operator[](unsigned int i) const |
| 166 |
{ |
| 167 |
return x[i]; |
| 168 |
} |
| 169 |
|
| 170 |
template <typename T> |
| 171 |
inline const unsigned int & AVec<T>::getSize() const |
| 172 |
{ |
| 173 |
return size; |
| 174 |
} |
| 175 |
|
| 176 |
template <typename T> inline AVec<T> makeAVec(T a1) |
| 177 |
{ |
| 178 |
return AVec<T>(1, a1); |
| 179 |
} |
| 180 |
|
| 181 |
template <typename T> inline AVec<T> makeAVec(T a1, T a2) |
| 182 |
{ |
| 183 |
AVec<T> v(2); |
| 184 |
v[0] = a1; |
| 185 |
v[1] = a2; |
| 186 |
return v; |
| 187 |
} |
| 188 |
|
| 189 |
template <typename T> inline AVec<T> makeAVec(T a1, T a2, T a3) |
| 190 |
{ |
| 191 |
AVec<T> v(3); |
| 192 |
v[0] = a1; |
| 193 |
v[1] = a2; |
| 194 |
v[2] = a3; |
| 195 |
return v; |
| 196 |
} |
| 197 |
|
| 198 |
template <typename T> inline AVec<T> makeAVec(T a1, T a2, T a3, T a4) |
| 199 |
{ |
| 200 |
AVec<T> v(4); |
| 201 |
v[0] = a1; |
| 202 |
v[1] = a2; |
| 203 |
v[2] = a3; |
| 204 |
v[3] = a4; |
| 205 |
return v; |
| 206 |
} |
| 207 |
|
| 208 |
template <typename T> inline std::ostream & operator<<(std::ostream & output, |
| 209 |
const AVec<T> & a) |
| 210 |
{ |
| 211 |
for (unsigned int i(0); i<a.getSize(); ++i) |
| 212 |
output << a[i]<< " "; |
| 213 |
return output; |
| 214 |
} |
| 215 |
|
| 216 |
template <typename T> |
| 217 |
inline const unsigned int nD(const AVec<T> a) |
| 218 |
{ |
| 219 |
return a.getSize(); |
| 220 |
} |
| 221 |
|
| 222 |
template <typename T> |
| 223 |
inline void AVec<T>::resize(unsigned int newSize) |
| 224 |
{ |
| 225 |
if (size != newSize){ |
| 226 |
delete[] x; |
| 227 |
x=new T[newSize]; |
| 228 |
size=newSize; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
} // asl |
| 233 |
|
| 234 |
#endif //ASLVECTORSDYNAMICLENGTH_H |
| 235 |
|