57 #ifdef ALAP_VECTOR_SPACE_TESTER_DUMP
58 # include "RTOpPack_SPMD_apply_op.hpp"
59 #endif // ALAP_VECTOR_SPACE_TESTER_DUMP
65 T my_max(
const T& v1,
const T& v2 ) {
return v1 > v2 ? v1 : v2; }
68 T my_min(
const T& v1,
const T& v2 ) {
return v1 < v2 ? v1 : v2; }
72 namespace AbstractLinAlgPack {
83 :print_all_tests_(print_all_tests)
84 ,print_vectors_(print_vectors)
85 ,throw_exception_(throw_exception)
86 ,num_random_tests_(num_random_tests)
87 ,warning_tol_(warning_tol)
88 ,error_tol_(error_tol)
99 bool success =
true, result =
false;
113 vec_ptr_t v_array[3];
115 {
for(
int k = 0; k < 3; ++k) {
119 v[k] = v_array[k].get();
123 vec_mut_ptr_t z_array[6];
125 {
for(
int k = 0; k < 6; ++k) {
127 z[k] = z_array[k].get();
130 if(out && print_all_tests())
131 *out << std::boolalpha
132 <<
"\n**************************************************"
133 <<
"\n*** VectorSpaceTester::check_vector_space(...) ***"
134 <<
"\n**************************************************\n";
142 char z_name[20], v_name[20];
145 if(out && print_all_tests())
146 *out <<
"\nspace->dim() = " << n << std::endl;
149 if(out && print_vectors()) {
150 *out <<
"\n*** Printing the immutable vectors\n";
151 {
for(
int j = 0; j < 3; ++j) {
152 sprintf( v_name,
"v[%d]", j );
153 *out << std::endl << v_name <<
" : " <<
typeName(*v[j]) << std::endl
156 *out <<
"\n*** Printing the mutable vectors\n";
157 {
for(
int k = 0; k < 6; ++k) {
158 sprintf( z_name,
"z[%d]", k );
159 *out << std::endl << z_name <<
" : " <<
typeName(*z[k]) << std::endl
165 if(out && print_all_tests())
166 *out <<
"\n*** Testing the obvious assertions\n";
168 {
for(
int k = 0; k < 6; ++k) {
171 sprintf( v_name,
"v[%d]", k );
175 sprintf( v_name,
"z[%d]", k-3 );
180 if(out && (print_all_tests() || !result))
181 *out <<
"\ncheck: " << v_name <<
"->space().is_compatible(space) : " << result << std::endl;
182 check_test( result ? 0.0 : -10.0 , out, &success );
185 if(out && (print_all_tests() || !result))
186 *out <<
"check: space.is_compatible(" << v_name <<
"->space()) : " << result << std::endl;
187 check_test( result ? 0.0 : -10.0 , out, &success );
189 err = vec->
dim() - space.
dim();
190 if(out && (print_all_tests() || !result))
191 *out <<
"check: " << v_name <<
"->dim() - space.dim() = " << vec->
dim() <<
" - "
192 << space.
dim() <<
" = " << err << std::endl;
195 result = vec->
nz() <= space.
dim();
196 if(out && (print_all_tests() || !result))
197 *out <<
"check: " << v_name <<
"->nz() <= space.dim() = " << vec->
nz() <<
" <= " << space.
dim()
198 <<
" : " << result << std::endl;
199 check_test( result ? 0.0 : -10.0 , out, &success );
205 if(out && print_all_tests())
206 *out <<
"\n*** Testing scalar assignment and element access methods\n";
209 sprintf( z_name,
"z[%d]", (
int)k );
210 if(out && print_all_tests())
211 *out <<
"\n0.0 -> " << z_name << std::endl;
213 if(out && print_vectors())
214 *out << std::endl << z_name <<
" =\n" << *z[k];
215 {
for(
index_type r = 0; r < num_random_tests(); ++r) {
216 std::srand( n / (1+r) + r );
221 as<index_type>(((
double)std::rand() / RAND_MAX) * n + 1.0),
228 if(out && print_all_tests())
229 *out << std::endl << z_name <<
".set_ele("<<i<<
","<<val<<
")\n";
230 #ifdef ALAP_VECTOR_SPACE_TESTER_DUMP
231 RTOpPack::show_spmd_apply_op_dump =
true;
233 z[k]->set_ele(i,val);
234 #ifdef ALAP_VECTOR_SPACE_TESTER_DUMP
235 RTOpPack::show_spmd_apply_op_dump =
false;
237 if(out && print_vectors())
238 *out << std::endl << z_name <<
" =\n" << *z[k];
239 #ifdef ALAP_VECTOR_SPACE_TESTER_DUMP
240 RTOpPack::show_spmd_apply_op_dump =
true;
243 val_get = z[k]->get_ele(i);
244 #ifdef ALAP_VECTOR_SPACE_TESTER_DUMP
245 RTOpPack::show_spmd_apply_op_dump =
false;
248 if(out && (print_all_tests() || ::fabs(err) >= warning_tol()) )
249 *out <<
"check: " << val <<
" - " << z_name <<
".get_ele(" << i <<
") = "
250 << val <<
" - " << val_get <<
" = " << err << std::endl;
254 z_k_sum =
sum(*z[k]);
256 if(out && (print_all_tests() || ::fabs(err) >= warning_tol()) )
257 *out <<
"check: " << val <<
" - sum(" << z_name <<
") = "
258 << val <<
" - " << z_k_sum <<
" = " << err << std::endl;
261 if(out && print_all_tests())
262 *out << z_name <<
".set_ele("<<i<<
",0.0)\n";
263 z[k]->set_ele(i,0.0);
264 if(out && print_vectors())
265 *out << std::endl << z_name <<
" =\n" << *z[k];
266 z_k_sum =
sum(*z[k]);
268 if(out && (print_all_tests() || ::fabs(err) >= warning_tol()) )
269 *out <<
"check: sum(" << z_name <<
") = " << z_k_sum << std::endl;
275 if(out && print_all_tests())
276 *out <<
"\n*** Testing vector assignment\n";
278 {
for(
int k = 0; k < 3; ++k ) {
279 sprintf( z_name,
"z[%d]", k );
280 sprintf( v_name,
"v[%d]", k );
281 if(out && print_all_tests())
282 *out <<
"\n" << v_name <<
" -> " << z_name <<
"\n";
285 sum_z_k =
sum(*z[k]),
286 sum_v_k =
sum(*v[k]);
287 err = (sum_z_k - sum_v_k)/n;
288 if(out && (print_all_tests() || ::fabs(err) >= warning_tol()) )
289 *out <<
"check: (sum(" << z_name <<
") - sum(" << v_name <<
"))/n = ("
290 << sum_z_k <<
" - " << sum_v_k <<
")/" << n <<
" = " << err << std::endl;
452 if(out && print_all_tests())
453 *out <<
"\n*** Testing norms\n";
456 sprintf( z_name,
"z[%d]", (
int)k );
461 if(out && print_all_tests())
462 *out << std::endl << val1 <<
" -> *" << z_name <<
".sub_view(1,"<<i_mid<<
")\n";
464 if(out && print_all_tests())
465 *out << val2 <<
" -> *" << z_name <<
".sub_view("<<i_mid+1<<
","<<n<<
")\n";
467 if(out && print_vectors())
468 *out << std::endl << z_name <<
" =\n" << *z[k] << std::endl;
472 expect_norm_1 = (::fabs(val1)*(i_mid) + ::fabs(val2)*(n-i_mid));
473 err = (norm_1 - expect_norm_1)/n;
474 if(out && (print_all_tests() || ::fabs(err) >= warning_tol()) )
475 *out <<
"check: (" << z_name <<
"->norm_1() - |"<<val1<<
"|*("<<i_mid<<
")+"
476 <<
"|"<<val2<<
"|*("<<n<<
"-"<<i_mid<<
"))/"<<n
477 <<
" = ("<<norm_1<<
" - "<<expect_norm_1<<
")/"<<n<<
" = "<<err<<std::endl;
482 expect_norm_2 =
::sqrt(val1*val1*(i_mid) + val2*val2*(n-i_mid));
483 err = (norm_2 - expect_norm_2)/n;
484 if(out && (print_all_tests() || ::fabs(err) >= warning_tol()) )
485 *out <<
"check: (" << z_name <<
"->norm_2() - ("<<val1<<
")^2*("<<i_mid<<
")+"
486 <<
"("<<val2<<
")^2*("<<n<<
"-"<<i_mid<<
"))/"<<n
487 <<
" = ("<<norm_2<<
" - "<<expect_norm_2<<
")/"<<n<<
" = "<<err<<std::endl;
492 expect_norm_inf = my_max(::fabs(val1),::fabs(val2));
493 err = (norm_inf - expect_norm_inf)/n;
494 if(out && (print_all_tests() || ::fabs(err) >= warning_tol()) )
495 *out <<
"check: (" << z_name <<
"->norm_inf() - max(|"<<val1<<
"|,"
496 <<
"|"<<val2<<
"|)/"<<n<<
" = ("<<norm_inf<<
" - "<<expect_norm_inf<<
")/"<<n
497 <<
" = "<<err<<std::endl;
502 if(out && print_all_tests())
503 *out <<
"space.dim() <= 1, can't test the norms...\n";
507 if(out && print_all_tests())
508 *out <<
"\n*** Testing clone() method\n";
510 if(out && print_all_tests())
511 *out <<
"\n(*vec = space.create_member()) = v[0]\n";
515 if(out && print_all_tests())
516 *out <<
"vec_clone = vec->clone()\n";
518 vec_clone = vec->clone();
519 if(out && print_all_tests())
520 *out <<
"vec = NULL\n";
523 sum_vec =
sum(*v[0]),
524 sum_vec_clone =
sum(*vec_clone);
525 err = (sum_vec - sum_vec_clone)/n;
526 if(out && (print_all_tests() || ::fabs(err) >= warning_tol()) )
527 *out <<
"check: (sum(v[0]) - sum(vec_clone))/n = ("
528 << sum_vec <<
" - " << sum_vec_clone <<
")/" << n
529 <<
" = " << err << std::endl;
534 catch(
const std::exception& except) {
536 *out <<
"Caught a std::exception: " << except.what() << std::endl;
538 if(throw_exception())
543 *out <<
"Caught an unknown exception!\n";
545 if(throw_exception())
554 if( ::fabs(err) >= error_tol() ) *success =
false;
555 if(out && (print_all_tests() || ::fabs(err) >= warning_tol()) ) {
556 if( ::fabs(err) >= error_tol() )
557 *out <<
"Error! |" << err <<
"| = " << ::fabs(err) <<
" >= error_tol = "
558 << error_tol() << std::endl;
559 else if( ::fabs(err) >= warning_tol() )
560 *out <<
"Warning! |" << err <<
"| = " << ::fabs(err) <<
" >= warning_tol = "
561 << warning_tol() << std::endl;
564 !*success && this->throw_exception(), std::logic_error
565 ,
"VectorSpaceTester::check_test(...): Error! |" << err <<
"| = " << ::fabs(err) <<
" >= error_tol = "
566 << error_tol() << std::endl );
virtual vec_mut_ptr_t sub_view(const Range1D &rng)
Create a mutable abstract view of a vector object.
virtual const VectorSpace & space() const =0
Return the vector space that this vector belongs to.
std::string typeName(const T &t)
Abstract interface for immutable, finite dimensional, coordinate vectors {abstract}.
RTOp_index_type index_type
virtual value_type norm_1() const
One norm. ||v||_1 = sum( |v(i)|, i = 1,,,this->dim() )
virtual value_type norm_inf() const
Infinity norm. ||v||_inf = max( |v(i)|, i = 1,,,this->dim() )
RTOp_value_type value_type
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual value_type norm_2() const
Two norm. ||v||_2 = sqrt( sum( v(i)^2, i = 1,,,this->dim() ) )
void check_test(value_type err, std::ostream *out, bool *success) const
RTOp_index_type size_type
Abstract interface for objects that represent a space for mutable coordinate vectors.
value_type norm_inf(const SparseVectorSlice< T_Ele > &sv_rhs)
result = ||sv_rhs||inf (BLAS IxAMAX)
virtual bool is_compatible(const VectorSpace &vec_spc) const =0
Compare the compatibility of two vector spaces.
TypeTo as(const TypeFrom &t)
void sqrt(DVectorSlice *vs_lhs, const DVectorSlice &vs_rhs)
vs_lhs = sqrt(vs_rhs)
virtual index_type dim() const =0
Return the dimmension of the vector space.
virtual index_type dim() const
Return the dimension of this vector.
virtual index_type nz() const
Return the number of nonzero elements in the vector.
value_type norm_2(const SparseVectorSlice< T_Ele > &sv_rhs)
result = ||sv_rhs||2 (BLAS xNRM2)
Abstract interface for mutable coordinate vectors {abstract}.
value_type sum(const Vector &v_rhs)
result = sum( v_rhs(i), i = 1,,,dim )
virtual bool check_vector_space(const VectorSpace &space, std::ostream *out) const
Run a vector space and the vectors it creates through a set of comprehensive tets.
virtual vec_mut_ptr_t create_member() const =0
Create a vector member from the vector space.
value_type norm_1(const SparseVectorSlice< T_Ele > &sv_rhs)
result = ||sv_rhs||1 (BLAS xASUM)
VectorSpaceTester(bool print_all_tests=false, bool print_vectors=false, bool throw_exception=true, size_type num_random_tests=4, value_type warning_tol=1e-14, value_type error_tol=1e-10)
Constructor (set default options).
void random_vector(value_type l, value_type u, VectorMutable *v)
Generate a random vector with elements uniformly distrubuted elements.