// sv-2.cc // A ZOOM PhysicsVectors tutorial example // // Tutorial example 2 for the SpaceVector class: // // Vector properties and arithmetic // // * Linear arithmetic // * Dot and cross products // * Vector properties // Key lines are indicated by // <------------- // Get the SpaceVector class #include "ZMutility/ZMenvironment.h" #include "ZMutility/FixedTypes.h" #include "PhysicsVectors/SpaceVector.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.) SpaceVector v1 (2, 4, -7); cout << "v1 =" << v1 << endl; SpaceVector v2 (3, 90, DEGREES, .1, RADIANS); // This is the first example of supplying an angle in DEGREES cout << "v2 =" << v2 << endl; SpaceVector v3, v4; // = (0,0,0) // ***** Linear arithmetic // Here is how to add, subtract, multiply by scalars, and so forth. //----------------------------------------------------------------- // Simple addition and subtraction v3 = v1 + v2; // <------------- cout << "v3 = v1 + v2 =" << v3 << endl; // Multiplication -- from either side v2 = 4*v3 - v1*2; // <------------- cout << "Now v2 =" << v2 << endl; // Division by a scalar v1 = v3 / 2.5; // <------------- // But NOT v1 = 2.5 / v3 !! cout << "v3 / 2.5 =" << v1 << endl; // Modify and assign operators v1 *= 3; // <------------- cout << "Now v1 =" << v1 << endl; // ***** Dot and cross products // Here is how to take a dot product v1.dot(v2) //--------------------------------------------- Float8 a; Float8 b; // Dot product is a method, not overloaded * operator a = v1.dot(v2); // <------------- b = v2.dot(v1); // These will be exactly equal cout << "v1.dot(v2) = " << a << " v2.dot(v1) = " << b << endl; // Here is how to take a cross product v1.cross(v2) //------------------------------------------------- // Cross product is a method, not overloaded * or ^ operator. v3 = v1.cross(v2); // <------------- v4 = v2.cross(v1); // These will be negative of one another cout << "v1.cross(v2) = " << v3 << "\n"; cout << "v2.cross(v1) = " << v4 << endl; // ***** Vector Properties // Here is how to find the magnitude of a vector //---------------------------------------------- v1.set ( 3, 4, 12 ); // The magnitude of this sample should be 13 Float8 m = v1.mag(); // <------------- cout << "Magnitude of " << v1 << " is " << m << endl; // If you can use the square of the magnitude, that is quicker: cout << "Squared magnitude of " << v1 << " is " << v1.mag2() << endl; // <--- // The spherical r component happens to be equivalent to mag(): // Here is how to find relativistic beta and gamma //------------------------------------------------ v2.set ( .04, -.03, .12 ); // Representing a velocity of v2 * speed of light Float8 beta = v2.beta(); // <------------- Float8 gamma = v2.gamma(); // = 1/sqrt(1-beta**2) // <------------- cout << "For velocity " << v2 << " beta = " << beta << " gamma = " << gamma << endl; // beta will match v2.mag() but will throw a WARNING if v2.mag() > 1. // gamma will throw a ERROR if v2.mag() >= 1. return 0; } /* end of main */