//////////////////////////////////////////////////////////////////////////////
//
//       Title           : MathTut1.cpp
//       Project         : OSCAR
//       Created         : 7/23/2003
//       Author          : Peter S. March
//       Platforms       : All
///       Copyright      : Copyright© The University of Texas at Austin, 2002. All rights reserved.
///                 
///          This software and documentation constitute an unpublished work
///          and contain valuable trade secrets and proprietary information
///          belonging to University.  None of the foregoing material may be
///          copied  or duplicated or disclosed without the express, written
///          permission of University.  UNIVERSITY EXPRESSLY DISCLAIMS ANY
///          AND ALL WARRANTIES CONCERNING THIS SOFTWARE AND DOCUMENTATION,
///          INCLUDING ANY WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
///          PARTICULAR PURPOSE, AND WARRANTIES OF PERFORMANCE, AND ANY WARRANTY
///          THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE.
///          NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF
///          THE SOFTWARE OR DOCUMENTATION.  Under no circumstances shall
///          University be liable for incidental, special, indirect, direct or
///          consequential damages or loss of profits, interruption of business,
///          or related expenses which may arise from use of software or documentation,
///          including but not limited to those resulting from defects in software
///          and/or documentation, or loss or inaccuracy of data of any kind.
//       Access          : Company Confidential
//       Purpose         :
//
//----------------------------------------------------------------------------
//
//       Classes:
//               <none>
//
//       Global Functions:
//               <none>
//
//       Global Variables:
//               <none>
//
//----------------------------------------------------------------------------
//
//       $Revisions$
//
//       $Log: MathTut1.cpp,v $
//       Revision 1.4  2006/08/25 19:42:09  jknoll
//       now compatible with current OSCAR
//
//       Revision 1.3  2005/01/11 17:41:15  pmarch
//       no message
//
//       Revision 1.2  2003/09/26 16:25:03  pmarch
//       Changed project settings to match new CVS structure and made minor formatting changes.
//
//       Revision 1.1  2003/08/14 16:47:35  pmarch
//       no message
//
//
//////////////////////////////////////////////////////////////////////////////

#include "Math/Vector.h"
#include "Math/Matrix.h"
#include "Math/Tensor.h"
#include "Math/MathUtilities.h"
#include "FileData/MatrixData.h"

using namespace OSCAR;
using namespace std;


int main(void){

  //Create the first 3x3 matrix
  Matrix matrix1(3,3);
 
  //Fill the matrix with data.  The at() method is used here to
  //access paticular data members.  The at() method does not check
  //for index out of range errors.
  matrix1.at(0,0)=1.0;  matrix1.at(0,1)=0.5;  matrix1.at(0,2)=-3.1;
  matrix1.at(1,0)=2.3;  matrix1.at(1,1)=2.7;  matrix1.at(1,2)=2.0;
  matrix1.at(2,0)=1.7;  matrix1.at(2,1)=0.5;  matrix1.at(2,2)=-0.7;

  //Print this matrix to the screen
  cout << "matrix1:" << endl;
  cout << matrix1 << endl;

  //Create a second matrix
  Matrix matrix2(3,3);

  //Fill the matrix with data.  The () method is used to access
  //particular data members.  This method is slightly slower than
  //at(), but performs more error checking
  matrix2(0,0)=0.5;  matrix2(0,1)=2.5;  matrix2(0,2)=0.1;
  matrix2(1,0)=-3.4; matrix2(1,1)=-0.7; matrix2(1,2)=3.5;
  matrix2(2,0)=2.1;  matrix2(2,1)=1.5;  matrix2(2,2)=0.7;

  //print the second matrix to the screen
  cout << "matrix2:" << endl;
  cout << matrix2 << endl;

  //create a matrix to store the result of the
  //multiplication
  Matrix resultMatrix(3,3);

  //store the result of matrix1 * matrix2 and then display
  //the matrix.  This line will display an error if the 
  //matrices are not the correct sizes.
  resultMatrix=matrix1*matrix2;
  cout << "resultMatrix: (matrix1*matrix2)" << endl;
  cout << resultMatrix << endl;

  //Use the t() method to display the transpose of the
  //matrix.  This returns a Matrix object.
  cout << "transpose of resultMatrix:" << endl;
  cout << resultMatrix.t() << endl;

  //Use the Inverse() method to calculate the inverse
  //of a matrix.
  Matrix inverseMatrix(3,3);
  resultMatrix.Inverse(inverseMatrix);

  //Show that resultMatrix * inverseMatrix is
  //the identity matrix.
  cout << "identity matrix = resultMatrix * inverse resultMatrix:" << endl;
  cout << resultMatrix*inverseMatrix << endl;

  //Use a MatrixData object to read in a matrix, and
  //then use GetParameters() to load the data into a
  //Matrix.
  MatrixData  matrixFile("../Datafiles/matrix.txt", 4);
  Matrix fileMatrix = matrixFile.GetParameters();
  cout << "matrix from matrix.txt file:" << endl;
  cout << fileMatrix << endl;

  //Use the GetRow() method to extract a Vector
  //from a Matrix
  Vector rowVec(4);
  GetRow(rowVec,2,fileMatrix);

  //Multiply a matrix by a vector
  cout << "vector from row index 2 of fileMatrix:" << endl;
  cout << rowVec << endl << endl;
  cout << "fileMatrix * row index 2:" << endl;
  cout << fileMatrix*rowVec << endl << endl;

  //Create and populate a Tensor object.
  Tensor tensor1(3, 3, 3);

  for(unsigned int i=0; i<3; i++){
    for(unsigned int j=0; j<3; j++){
      for(unsigned int k=0; k<3; k++){
        tensor1.at(i, j, k) = (i+1)*(j+1)*(k+1);
      }
    }
  }
  cout << "Tensor:\n" << tensor1 << endl;
  return 0;
}