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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
/*
* Copyright (c) 2007, Michael Lehn * Copyright (c) 2011, Michael Lehn * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1) Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2) Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3) Neither the name of the FLENS development group nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef FLENS_VECTORTYPES_IMPL_DENSEVECTOR_TCC #define FLENS_VECTORTYPES_IMPL_DENSEVECTOR_TCC 1 #include <flens/blas/blas.h> #include <flens/typedefs.h> #include <flens/vectortypes/impl/densevector.h> #include <flens/vectortypes/impl/dv/constelementclosure.tcc> #include <flens/vectortypes/impl/dv/elementclosure.tcc> #include <flens/vectortypes/impl/dv/initializer.tcc> namespace flens { template <typename A> DenseVector<A>::DenseVector() : _reverse(false) { } template <typename A> DenseVector<A>::DenseVector(IndexType length) : _array(length), _reverse(false) { ASSERT(length>=0); } template <typename A> DenseVector<A>::DenseVector(IndexType length, IndexType firstIndex) : _array(length, firstIndex), _reverse(false) { ASSERT(length>=0); } template <typename A> DenseVector<A>::DenseVector(const Range<IndexType> &range) : _array(range.numTicks(), range.firstIndex()), _reverse(false) { ASSERT(range.stride()>0); } template <typename A> DenseVector<A>::DenseVector(const Engine &engine, bool reverse) : _array(engine), _reverse(reverse) { } template <typename A> DenseVector<A>::DenseVector(const DenseVector &rhs) : Vector<DenseVector>(), _array(rhs._array), _reverse(rhs._reverse) { } template <typename A> template <typename RHS> DenseVector<A>::DenseVector(const DenseVector<RHS> &rhs) : _array(rhs.engine()), _reverse(false) { } template <typename A> template <typename RHS> DenseVector<A>::DenseVector(DenseVector<RHS> &rhs) : _array(rhs.engine()), _reverse(false) { } template <typename A> template <typename RHS> DenseVector<A>::DenseVector(const Vector<RHS> &rhs) : _reverse(false) { assign(rhs, *this); } //-- operators ----------------------------------------------------------------- template <typename A> typename DenseVector<A>::Initializer DenseVector<A>::operator=(const ElementType &value) { _array.fill(value); return Initializer(*this, firstIndex()); } template <typename A> DenseVector<A> & DenseVector<A>::operator=(const DenseVector &rhs) { if (this!=&rhs) { assign(rhs, *this); } return *this; } template <typename A> template <typename E> DenseVector<A> & DenseVector<A>::operator=(const Vector<E> &rhs) { assign(rhs, *this); return *this; } template <typename A> template <typename E> DenseVector<A> & DenseVector<A>::operator+=(const Vector<E> &rhs) { plusAssign(rhs, *this); return *this; } template <typename A> template <typename E> DenseVector<A> & DenseVector<A>::operator-=(const Vector<E> &rhs) { minusAssign(rhs, *this); return *this; } template <typename A> DenseVector<A> & DenseVector<A>::operator+=(const ElementType &alpha) { for (int i=firstIndex(); i<=lastIndex(); ++i) { (*this)(i) += alpha; } return *this; } template <typename A> DenseVector<A> & DenseVector<A>::operator-=(const ElementType &alpha) { for (int i=firstIndex(); i<=lastIndex(); ++i) { (*this)(i) -= alpha; } return *this; } template <typename A> DenseVector<A> & DenseVector<A>::operator*=(const ElementType &alpha) { blas::scal(alpha, *this); return *this; } template <typename A> DenseVector<A> & DenseVector<A>::operator/=(const ElementType &alpha) { blas::rscal(alpha, *this); return *this; } template <typename A> const typename DenseVector<A>::ElementType & DenseVector<A>::operator()(IndexType index) const { return _array(index); } template <typename A> typename DenseVector<A>::ElementType & DenseVector<A>::operator()(IndexType index) { return _array(index); } template <typename A> template <typename S> const densevector::ConstElementClosure<DenseVector<A>, typename Scalar<S>::Impl> DenseVector<A>::operator()(const Scalar<S> &index) const { typedef typename Scalar<S>::Impl ScalarImpl; typedef densevector::ConstElementClosure<DenseVector, ScalarImpl> CEC; return CEC(*this, index.impl()); } template <typename A> const typename DenseVector<A>::ConstElementClosure DenseVector<A>::operator()(const IndexVariable &index) const { return ConstElementClosure(*this, index); } template <typename A> typename DenseVector<A>::ElementClosure DenseVector<A>::operator()(IndexVariable &index) { return ElementClosure(*this, index); } //-- views --------------------------------------------------------------------- template <typename A> const typename DenseVector<A>::ConstView DenseVector<A>::operator()(const Range<IndexType> &range) const { ASSERT(range.firstIndex()>=firstIndex()); ASSERT(range.lastIndex()<=lastIndex()); return _array.view(range.firstIndex(), range.lastIndex(), range.stride(), firstIndex()); } template <typename A> typename DenseVector<A>::View DenseVector<A>::operator()(const Range<IndexType> &range) { ASSERT(range.firstIndex()>=firstIndex()); ASSERT(range.lastIndex()<=lastIndex()); return _array.view(range.firstIndex(), range.lastIndex(), range.stride(), firstIndex()); } template <typename A> const typename DenseVector<A>::ConstView DenseVector<A>::operator()(const Range<IndexType> &range, IndexType _firstViewIndex) const { ASSERT(range.firstIndex()>=firstIndex()); ASSERT(range.lastIndex()<=lastIndex()); return _array.view(range.firstIndex(), range.lastIndex(), range.stride(), _firstViewIndex); } template <typename A> typename DenseVector<A>::View DenseVector<A>::operator()(const Range<IndexType> &range, IndexType _firstViewIndex) { ASSERT(range.firstIndex()>=firstIndex()); ASSERT(range.lastIndex()<=lastIndex()); return _array.view(range.firstIndex(), range.lastIndex(), range.stride(), _firstViewIndex); } template <typename A> const typename DenseVector<A>::ConstView DenseVector<A>::operator()(const Underscore<IndexType> &/*all*/, IndexType _firstViewIndex) const { return _array.view(_array.firstIndex(), _array.lastIndex(), _array.stride(), _firstViewIndex); } template <typename A> typename DenseVector<A>::View DenseVector<A>::operator()(const Underscore<IndexType> &/*all*/, IndexType _firstViewIndex) { return _array.view(_array.firstIndex(), _array.lastIndex(), _array.stride(), _firstViewIndex); } template <typename A> const typename DenseVector<A>::ConstView DenseVector<A>::reverse() const { return ConstView(_array, !_reverse); } template <typename A> typename DenseVector<A>::View DenseVector<A>::reverse() { return View(_array, !_reverse); } // -- methods ------------------------------------------------------------------ template <typename A> Range<typename DenseVector<A>::IndexType> DenseVector<A>::range() const { return Range<IndexType>(_array.firstIndex(),_array.lastIndex()); } template <typename A> typename DenseVector<A>::IndexType DenseVector<A>::firstIndex() const { if (_reverse) { return _array.lastIndex(); } return _array.firstIndex(); } template <typename A> typename DenseVector<A>::IndexType DenseVector<A>::lastIndex() const { if (_reverse) { return _array.firstIndex(); } return _array.lastIndex(); } template <typename A> typename DenseVector<A>::IndexType DenseVector<A>::length() const { return _array.length(); } template <typename A> typename DenseVector<A>::IndexType DenseVector<A>::inc() const { if (_reverse) { return -1; } return 1; } template <typename A> typename DenseVector<A>::IndexType DenseVector<A>::endIndex() const { return lastIndex() + inc(); } template <typename A> const typename DenseVector<A>::ElementType * DenseVector<A>::data() const { return _array.data(); } template <typename A> typename DenseVector<A>::ElementType * DenseVector<A>::data() { return _array.data(); } template <typename A> typename DenseVector<A>::IndexType DenseVector<A>::stride() const { if (_reverse) { return -_array.stride(); } return _array.stride(); } template <typename A> typename DenseVector<A>::IndexType DenseVector<A>::indexBase() const { return _array.firstIndex(); } template <typename A> template <typename RHS> bool DenseVector<A>::resize(const DenseVector<RHS> &rhs, const ElementType &value) { bool resized = _array.resize(rhs.engine(), value); if (_reverse != rhs.reversed()) { _reverse = rhs.reversed(); return true; } return resized; } template <typename A> bool DenseVector<A>::resize(IndexType length, IndexType firstIndex, const ElementType &value) { return _array.resize(length, firstIndex, value); } template <typename A> bool DenseVector<A>::fill(const ElementType &value) { return _array.fill(value); } template <typename A> void DenseVector<A>::changeIndexBase(IndexType firstIndex) { _array.changeIndexBase(firstIndex); } // -- implementation ----------------------------------------------------------- template <typename A> const A & DenseVector<A>::engine() const { return _array; } template <typename A> A & DenseVector<A>::engine() { return _array; } template <typename A> bool DenseVector<A>::reversed() const { return _reverse; } //-- DenseVector specific functions -------------------------------------------- // // real // template <typename VZ> typename RealVector<DenseVector<VZ> >::ConstView real(const DenseVector<VZ> &z) { typedef typename RealVector<DenseVector<VZ> >::ConstViewEngine Engine; typedef typename VZ::ElementType T; typedef typename Engine::ElementType PT; static_assert(sizeof(T)%sizeof(PT)==0, "Are you kidding me?"); return Engine(z.length(), reinterpret_cast<const PT *>(z.data()), z.stride()*sizeof(T)/sizeof(PT), z.firstIndex()); } template <typename VZ> typename RestrictTo<IsDenseVector<VZ>::value, typename RealVector<typename RemoveRef<VZ>::Type>::View>::Type real(VZ &&z) { typedef typename RemoveRef<VZ>::Type VectorZ; typedef typename RealVector<VectorZ>::ViewEngine Engine; typedef typename VectorZ::ElementType T; typedef typename Engine::ElementType PT; static_assert(sizeof(T)%sizeof(PT)==0, "Are you kidding me?"); return Engine(z.length(), reinterpret_cast<PT *>(z.data()), z.stride()*sizeof(T)/sizeof(PT), z.firstIndex()); } // // imag // template <typename VZ> typename ImagVector<DenseVector<VZ> >::ConstView imag(const DenseVector<VZ> &z) { typedef typename ImagVector<DenseVector<VZ> >::ConstViewEngine Engine; typedef typename VZ::ElementType T; typedef typename Engine::ElementType PT; static_assert(sizeof(T)==2*sizeof(PT), "Are you kidding me?"); return Engine(z.length(), reinterpret_cast<const PT *>(z.data()) + 1, 2*z.stride(), z.firstIndex()); } template <typename VZ> typename RestrictTo<IsDenseVector<VZ>::value, typename ImagVector<typename RemoveRef<VZ>::Type>::View>::Type imag(VZ &&z) { typedef typename RemoveRef<VZ>::Type VectorZ; typedef typename ImagVector<VectorZ>::ViewEngine Engine; typedef typename VectorZ::ElementType T; typedef typename Engine::ElementType PT; static_assert(sizeof(T)%sizeof(PT)==0, "Are you kidding me?"); return Engine(z.length(), reinterpret_cast<PT *>(z.data()) + 1, 2*z.stride(), z.firstIndex()); } // // fillRandom // template <typename VX> typename RestrictTo<IsDenseVector<VX>::value, bool>::Type fillRandom(VX &&x) { return fillRandom(x.engine()); } } // namespace flens #endif // FLENS_VECTORTYPES_IMPL_DENSEVECTOR_TCC |