function _denseTrace(m) {
    // matrix size & data
    var size = m._size;
    var data = m._data; // process dimensions

    switch (size.length) {
      case 1:
        // vector
        if (size[0] === 1) {
          // return data[0]
          return clone(data[0]);
        }

        throw new RangeError('Matrix must be square (size: ' + format(size) + ')');

      case 2:
        // two dimensional
        var rows = size[0];
        var cols = size[1];

        if (rows === cols) {
          // calulate sum
          var sum = 0; // loop diagonal

          for (var i = 0; i < rows; i++) {
            sum = add(sum, data[i][i]);
          } // return trace


          return sum;
        }

        throw new RangeError('Matrix must be square (size: ' + format(size) + ')');

      default:
        // multi dimensional
        throw new RangeError('Matrix must be two dimensional (size: ' + format(size) + ')');
    }
  }
Calculate the trace of a matrix: the sum of the elements on the main
diagonal of a square matrix.

Syntax:

   math.trace(x)

Examples:

   math.trace([[1, 2], [3, 4]]) // returns 5

   const A = [
     [1, 2, 3],
     [-1, 2, 3],
     [2, 0, 3]
   ]
   math.trace(A) // returns 6

See also:

   diag

@param {Array | Matrix} x  A matrix

@return {number} The trace of `x`