00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00025 #include "matpackIII.h"
00026 #include "exceptions.h"
00027
00028
00029
00030
00034 ConstTensor3View ConstTensor3View::operator()(const Range& p,
00035 const Range& r,
00036 const Range& c) const
00037 {
00038 return ConstTensor3View(mdata, mpr, mrr, mcr, p, r, c);
00039 }
00040
00043 ConstMatrixView ConstTensor3View::operator()(const Range& p,
00044 const Range& r,
00045 Index c) const
00046 {
00047
00048 assert( 0 <= c );
00049 assert( c < mcr.mextent );
00050
00051 return ConstMatrixView(mdata + mcr.mstart + c*mcr.mstride,
00052 mpr, mrr,
00053 p, r);
00054 }
00055
00058 ConstMatrixView ConstTensor3View::operator()(const Range& p,
00059 Index r,
00060 const Range& c) const
00061 {
00062
00063 assert( 0 <= r );
00064 assert( r < mrr.mextent );
00065
00066 return ConstMatrixView(mdata + mrr.mstart + r*mrr.mstride,
00067 mpr, mcr,
00068 p, c);
00069 }
00070
00073 ConstMatrixView ConstTensor3View::operator()(Index p,
00074 const Range& r,
00075 const Range& c) const
00076 {
00077
00078 assert( 0 <= p );
00079 assert( p < mpr.mextent );
00080
00081 return ConstMatrixView(mdata + mpr.mstart + p*mpr.mstride,
00082 mrr, mcr,
00083 r, c);
00084 }
00085
00088 ConstVectorView ConstTensor3View::operator()(Index p,
00089 Index r,
00090 const Range& c) const
00091 {
00092
00093 assert( 0 <= p );
00094 assert( 0 <= r );
00095 assert( p < mpr.mextent );
00096 assert( r < mrr.mextent );
00097
00098 return ConstVectorView(mdata +
00099 mpr.mstart + p*mpr.mstride +
00100 mrr.mstart + r*mrr.mstride,
00101 mcr, c);
00102 }
00103
00106 ConstVectorView ConstTensor3View::operator()(Index p,
00107 const Range& r,
00108 Index c) const
00109 {
00110
00111 assert( 0 <= p );
00112 assert( 0 <= c );
00113 assert( p < mpr.mextent );
00114 assert( c < mcr.mextent );
00115
00116 return ConstVectorView(mdata +
00117 mpr.mstart + p*mpr.mstride +
00118 mcr.mstart + c*mcr.mstride,
00119 mrr, r);
00120 }
00121
00124 ConstVectorView ConstTensor3View::operator()(const Range& p,
00125 Index r,
00126 Index c) const
00127 {
00128
00129 assert( 0 <= r );
00130 assert( 0 <= c );
00131 assert( r < mrr.mextent );
00132 assert( c < mcr.mextent );
00133
00134 return ConstVectorView(mdata +
00135 mrr.mstart + r*mrr.mstride +
00136 mcr.mstart + c*mcr.mstride,
00137 mpr, p);
00138 }
00139
00141 ConstIterator3D ConstTensor3View::begin() const
00142 {
00143 return ConstIterator3D(ConstMatrixView(mdata+mpr.mstart,
00144 mrr,
00145 mcr),
00146 mpr.mstride);
00147 }
00148
00150 ConstIterator3D ConstTensor3View::end() const
00151 {
00152 return ConstIterator3D( ConstMatrixView(mdata + mpr.mstart +
00153 (mpr.mextent)*mpr.mstride,
00154 mrr,
00155 mcr),
00156 mpr.mstride );
00157 }
00158
00160 ConstTensor3View::ConstTensor3View(const ConstMatrixView& a) :
00161 mpr(0,1,a.mrr.mextent*a.mcr.mextent),
00162 mrr(a.mrr),
00163 mcr(a.mcr),
00164 mdata(a.mdata)
00165 {
00166
00167 }
00168
00171 ConstTensor3View::ConstTensor3View() :
00172 mpr(0,0,1), mrr(0,0,1), mcr(0,0,1), mdata(NULL)
00173 {
00174
00175 }
00176
00181 ConstTensor3View::ConstTensor3View(Numeric *data,
00182 const Range& pr,
00183 const Range& rr,
00184 const Range& cr) :
00185 mpr(pr),
00186 mrr(rr),
00187 mcr(cr),
00188 mdata(data)
00189 {
00190
00191 }
00192
00200 ConstTensor3View::ConstTensor3View(Numeric *data,
00201 const Range& pp,
00202 const Range& pr,
00203 const Range& pc,
00204 const Range& np,
00205 const Range& nr,
00206 const Range& nc) :
00207 mpr(pp,np),
00208 mrr(pr,nr),
00209 mcr(pc,nc),
00210 mdata(data)
00211 {
00212
00213 }
00214
00218 ostream& operator<<(ostream& os, const ConstTensor3View& v)
00219 {
00220
00221 ConstIterator3D ip=v.begin();
00222 const ConstIterator3D end_page=v.end();
00223
00224 if ( ip!=end_page )
00225 {
00226 os << *ip;
00227 ++ip;
00228 }
00229
00230 for ( ; ip!=end_page; ++ip )
00231 {
00232 os << "\n\n";
00233 os << *ip;
00234 }
00235
00236 return os;
00237 }
00238
00239
00240
00241
00242
00247 ConstTensor3View Tensor3View::operator()(const Range& p,
00248 const Range& r,
00249 const Range& c) const
00250 {
00251 return ConstTensor3View::operator()(p,r,c);
00252 }
00253
00258 ConstMatrixView Tensor3View::operator()(const Range& p,
00259 const Range& r,
00260 Index c) const
00261 {
00262 return ConstTensor3View::operator()(p,r,c);
00263 }
00264
00269 ConstMatrixView Tensor3View::operator()(const Range& p,
00270 Index r,
00271 const Range& c) const
00272 {
00273 return ConstTensor3View::operator()(p,r,c);
00274 }
00275
00280 ConstMatrixView Tensor3View::operator()(Index p,
00281 const Range& r,
00282 const Range& c) const
00283 {
00284 return ConstTensor3View::operator()(p,r,c);
00285 }
00286
00291 ConstVectorView Tensor3View::operator()(Index p,
00292 Index r,
00293 const Range& c) const
00294 {
00295 return ConstTensor3View::operator()(p,r,c);
00296 }
00297
00302 ConstVectorView Tensor3View::operator()(Index p,
00303 const Range& r,
00304 Index c) const
00305 {
00306 return ConstTensor3View::operator()(p,r,c);
00307 }
00308
00313 ConstVectorView Tensor3View::operator()(const Range& p,
00314 Index r,
00315 Index c) const
00316 {
00317 return ConstTensor3View::operator()(p,r,c);
00318 }
00319
00320
00324 Tensor3View Tensor3View::operator()(const Range& p,
00325 const Range& r,
00326 const Range& c)
00327 {
00328 return Tensor3View(mdata, mpr, mrr, mcr, p, r, c);
00329 }
00330
00333 MatrixView Tensor3View::operator()(const Range& p,
00334 const Range& r,
00335 Index c)
00336 {
00337
00338 assert( 0 <= c );
00339 assert( c < mcr.mextent );
00340
00341 return MatrixView(mdata + mcr.mstart + c*mcr.mstride,
00342 mpr, mrr,
00343 p, r);
00344 }
00345
00348 MatrixView Tensor3View::operator()(const Range& p,
00349 Index r,
00350 const Range& c)
00351 {
00352
00353 assert( 0 <= r );
00354 assert( r < mrr.mextent );
00355
00356 return MatrixView(mdata + mrr.mstart + r*mrr.mstride,
00357 mpr, mcr,
00358 p, c);
00359 }
00360
00363 MatrixView Tensor3View::operator()(Index p,
00364 const Range& r,
00365 const Range& c)
00366 {
00367
00368 assert( 0 <= p );
00369 assert( p < mpr.mextent );
00370
00371 return MatrixView(mdata + mpr.mstart + p*mpr.mstride,
00372 mrr, mcr,
00373 r, c);
00374 }
00375
00378 VectorView Tensor3View::operator()(Index p,
00379 Index r,
00380 const Range& c)
00381 {
00382
00383 assert( 0 <= p );
00384 assert( 0 <= r );
00385 assert( p < mpr.mextent );
00386 assert( r < mrr.mextent );
00387
00388 return VectorView(mdata +
00389 mpr.mstart + p*mpr.mstride +
00390 mrr.mstart + r*mrr.mstride,
00391 mcr, c);
00392 }
00393
00396 VectorView Tensor3View::operator()(Index p,
00397 const Range& r,
00398 Index c)
00399 {
00400
00401 assert( 0 <= p );
00402 assert( 0 <= c );
00403 assert( p < mpr.mextent );
00404 assert( c < mcr.mextent );
00405
00406 return VectorView(mdata +
00407 mpr.mstart + p*mpr.mstride +
00408 mcr.mstart + c*mcr.mstride,
00409 mrr, r);
00410 }
00411
00414 VectorView Tensor3View::operator()(const Range& p,
00415 Index r,
00416 Index c)
00417 {
00418
00419 assert( 0 <= r );
00420 assert( 0 <= c );
00421 assert( r < mrr.mextent );
00422 assert( c < mcr.mextent );
00423
00424 return VectorView(mdata +
00425 mrr.mstart + r*mrr.mstride +
00426 mcr.mstart + c*mcr.mstride,
00427 mpr, p);
00428 }
00429
00436 Numeric *Tensor3View::get_c_array()
00437 {
00438 if (mpr.mstart != 0 || mpr.mstride != mrr.mextent * mcr.mextent
00439 || mrr.mstart != 0 || mrr.mstride != mcr.mextent
00440 || mcr.mstart != 0 || mcr.mstride != 1)
00441 throw runtime_error("A Tensor3View can only be converted to a plain C-array if mpr.mstart == 0 and mpr.mstride == mrr.extent*mcr.extent and mrr.mstart == 0 and mrr.mstride == mcr.extent and mcr.mstart == 0 and mcr.mstride == 1");
00442
00443 return mdata;
00444 }
00445
00452 const Numeric *Tensor3View::get_c_array() const
00453 {
00454 if (mpr.mstart != 0 || mpr.mstride != mrr.mextent * mcr.mextent
00455 || mrr.mstart != 0 || mrr.mstride != mcr.mextent
00456 || mcr.mstart != 0 || mcr.mstride != 1)
00457 throw runtime_error("A Tensor3View can only be converted to a plain C-array if mpr.mstart == 0 and mpr.mstride == mrr.extent*mcr.extent and mrr.mstart == 0 and mrr.mstride == mcr.extent and mcr.mstart == 0 and mcr.mstride == 1");
00458
00459 return mdata;
00460 }
00461
00464 ConstIterator3D Tensor3View::begin() const
00465 {
00466 return ConstTensor3View::begin();
00467 }
00468
00470 ConstIterator3D Tensor3View::end() const
00471 {
00472 return ConstTensor3View::end();
00473 }
00474
00476 Iterator3D Tensor3View::begin()
00477 {
00478 return Iterator3D(MatrixView(mdata+mpr.mstart,
00479 mrr,
00480 mcr),
00481 mpr.mstride);
00482 }
00483
00485 Iterator3D Tensor3View::end()
00486 {
00487 return Iterator3D( MatrixView(mdata + mpr.mstart +
00488 (mpr.mextent)*mpr.mstride,
00489 mrr,
00490 mcr),
00491 mpr.mstride );
00492 }
00493
00498 Tensor3View& Tensor3View::operator=(const ConstTensor3View& m)
00499 {
00500
00501 assert(mpr.mextent==m.mpr.mextent);
00502 assert(mrr.mextent==m.mrr.mextent);
00503 assert(mcr.mextent==m.mcr.mextent);
00504
00505 copy( m.begin(), m.end(), begin() );
00506 return *this;
00507 }
00508
00514 Tensor3View& Tensor3View::operator=(const Tensor3View& m)
00515 {
00516
00517 assert(mpr.mextent==m.mpr.mextent);
00518 assert(mrr.mextent==m.mrr.mextent);
00519 assert(mcr.mextent==m.mcr.mextent);
00520
00521 copy( m.begin(), m.end(), begin() );
00522 return *this;
00523 }
00524
00528 Tensor3View& Tensor3View::operator=(const Tensor3& m)
00529 {
00530
00531 assert(mpr.mextent==m.mpr.mextent);
00532 assert(mrr.mextent==m.mrr.mextent);
00533 assert(mcr.mextent==m.mcr.mextent);
00534
00535 copy( m.begin(), m.end(), begin() );
00536 return *this;
00537 }
00538
00541 Tensor3View& Tensor3View::operator=(Numeric x)
00542 {
00543 copy( x, begin(), end() );
00544 return *this;
00545 }
00546
00547
00548
00549
00550 Numeric add(Numeric x, Numeric y)
00551 {
00552 return x+y;
00553 }
00554
00556 Tensor3View& Tensor3View::operator*=(Numeric x)
00557 {
00558 const Iterator3D ep=end();
00559 for ( Iterator3D p=begin(); p!=ep ; ++p )
00560 {
00561 *p *= x;
00562 }
00563 return *this;
00564 }
00565
00567 Tensor3View& Tensor3View::operator/=(Numeric x)
00568 {
00569 const Iterator3D ep=end();
00570 for ( Iterator3D p=begin(); p!=ep ; ++p )
00571 {
00572 *p /= x;
00573 }
00574 return *this;
00575 }
00576
00578 Tensor3View& Tensor3View::operator+=(Numeric x)
00579 {
00580 const Iterator3D ep=end();
00581 for ( Iterator3D p=begin(); p!=ep ; ++p )
00582 {
00583 *p += x;
00584 }
00585 return *this;
00586 }
00587
00589 Tensor3View& Tensor3View::operator-=(Numeric x)
00590 {
00591 const Iterator3D ep=end();
00592 for ( Iterator3D p=begin(); p!=ep ; ++p )
00593 {
00594 *p -= x;
00595 }
00596 return *this;
00597 }
00598
00600 Tensor3View& Tensor3View::operator*=(const ConstTensor3View& x)
00601 {
00602 assert( npages() == x.npages() );
00603 assert( nrows() == x.nrows() );
00604 assert( ncols() == x.ncols() );
00605 ConstIterator3D xp = x.begin();
00606 Iterator3D p = begin();
00607 const Iterator3D ep = end();
00608 for ( ; p!=ep ; ++p,++xp )
00609 {
00610 *p *= *xp;
00611 }
00612 return *this;
00613 }
00614
00616 Tensor3View& Tensor3View::operator/=(const ConstTensor3View& x)
00617 {
00618 assert( npages() == x.npages() );
00619 assert( nrows() == x.nrows() );
00620 assert( ncols() == x.ncols() );
00621 ConstIterator3D xp = x.begin();
00622 Iterator3D p = begin();
00623 const Iterator3D ep = end();
00624 for ( ; p!=ep ; ++p,++xp )
00625 {
00626 *p /= *xp;
00627 }
00628 return *this;
00629 }
00630
00632 Tensor3View& Tensor3View::operator+=(const ConstTensor3View& x)
00633 {
00634 assert( npages() == x.npages() );
00635 assert( nrows() == x.nrows() );
00636 assert( ncols() == x.ncols() );
00637 ConstIterator3D xp = x.begin();
00638 Iterator3D p = begin();
00639 const Iterator3D ep = end();
00640 for ( ; p!=ep ; ++p,++xp )
00641 {
00642 *p += *xp;
00643 }
00644 return *this;
00645 }
00646
00648 Tensor3View& Tensor3View::operator-=(const ConstTensor3View& x)
00649 {
00650 assert( npages() == x.npages() );
00651 assert( nrows() == x.nrows() );
00652 assert( ncols() == x.ncols() );
00653 ConstIterator3D xp = x.begin();
00654 Iterator3D p = begin();
00655 const Iterator3D ep = end();
00656 for ( ; p!=ep ; ++p,++xp )
00657 {
00658 *p -= *xp;
00659 }
00660 return *this;
00661 }
00662
00664 Tensor3View::Tensor3View(const MatrixView& a) :
00665 ConstTensor3View( a.mdata,
00666 Range(0,1,a.mrr.mextent*a.mcr.mextent),
00667 a.mrr,
00668 a.mcr )
00669 {
00670
00671 }
00672
00675 Tensor3View::Tensor3View() :
00676 ConstTensor3View()
00677 {
00678
00679 }
00680
00684 Tensor3View::Tensor3View(Numeric *data,
00685 const Range& pr,
00686 const Range& rr,
00687 const Range& cr) :
00688 ConstTensor3View(data, pr, rr, cr)
00689 {
00690
00691 }
00692
00709 Tensor3View::Tensor3View(Numeric *data,
00710 const Range& pp,
00711 const Range& pr,
00712 const Range& pc,
00713 const Range& np,
00714 const Range& nr,
00715 const Range& nc) :
00716 ConstTensor3View(data,pp,pr,pc,np,nr,nc)
00717 {
00718
00719 }
00720
00725 void copy(ConstIterator3D origin,
00726 const ConstIterator3D& end,
00727 Iterator3D target)
00728 {
00729 for ( ; origin!=end ; ++origin,++target )
00730 {
00731
00732
00733 copy(origin->begin(),
00734 origin->end(),
00735 target->begin());
00736 }
00737 }
00738
00740 void copy(Numeric x,
00741 Iterator3D target,
00742 const Iterator3D& end)
00743 {
00744 for ( ; target!=end ; ++target )
00745 {
00746
00747
00748 copy(x,target->begin(),target->end());
00749 }
00750 }
00751
00752
00753
00754
00755
00757 Tensor3::Tensor3() :
00758 Tensor3View::Tensor3View()
00759 {
00760
00761
00762
00763
00764 }
00765
00768 Tensor3::Tensor3(Index p, Index r, Index c) :
00769 Tensor3View( new Numeric[p*r*c],
00770 Range(0,p,r*c),
00771 Range(0,r,c),
00772 Range(0,c))
00773 {
00774
00775 }
00776
00778 Tensor3::Tensor3(Index p, Index r, Index c, Numeric fill) :
00779 Tensor3View( new Numeric[p*r*c],
00780 Range(0,p,r*c),
00781 Range(0,r,c),
00782 Range(0,c))
00783 {
00784
00785
00786 const Numeric *stop = mdata+p*r*c;
00787 for ( Numeric *x=mdata; x<stop; ++x )
00788 *x = fill;
00789 }
00790
00793 Tensor3::Tensor3(const ConstTensor3View& m) :
00794 Tensor3View( new Numeric[m.npages()*m.nrows()*m.ncols()],
00795 Range( 0, m.npages(), m.nrows()*m.ncols() ),
00796 Range( 0, m.nrows(), m.ncols() ),
00797 Range( 0, m.ncols() ) )
00798 {
00799 copy(m.begin(),m.end(),begin());
00800 }
00801
00804 Tensor3::Tensor3(const Tensor3& m) :
00805 Tensor3View( new Numeric[m.npages()*m.nrows()*m.ncols()],
00806 Range( 0, m.npages(), m.nrows()*m.ncols() ),
00807 Range( 0, m.nrows(), m.ncols() ),
00808 Range( 0, m.ncols() ) )
00809 {
00810
00811
00812
00813
00814
00815 copy(m.begin(),m.end(),begin());
00816 }
00817
00819
00842 Tensor3& Tensor3::operator=(const Tensor3& m)
00843 {
00844
00845
00846
00847 resize( m.mpr.mextent, m.mrr.mextent, m.mcr.mextent );
00848 copy( m.begin(), m.end(), begin() );
00849 return *this;
00850 }
00851
00854 Tensor3& Tensor3::operator=(Numeric x)
00855 {
00856 copy( x, begin(), end() );
00857 return *this;
00858 }
00859
00863 void Tensor3::resize(Index p, Index r, Index c)
00864 {
00865 assert( 0<=p );
00866 assert( 0<=r );
00867 assert( 0<=c );
00868
00869 if ( mpr.mextent!=p ||
00870 mrr.mextent!=r ||
00871 mcr.mextent!=c )
00872 {
00873 delete[] mdata;
00874 mdata = new Numeric[p*r*c];
00875
00876 mpr.mstart = 0;
00877 mpr.mextent = p;
00878 mpr.mstride = r*c;
00879
00880 mrr.mstart = 0;
00881 mrr.mextent = r;
00882 mrr.mstride = c;
00883
00884 mcr.mstart = 0;
00885 mcr.mextent = c;
00886 mcr.mstride = 1;
00887 }
00888 }
00889
00892 Tensor3::~Tensor3()
00893 {
00894
00895
00896 delete[] mdata;
00897 }
00898
00899
00915 void transform( Tensor3View y,
00916 double (&my_func)(double),
00917 ConstTensor3View x )
00918 {
00919
00920 assert( y.npages() == x.npages() );
00921 assert( y.nrows() == x.nrows() );
00922 assert( y.ncols() == x.ncols() );
00923
00924 const ConstIterator3D xe = x.end();
00925 ConstIterator3D xi = x.begin();
00926 Iterator3D yi = y.begin();
00927 for ( ; xi!=xe; ++xi, ++yi )
00928 {
00929
00930
00931 transform(*yi,my_func,*xi);
00932 }
00933 }
00934
00936 Numeric max(const ConstTensor3View& x)
00937 {
00938 const ConstIterator3D xe = x.end();
00939 ConstIterator3D xi = x.begin();
00940
00941
00942 Numeric themax = max(*xi);
00943 ++xi;
00944
00945 for ( ; xi!=xe ; ++xi )
00946 {
00947
00948
00949 Numeric maxi = max(*xi);
00950 if ( maxi > themax )
00951 themax = maxi;
00952 }
00953
00954 return themax;
00955 }
00956
00958 Numeric min(const ConstTensor3View& x)
00959 {
00960 const ConstIterator3D xe = x.end();
00961 ConstIterator3D xi = x.begin();
00962
00963
00964 Numeric themin = min(*xi);
00965 ++xi;
00966
00967 for ( ; xi!=xe ; ++xi )
00968 {
00969
00970
00971 Numeric mini = min(*xi);
00972 if ( mini < themin )
00973 themin = mini;
00974 }
00975
00976 return themin;
00977 }
00978
00979
00981
00982 #ifndef NDEBUG
00983
00998 Numeric debug_tensor3view_get_elem (Tensor3View& tv,
00999 Index p, Index r, Index c)
01000 {
01001 return tv(p, r, c);
01002 }
01003
01004 #endif
01006