00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00094 #ifndef matpackI_h
00095 #define matpackI_h
00096
00097 #include "matpack.h"
00098 #include <cassert>
00099 #include "array.h"
00100
00101
00102 class bofstream;
00103
00114 class Joker {
00115
00116 };
00117
00118
00119 extern Joker joker;
00120
00121
00122 class VectorView;
00123
00124
00125 class ConstVectorView;
00126
00127
00128 class ConstMatrixView;
00129
00145 class Range{
00146 public:
00147
00148 Range(Index start, Index extent, Index stride=1);
00149 Range(Index start, Joker j, Index stride=1);
00150 Range(Joker j, Index stride=1);
00151 Range(Index max_size, const Range& r);
00152 Range(const Range& p, const Range& n);
00153
00154
00155 friend class ConstVectorView;
00156 friend class VectorView;
00157 friend class Vector;
00158 friend class ConstMatrixView;
00159 friend class MatrixView;
00160 friend class Matrix;
00161 friend class Iterator2D;
00162 friend class Iterator3D;
00163 friend class Iterator4D;
00164 friend class Iterator5D;
00165 friend class Iterator6D;
00166 friend class Iterator7D;
00167 friend class ConstIterator2D;
00168 friend class ConstIterator3D;
00169 friend class ConstIterator4D;
00170 friend class ConstIterator5D;
00171 friend class ConstIterator6D;
00172 friend class ConstIterator7D;
00173 friend class ConstTensor3View;
00174 friend class Tensor3View;
00175 friend class Tensor3;
00176 friend class ConstTensor4View;
00177 friend class Tensor4View;
00178 friend class Tensor4;
00179 friend class ConstTensor5View;
00180 friend class Tensor5View;
00181 friend class Tensor5;
00182 friend class ConstTensor6View;
00183 friend class Tensor6View;
00184 friend class Tensor6;
00185 friend class ConstTensor7View;
00186 friend class Tensor7View;
00187 friend class Tensor7;
00188 friend class Sparse;
00189 friend void mult (VectorView, const ConstMatrixView&,
00190 const ConstVectorView&);
00191
00193 Index get_start () const { return mstart; }
00195 Index get_extent () const { return mextent; }
00197 Index get_stride () const { return mstride; }
00198
00199 private:
00201 Index mstart;
00204 Index mextent;
00206 Index mstride;
00207 };
00208
00211 class Iterator1D {
00212 public:
00214 Iterator1D() : mx(NULL), mstride(0) { }
00215
00217 Iterator1D(const Iterator1D& o) : mx(o.mx), mstride(o.mstride)
00218 { }
00219
00221 Iterator1D(Numeric *x, Index stride) : mx(x), mstride(stride)
00222 { }
00223
00224
00225
00227 Iterator1D& operator++()
00228 { mx += mstride; return *this; }
00229
00231 Numeric& operator*() const { return *mx; }
00232
00234 bool operator!=(const Iterator1D& other) const
00235 { if (mx != other.mx) return true; else return false; }
00236
00237
00238 private:
00240 Numeric *mx;
00242 Index mstride;
00243 };
00244
00247 class ConstIterator1D {
00248 public:
00250 ConstIterator1D() : mx(NULL), mstride(0)
00251 { }
00252
00254 ConstIterator1D(const ConstIterator1D& o) : mx(o.mx), mstride(o.mstride)
00255 { }
00256
00258 ConstIterator1D(Numeric *x, Index stride) : mx(x), mstride(stride)
00259 { }
00260
00261
00263 ConstIterator1D& operator++()
00264 { mx += mstride; return *this; }
00265
00267 const Numeric& operator*() const { return *mx; }
00268
00270 bool operator!=(const ConstIterator1D& other) const
00271 { if (mx != other.mx) return true; else return false; }
00272
00273 private:
00275 const Numeric *mx;
00277 Index mstride;
00278 };
00279
00280
00281 class Vector;
00282
00283
00284 class MatrixView;
00285
00291 class ConstVectorView {
00292 public:
00293
00294 typedef ConstIterator1D const_iterator;
00295
00296
00297 Index nelem() const;
00298 Numeric sum() const;
00299
00300
00302 Numeric operator[](Index n) const
00303 {
00304 assert( 0<=n );
00305 assert( n<mrange.mextent );
00306 return *( mdata +
00307 mrange.mstart +
00308 n*mrange.mstride );
00309 }
00310
00311 ConstVectorView operator[](const Range& r) const;
00312
00313
00314 ConstIterator1D begin() const;
00315 ConstIterator1D end() const;
00316
00317
00318 operator ConstMatrixView() const;
00319
00321 virtual ~ConstVectorView() {};
00322
00323
00324 friend class VectorView;
00325 friend class ConstIterator2D;
00326 friend class ConstMatrixView;
00327 friend class ConstTensor3View;
00328 friend class ConstTensor4View;
00329 friend class ConstTensor5View;
00330 friend class ConstTensor6View;
00331 friend class ConstTensor7View;
00332 friend int poly_root_solve (Matrix& roots, Vector& coeffs);
00333 friend void mult (VectorView, const ConstMatrixView&,
00334 const ConstVectorView&);
00335
00336
00337 ConstVectorView(const Numeric& a);
00338
00339 protected:
00340
00341 ConstVectorView();
00342 ConstVectorView(Numeric *data, const Range& range);
00343 ConstVectorView(Numeric *data, const Range& p, const Range& n);
00344
00345
00346
00348 Range mrange;
00350 Numeric *mdata;
00351 };
00352
00364 class VectorView : public ConstVectorView {
00365 public:
00366 VectorView (const Vector&);
00367 VectorView (Vector& v);
00368
00369
00370 typedef Iterator1D iterator;
00371
00372
00375 Numeric operator[](Index n) const
00376 { return ConstVectorView::operator[](n); }
00377
00378 ConstVectorView operator[](const Range& r) const;
00379
00381 Numeric& operator[](Index n)
00382 {
00383 assert( 0<=n );
00384 assert( n<mrange.mextent );
00385 return *( mdata +
00386 mrange.mstart +
00387 n*mrange.mstride );
00388 }
00389
00390 VectorView operator[](const Range& r);
00391
00392
00393 ConstIterator1D begin() const;
00394 ConstIterator1D end() const;
00395
00396 Iterator1D begin();
00397 Iterator1D end();
00398
00399
00400 VectorView& operator=(const ConstVectorView& v);
00401 VectorView& operator=(const VectorView& v);
00402 VectorView& operator=(const Vector& v);
00403 VectorView& operator=(const Array<Numeric>& v);
00404 VectorView& operator=(Numeric x);
00405
00406
00407 VectorView operator*=(Numeric x);
00408 VectorView operator/=(Numeric x);
00409 VectorView operator+=(Numeric x);
00410 VectorView operator-=(Numeric x);
00411
00412 VectorView operator*=(const ConstVectorView& x);
00413 VectorView operator/=(const ConstVectorView& x);
00414 VectorView operator+=(const ConstVectorView& x);
00415 VectorView operator-=(const ConstVectorView& x);
00416
00417
00418 operator MatrixView();
00419
00420 const Numeric *get_c_array() const;
00421 Numeric *get_c_array();
00422
00424 virtual ~VectorView() {};
00425
00426
00427 friend class ConstIterator2D;
00428 friend class Iterator2D;
00429 friend class MatrixView;
00430 friend class Tensor3View;
00431 friend class Tensor4View;
00432 friend class Tensor5View;
00433 friend class Tensor6View;
00434 friend class Tensor7View;
00435
00436
00437 VectorView(Numeric& a);
00438
00439
00440 protected:
00441
00442 VectorView();
00443 VectorView(Numeric *data, const Range& range);
00444 VectorView(Numeric *data, const Range& p, const Range& n);
00445 };
00446
00450 class Iterator2D {
00451 public:
00452
00454 Iterator2D() : msv(), mstride(0) { }
00455
00457 Iterator2D(const Iterator2D& o) : msv(o.msv), mstride(o.mstride)
00458 { }
00459
00461 Iterator2D(const VectorView& x, Index stride) : msv(x), mstride(stride)
00462 { }
00463
00464
00466 Iterator2D& operator++() { msv.mdata += mstride; return *this; }
00467
00469 bool operator!=(const Iterator2D& other) const
00470 { if ( msv.mdata + msv.mrange.mstart !=
00471 other.msv.mdata + other.msv.mrange.mstart )
00472 return true;
00473 else
00474 return false;
00475 }
00476
00479 VectorView * operator->() { return &msv; }
00480
00482 VectorView& operator*() { return msv; }
00483
00484 private:
00486 VectorView msv;
00488 Index mstride;
00489 };
00490
00494 class ConstIterator2D {
00495 public:
00496
00498 ConstIterator2D() : msv(), mstride(0) { }
00499
00501 ConstIterator2D(const ConstIterator2D& o) : msv(o.msv), mstride(o.mstride)
00502 { }
00503
00505 ConstIterator2D(const ConstVectorView& x, Index stride)
00506 : msv(x), mstride(stride)
00507 { }
00508
00509
00511 ConstIterator2D& operator++() { msv.mdata += mstride; return *this; }
00512
00514 bool operator!=(const ConstIterator2D& other) const
00515 { if ( msv.mdata + msv.mrange.mstart !=
00516 other.msv.mdata + other.msv.mrange.mstart )
00517 return true;
00518 else
00519 return false;
00520 }
00521
00524 const ConstVectorView* operator->() const { return &msv; }
00525
00527 const ConstVectorView& operator*() const { return msv; }
00528
00529 private:
00531 ConstVectorView msv;
00533 Index mstride;
00534 };
00535
00546 class Vector : public VectorView {
00547 public:
00548
00549 Vector();
00550 explicit Vector(Index n);
00551 Vector(Index n, Numeric fill);
00552 Vector(Numeric start, Index extent, Numeric stride);
00553 Vector(const ConstVectorView& v);
00554 Vector(const Vector& v);
00555
00556
00557 Vector& operator=(const Vector& v);
00558 Vector& operator=(const Array<Numeric>& v);
00559 Vector& operator=(Numeric x);
00560
00561
00562 void resize(Index n);
00563
00564
00565 virtual ~Vector();
00566 };
00567
00568
00569 class Matrix;
00570
00571
00582 class ConstMatrixView {
00583 public:
00584
00585 typedef ConstIterator2D const_iterator;
00586
00587
00588 Index nrows() const;
00589 Index ncols() const;
00590
00591
00593 Numeric operator()(Index r, Index c) const
00594 {
00595 assert( 0<=r );
00596 assert( 0<=c );
00597 assert( r<mrr.mextent );
00598 assert( c<mcr.mextent );
00599
00600 return *( mdata +
00601 mrr.mstart +
00602 r*mrr.mstride +
00603 mcr.mstart +
00604 c*mcr.mstride );
00605 }
00606
00607 ConstMatrixView operator()(const Range& r, const Range& c) const;
00608 ConstVectorView operator()(const Range& r, Index c) const;
00609 ConstVectorView operator()(Index r, const Range& c) const;
00610
00611
00612 ConstIterator2D begin() const;
00613 ConstIterator2D end() const;
00614
00616 virtual ~ConstMatrixView() {};
00617
00618
00619 friend class MatrixView;
00620 friend class ConstIterator3D;
00621 friend class ConstVectorView;
00622 friend class ConstTensor3View;
00623 friend class ConstTensor4View;
00624 friend class ConstTensor5View;
00625 friend class ConstTensor6View;
00626 friend class ConstTensor7View;
00627 friend ConstMatrixView transpose(ConstMatrixView m);
00628 friend int poly_root_solve (Matrix& roots, Vector& coeffs);
00629 friend void mult (VectorView, const ConstMatrixView&,
00630 const ConstVectorView&);
00631
00632 protected:
00633
00634 ConstMatrixView();
00635 ConstMatrixView(Numeric *data, const Range& r, const Range& c);
00636 ConstMatrixView(Numeric *data,
00637 const Range& pr, const Range& pc,
00638 const Range& nr, const Range& nc);
00639
00640
00641
00643 Range mrr;
00645 Range mcr;
00647 Numeric *mdata;
00648 };
00649
00659 class MatrixView : public ConstMatrixView {
00660 public:
00661
00662 typedef Iterator2D iterator;
00663
00664
00667 Numeric operator()(Index r, Index c) const
00668 { return ConstMatrixView::operator()(r,c); }
00669
00670 ConstMatrixView operator()(const Range& r, const Range& c) const;
00671 ConstVectorView operator()(const Range& r, Index c) const;
00672 ConstVectorView operator()(Index r, const Range& c) const;
00673
00675 Numeric& operator()(Index r, Index c)
00676 {
00677 assert( 0<=r );
00678 assert( 0<=c );
00679 assert( r<mrr.mextent );
00680 assert( c<mcr.mextent );
00681
00682 return *( mdata +
00683 mrr.mstart +
00684 r*mrr.mstride +
00685 mcr.mstart +
00686 c*mcr.mstride );
00687 }
00688
00689 MatrixView operator()(const Range& r, const Range& c);
00690 VectorView operator()(const Range& r, Index c);
00691 VectorView operator()(Index r, const Range& c);
00692
00693
00694 ConstIterator2D begin() const;
00695 ConstIterator2D end() const;
00696
00697 Iterator2D begin();
00698 Iterator2D end();
00699
00700
00701 MatrixView& operator=(const ConstMatrixView& v);
00702 MatrixView& operator=(const MatrixView& v);
00703 MatrixView& operator=(const Matrix& v);
00704 MatrixView& operator=(const ConstVectorView& v);
00705 MatrixView& operator=(Numeric x);
00706
00707
00708 MatrixView& operator*=(Numeric x);
00709 MatrixView& operator/=(Numeric x);
00710 MatrixView& operator+=(Numeric x);
00711 MatrixView& operator-=(Numeric x);
00712
00713 MatrixView& operator*=(const ConstMatrixView& x);
00714 MatrixView& operator/=(const ConstMatrixView& x);
00715 MatrixView& operator+=(const ConstMatrixView& x);
00716 MatrixView& operator-=(const ConstMatrixView& x);
00717
00718 MatrixView& operator*=(const ConstVectorView& x);
00719 MatrixView& operator/=(const ConstVectorView& x);
00720 MatrixView& operator+=(const ConstVectorView& x);
00721 MatrixView& operator-=(const ConstVectorView& x);
00722
00723
00724 const Numeric *get_c_array() const;
00725 Numeric *get_c_array();
00726
00728 virtual ~MatrixView() {};
00729
00730
00731 friend class VectorView;
00732 friend class Iterator3D;
00733 friend class Tensor3View;
00734 friend class Tensor4View;
00735 friend class Tensor5View;
00736 friend class Tensor6View;
00737 friend class Tensor7View;
00738 friend ConstMatrixView transpose(ConstMatrixView m);
00739 friend MatrixView transpose(MatrixView m);
00740
00741 protected:
00742
00743 MatrixView();
00744 MatrixView(Numeric *data, const Range& r, const Range& c);
00745 MatrixView(Numeric *data,
00746 const Range& pr, const Range& pc,
00747 const Range& nr, const Range& nc);
00748 };
00749
00758 class Matrix : public MatrixView {
00759 public:
00760
00761 Matrix();
00762 Matrix(Index r, Index c);
00763 Matrix(Index r, Index c, Numeric fill);
00764 Matrix(const ConstMatrixView& v);
00765 Matrix(const Matrix& v);
00766
00767
00768 Matrix& operator=(const Matrix& x);
00769 Matrix& operator=(Numeric x);
00770 Matrix& operator=(const ConstVectorView& v);
00771
00772
00773 void resize(Index r, Index c);
00774
00775
00776 virtual ~Matrix();
00777
00778 Numeric *get_raw_data() { return mdata; };
00779 };
00780
00781
00782
00783
00784 void copy(ConstIterator1D origin,
00785 const ConstIterator1D& end,
00786 Iterator1D target);
00787
00788 void copy(Numeric x,
00789 Iterator1D target,
00790 const Iterator1D& end);
00791
00792 void copy(ConstIterator2D origin,
00793 const ConstIterator2D& end,
00794 Iterator2D target);
00795
00796 void copy(Numeric x,
00797 Iterator2D target,
00798 const Iterator2D& end);
00799
00800 void mult( VectorView y,
00801 const ConstMatrixView& M,
00802 const ConstVectorView& x );
00803
00804 void mult( MatrixView A,
00805 const ConstMatrixView& B,
00806 const ConstMatrixView& C );
00807
00808 ConstMatrixView transpose(ConstMatrixView m);
00809
00810 MatrixView transpose(MatrixView m);
00811
00812 void transform( VectorView y,
00813 double (&my_func)(double),
00814 ConstVectorView x );
00815
00816 void transform( MatrixView y,
00817 double (&my_func)(double),
00818 ConstMatrixView x );
00819
00820 Numeric max(const ConstVectorView& x);
00821
00822 Numeric max(const ConstMatrixView& x);
00823
00824 Numeric min(const ConstVectorView& x);
00825
00826 Numeric min(const ConstMatrixView& x);
00827
00828 Numeric mean(const ConstVectorView& x);
00829
00830 Numeric mean(const ConstMatrixView& x);
00831
00832 Numeric operator*(const ConstVectorView& a, const ConstVectorView& b);
00833
00834 ostream& operator<<(ostream& os, const ConstVectorView& v);
00835
00836 ostream& operator<<(ostream& os, const ConstMatrixView& v);
00837
00839
00840 #ifndef NDEBUG
00841
00842 Numeric debug_matrixview_get_elem (MatrixView& mv, Index r, Index c);
00843
00844 #endif
00846
00847 #endif // matpackI_h