// sv-4.cc // A ZOOM PhysicsVectors tutorial example // // Tutorial example 4 for the SpaceVector class: // // Comparing Vectors // // * Exact Comparison // * Relative difference // * DeltaR // * Parallel and Orthogonal // Key lines are marked with // <------------------ // Get the SpaceVector and UnitVector classes #include "ZMutility/ZMenvironment.h" #include "ZMutility/FixedTypes.h" #include "PhysicsVectors/SpaceVector.h" #include "PhysicsVectors/UnitVector.h" ZM_USING_NAMESPACE( zmpv ) USING( std::cout ) USING( std::endl ) // This example will use iostream output, so get that -- // in a portable way. #include "ZMutility/iostream" using std::cout; using std::endl; int main() { // (See tutorial example sv-1 for how to construct and access // SpaceVectors.) // The methods illustrated here may all be applied to UnitVector // as well. (See example sv-3 for how to construct UnitVectors.) // generate a few sample vectors SpaceVector v1 ( 3, 2, 5 ); SpaceVector v2 ( 4, -2, 3 ); SpaceVector v3 ( 1, 2, -6 ); SpaceVector v4 ( -1, 3, 4 ); // ***** Exact Comparison // Here is how to apply exact comparisons to vectors //-------------------------------------------------- v2 = v1; v3 = v2 + v4; if ( v2 == v1 ) { // <------------------ cout << v2 << " == " << v1 << endl; } if ( v4 != v1 ) { // <------------------ cout << v4 << " != " << v1 << endl; } // Other comparisons > < >= <= are also provided so that // sort templates can be used on lists of SpaceVectors. // ***** Relative Difference // Here is how to determine if two vectors are "nearly equal" //----------------------------------------------------------- v1.set ( 1, 2, 3 ); v3.set ( -3, 3, 1 ); Float8 epsilon = 1.0E-17; v2 = v1 + epsilon * v3; // Some calculations have generated two // vectors which are effectively equal: if ( v2 != v1 ) { // These vectors are not exactly equal cout << v2 << " != " << v1 << endl; } if ( v2.isNear (v1) ) { // <------------------ cout << " but " << v2 << ".isNear" << v1 << endl; } // isNear does a relative (not absolute) comparison: // Re-scaling does not affect the result. SpaceVector v1BIG = v1 * 1.0E20; SpaceVector v2BIG = v2 * 1.0E20; if ( v2BIG.isNear (v1BIG) ) { // <------------------ cout << " but " << v2BIG << ".isNear" << v1BIG <