|
AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization 00005 // Copyright (2003) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #include "AbstractLinAlgPack_SpVectorView.hpp" 00030 00031 namespace { 00032 00033 // Setup some template classes to check at complile time that 00034 // the layout of SpVectorSlice::element_type is proper. 00035 template<int N, class T> 00036 class assert_compile_time { 00037 public: 00038 assert_compile_time() 00039 { 00040 // This should not compile if instantiated with a type T that 00041 // is not an integer. However, if the compiler checks this 00042 // function without instantiating it, it can not cause an error 00043 // because it does not know the type of T to see if the 00044 // conversion is legal or not. 00045 T d; 00046 static_cast<int*>(d); 00047 } 00048 }; 00049 00050 // Template specialization for no error 00051 template<> 00052 class assert_compile_time<0,double> { 00053 public: 00054 assert_compile_time() 00055 {} 00056 }; 00057 00058 // Validate that there is an integer stride between values 00059 assert_compile_time< 00060 ((int)sizeof(AbstractLinAlgPack::SpVectorSlice::element_type) 00061 % (int)sizeof(AbstractLinAlgPack::value_type)) 00062 , double 00063 > 00064 validate_value_stride; 00065 00066 // Validate that there is an integer stride between indexes 00067 assert_compile_time< 00068 ((int)sizeof(AbstractLinAlgPack::SpVectorSlice::element_type) 00069 % (int)sizeof(AbstractLinAlgPack::index_type)) 00070 , double 00071 > 00072 validate_index_stride; 00073 00074 // Compute the stride between values 00075 const int values_stride = (int)sizeof(AbstractLinAlgPack::SpVectorSlice::element_type) 00076 / (int)sizeof(AbstractLinAlgPack::value_type); 00077 00078 // Compute the stride between indexes 00079 const int indices_stride = (int)sizeof(AbstractLinAlgPack::SpVectorSlice::element_type) 00080 / (int)sizeof(AbstractLinAlgPack::index_type); 00081 00082 } // end namespace 00083 00084 RTOpPack::SparseSubVector 00085 AbstractLinAlgPack::sub_vec_view( 00086 const SpVectorSlice& sv_in 00087 ,const Range1D& rng_in 00088 ) 00089 { 00090 const Range1D rng = RangePack::full_range(rng_in,1,sv_in.dim()); 00091 const SpVectorSlice sv = sv_in(rng); 00092 00093 RTOpPack::SparseSubVector sub_vec; 00094 00095 if(!sv.nz()) { 00096 sub_vec.initialize( 00097 rng.lbound()-1 // global_offset 00098 ,rng.size() // sub_dim 00099 ,0 // nz 00100 ,NULL // vlaues 00101 ,1 // values_stride 00102 ,NULL // indices 00103 ,1 // indices_stride 00104 ,0 // local_offset 00105 ,1 // is_sorted 00106 ); 00107 } 00108 else { 00109 SpVectorSlice::const_iterator 00110 itr = sv.begin(); 00111 TEST_FOR_EXCEPT( !( itr != sv.end() ) ); 00112 const value_type *values = &itr->value(); 00113 if( sv.dim() && sv.nz() == sv.dim() && sv.is_sorted() ) { 00114 sub_vec.initialize( 00115 rng.lbound()-1 // global_offset 00116 ,rng.size() // sub_dim 00117 ,values // values 00118 ,values_stride // values_stride 00119 ); 00120 } 00121 else { 00122 const value_type *values = &itr->value(); 00123 const index_type *indexes = &itr->index(); 00124 sub_vec.initialize( 00125 rng.lbound()-1 // global_offset 00126 ,sv.dim() // sub_dim 00127 ,sv.nz() // sub_nz 00128 ,values // values 00129 ,values_stride // values_stride 00130 ,indexes // indices 00131 ,indices_stride // indices_stride 00132 ,sv.offset() // local_offset 00133 ,sv.is_sorted() // is_sorted 00134 ); 00135 } 00136 } 00137 00138 return sub_vec; 00139 }
1.7.4