MADNESS  0.10.1
slice.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 /// \file slice.h
36 /// \brief Declares and implements Slice
37 /// \ingroup tensor
38 
39 #ifndef MADNESS_TENSOR_SLICE_H__INCLUDED
40 #define MADNESS_TENSOR_SLICE_H__INCLUDED
41 
43 
44 namespace madness {
45 
46  /*!
47  \brief A slice defines a sub-range or patch of a dimension.
48  \ingroup tensor
49 
50  \par Introduction
51 
52  The slice \c Slice(start,end,step) defines the subvector
53  \code
54  [start, start+step, start+2*step, ..., end]
55  \endcode
56  with indices as if generated from these loops
57  \code
58  if (step > 0)
59  for (i=start; i<=end; i+=step) i;
60  else if (step < 0)
61  for (i=start; i>=end; i+=step) i;
62  else if (step == 0 && start == end)
63  defines a scalar
64  else
65  runtime error detected when slice is used
66  \endcode
67 
68  Note that \c start and \c end are \em inclusive, unlike the Python
69  convention of specifying end+1 (note that we \em cannot do
70  this easily in C/C++ unless we also define a special value
71  to indicate the end of a dimension of unknown size).
72 
73  \par Special meanings and conventions
74 
75  Negative values for \c start or \c end (similar to Python)
76  are relative to the end of the (possibly unknown) dimension.
77  E.g.,
78  - \c end=-1 is equivalent to \c end=dim-1
79  - \c start=-4 is equivalent to \c start=dim-4
80  - \c step=0 and \c start==end implies dimension will be eliminated
81  when the slice is used to index a tensor
82  - The default constructor specifies the entire dimension
83  - If the input length is not an exact multiple of step, end is
84  rounded towards start to recover the behaviour of the
85  \c <= or \c >= bounds in the loops specified above.
86 
87  Special slices have been defined as constants
88  - \c _ (1 underscore) = \c Slice(0,-1,1) = full slice in current order
89  - \c ___ (3 underscores) = Array of Slices with value \c _ so that \c t(___)
90  will generate an assignable view of the entire tensor \c t .
91  - _reverse = \c Slice(-1,0,-1) = full dimension reversed
92 
93  \par Examples
94 
95  - \c Slice() --- full slice in current order
96  - \c Slice(0,-1,1) --- full slice in current order
97  - \c Slice(3,3,0) --- eliminate this dimension setting index=3 (step=0)
98  - \c Slice(3,3,1) --- reduce a dimension to length 1 using index=3 (step=1)
99  - \c Slice(-1,0,-1) --- reverse a dimension
100  - \c Slice(0,-1,2) --- use all even numbered elements
101  - \c Slice(1,-1,2) --- use all odd numbered elements
102  */
103  class Slice {
104 
105  public:
106  long start;
107  long end;
108  long step;
109 
110  inline Slice() : start(0), end(-1), step(1) {};
111  inline Slice(long s, long e, long stp=1) : start(s), end(e), step(stp) {};
112  inline Slice& operator=(const Slice& s) {
113  start=s.start;
114  end=s.end;
115  step=s.step;
116  return *this;
117  };
118  template <typename Archive>
119  void serialize(Archive& ar) {
120  ar & start & end & step;
121  }
122 
123  };
124 
125  std::ostream& operator<<(std::ostream& stream, const Slice& s);
126 
127  static const Slice _(0,-1,1); /// Entire dimension
128  static const std::vector<Slice> ___ = vector_factory(_,_,_,_,_,_); /// Entire tensor
129  static const Slice _reverse(-1,0,-1); /// Reversed dimension
130 
131 }
132 #endif // MADNESS_TENSOR_SLICE_H__INCLUDED
A slice defines a sub-range or patch of a dimension.
Definition: slice.h:103
long end
Definition: slice.h:107
void serialize(Archive &ar)
Definition: slice.h:119
Slice()
Definition: slice.h:110
Slice & operator=(const Slice &s)
Definition: slice.h:112
Slice(long s, long e, long stp=1)
Definition: slice.h:111
long step
Definition: slice.h:108
long start
Definition: slice.h:106
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
static const std::vector< Slice > ___
Entire dimension.
Definition: slice.h:128
static const Slice _(0,-1, 1)
std::ostream & operator<<(std::ostream &os, const particle< PDIM > &p)
Definition: lowrankfunction.h:397
std::vector< T > vector_factory(const T &v0)
Returns a std::vector<T> initialized from the arguments.
Definition: vector_factory.h:50
static const Slice _reverse(-1, 0,-1)
Entire tensor.
void e()
Definition: test_sig.cc:75
Declares and implements factories for short vectors.