~kb/matrix

40d97f83d6a7bb5ac0c8e25ae4f15c9bb1594dec — Kim Burgess 5 years ago 5f04016
implement transpose
2 files changed, 16 insertions(+), 0 deletions(-)

M spec/matrix_spec.cr
M src/matrix.cr
M spec/matrix_spec.cr => spec/matrix_spec.cr +8 -0
@@ 140,6 140,14 @@ describe Matrix do
    end
  end

  describe "#transpose" do
    it "creates a transposed matrix" do
      a = Matrix(UInt32, 2, 3).build { |i, j| i * j }
      b = Matrix(UInt32, 3, 2).build { |i, j| j * i }
      a.transpose.should eq(b)
    end
  end

  describe "#dimensions" do
    it "provides the dimensions as a tuple" do
      a = Matrix(Nil, 10, 5).of nil

M src/matrix.cr => src/matrix.cr +8 -0
@@ 168,6 168,14 @@ struct Matrix(T, M, N)
    end
  end

  # Creates a new matrix that is the result of inverting the rows and columns
  # of `self`.
  def transpose
    Matrix(T, N, M).build do |i, j|
      self[j, i]
    end
  end

  # Returns the dimensions of `self` as a tuple of `{rows, cols}`.
  def dimensions
    {M, N}