1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/*
* Copyright © 2004-2011 Ondra Kamenik
* Copyright © 2019 Dynare Team
*
* This file is part of Dynare.
*
* Dynare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dynare 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Vector.hh"
#include "GeneralMatrix.hh"
#include "SylvException.hh"
#include <dynblas.h>
#include <cmath>
#include <algorithm>
#include <limits>
#include <iostream>
#include <iomanip>
Vector::Vector(const Vector &v)
: len(v.len), data{new double[len]}
{
copy(v.data, v.s);
}
Vector::Vector(const ConstVector &v)
: len(v.len), data{new double[len]}
{
copy(v.data, v.s);
}
Vector &
Vector::operator=(const Vector &v)
{
if (this == &v)
return *this;
if (v.len != len)
throw SYLV_MES_EXCEPTION("Attempt to assign vectors with different lengths.");
if (s == v.s
&& (data <= v.data && v.data < data+len*s
|| v.data <= data && data < v.data+v.len*v.s)
&& (data-v.data) % s == 0)
throw SYLV_MES_EXCEPTION("Attempt to assign overlapping vectors.");
copy(v.data, v.s);
return *this;
}
Vector &
Vector::operator=(Vector &&v)
{
if (v.len != len)
throw SYLV_MES_EXCEPTION("Attempt to assign vectors with different lengths.");
copy(v.data, v.s);
return *this;
}
Vector &
Vector::operator=(const ConstVector &v)
{
if (v.len != len)
throw SYLV_MES_EXCEPTION("Attempt to assign vectors with different lengths.");
if (s == v.s
&& (data <= v.data && v.data < data+len*s
|| v.data <= data && data < v.data+v.len*v.s)
&& (data-v.data) % s == 0)
throw SYLV_MES_EXCEPTION("Attempt to assign overlapping vectors.");
copy(v.data, v.s);
return *this;
}
void
Vector::copy(const double *d, int inc)
{
blas_int n = len;
blas_int incy = s;
blas_int inc2 = inc;
dcopy(&n, d, &inc2, data, &incy);
}
Vector::Vector(Vector &v, int off_arg, int l)
: len(l), s(v.s), data{v.data+off_arg*v.s}, destroy{false}
{
if (off_arg < 0 || off_arg + len > v.len)
throw SYLV_MES_EXCEPTION("Subvector not contained in supvector.");
}
Vector::Vector(const Vector &v, int off_arg, int l)
: len(l), data{new double[len]}
{
if (off_arg < 0 || off_arg + len > v.len)
throw SYLV_MES_EXCEPTION("Subvector not contained in supvector.");
copy(v.data+off_arg*v.s, v.s);
}
Vector::Vector(Vector &v, int off_arg, int skip, int l)
: len(l), s(v.s*skip), data{v.data+off_arg*v.s}, destroy{false}
{
}
Vector::Vector(const Vector &v, int off_arg, int skip, int l)
: len(l), data{new double[len]}
{
copy(v.data+off_arg*v.s, v.s*skip);
}
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
Vector::Vector(mxArray *p)
: len{static_cast<int>(mxGetNumberOfElements(p))},
data{mxGetPr(p)}, destroy{false}
{
if (!mxIsDouble(p))
throw SYLV_MES_EXCEPTION("This is not a MATLAB array of doubles.");
}
#endif
bool
Vector::operator==(const Vector &y) const
{
return ConstVector(*this) == y;
}
bool
Vector::operator!=(const Vector &y) const
{
return ConstVector(*this) != y;
}
bool
Vector::operator<(const Vector &y) const
{
return ConstVector(*this) < y;
}
bool
Vector::operator<=(const Vector &y) const
{
return ConstVector(*this) <= y;
}
bool
Vector::operator>(const Vector &y) const
{
return ConstVector(*this) > y;
}
bool
Vector::operator>=(const Vector &y) const
{
return ConstVector(*this) >= y;
}
void
Vector::zeros()
{
if (s == 1)
std::fill_n(data, len, 0.0);
else
for (int i = 0; i < len; i++)
operator[](i) = 0.0;
}
void
Vector::nans()
{
for (int i = 0; i < len; i++)
operator[](i) = std::numeric_limits<double>::quiet_NaN();
}
void
Vector::infs()
{
for (int i = 0; i < len; i++)
operator[](i) = std::numeric_limits<double>::infinity();
}
void
Vector::rotatePair(double alpha, double beta1, double beta2, int i)
{
double tmp = alpha*operator[](i) - beta1*operator[](i+1);
operator[](i+1) = alpha*operator[](i+1) - beta2*operator[](i);
operator[](i) = tmp;
}
void
Vector::add(double r, const Vector &v)
{
add(r, ConstVector(v));
}
void
Vector::add(double r, const ConstVector &v)
{
blas_int n = len;
blas_int incx = v.s;
blas_int incy = s;
daxpy(&n, &r, v.data, &incx, data, &incy);
}
void
Vector::addComplex(const std::complex<double> &z, const Vector &v)
{
addComplex(z, ConstVector(v));
}
void
Vector::addComplex(const std::complex<double> &z, const ConstVector &v)
{
blas_int n = len/2;
blas_int incx = v.s;
blas_int incy = s;
zaxpy(&n, reinterpret_cast<const double(&)[2]>(z), v.data, &incx, data, &incy);
}
void
Vector::mult(double r)
{
blas_int n = len;
blas_int incx = s;
dscal(&n, &r, data, &incx);
}
void
Vector::mult2(double alpha, double beta1, double beta2,
Vector &x1, Vector &x2,
const Vector &b1, const Vector &b2)
{
x1.zeros();
x2.zeros();
mult2a(alpha, beta1, beta2, x1, x2, b1, b2);
}
void
Vector::mult2a(double alpha, double beta1, double beta2,
Vector &x1, Vector &x2,
const Vector &b1, const Vector &b2)
{
x1.add(alpha, b1);
x1.add(-beta1, b2);
x2.add(alpha, b2);
x2.add(-beta2, b1);
}
double
Vector::getNorm() const
{
ConstVector v(*this);
return v.getNorm();
}
double
Vector::getMax() const
{
ConstVector v(*this);
return v.getMax();
}
double
Vector::getNorm1() const
{
ConstVector v(*this);
return v.getNorm1();
}
double
Vector::dot(const Vector &y) const
{
return ConstVector(*this).dot(ConstVector(y));
}
bool
Vector::isFinite() const
{
return (ConstVector(*this)).isFinite();
}
void
Vector::print() const
{
auto ff = std::cout.flags();
std::cout << std::setprecision(4);
for (int i = 0; i < len; i++)
std::cout << i << '\t' << std::setw(8) << operator[](i) << std::endl;
std::cout.flags(ff);
}
ConstVector::ConstVector(const Vector &v)
: len{v.len}, s{v.s}, data{v.data}
{
}
ConstVector::ConstVector(const ConstVector &v, int off_arg, int l)
: len{l}, s{v.s}, data{v.data+off_arg*v.s}
{
if (off_arg < 0 || off_arg + len > v.len)
throw SYLV_MES_EXCEPTION("Subvector not contained in supvector.");
}
ConstVector::ConstVector(const ConstVector &v, int off_arg, int skip, int l)
: len(l), s{v.s*skip}, data{v.data+off_arg*v.s}
{
}
ConstVector::ConstVector(const double *d, int skip, int l)
: len{l}, s{skip}, data{d}
{
}
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
ConstVector::ConstVector(const mxArray *p)
: len{static_cast<int>(mxGetNumberOfElements(p))},
data{mxGetPr(p)}
{
if (!mxIsDouble(p))
throw SYLV_MES_EXCEPTION("This is not a MATLAB array of doubles.");
}
#endif
bool
ConstVector::operator==(const ConstVector &y) const
{
if (len != y.len)
return false;
if (len == 0)
return true;
int i = 0;
while (i < len && operator[](i) == y[i])
i++;
return i == len;
}
bool
ConstVector::operator<(const ConstVector &y) const
{
int i = std::min(len, y.len);
int ii = 0;
while (ii < i && operator[](ii) == y[ii])
ii++;
if (ii < i)
return operator[](ii) < y[ii];
else
return len < y.len;
}
double
ConstVector::getNorm() const
{
double s = 0;
for (int i = 0; i < len; i++)
s += operator[](i)*operator[](i);
return sqrt(s);
}
double
ConstVector::getMax() const
{
double r = 0;
for (int i = 0; i < len; i++)
r = std::max(r, std::abs(operator[](i)));
return r;
}
double
ConstVector::getNorm1() const
{
double norm = 0.0;
for (int i = 0; i < len; i++)
norm += std::abs(operator[](i));
return norm;
}
double
ConstVector::dot(const ConstVector &y) const
{
if (len != y.len)
throw SYLV_MES_EXCEPTION("Vector has different length in ConstVector::dot.");
blas_int n = len;
blas_int incx = s;
blas_int incy = y.s;
return ddot(&n, data, &incx, y.data, &incy);
}
bool
ConstVector::isFinite() const
{
int i = 0;
while (i < len && std::isfinite(operator[](i)))
i++;
return i == len;
}
void
ConstVector::print() const
{
auto ff = std::cout.flags();
std::cout << std::setprecision(4);
for (int i = 0; i < len; i++)
std::cout << i << '\t' << std::setw(8) << operator[](i) << std::endl;
std::cout.flags(ff);
}
|