ARTS  2.2.66
matpackIV.cc
Go to the documentation of this file.
1 /* Copyright (C) 2002-2012 Stefan Buehler <sbuehler@ltu.se>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
25 #include "matpackIV.h"
26 #include "exceptions.h"
27 
28 using std::runtime_error;
29 
30 
34 {
35  return &msv;
36 }
37 
40 {
41  return msv;
42 }
43 
47 {
48  return &msv;
49 }
50 
53 {
54  return msv;
55 }
56 
57 
58 
59 // Functions for ConstTensor4View:
60 // ------------------------------
61 
64 {
65  return mbr.mextent;
66 }
67 
70 {
71  return mpr.mextent;
72 }
73 
76 {
77  return mrr.mextent;
78 }
79 
82 {
83  return mcr.mextent;
84 }
85 
90  const Range& p,
91  const Range& r,
92  const Range& c) const
93 {
94  return ConstTensor4View(mdata,
95  mbr, mpr, mrr, mcr,
96  b, p, r, c);
97 }
98 
102  const Range& p,
103  const Range& r,
104  Index c ) const
105 {
106  // Check that c is valid:
107  assert( 0 <= c );
108  assert( c < mcr.mextent );
109 
110  return ConstTensor3View(mdata +
111  mcr.mstart + c * mcr.mstride,
112  mbr, mpr, mrr,
113  b, p, r);
114 }
115 
119  const Range& p,
120  Index r,
121  const Range& c) const
122 {
123  // Check that r is valid:
124  assert( 0 <= r );
125  assert( r < mrr.mextent );
126 
127  return ConstTensor3View(mdata +
128  mrr.mstart + r * mrr.mstride,
129  mbr, mpr, mcr,
130  b, p, c);
131 }
132 
136  Index p,
137  const Range& r,
138  const Range& c) const
139 {
140  // Check that p is valid:
141  assert( 0 <= p );
142  assert( p < mpr.mextent );
143 
144  return ConstTensor3View(mdata +
145  mpr.mstart + p * mpr.mstride,
146  mbr, mrr, mcr,
147  b, r, c);
148 }
149 
153  const Range& p,
154  const Range& r,
155  const Range& c) const
156 {
157  // Check that b is valid:
158  assert( 0 <= b );
159  assert( b < mbr.mextent );
160 
161  return ConstTensor3View(mdata +
162  mbr.mstart + b * mbr.mstride,
163  mpr, mrr, mcr,
164  p, r, c);
165 }
166 
170  const Range& p,
171  Index r,
172  Index c ) const
173 {
174  // Check that r and c are valid:
175  assert( 0 <= r );
176  assert( 0 <= c );
177  assert( r < mrr.mextent );
178  assert( c < mcr.mextent );
179 
180  return ConstMatrixView(mdata +
181  mrr.mstart + r * mrr.mstride +
182  mcr.mstart + c * mcr.mstride,
183  mbr, mpr,
184  b, p);
185 }
186 
190  Index p,
191  const Range& r,
192  Index c ) const
193 {
194  // Check that p and c are valid:
195  assert( 0 <= p );
196  assert( 0 <= c );
197  assert( p < mpr.mextent );
198  assert( c < mcr.mextent );
199 
200  return ConstMatrixView(mdata +
201  mpr.mstart + p * mpr.mstride +
202  mcr.mstart + c * mcr.mstride,
203  mbr, mrr,
204  b, r);
205 }
206 
210  Index p,
211  Index r,
212  const Range& c) const
213 {
214  // Check that p and r are valid:
215  assert( 0 <= p );
216  assert( 0 <= r );
217  assert( p < mpr.mextent );
218  assert( r < mrr.mextent );
219 
220  return ConstMatrixView(mdata +
221  mpr.mstart + p * mpr.mstride +
222  mrr.mstart + r * mrr.mstride,
223  mbr, mcr,
224  b, c);
225 }
226 
230  const Range& p,
231  Index r,
232  const Range& c) const
233 {
234  // Check that b and r are valid:
235  assert( 0 <= b );
236  assert( 0 <= r );
237  assert( b < mbr.mextent );
238  assert( r < mrr.mextent );
239 
240  return ConstMatrixView(mdata +
241  mbr.mstart + b * mbr.mstride +
242  mrr.mstart + r * mrr.mstride,
243  mpr, mcr,
244  p, c);
245 }
246 
250  const Range& p,
251  const Range& r,
252  Index c ) const
253 {
254  // Check that b and c are valid:
255  assert( 0 <= b );
256  assert( 0 <= c );
257  assert( b < mbr.mextent );
258  assert( c < mcr.mextent );
259 
260  return ConstMatrixView(mdata +
261  mbr.mstart + b * mbr.mstride +
262  mcr.mstart + c * mcr.mstride,
263  mpr, mrr,
264  p, r);
265 }
266 
270  Index p,
271  const Range& r,
272  const Range& c) const
273 {
274  // Check that b and p are valid:
275  assert( 0 <= b );
276  assert( 0 <= p );
277  assert( b < mbr.mextent );
278  assert( p < mpr.mextent );
279 
280  return ConstMatrixView(mdata +
281  mbr.mstart + b * mbr.mstride +
282  mpr.mstart + p * mpr.mstride,
283  mrr, mcr,
284  r, c);
285 }
286 
290  Index p,
291  Index r,
292  Index c ) const
293 {
294  // Check that p, r and c are valid:
295  assert( 0 <= p );
296  assert( 0 <= r );
297  assert( 0 <= c );
298  assert( p < mpr.mextent );
299  assert( r < mrr.mextent );
300  assert( c < mcr.mextent );
301 
302  return ConstVectorView(mdata +
303  mpr.mstart + p * mpr.mstride +
304  mrr.mstart + r * mrr.mstride +
305  mcr.mstart + c * mcr.mstride,
306  mbr,
307  b);
308 }
309 
313  const Range& p,
314  Index r,
315  Index c ) const
316 {
317  // Check that b, r and c are valid:
318  assert( 0 <= b );
319  assert( 0 <= r );
320  assert( 0 <= c );
321  assert( b < mbr.mextent );
322  assert( r < mrr.mextent );
323  assert( c < mcr.mextent );
324 
325  return ConstVectorView(mdata +
326  mbr.mstart + b * mbr.mstride +
327  mrr.mstart + r * mrr.mstride +
328  mcr.mstart + c * mcr.mstride,
329  mpr,
330  p);
331 }
332 
336  Index p,
337  const Range& r,
338  Index c ) const
339 {
340  // Check that b, p and c are valid:
341  assert( 0 <= b );
342  assert( 0 <= p );
343  assert( 0 <= c );
344  assert( b < mbr.mextent );
345  assert( p < mpr.mextent );
346  assert( c < mcr.mextent );
347 
348  return ConstVectorView(mdata +
349  mbr.mstart + b * mbr.mstride +
350  mpr.mstart + p * mpr.mstride +
351  mcr.mstart + c * mcr.mstride,
352  mrr,
353  r);
354 }
355 
359  Index p,
360  Index r,
361  const Range& c) const
362 {
363  // Check that b, p and r are valid:
364  assert( 0 <= b );
365  assert( 0 <= p );
366  assert( 0 <= r );
367  assert( b < mbr.mextent );
368  assert( p < mpr.mextent );
369  assert( r < mrr.mextent );
370 
371  return ConstVectorView(mdata +
372  mbr.mstart + b * mbr.mstride +
373  mpr.mstart + p * mpr.mstride +
374  mrr.mstart + r * mrr.mstride,
375  mcr,
376  c);
377 }
378 
386 {
387  if (mbr.mstart != 0 || mbr.mstride != mpr.mextent * mrr.mextent * mcr.mextent
388  || mpr.mstart != 0 || mpr.mstride != mrr.mextent * mcr.mextent
389  || mrr.mstart != 0 || mrr.mstride != mcr.mextent
390  || mcr.mstart != 0 || mcr.mstride != 1)
391  throw std::runtime_error("A Tensor4View can only be converted to a plain C-array if it's pointing to a continuous block of data");
392 
393  return mdata;
394 }
395 
403 {
404  if (mbr.mstart != 0 || mbr.mstride != mpr.mextent * mrr.mextent * mcr.mextent
405  || mpr.mstart != 0 || mpr.mstride != mrr.mextent * mcr.mextent
406  || mrr.mstart != 0 || mrr.mstride != mcr.mextent
407  || mcr.mstart != 0 || mcr.mstride != 1)
408  throw std::runtime_error("A Tensor4View can only be converted to a plain C-array if it's pointing to a continuous block of data");
409 
410  return mdata;
411 }
412 
415 {
417  mpr, mrr, mcr),
418  mbr.mstride );
419 }
420 
423 {
425  (mbr.mextent) * mbr.mstride,
426  mpr, mrr, mcr),
427  mbr.mstride );
428 }
429 
432  mbr(0,1,a.mpr.mextent*a.mrr.mextent*a.mcr.mextent),
433  mpr(a.mpr),
434  mrr(a.mrr),
435  mcr(a.mcr),
436  mdata(a.mdata)
437 {
438  // Nothing to do here.
439 }
440 
444  mbr(0,0,1),
445  mpr(0,0,1),
446  mrr(0,0,1),
447  mcr(0,0,1),
448  mdata(NULL)
449 {
450  // Nothing to do here.
451 }
452 
458  const Range& br,
459  const Range& pr,
460  const Range& rr,
461  const Range& cr) :
462  mbr(br),
463  mpr(pr),
464  mrr(rr),
465  mcr(cr),
466  mdata(data)
467 {
468  // Nothing to do here.
469 }
470 
479  const Range& pb,
480  const Range& pp,
481  const Range& pr,
482  const Range& pc,
483  const Range& nb,
484  const Range& np,
485  const Range& nr,
486  const Range& nc) :
487  mbr(pb,nb),
488  mpr(pp,np),
489  mrr(pr,nr),
490  mcr(pc,nc),
491  mdata(data)
492 {
493  // Nothing to do here.
494 }
495 
499 std::ostream& operator<<(std::ostream& os, const ConstTensor4View& v)
500 {
501  // Page iterators:
502  ConstIterator4D ib = v.begin();
503  const ConstIterator4D end_book = v.end();
504 
505  if ( ib != end_book ) {
506  os << *ib;
507  ++ib;
508  }
509 
510  for ( ; ib != end_book; ++ib ) {
511  os << "\n\n";
512  os << *ib;
513  }
514 
515  return os;
516 }
517 
518 
519 // Functions for Tensor4View:
520 // -------------------------
521 
527  const Range& p,
528  const Range& r,
529  const Range& c) const
530 {
531  return ConstTensor4View::operator()(b,p,r,c);
532 }
533 
539  const Range& p,
540  const Range& r,
541  Index c ) const
542 {
543  return ConstTensor4View::operator()(b,p,r,c);
544 }
545 
551  const Range& p,
552  Index r,
553  const Range& c) const
554 {
555  return ConstTensor4View::operator()(b,p,r,c);
556 }
557 
563  Index p,
564  const Range& r,
565  const Range& c) const
566 {
567  return ConstTensor4View::operator()(b,p,r,c);
568 }
569 
575  const Range& p,
576  const Range& r,
577  const Range& c) const
578 {
579  return ConstTensor4View::operator()(b,p,r,c);
580 }
581 
587  const Range& p,
588  Index r,
589  Index c ) const
590 {
591  return ConstTensor4View::operator()(b,p,r,c);
592 }
593 
599  Index p,
600  const Range& r,
601  Index c ) const
602 {
603  return ConstTensor4View::operator()(b,p,r,c);
604 }
605 
611  Index p,
612  Index r,
613  const Range& c) const
614 {
615  return ConstTensor4View::operator()(b,p,r,c);
616 }
617 
623  const Range& p,
624  Index r,
625  const Range& c) const
626 {
627  return ConstTensor4View::operator()(b,p,r,c);
628 }
629 
635  const Range& p,
636  const Range& r,
637  Index c ) const
638 {
639  return ConstTensor4View::operator()(b,p,r,c);
640 }
641 
647  Index p,
648  const Range& r,
649  const Range& c) const
650 {
651  return ConstTensor4View::operator()(b,p,r,c);
652 }
653 
659  Index p,
660  Index r,
661  Index c ) const
662 {
663  return ConstTensor4View::operator()(b,p,r,c);
664 }
665 
671  const Range& p,
672  Index r,
673  Index c ) const
674 {
675  return ConstTensor4View::operator()(b,p,r,c);
676 }
677 
683  Index p,
684  const Range& r,
685  Index c ) const
686 {
687  return ConstTensor4View::operator()(b,p,r,c);
688 }
689 
695  Index p,
696  Index r,
697  const Range& c) const
698 {
699  return ConstTensor4View::operator()(b,p,r,c);
700 }
701 
706  const Range& p,
707  const Range& r,
708  const Range& c)
709 {
710  return Tensor4View(mdata,
711  mbr, mpr, mrr, mcr,
712  b, p, r, c);
713 }
714 
718  const Range& p,
719  const Range& r,
720  Index c)
721 {
722  // Check that c is valid:
723  assert( 0 <= c );
724  assert( c < mcr.mextent );
725 
726  return Tensor3View(mdata +
727  mcr.mstart + c * mcr.mstride,
728  mbr, mpr, mrr,
729  b, p, r);
730 }
731 
735  const Range& p,
736  Index r,
737  const Range& c)
738 {
739  // Check that r is valid:
740  assert( 0 <= r );
741  assert( r < mrr.mextent );
742 
743  return Tensor3View(mdata +
744  mrr.mstart + r * mrr.mstride,
745  mbr, mpr, mcr,
746  b, p, c);
747 }
748 
752  Index p,
753  const Range& r,
754  const Range& c)
755 {
756  // Check that p is valid:
757  assert( 0 <= p );
758  assert( p < mpr.mextent );
759 
760  return Tensor3View(mdata +
761  mpr.mstart + p * mpr.mstride,
762  mbr, mrr, mcr,
763  b, r, c);
764 }
765 
769  const Range& p,
770  const Range& r,
771  const Range& c)
772 {
773  // Check that b is valid:
774  assert( 0 <= b );
775  assert( b < mbr.mextent );
776 
777  return Tensor3View(mdata +
778  mbr.mstart + b * mbr.mstride,
779  mpr, mrr, mcr,
780  p, r, c);
781 }
782 
786  const Range& p,
787  Index r,
788  Index c)
789 {
790  // Check that r and c are valid:
791  assert( 0 <= r );
792  assert( 0 <= c );
793  assert( r < mrr.mextent );
794  assert( c < mcr.mextent );
795 
796  return MatrixView(mdata +
797  mrr.mstart + r * mrr.mstride +
798  mcr.mstart + c * mcr.mstride,
799  mbr, mpr,
800  b, p);
801 }
802 
806  Index p,
807  const Range& r,
808  Index c)
809 {
810  // Check that p and c are valid:
811  assert( 0 <= p );
812  assert( 0 <= c );
813  assert( p < mpr.mextent );
814  assert( c < mcr.mextent );
815 
816  return MatrixView(mdata +
817  mpr.mstart + p * mpr.mstride +
818  mcr.mstart + c * mcr.mstride,
819  mbr, mrr,
820  b, r);
821 }
822 
826  Index p,
827  Index r,
828  const Range& c)
829 {
830  // Check that p and r are valid:
831  assert( 0 <= p );
832  assert( 0 <= r );
833  assert( p < mpr.mextent );
834  assert( r < mrr.mextent );
835 
836  return MatrixView(mdata +
837  mpr.mstart + p * mpr.mstride +
838  mrr.mstart + r * mrr.mstride,
839  mbr, mcr,
840  b, c);
841 }
842 
846  const Range& p,
847  Index r,
848  const Range& c)
849 {
850  // Check that b and r are valid:
851  assert( 0 <= b );
852  assert( 0 <= r );
853  assert( b < mbr.mextent );
854  assert( r < mrr.mextent );
855 
856  return MatrixView(mdata +
857  mbr.mstart + b * mbr.mstride +
858  mrr.mstart + r * mrr.mstride,
859  mpr, mcr,
860  p, c);
861 }
862 
866  const Range& p,
867  const Range& r,
868  Index c)
869 {
870  // Check that b and c are valid:
871  assert( 0 <= b );
872  assert( 0 <= c );
873  assert( b < mbr.mextent );
874  assert( c < mcr.mextent );
875 
876  return MatrixView(mdata +
877  mbr.mstart + b * mbr.mstride +
878  mcr.mstart + c * mcr.mstride,
879  mpr, mrr,
880  p, r);
881 }
882 
886  Index p,
887  const Range& r,
888  const Range& c)
889 {
890  // Check that b and p are valid:
891  assert( 0 <= b );
892  assert( 0 <= p );
893  assert( b < mbr.mextent );
894  assert( p < mpr.mextent );
895 
896  return MatrixView(mdata +
897  mbr.mstart + b * mbr.mstride +
898  mpr.mstart + p * mpr.mstride,
899  mrr, mcr,
900  r, c);
901 }
902 
906  Index p,
907  Index r,
908  Index c)
909 {
910  // Check that p, r and c are valid:
911  assert( 0 <= p );
912  assert( 0 <= r );
913  assert( 0 <= c );
914  assert( p < mpr.mextent );
915  assert( r < mrr.mextent );
916  assert( c < mcr.mextent );
917 
918  return VectorView(mdata +
919  mpr.mstart + p * mpr.mstride +
920  mrr.mstart + r * mrr.mstride +
921  mcr.mstart + c * mcr.mstride,
922  mbr,
923  b);
924 }
925 
929  const Range& p,
930  Index r,
931  Index c)
932 {
933  // Check that b, r and c are valid:
934  assert( 0 <= b );
935  assert( 0 <= r );
936  assert( 0 <= c );
937  assert( b < mbr.mextent );
938  assert( r < mrr.mextent );
939  assert( c < mcr.mextent );
940 
941  return VectorView(mdata +
942  mbr.mstart + b * mbr.mstride +
943  mrr.mstart + r * mrr.mstride +
944  mcr.mstart + c * mcr.mstride,
945  mpr,
946  p);
947 }
948 
952  Index p,
953  const Range& r,
954  Index c)
955 {
956  // Check that b, p and c are valid:
957  assert( 0 <= b );
958  assert( 0 <= p );
959  assert( 0 <= c );
960  assert( b < mbr.mextent );
961  assert( p < mpr.mextent );
962  assert( c < mcr.mextent );
963 
964  return VectorView(mdata +
965  mbr.mstart + b * mbr.mstride +
966  mpr.mstart + p * mpr.mstride +
967  mcr.mstart + c * mcr.mstride,
968  mrr,
969  r);
970 }
971 
975  Index p,
976  Index r,
977  const Range& c)
978 {
979  // Check that b, p and r are valid:
980  assert( 0 <= b );
981  assert( 0 <= p );
982  assert( 0 <= r );
983  assert( b < mbr.mextent );
984  assert( p < mpr.mextent );
985  assert( r < mrr.mextent );
986 
987  return VectorView(mdata +
988  mbr.mstart + b * mbr.mstride +
989  mpr.mstart + p * mpr.mstride +
990  mrr.mstart + r * mrr.mstride,
991  mcr,
992  c);
993 }
994 
998 {
999  return ConstTensor4View::begin();
1000 }
1001 
1004 {
1005  return ConstTensor4View::end();
1006 }
1007 
1010 {
1012  mpr, mrr, mcr),
1013  mbr.mstride );
1014 }
1015 
1018 {
1019  return Iterator4D( Tensor3View(mdata + mbr.mstart +
1020  (mbr.mextent) * mbr.mstride,
1021  mpr, mrr, mcr),
1022  mbr.mstride );
1023 }
1024 
1030 {
1031  // Check that sizes are compatible:
1032  assert( mbr.mextent == m.mbr.mextent );
1033  assert( mpr.mextent == m.mpr.mextent );
1034  assert( mrr.mextent == m.mrr.mextent );
1035  assert( mcr.mextent == m.mcr.mextent );
1036 
1037  copy( m.begin(), m.end(), begin() );
1038  return *this;
1039 }
1040 
1047 {
1048  // Check that sizes are compatible:
1049  assert( mbr.mextent == m.mbr.mextent );
1050  assert( mpr.mextent == m.mpr.mextent );
1051  assert( mrr.mextent == m.mrr.mextent );
1052  assert( mcr.mextent == m.mcr.mextent );
1053 
1054  copy( m.begin(), m.end(), begin() );
1055  return *this;
1056 }
1057 
1062 {
1063  // Check that sizes are compatible:
1064  assert( mbr.mextent == m.mbr.mextent );
1065  assert( mpr.mextent == m.mpr.mextent );
1066  assert( mrr.mextent == m.mrr.mextent );
1067  assert( mcr.mextent == m.mcr.mextent );
1068 
1069  copy( m.begin(), m.end(), begin() );
1070  return *this;
1071 }
1072 
1076 {
1077  copy( x, begin(), end() );
1078  return *this;
1079 }
1080 
1081 // Some little helper functions:
1082 //------------------------------
1083 
1086 {
1087  const Iterator4D eb = end();
1088  for ( Iterator4D b = begin(); b != eb ; ++b )
1089  {
1090  *b *= x;
1091  }
1092  return *this;
1093 }
1094 
1097 {
1098  const Iterator4D eb = end();
1099  for ( Iterator4D b = begin(); b != eb ; ++b )
1100  {
1101  *b /= x;
1102  }
1103  return *this;
1104 }
1105 
1108 {
1109  const Iterator4D eb = end();
1110  for ( Iterator4D b = begin(); b != eb ; ++b )
1111  {
1112  *b += x;
1113  }
1114  return *this;
1115 }
1116 
1119 {
1120  const Iterator4D eb = end();
1121  for ( Iterator4D b = begin(); b != eb ; ++b )
1122  {
1123  *b -= x;
1124  }
1125  return *this;
1126 }
1127 
1130 {
1131  assert( nbooks() == x.nbooks() );
1132  assert( npages() == x.npages() );
1133  assert( nrows() == x.nrows() );
1134  assert( ncols() == x.ncols() );
1135  ConstIterator4D xb = x.begin();
1136  Iterator4D b = begin();
1137  const Iterator4D eb = end();
1138  for ( ; b != eb ; ++b, ++xb )
1139  {
1140  *b *= *xb;
1141  }
1142  return *this;
1143 }
1144 
1147 {
1148  assert( nbooks() == x.nbooks() );
1149  assert( npages() == x.npages() );
1150  assert( nrows() == x.nrows() );
1151  assert( ncols() == x.ncols() );
1152  ConstIterator4D xb = x.begin();
1153  Iterator4D b = begin();
1154  const Iterator4D eb = end();
1155  for ( ; b != eb ; ++b, ++xb )
1156  {
1157  *b /= *xb;
1158  }
1159  return *this;
1160 }
1161 
1164 {
1165  assert( nbooks() == x.nbooks() );
1166  assert( npages() == x.npages() );
1167  assert( nrows() == x.nrows() );
1168  assert( ncols() == x.ncols() );
1169  ConstIterator4D xb = x.begin();
1170  Iterator4D b = begin();
1171  const Iterator4D eb = end();
1172  for ( ; b != eb ; ++b, ++xb )
1173  {
1174  *b += *xb;
1175  }
1176  return *this;
1177 }
1178 
1181 {
1182  assert( nbooks() == x.nbooks() );
1183  assert( npages() == x.npages() );
1184  assert( nrows() == x.nrows() );
1185  assert( ncols() == x.ncols() );
1186  ConstIterator4D xb = x.begin();
1187  Iterator4D b = begin();
1188  const Iterator4D eb = end();
1189  for ( ; b != eb ; ++b, ++xb )
1190  {
1191  *b -= *xb;
1192  }
1193  return *this;
1194 }
1195 
1198  ConstTensor4View( a.mdata,
1199  Range(0,1,
1200  a.mpr.mextent*
1201  a.mrr.mextent*
1202  a.mcr.mextent),
1203  a.mpr,
1204  a.mrr,
1205  a.mcr )
1206 {
1207  // Nothing to do here.
1208 }
1209 
1214 {
1215  // Nothing to do here.
1216 }
1217 
1222  const Range& br,
1223  const Range& pr,
1224  const Range& rr,
1225  const Range& cr) :
1226  ConstTensor4View(data, br, pr, rr, cr)
1227 {
1228  // Nothing to do here.
1229 }
1230 
1250  const Range& pb,
1251  const Range& pp,
1252  const Range& pr,
1253  const Range& pc,
1254  const Range& nb,
1255  const Range& np,
1256  const Range& nr,
1257  const Range& nc) :
1258  ConstTensor4View(data, pb, pp, pr, pc, nb, np, nr, nc)
1259 {
1260  // Nothing to do here.
1261 }
1262 
1267 void copy(ConstIterator4D origin,
1268  const ConstIterator4D& end,
1269  Iterator4D target)
1270 {
1271  for ( ; origin != end ; ++origin, ++target )
1272  {
1273  // We use the copy function for the next smaller rank of tensor
1274  // recursively:
1275  copy( origin->begin(), origin->end(), target->begin() );
1276  }
1277 }
1278 
1280 void copy(Numeric x,
1281  Iterator4D target,
1282  const Iterator4D& end)
1283 {
1284  for ( ; target != end ; ++target )
1285  {
1286  // We use the copy function for the next smaller rank of tensor
1287  // recursively:
1288  copy( x, target->begin(), target->end() );
1289  }
1290 }
1291 
1292 
1293 // Functions for Tensor4:
1294 // ---------------------
1295 
1299 {
1300  // Nothing to do here. However, note that the default constructor
1301  // for Tensor4View has been called in the initializer list. That is
1302  // crucial, otherwise internal range objects will not be properly
1303  // initialized.
1304 }
1305 
1309  Tensor4View( new Numeric[b*p*r*c],
1310  Range( 0, b, p*r*c ),
1311  Range( 0, p, r*c ),
1312  Range( 0, r, c ),
1313  Range( 0, c) )
1314 {
1315  // Nothing to do here.
1316 }
1317 
1320  Tensor4View( new Numeric[b*p*r*c],
1321  Range( 0, b, p*r*c ),
1322  Range( 0, p, r*c ),
1323  Range( 0, r, c ),
1324  Range( 0, c) )
1325 {
1326  // Here we can access the raw memory directly, for slightly
1327  // increased efficiency:
1328  const Numeric *stop = mdata + b*p*r*c;
1329  for ( Numeric *x = mdata; x < stop; ++x )
1330  *x = fill;
1331 }
1332 
1336  Tensor4View( new Numeric[m.nbooks()*m.npages()*m.nrows()*m.ncols()],
1337  Range( 0, m.nbooks(), m.npages()*m.nrows()*m.ncols() ),
1338  Range( 0, m.npages(), m.nrows()*m.ncols() ),
1339  Range( 0, m.nrows(), m.ncols() ),
1340  Range( 0, m.ncols() ) )
1341 {
1342  copy( m.begin(), m.end(), begin() );
1343 }
1344 
1348  Tensor4View( new Numeric[m.nbooks()*m.npages()*m.nrows()*m.ncols()],
1349  Range( 0, m.nbooks(), m.npages()*m.nrows()*m.ncols() ),
1350  Range( 0, m.npages(), m.nrows()*m.ncols() ),
1351  Range( 0, m.nrows(), m.ncols() ),
1352  Range( 0, m.ncols() ) )
1353 {
1354  // There is a catch here: If m is an empty tensor, then it will have
1355  // dimensions of size 0. But these are used to initialize the stride
1356  // for higher dimensions! Thus, this method has to be consistent
1357  // with the behaviour of Range::Range. For now, Range::Range allows
1358  // also stride 0.
1359  copy( m.begin(), m.end(), begin());
1360 }
1361 
1363 
1387 {
1388  swap(*this, x);
1389  return *this;
1390 }
1391 
1395 {
1396  copy( x, begin(), end() );
1397  return *this;
1398 }
1399 
1404 {
1405  assert( 0 <= b );
1406  assert( 0 <= p );
1407  assert( 0 <= r );
1408  assert( 0 <= c );
1409 
1410  if ( mbr.mextent != b ||
1411  mpr.mextent != p ||
1412  mrr.mextent != r ||
1413  mcr.mextent != c )
1414  {
1415  delete[] mdata;
1416  mdata = new Numeric[b*p*r*c];
1417 
1418  mbr.mstart = 0;
1419  mbr.mextent = b;
1420  mbr.mstride = p*r*c;
1421 
1422  mpr.mstart = 0;
1423  mpr.mextent = p;
1424  mpr.mstride = r*c;
1425 
1426  mrr.mstart = 0;
1427  mrr.mextent = r;
1428  mrr.mstride = c;
1429 
1430  mcr.mstart = 0;
1431  mcr.mextent = c;
1432  mcr.mstride = 1;
1433  }
1434 }
1435 
1436 
1438 void swap(Tensor4& t1, Tensor4& t2)
1439 {
1440  std::swap(t1.mbr, t2.mbr);
1441  std::swap(t1.mpr, t2.mpr);
1442  std::swap(t1.mrr, t2.mrr);
1443  std::swap(t1.mcr, t2.mcr);
1444  std::swap(t1.mdata, t2.mdata);
1445 }
1446 
1447 
1451 {
1452 // cout << "Destroying a Tensor4:\n"
1453 // << *this << "\n........................................\n";
1454  delete[] mdata;
1455 }
1456 
1457 
1474  double (&my_func)(double),
1475  ConstTensor4View x )
1476 {
1477  // Check dimensions:
1478  assert( y.nbooks() == x.nbooks() );
1479  assert( y.npages() == x.npages() );
1480  assert( y.nrows() == x.nrows() );
1481  assert( y.ncols() == x.ncols() );
1482 
1483  const ConstIterator4D xe = x.end();
1484  ConstIterator4D xi = x.begin();
1485  Iterator4D yi = y.begin();
1486  for ( ; xi != xe; ++xi, ++yi )
1487  {
1488  // Use the transform function of lower dimensional tensors
1489  // recursively:
1490  transform( *yi, my_func, *xi );
1491  }
1492 }
1493 
1496 {
1497  const ConstIterator4D xe = x.end();
1498  ConstIterator4D xi = x.begin();
1499 
1500  // Initial value for max:
1501  Numeric themax = max(*xi);
1502  ++xi;
1503 
1504  for ( ; xi != xe ; ++xi )
1505  {
1506  // Use the max function of lower dimensional tensors
1507  // recursively:
1508  Numeric maxi = max(*xi);
1509  if ( maxi > themax )
1510  themax = maxi;
1511  }
1512 
1513  return themax;
1514 }
1515 
1518 {
1519  const ConstIterator4D xe = x.end();
1520  ConstIterator4D xi = x.begin();
1521 
1522  // Initial value for min:
1523  Numeric themin = min(*xi);
1524  ++xi;
1525 
1526  for ( ; xi != xe ; ++xi )
1527  {
1528  // Use the min function of lower dimensional tensors
1529  // recursively:
1530  Numeric mini = min(*xi);
1531  if ( mini < themin )
1532  themin = mini;
1533  }
1534 
1535  return themin;
1536 }
1537 
1538 
1540 // Helper function for debugging
1541 #ifndef NDEBUG
1542 
1559  Index b, Index p, Index r, Index c)
1560 {
1561  return tv(b, p, r, c);
1562 }
1563 
1564 #endif
1565 
Tensor3View::end
ConstIterator3D end() const
Return const iterator behind last row.
Definition: matpackIII.cc:473
Tensor4::resize
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1403
ConstTensor4View::mpr
Range mpr
The page range of mdata that is actually used.
Definition: matpackIV.h:225
MatrixView
The MatrixView class.
Definition: matpackI.h:679
exceptions.h
The declarations of all the exception classes.
ConstTensor4View::end
ConstIterator4D end() const
Return const iterator behind last book.
Definition: matpackIV.cc:422
Tensor4::operator=
Tensor4 & operator=(Tensor4 x)
Assignment operator from another tensor.
Definition: matpackIV.cc:1386
Tensor4View::Tensor4View
Tensor4View()
Default constructor.
Definition: matpackIV.cc:1212
swap
void swap(Tensor4 &t1, Tensor4 &t2)
Swaps two objects.
Definition: matpackIV.cc:1438
min
Numeric min(const ConstTensor4View &x)
Min function, tensor version.
Definition: matpackIV.cc:1517
transform
void transform(Tensor4View y, double(&my_func)(double), ConstTensor4View x)
A generic transform function for tensors, which can be used to implement mathematical functions opera...
Definition: matpackIV.cc:1473
Tensor4View::operator*=
Tensor4View & operator*=(Numeric x)
Multiplication by scalar.
Definition: matpackIV.cc:1085
ConstTensor4View::mbr
Range mbr
The book range of mdata that is actually used.
Definition: matpackIV.h:223
ConstTensor4View::operator()
ConstTensor4View operator()(const Range &b, const Range &p, const Range &r, const Range &c) const
Const index operator for subrange.
Definition: matpackIV.cc:89
ConstTensor4View::mcr
Range mcr
The column range of mdata that is actually used.
Definition: matpackIV.h:229
Tensor4::~Tensor4
virtual ~Tensor4()
Destructor for Tensor4.
Definition: matpackIV.cc:1450
Tensor4::swap
friend void swap(Tensor4 &t1, Tensor4 &t2)
Swaps two objects.
Definition: matpackIV.cc:1438
Range::mstart
Index mstart
The start index.
Definition: matpackI.h:204
Tensor4
The Tensor4 class.
Definition: matpackIV.h:383
Tensor4::Tensor4
Tensor4()
Default constructor.
Definition: matpackIV.cc:1297
matpackIV.h
Iterator4D::operator->
Tensor3View * operator->()
The -> operator is needed, so that we can write i->begin() to get the 3D iterators.
Definition: matpackIV.cc:33
ConstIterator4D
Const version of Iterator4D.
Definition: matpackIV.h:80
ConstTensor4View
A constant view of a Tensor4.
Definition: matpackIV.h:141
Tensor3View
The Tensor3View class.
Definition: matpackIII.h:232
Tensor4View::operator+=
Tensor4View & operator+=(Numeric x)
Addition of scalar.
Definition: matpackIV.cc:1107
ConstIterator4D::operator->
const ConstTensor3View * operator->() const
The -> operator is needed, so that we can write i->begin() to get the 3D iterators.
Definition: matpackIV.cc:46
ConstTensor3View::begin
ConstIterator3D begin() const
Return const iterator to first page.
Definition: matpackIII.cc:144
eb
#define eb
Definition: continua.cc:21933
debug_tensor4view_get_elem
Numeric debug_tensor4view_get_elem(Tensor4View &tv, Index b, Index p, Index r, Index c)
Helper function to access tensor elements.
Definition: matpackIV.cc:1558
operator<<
std::ostream & operator<<(std::ostream &os, const ConstTensor4View &v)
Output operator.
Definition: matpackIV.cc:499
VectorView
The VectorView class.
Definition: matpackI.h:372
ConstTensor4View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:81
ConstTensor4View::begin
ConstIterator4D begin() const
Return const iterator to first book.
Definition: matpackIV.cc:414
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:29
ConstTensor4View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:69
ConstTensor4View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:63
Tensor4View::operator=
Tensor4View & operator=(const ConstTensor4View &v)
Assignment operator.
Definition: matpackIV.cc:1029
Tensor4View::end
ConstIterator4D end() const
Return const iterator behind last book.
Definition: matpackIV.cc:1003
Iterator4D::msv
Tensor3View msv
Current position.
Definition: matpackIV.h:74
Tensor4View::operator/=
Tensor4View & operator/=(Numeric x)
Division by scalar.
Definition: matpackIV.cc:1096
ConstMatrixView
A constant view of a Matrix.
Definition: matpackI.h:596
Tensor4View::begin
ConstIterator4D begin() const
Return const iterator to first book.
Definition: matpackIV.cc:997
ConstTensor4View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:75
ConstTensor3View::end
ConstIterator3D end() const
Return const iterator behind last page.
Definition: matpackIII.cc:153
ConstIterator4D::operator*
const ConstTensor3View & operator*() const
Dereferencing.
Definition: matpackIV.cc:52
Range
The range class.
Definition: matpackI.h:148
Range::mextent
Index mextent
The number of elements.
Definition: matpackI.h:207
Iterator4D::operator*
Tensor3View & operator*()
Dereferencing.
Definition: matpackIV.cc:39
Iterator4D
Implementation of Tensors of Rank 4.
Definition: matpackIV.h:38
Tensor3View::begin
ConstIterator3D begin() const
Return const iterator to first row.
Definition: matpackIII.cc:467
copy
void copy(ConstIterator4D origin, const ConstIterator4D &end, Iterator4D target)
Copy data between begin and end to target.
Definition: matpackIV.cc:1267
Tensor4View::operator()
ConstTensor4View operator()(const Range &b, const Range &p, const Range &r, const Range &c) const
Const index operator for subrange.
Definition: matpackIV.cc:526
ConstTensor4View::mrr
Range mrr
The row range of mdata that is actually used.
Definition: matpackIV.h:227
ConstTensor3View
A constant view of a Tensor3.
Definition: matpackIII.h:139
Tensor4View
The Tensor4View class.
Definition: matpackIV.h:243
ConstIterator4D::msv
ConstTensor3View msv
Current position.
Definition: matpackIV.h:119
Tensor4View::operator-=
Tensor4View & operator-=(Numeric x)
Subtraction of scalar.
Definition: matpackIV.cc:1118
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
max
Numeric max(const ConstTensor4View &x)
Max function, tensor version.
Definition: matpackIV.cc:1495
Range::mstride
Index mstride
The stride.
Definition: matpackI.h:209
ConstTensor4View::ConstTensor4View
ConstTensor4View()
Default constructor.
Definition: matpackIV.cc:443
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:292
Tensor4View::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIV.cc:402
ConstTensor4View::mdata
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIV.h:231