AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AbstractLinAlgPack_VectorSpaceSubSpace.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <assert.h>
43 
44 #include "AbstractLinAlgPack_VectorSpaceSubSpace.hpp"
45 #include "AbstractLinAlgPack_VectorMutableSubView.hpp"
46 #include "Teuchos_Assert.hpp"
47 
48 namespace AbstractLinAlgPack {
49 
51 {
52  this->initialize(full_space,rng);
53 }
54 
55 void VectorSpaceSubSpace::initialize( const space_ptr_t& full_space, const Range1D& rng )
56 {
57 #ifdef TEUCHOS_DEBUG
59  full_space.get() == NULL, std::invalid_argument
60  ,"VectorSpaceSubSpace::initialize(...): Error!" );
61 #endif
62  const index_type n = full_space->dim();
63 #ifdef TEUCHOS_DEBUG
65  !rng.full_range() && rng.ubound() > n, std::out_of_range
66  ,"VectorSpaceSubSpace::initialize(...): Error, "
67  "rng = [" << rng.lbound() << "," << rng.ubound() << "] is not in the range "
68  "[1,vec->dim()] = [1," << n << "]" );
69 #endif
70  full_space_ = full_space;
71  rng_ = rng.full_range() ? Range1D(1,n) : rng;
72 }
73 
75 {
76  full_space_ = Teuchos::null;
77  rng_ = Range1D::Invalid;
78 }
79 
80 #ifdef TEUCHOS_DEBUG
81 void VectorSpaceSubSpace::validate_range(const Range1D& rng) const
82 {
83  const index_type n = this->dim();
85  full_space_.get() == NULL, std::logic_error
86  ,"VectorSpaceSubSpace::validate_range(rng): Error, Uninitialized" );
88  full_space_.get() && !rng.full_range() && rng.ubound() > n, std::logic_error
89  ,"VectorSpaceSubSpace::validate_range(rng): Error, "
90  "rng = [" << rng.lbound() << "," << rng.ubound() << "] is not in the range "
91  "[1,this->dim] = [1," << n << "]" );
92 }
93 #endif
94 
95 // Overridden from VectorSpace
96 
97 bool VectorSpaceSubSpace::is_compatible(const VectorSpace& another_space) const
98 {
99  if( this->dim() == another_space.dim() && this->is_in_core() && another_space.is_in_core() )
100  return true;
101  const VectorSpaceSubSpace
102  *a_space = dynamic_cast<const VectorSpaceSubSpace*>(&another_space);
103  if(!a_space)
104  return false;
105  return
106  ( this->full_space_.get() == NULL && a_space->full_space_.get() == NULL )
107  ||
108  ( this->rng_ == a_space->rng_ && this->full_space_->is_compatible(*a_space->full_space_) );
109 }
110 
112 {
113  return full_space_->is_in_core();
114 }
115 
116 index_type VectorSpaceSubSpace::dim() const
117 {
118  return full_space_.get() ? rng_.size() : 0;
119 }
120 
122 {
123  namespace rcp = MemMngPack;
124  if( full_space_.get() )
125  return Teuchos::rcp(
127  full_space_->create_member(), rng_
128  ) );
129  return Teuchos::null;
130 }
131 
133 {
134  namespace rcp = MemMngPack;
135  if( full_space_.get() )
136  return Teuchos::rcp(new VectorSpaceSubSpace( full_space_->clone(), rng_ ));
137  return Teuchos::rcp(new VectorSpaceSubSpace());
138 }
139 
141 {
142  namespace rcp = MemMngPack;
143  if( full_space_.get() == NULL && rng_in == Range1D::Invalid )
144  return Teuchos::rcp(this,false);
145  validate_range(rng_in);
146  const index_type dim = this->dim();
147  const Range1D rng = rng_in.full_range() ? Range1D(1,dim) : rng_in;
148  if( rng.lbound() == 1 && rng.ubound() == dim )
149  return space_ptr_t( this, false );
150  const index_type this_offset = rng_.lbound() - 1;
151  return Teuchos::rcp(
153  full_space_
154  ,Range1D(
155  this_offset + rng.lbound()
156  ,this_offset + rng.ubound() )
157  ) );
158 }
159 
160 } // end namespace AbstractLinAlgPack
void initialize(const space_ptr_t &full_space, const Range1D &rng)
Initialize.
Concrete subclass for a default sub-space of a vector.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Index size() const
Index ubound() const
T * get() const
virtual bool is_in_core() const
Returns true if the vectors are in core.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const VectorSpace > space_ptr_t
void validate_range(const Range1D &rng) const
Validate rng.
Abstract interface for objects that represent a space for mutable coordinate vectors.
bool full_range() const
Concrete subclass for a sub-view of a VectorMutable object.
virtual index_type dim() const =0
Return the dimmension of the vector space.
static const Range1D Invalid
Index lbound() const