// tsntra1.cc // Nick Macks (macks@fnal.gov), Summer 2002 // LinearAlgebra: calculate the trace of a 5x5 or a 6x6 Matrix // (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) #include "LinearAlgebra/Matrix.h" #include "ZMtools/ZMtimer.h" using namespace std; // Function declarations void Identification(); void GetDimension(int&); void Describe(const int&); void BuildMatrix(Matrix&, const int&); void DeclareMatrixSpecialized(Matrix&, const int&); void DeclareMatrixSymmetric(Matrix&); void Show(const Matrix&); void GetTraces(int&); void GetRuns(int&); void Trace(const 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 dimension void GetDimension(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 was defined void Describe(const int& dimension) { cout << "Matrix was defined 'Matrix (" << dimension << ", " << dimension << ")'\n"; } // Build the Matrix void BuildMatrix(Matrix& matrix, 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 void Show(const Matrix& matrix) { cout << "Using Matrix =\n" << matrix << "\n"; } // Get the number of times to calculate the trace void GetTraces(int& N) { cout << "How many times should the trace be calculated? "; cin >> N; } // Get the number of runs void GetRuns(int& run) { cout << "How many runs? "; cin >> run; cout << "\n"; } // Find the trace of the Matrix void Trace(const Matrix& matrix, const int& N, const int& run, double& minimum, double& maximum) { // Define a timer ZMcpuTimer timer; // Define a counter int counter = 0; // Loop "run" times for (int r=0; r maximum) maximum = timer.cpuTime(); // Write the output cout << counter << " traces took place\n"; cout << "Time is " << timer.cpuTime() << " secs\n"; cout << "Tracing 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(-ension) int dim = 0; GetDimension(dim); // Define a "dim x dim" Matrix Matrix m (dim, dim); // Describe how the Matrix was defined Describe (dim); // Declare the matrix specialized? DeclareMatrixSpecialized(m, dim); // Declare the matrix declared symmetric? DeclareMatrixSymmetric(m); // Build the "dim x dim" Matrix BuildMatrix(m, dim); // Show the actual Matrix Show(m); // Tracing parameters int tra = 0; GetTraces(tra); // number of trace calculations int run = 0; GetRuns(run); // number of runs double min = 0.; // minimum value double max = 0.; // maxinum value // Find the trace of the Matrix Trace(m, tra, run, min, max); // Display the minimum and maximum Summary(min, max); return 0; }