#include <iostream>
#include <fstream>
#include <stdexcept>
#include "matrix.h"
using namespace std;

int main() {

  // perform tests based on a matrix read from a file
  ifstream fin;

  do {
    if (fin.fail()) {
      cout << "Unable to open file." << endl;
      fin.clear();
    }
    string filename;
    cout << "Enter filename containing origin matrix: ";
    cin >> filename;
    fin.open(filename.c_str());
  } while (fin.fail());

  matrix A;
  fin >> A;

  cout << "Matrix A was read as:" << endl;
  cout << A << endl;

  cout << "Submatrix A(range(2,5), range(3,2,9)) is" << endl;
  cout << A(range(2,5), range(3,2,9)) << endl;


  matrix_proxy mp = A(6, range(3,2,9));
  cout << "Submatrix A(6, range(3,2,9)) is..." << endl;
  cout << mp << endl;
  cout << "Entry (0,1) of that submatrix is " << mp(0,1) << endl;
  mp(0,1) = 1000;
  cout << "Entry (0,1) of that submatrix changed to " << mp(0,1) << endl;
  cout << "Submatrix A(6, range(3,2,9)) now appears as..." << endl;
  cout << mp << endl;
  cout << "A now appears as:" << endl;
  cout << A << endl;


  A(range(2,5), range(3,2,9)) = matrix(3,3,-1);
  cout << "After A(range(2,5), range(3,2,9)) = matrix(3,3,-1) A has contents:" << endl;
  cout << A << endl;



  // problem cases follow...

  cout << "Submatrix A(2, range(3,2,9)) + A(4, range(0,3))  is..." << endl;
  cout << A(2, range(3,2,9)) + A(4, range(0,3)) << endl;


  cout << "Submatrix A(2, range(3,2,9)) + matrix(1,3,1)  is..." << endl;
  cout << A(2, range(3,2,9)) + matrix(1,3,1) << endl;

  cout << "By the conclusion, A has contents:" << endl;  // should be unaffected by additions
  cout << A << endl;

  matrix B(A(range(0,2), range(0,4)));    // B is a new (independent) matrix
  cout << "B created based upon A(range(0,2), range(0,4)) has contents..." << endl;
  cout << B;
  B(0,1) = -20;
  cout << "Entry(0,1) of that submatrix set to " << B(0,1) << endl;

  cout << "Notice that A has contents:" << endl;  // should be unaffected by change to B
  cout << A << endl;

}
