New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Matrix#antisymmetric? and Matrix#reflexive? #1730
Conversation
Add two methods (“antisymmetric?” and “reflexive?”) determine if the matrix is reflexive, antisymmetric or not.
Thanks for the PR.
Would you like to amend your PR? |
# Conflicts: # lib/matrix.rb
1. implement method — antisymmetric? 2. use all? instead of the return true/false 3. add specs for both methods
lib/matrix.rb
Outdated
@@ -28,6 +28,7 @@ module ExceptionForMatrix # :nodoc: | |||
# | |||
# The +Matrix+ class represents a mathematical matrix. It provides methods for creating | |||
# matrices, operating on them arithmetically and algebraically, | |||
<<<<<<< HEAD |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops
antisymmetric_spec.rb
Outdated
|
||
describe "Matrix.antisymmetric?" do | ||
it "returns true for an antisymmetric matrix" do | ||
Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]].antisymmetric?.should be_true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not an antisymmetric matrix
antisymmetric_spec.rb
Outdated
end | ||
|
||
it "returns true for a 0x0 empty matrix" do | ||
Matrix.empty.symmetric?should be_true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong method
antisymmetric_spec.rb
Outdated
Matrix.empty.symmetric?should be_true | ||
end | ||
|
||
it "returns false for an asymmetric Matrix" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
??? (An asymmetric matrix can be antisymmetric; what‘s the point of this test?)
antisymmetric_spec.rb
Outdated
end | ||
|
||
it "returns false for an asymmetric Matrix" do | ||
Matrix[[1, 2],[-2, 1]].symmetric?.should be_false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong method
antisymmetric_spec.rb
Outdated
Matrix[[1, 2],[-2, 1]].symmetric?.should be_false | ||
end | ||
|
||
it "raises an error for rectangular matrices" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO clearer: non-square matrices
# Returns +true+ if this is a antisymmetric matrix. | ||
# Raises an error if matrix is not square. | ||
# | ||
def antisymmetric? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
implementation does not test for antisymmetric!
end | ||
|
||
it "returns true for a 0x0 empty matrix" do | ||
Matrix.empty.symmetric?.should be_true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for being so patient. Most of the code were follow the style of method "symmetric?". I've learned 1-0 matrix, and it seems to me that reflexive relation cannot applied in a general matrix, this method might not be very helpful, but I'll still fix this problem.
Matrix.empty.symmetric?.should be_true | ||
end | ||
|
||
it "returns true for a non-reflexive Matrix" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true?
Matrix[[1, 1],[2, 2]].reflexive?.should be_false | ||
end | ||
|
||
it "raises an error for rectangular matrices" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-square
Matrix.empty(2, 0), | ||
].each do |rectangular_matrix| | ||
lambda { | ||
rectangular_matrix.symmetric? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong method
and: antisymmetric_spec.rb is not in the correct directory |
1. in reflexive_spec.rb: change on some wrong code 2.in matrix.rb: method error 3.in antisymmetric_spec.rb: change on some wrong code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
antisymetric?
has already been added, please remove it and keep only reflexive?
# | ||
def reflexive? | ||
Matrix.Raise ErrDimensionMismatch unless square? | ||
self.each(:diagonal) do |e| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self
is superfluous. consider using each(:diagonal).all?
self.each(:diagonal) do |e| | ||
return false if e != 1 | ||
end | ||
all? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect.
Implementation fixed, code merged 😄 Thanks @Yiloo! |
Adapted from a patch by Yilo git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64796 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Is "reflexive" a common word to indicate a matrix whose diagonals are all 1s? I couldn't (easily) find such a usage by searching with "reflexive matrix" diagonal. I could find some articles including the definition by searching with "matrix is reflexive", but its hit count is just 44. I guess that the word came from "reflexive relation", but I'm unsure if it is also common for matrix. @marcandre What do you think? /cc @znz |
Hmmm, it looks like indeed it's not needed (and easy to do anyways). I'll revert this. |
/cc @stomar @marcandre Well, there seems to be confusion about the word "antisymmetric". I believe the OP expected antisymmetric relation: a_ij = 0 or a_ji = 0 for all i ≠ j. However, the PR's implementation checks if a_ij != a_ji for all i ≠ j. I guess it was a bug. Wikipedia's "Skew-symmetric matrix" says (emphasized by me):
If we have However, I'm not sure whether the name "antisymmetric?" is best. Isn't "skew_symmetric?" better? I'm unfamiliar with math. Wolfram uses the name AntisymmetricMatrix, but it also says:
Strictly speaking, there is no actual request for the current Matrix#antisymmetric?. Is it really needed? Again, I'm unfamiliar with math. |
@mrkn found: https://physics.stackexchange.com/questions/96687/what-is-the-difference-between-a-skew-symmetric-and-an-antisymmetric-tensor
|
I think that |
Actually, I am a physicist 😄. I thought the concept useful and used enough to have it added to |
It was my idea to implement this method, as I was taking Discrete Mathematics course in the college. You are right, the reflexive matrix is representing a reflexive relation by using 0-1 matrix. I was not sure if this method would be widely used since a general matrix can have more values than just 0 and 1. |
Thank you for your reply. Now I'm satisfied about @marcandre May I remove |
I'll do it tomorrow, and merge the mutable matrix too, assuming that's ok for you, @mame? |
This reverts commit 19fe655.
Add two methods (“antisymmetric?” and “reflexive?”) determine if the
matrix is reflexive, antisymmetric or not.