Method: Matrix#trace

Defined in:
lib/matrix.rb

#traceObject Also known as: tr

Returns the trace (sum of diagonal elements) of the matrix.

Matrix[[7,6], [3,9]].trace
#  => 16


1473
1474
1475
1476
1477
1478
# File 'lib/matrix.rb', line 1473

def trace
  raise ErrDimensionMismatch unless square?
  (0...column_count).inject(0) do |tr, i|
    tr + @rows[i][i]
  end
end