trace method

  1. @override
double trace()
override

The trace of a square matrix A, denoted tr(A), is defined to be the sum of elements on the main diagonal (from the upper left to the lower right).

A MatrixException object is thrown if the matrix isn't square.

Implementation

@override
double trace() {
  // Making sure that the matrix is squared
  if (!isSquareMatrix) {
    throw const MatrixException('The matrix is not square!');
  }

  // The trace value
  var trace = 0.0;

  // Computing the trace
  for (var i = 0; i < columnCount; ++i) {
    trace += this(i, i);
  }

  return trace;
}