Ruby 2.6 adds Matrix#antisymmetric?
Ruby 2.6.0-preview2 was recently released.
A matrix according to Wikipedia is a rectangular array of numbers. Matrix class in ruby represents a mathematical matrix.
>> x = Matrix[[1, 2], [3, 4]]
=> Matrix[[1, 2], [3, 4]]
Ruby 2.5.0
Let’s say we want to find out if a matrix is a skew-symmetric (or antisymmetric) matrix.
>> matrix1 = Matrix[[0, 1], [-1, 0]]
=> Matrix[[0, 1], [-1, 0]]
>> matrix1 == -matrix1.transpose
=> true
=> matrix2 = Matrix[[0, 2, -1], [-2, 0, -4], [1, 4, 0]]
>> matrix2 == -matrix2.transpose
=> true
Ruby 2.6.0
In Ruby 2.6, we can use Matrix#antisymmetric?
method to check if the matrix is
skew-symmetric (or antisymmetric or antimetric) matrix.
It returns true
for skew-symmetric matrix.
>> matrix1 = Matrix[[0, 1], [-1, 0]]
=> Matrix[[0, 1], [-1, 0]]
>> y.antisymmetric?
=> true
>> matrix2 = Matrix[[0, 2, -1], [-2, 0, -4], [1, 4, 0]]
=> Matrix[[0, 2, -1], [-2, 0, -4], [1, 4, 0]]
>> matrix2.antisymmetric?
=> true
Matrix#antisymmetric?
returns true
for a 0x0 empty matrix.
>> Matrix.empty
=> Matrix.empty(0, 0)
>> Matrix.empty.antisymmetric?
=> true
It returns false
for a non-antisymmetric matrix.
>> x = Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
=> Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>> x.antisymmetric?
=> false
It raises a Matrix::ErrDimensionMismatch
error for rectangular matrix i.e. a matrix for which horizontal and
vertical dimensions are not the same.
>> x = Matrix[[1, 2, 3], [4, 5, 6]]
=> Matrix[[1, 2, 3], [4, 5, 6]]
>> x.antisymmetric?
Traceback (most recent call last):
3: from /home/atul/.rvm/rubies/ruby-2.6.0-preview2/bin/irb:11:in `<main>'
2: from (irb):24
1: from /home/atul/.rvm/rubies/ruby-2.6.0-preview2/lib/ruby/2.6.0/matrix.rb:809:in `antisymmetric?'
ExceptionForMatrix::ErrDimensionMismatch (Matrix dimension mismatch)
Here is the relevant commit and discussion for the change.