// tsnmul10.cc // Nick Macks (macks@fnal.gov), Summer 2002 // LinearAlgebra: multiply a 5x5 Matrix with a 10-element BiVector // or a 6x6 Matrix with a 12-element BiVector // (user can specify if the matrix is to be declared specialized // and/or symmetric) // (user can specify any dimension from 1 to 6, not just 5 or 6) // Note: since we don't have a BiVector in LinearAlgebra, // we used a 2-row Matrix instead of a bivector #include "LinearAlgebra/Matrix.h" //#include "LinearAlgebra/ColumnVector.h" #include "ZMtools/ZMtimer.h" using namespace std; // Function declarations void Identification(); void GetDimensions(int&); void Describe(const int&); void BuildMatrix(Matrix&, Matrix&, const int&); void DeclareMatrixSpecialized(Matrix&, const int&); void DeclareMatrixSymmetric(Matrix&); void Show(const Matrix&, const Matrix&); void GetCalcs(int&); void GetRuns(int&); void Calculate(Matrix&, Matrix&, const int&, const int&, double&, double&); void Summary(const double&, const double&); // Package ID void Identification() { cout << "Using LinearAlgebra\n"; } // Ask for the desired dimensions void GetDimensions(int& dimension) { cout << "Enter the desired dimension(1-6): "; cin >> dimension; while ( (dimension < 1) || (dimension > 6) ) { cout << "Enter an integer between 1 and 6: "; cin >> dimension; } } // Describe how the matrix and the "bivector" were defined void Describe(const int& dimension) { cout << "Matrix was defined 'Matrix (" << dimension << ", " << dimension << ")'\n"; cout << "\"BiVector\" was defined 'Matrix (" << dimension << ", 2)'\n"; } // Build the matrix and the "bivector" void BuildMatrix(Matrix& matrix, Matrix& bivector, const int& dimension) { for (int i=0; i> special; while ( (special != 'y') && (special != 'Y') && (special != 'n') && (special != 'N') ) { cout << "Please enter y or n: "; cin >> special; } if ( (special == 'y') || (special == 'Y') ) { if (dimension == 5) matrix.declareM55(); else if (dimension == 6) matrix.declareM66(); else if (dimension == 4) matrix.declareM44(); else if (dimension == 3) matrix.declareM33(); else if (dimension == 2) matrix.declareM22(); else if (dimension == 1) cout << "Matrix wasn't declared as specialized since dimension is 1\n"; } } // Ask the user if he wants the matrix declared symmetric void DeclareMatrixSymmetric(Matrix& matrix) { char symmetry; cout << "Declare Matrix as Symmetric? (y/n): "; cin >> symmetry; while ( (symmetry != 'y') && (symmetry != 'Y') && (symmetry != 'n') && (symmetry != 'N') ) { cout << "Please enter y or n: "; cin >> symmetry; } if ( (symmetry == 'y') || (symmetry == 'Y') ) matrix.declareSymmetric(); } // Show the matrix and the "bivector" void Show(const Matrix& matrix, const Matrix& bivector) { cout << "Using matrix = \n" << matrix << "\n"; cout << "Using \"bivector\" = \n" << bivector << "\n"; } // Get the number of calculations void GetCalcs(int& N) { cout << "How many multiplications? "; cin >> N; } // Get the number of runs void GetRuns(int& run) { cout << "How many runs? "; cin >> run; cout << "\n"; } // Multiply the matrix with the "bi-vector" void Calculate(Matrix& matrix, Matrix& bivector, const int& runs, const int& N, double& minimum, double& maximum) { // Define a timer ZMcpuTimer timer; // Define a counter for the multiplications int counter = 0; // Loop "run" times for (int r=0; r maximum) maximum = timer.cpuTime(); // Write the output cout << counter << " multiplications took place\n"; cout << "Time is " << timer.cpuTime() << " secs\n"; cout << "Multiplication time is " << 1000000 * timer.cpuTime() / N << " µsecs\n"; // Reset the timer and the counter timer.reset(); counter = 0; } } // Write the minimum and maximum values void Summary(const double& minimum, const double& maximum) { cout << "\nThe minimum time for 1 run was " << minimum << " secs\n"; cout << "The maximum time for 1 run was " << maximum << " secs\n"; } int main() { // What package are we using? Identification(); // Get the dim(-ensions) int dim = 0; GetDimensions(dim); // Define a "dim x dim" matrix Matrix m (dim, dim); // Define a "dim x 2" "BiVector" Matrix v (dim, 2); // Describe how the matrix and the "bivector" were declared Describe (dim); // Build the matrix and the "bivector" BuildMatrix(m, v, dim); // Declare the matrix specialized? DeclareMatrixSpecialized(m, dim); // Declare the matrix symmetric? DeclareMatrixSymmetric(m); // Show the actual matrix/"bivector" Show(m, v); // Multiplication parameters int mul = 0; GetCalcs(mul); // multiplication calculations int run = 0; GetRuns(run); // number of runs double min = 0.; // minimum value double max = 0.; // maxinum value // Multiply the matrix with the vector Calculate(m, v, run, mul, min, max); // Display the minimum and maximum Summary(min, max); return 0; }