#include // The 'Vector_3d.hpp' file contains all the class and // function declarations, as well as the class definition. // // The 'Vector_3d.cpp' file contains all the function // definitions. You can treat all of these functions as if // they were defined in this file. // // Together, these two files provide the Vector_3d class and // the functions that operate on Vector_3d objects. #include "Vector_3d.hpp" // Function declaration (for this file only) int main(); // Function definition (for this file only) int main() { Vector_3d u{ 3.2, -4.5, 2.1 }; Vector_3d v{ 2.5, 7.2, -9.1 }; std::cout << "The original vectors:" << std::endl; std::cout << " u = " << to_string( u ) << std::endl; std::cout << " v = " << to_string( v ) << std::endl; // Test inner( u, v ). std::cout << std::endl << "The inner product of u and v:" << std::endl; std::cout << " inner( u, v ) = " << inner( u, v ) << std::endl; // Test norm( u ). std::cout << std::endl << "The norm of u:" << std::endl; std::cout << " norm( u ) = " << norm( u ) << std::endl; // Test add( u, v ). std::cout << std::endl << "Adding u and v:" << std::endl; std::cout << " u + v = " << to_string( add( u, v ) ) << std::endl; // Test both versions of mul(...). std::cout << std::endl << "Multiplying u by 3:" << std::endl; std::cout << " 3u = " << to_string( mul( 3.0, u ) ) << std::endl; std::cout << " u3 = " << to_string( mul( u, 3.0 ) ) << std::endl; // Test negate( u ). std::cout << std::endl << "The additive inverse of u:" << std::endl; std::cout << " -u = " << to_string( negate( u ) ) << std::endl; // Test sub( u, v ). std::cout << std::endl << "Subtracting v from u:" << std::endl; std::cout << " u - v = " << to_string( sub( u, v ) ) << std::endl; // Test div( u, 2.0 ). std::cout << std::endl << "Dividing u by 2:" << std::endl; std::cout << " u/2 = " << to_string( div( u, 2.0 ) ) << std::endl; // Test proj( u, v ). std::cout << std::endl << "Projecting u onto v:" << std::endl; std::cout << " proj_v(u) = " << to_string( proj( u, v ) ) << std::endl; // Test cross( u, v ). std::cout << std::endl << "The cross product of u and v:" << std::endl; std::cout << " u x v = " << to_string( cross( u, v ) ) << std::endl; // Display Boolean values as 'true' and 'false' rather // than as the integers 1 and 0. std::cout << std::boolalpha; // Test equal( u, v ). std::cout << std::endl << "Testing vectors for equality:" << std::endl; std::cout << " equal( u, v ) = " << equal( u, v ) << std::endl; std::cout << " equal( u, u ) = " << equal( u, u ) << std::endl; // The calls to to_string( u ) and to_string( v ) above // have already demonstrated the to_string(...) function. ////////////////////////////////////////////////////////// // The remaining functions modify their first argument. // ////////////////////////////////////////////////////////// // Test normalize( w ). Vector_3d w{ u }; std::cout << std::endl << "Normalizing a copy of u:" << std::endl; std::cout << " before: w = " << to_string( w ) << std::endl; normalize( w ); std::cout << " after: w = " << to_string( w ) << std::endl; std::cout << " norm( w ) = " << norm( w ) << std::endl; // Test add_to( w, v ). w = u; std::cout << std::endl << "Adding v to a copy of u:" << std::endl; std::cout << " before: w = " << to_string( w ) << std::endl; add_to( w, v ); std::cout << " after: w = " << to_string( w ) << std::endl; // Test sub_from( w, v ). w = u; std::cout << std::endl << "Subtracting v from a copy of u:" << std::endl; std::cout << " before: w = " << to_string( w ) << std::endl; sub_from( w, v ); std::cout << " after: w = " << to_string( w ) << std::endl; // Test mul_by( w, 3.0 ). w = u; std::cout << std::endl << "Multiplying a copy of u by 3:" << std::endl; std::cout << " before: w = " << to_string( w ) << std::endl; mul_by( w, 3.0 ); std::cout << " after: w = " << to_string( w ) << std::endl; // Test div_by( w, 2.0 ). w = u; std::cout << std::endl << "Dividing a copy of u by 2:" << std::endl; std::cout << " before: w = " << to_string( w ) << std::endl; div_by( w, 2.0 ); std::cout << " after: w = " << to_string( w ) << std::endl; return 0; }