This documentation is automatically generated by online-judge-tools/verification-helper

:warning: spec/math/matrix_spec.cr

Depends on

Code

require "spec"
require "../../src/math/matrix"
require "../../src/math/mint"

private M = Matrix[[1, 2], [3, 4]]
i_j_value = [
  {0, 0, 1}, {0, 1, 2}, {1, 0, 3}, {1, 1, 4},
  {-2, -2, 1}, {-2, -1, 2}, {-1, -2, 3}, {-1, -1, 4},
]
out_of_range = [
  {0, 2}, {2, 0}, {2, 2},
  {-3, 0}, {0, -3}, {-3, -3},
]

describe Matrix do
  it ".identity" do
    Matrix(Int32).identity(1).should eq Matrix[[1]]
    Matrix(Int32).identity(2).should eq Matrix[[1, 0], [0, 1]]
    Matrix(Int32).identity(3).should eq Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
    expect_raises(ArgumentError) { Matrix(Int32).identity(-1) }
  end

  it "[]" do
    M.should eq Matrix.new [[1, 2], [3, 4]]
  end

  it ".from" do
    Matrix(Int64).from([1, 2], [3, 4]).should eq Matrix[[1i64, 2i64], [3i64, 4i64]]
    Matrix(Int64).from({1, 2}, {3, 4}).should eq Matrix[[1i64, 2i64], [3i64, 4i64]]
    Matrix(Int32).from(%w[1 2], %w[3 4]).should eq Matrix[[1, 2], [3, 4]]
    Matrix(Int32).from(1..2, 3..4).should eq Matrix[[1, 2], [3, 4]]
  end

  describe ".new" do
    it "receives initial value" do
      Matrix.new(1, 1, 0).should eq Matrix[[0]]
      Matrix.new(2, 3, 0).should eq Matrix[[0, 0, 0], [0, 0, 0]]
      expect_raises(ArgumentError) { Matrix.new(-1, 1, 0) }
      expect_raises(ArgumentError) { Matrix.new(1, -1, 0) }
    end

    it "receives block" do
      Matrix({Int32, Int32}).new(3, 3) { |i, j| {i, j} }.should eq Matrix[
        [{0, 0}, {0, 1}, {0, 2}],
        [{1, 0}, {1, 1}, {1, 2}],
        [{2, 0}, {2, 1}, {2, 2}],
      ]
      expect_raises(ArgumentError) { Matrix(Int32).new(-1, 1) { 0 } }
      expect_raises(ArgumentError) { Matrix(Int32).new(1, -1) { 0 } }
    end

    it "receives Array" do
      Matrix.new([[1, 2], [3, 4]]).should eq Matrix[[1, 2], [3, 4]]
      expect_raises(ArgumentError) { Matrix.new [[1, 2], [3]] }
    end
  end

  it "#fetch(i, j, &block)" do
    i_j_value.each do |i, j, value|
      M.fetch(i, j) { nil }.should eq value
    end
    out_of_range.each do |i, j|
      M.fetch(i, j) { |i, j| [i, j] }.should eq [i, j]
    end
  end

  it "#fetch(i, j, default)" do
    i_j_value.each do |i, j, value|
      M.fetch(i, j, nil).should eq value
    end
    out_of_range.each do |i, j|
      M.fetch(i, j, nil).should be_nil
    end
  end

  it "#[](i, j)" do
    i_j_value.each do |i, j, value|
      M[i, j].should eq value
    end
    out_of_range.each do |i, j|
      expect_raises(IndexError) { M[i, j] }
    end
  end

  it "#[]?(i, j)" do
    i_j_value.each do |i, j, value|
      M[i, j]?.should eq value
    end
    out_of_range.each do |i, j|
      M[i, j]?.should be_nil
    end
  end

  it "#unsafe_fetch(i, j)" do
    M.unsafe_fetch(0, 0).should eq 1
    M.unsafe_fetch(0, 1).should eq 2
    M.unsafe_fetch(1, 0).should eq 3
    M.unsafe_fetch(1, 1).should eq 4
  end

  it "#+(other)" do
    (Matrix[[1, 2], [3, 4]] + Matrix[[5, 6], [7, 8]]).should eq Matrix[[6, 8], [10, 12]]
    expect_raises(IndexError) { Matrix[[1, 2], [3, 4]] + Matrix[[1], [2]] }
  end

  it "#-(other)" do
    (Matrix[[5, 6], [7, 8]] - Matrix[[1, 2], [3, 4]]).should eq Matrix[[4, 4], [4, 4]]
    expect_raises(IndexError) { Matrix[[1, 2], [3, 4]] - Matrix[[1], [2]] }
  end

  it "#*(other)" do
    a = Matrix[[1, 2], [3, 4], [5, 6]]
    b = Matrix[[7, 8, 9], [10, 11, 12]]
    c = Matrix[[27, 30, 33], [61, 68, 75], [95, 106, 117]]
    (a * b).should eq c
    expect_raises(IndexError) { a * a }
    expect_raises(IndexError) { b * b }
  end

  it "#**(k)" do
    a1 = Matrix[[1, 2], [3, 4]]
    (a1 ** 0).should eq Matrix(Int32).identity(2)
    (a1 ** 1).should eq a1
    (a1 ** 2).should eq a1 * a1
    (a1 ** 3).should eq a1 * a1 * a1

    m1 = Matrix(Mint).from [1, 2], [3, 4]
    m2 = Matrix(Mint).from [414846427, 59557274], [89335911, 504182338]
    (m1 ** (10i64**18)).should eq m2
  end

  it "#to_s" do
    M.to_s.should eq "[[1, 2], [3, 4]]"
  end

  it "#inspect" do
    M.inspect.should eq "Matrix[[1, 2], [3, 4]]"
  end
end
require "spec"

# require "../../src/math/matrix"
class Matrix(T)
  include Indexable(Array(T))

  def Matrix.identity(size : Int32) : self
    result = Matrix(T).new(size, size, T.zero)
    (0...size).each do |i|
      result[i][i] = T.new(1)
    end
    result
  end

  macro [](*rows)
    Matrix.new [{{rows.splat}}]
  end

  getter height : Int32, width : Int32, data : Array(Array(T))

  def initialize
    @height = 0
    @width = 0
    @data = Array(Array(T)).new
  end

  def initialize(@height, @width, value : T)
    raise ArgumentError.new("Negative matrix height: #{@height}") unless @height >= 0
    raise ArgumentError.new("Negative matrix width: #{@width}") unless @width >= 0
    @data = Array.new(height) { Array(T).new(width, value) }
  end

  def initialize(@height, @width, &block : Int32, Int32 -> T)
    raise ArgumentError.new("Negative matrix height: #{@height}") unless @height >= 0
    raise ArgumentError.new("Negative matrix width: #{@width}") unless @width >= 0
    @data = Array.new(height) { |i| Array.new(width) { |j| yield i, j } }
  end

  def initialize(@data : Array(Array(T)))
    @height = @data.size
    @width = @data[0].size
    raise ArgumentError.new unless @data.all? { |a| a.size == width }
  end

  def self.from(*rows) : self
    Matrix(T).new rows.map { |row|
      row.map { |value| T.new(value) }.to_a
    }.to_a
  end

  def size : Int32
    @data.size
  end

  def unsafe_fetch(index : Int) : Array(T)
    @data.unsafe_fetch(index)
  end

  private def check_index_out_of_bounds(i, j)
    check_index_out_of_bounds(i, j) { raise IndexError.new }
  end

  private def check_index_out_of_bounds(i, j)
    i += height if i < 0
    j += width if j < 0
    if 0 <= i < height && 0 <= j < width
      {i, j}
    else
      yield
    end
  end

  def fetch(i : Int, j : Int, &)
    i, j = check_index_out_of_bounds(i, j) { return yield i, j }
    unsafe_fetch(i, j)
  end

  def fetch(i : Int, j : Int, default)
    fetch(i, j) { default }
  end

  def [](i : Int, j : Int) : T
    fetch(i, j) { raise IndexError.new }
  end

  def []?(i : Int, j : Int) : T?
    fetch(i, j, nil)
  end

  def unsafe_fetch(i : Int, j : Int) : T
    @data.unsafe_fetch(i).unsafe_fetch(j)
  end

  def +(other : self) : self
    raise IndexError.new unless height == other.height && width == other.width
    Matrix(T).new(height, width) { |i, j|
      unsafe_fetch(i, j) + other.unsafe_fetch(i, j)
    }
  end

  def -(other : self) : self
    raise IndexError.new unless height == other.height && width == other.width
    Matrix(T).new(height, width) { |i, j|
      unsafe_fetch(i, j) - other.unsafe_fetch(i, j)
    }
  end

  def *(other : self) : self
    raise IndexError.new unless width == other.height
    Matrix(T).new(height, other.width) { |i, j|
      (0...width).sum { |k| unsafe_fetch(i, k) * other.unsafe_fetch(k, j) }
    }
  end

  def **(k : Int) : self
    result = Matrix(T).identity(height)
    memo = Matrix.new(data)
    while k > 0
      result *= memo if k.odd?
      memo *= memo
      k >>= 1
    end
    result
  end

  def ==(other : Matrix) : Bool
    return false unless height == other.height && width == other.width
    data == other.data
  end

  def to_s(io) : Nil
    io << data
  end

  def inspect(io) : Nil
    io << "Matrix" << data
  end
end

# require "../../src/math/mint"
struct ModInt(MOD)
  def self.mod
    MOD
  end

  def self.zero
    new
  end

  def self.raw(value : Int64)
    result = new
    result.value = value
    result
  end

  macro [](*nums)
    Array({{@type}}).build({{nums.size}}) do |buffer|
      {% for num, i in nums %}
        buffer[{{i}}] = {{@type}}.new({{num}})
      {% end %}
      {{nums.size}}
    end
  end

  getter value : Int64

  private macro check_mod
    {% if MOD.is_a?(NumberLiteral) %}
      {% raise "can't instantiate ModInt(MOD) with MOD = #{MOD} (MOD must be positive)" unless MOD >= 1 %}
      {% raise "can't instantiate ModInt(MOD) with MOD = #{MOD.kind} (MOD must be Int64)" unless MOD.kind == :i64 %}
    {% else %}
      {% raise "can't instantiate ModInt(MOD) with MOD = #{MOD.class_name.id} (MOD must be an integer)" %}
    {% end %}
  end

  def initialize
    check_mod
    @value = 0i64
  end

  def initialize(value)
    check_mod
    @value = value.to_i64 % MOD
  end

  def initialize(m : ModInt(MOD2)) forall MOD2
    {% raise "Can't create ModInt(#{MOD}) from ModInt(#{MOD2})" if MOD != MOD2 %}
    check_mod
    @value = m.value
  end

  protected def value=(value : Int64)
    @value = value
  end

  def self.scan(scanner, io : IO) : self
    new scanner.i64(io)
  end

  def ==(m : ModInt(MOD2)) forall MOD2
    {% raise "Can't compare ModInt(#{MOD}) and ModInt(#{MOD2})" if MOD != MOD2 %}
    value == m.value
  end

  def ==(m : Int)
    value == m
  end

  def + : self
    self
  end

  def - : self
    self.class.raw(value != 0 ? MOD &- value : 0i64)
  end

  def +(v)
    self + self.class.new(v)
  end

  def +(m : self)
    x = value &+ m.value
    x &-= MOD if x >= MOD
    self.class.raw(x)
  end

  def -(v)
    self - self.class.new(v)
  end

  def -(m : self)
    x = value &- m.value
    x &+= MOD if x < 0
    self.class.raw(x)
  end

  def *(v)
    self * self.class.new(v)
  end

  def *(m : self)
    self.class.new(value &* m.value)
  end

  def /(v)
    self / self.class.new(v)
  end

  def /(m : self)
    raise DivisionByZeroError.new if m.value == 0
    a, b, u, v = m.value, MOD, 1i64, 0i64
    while b != 0
      t = a // b
      a &-= t &* b
      a, b = b, a
      u &-= t &* v
      u, v = v, u
    end
    self.class.new(value &* u)
  end

  def //(v)
    self / v
  end

  def **(exponent : Int)
    t, res = self, self.class.raw(1i64)
    while exponent > 0
      res *= t if exponent & 1 == 1
      t *= t
      exponent >>= 1
    end
    res
  end

  {% for op in %w[< <= > >=] %}
    def {{op.id}}(other)
      raise NotImplementedError.new({{op}})
    end
  {% end %}

  def inv
    self.class.raw(1) // self
  end

  def succ
    self.class.raw(value != MOD &- 1 ? value &+ 1 : 0i64)
  end

  def pred
    self.class.raw(value != 0 ? value &- 1 : MOD &- 1)
  end

  def abs
    self
  end

  def abs2
    self * self
  end

  def to_i64 : Int64
    value
  end

  def to_s(io : IO) : Nil
    value.to_s(io)
  end

  def inspect(io : IO) : Nil
    value.inspect(io)
  end
end

struct Int
  def to_m(type : M.class) forall M
    M.new(self)
  end
end

class String
  def to_m(type : M.class) forall M
    M.new(self)
  end
end

alias Mint = ModInt(1000000007i64)
alias Mint2 = ModInt(998244353i64)

private M = Matrix[[1, 2], [3, 4]]
i_j_value = [
  {0, 0, 1}, {0, 1, 2}, {1, 0, 3}, {1, 1, 4},
  {-2, -2, 1}, {-2, -1, 2}, {-1, -2, 3}, {-1, -1, 4},
]
out_of_range = [
  {0, 2}, {2, 0}, {2, 2},
  {-3, 0}, {0, -3}, {-3, -3},
]

describe Matrix do
  it ".identity" do
    Matrix(Int32).identity(1).should eq Matrix[[1]]
    Matrix(Int32).identity(2).should eq Matrix[[1, 0], [0, 1]]
    Matrix(Int32).identity(3).should eq Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
    expect_raises(ArgumentError) { Matrix(Int32).identity(-1) }
  end

  it "[]" do
    M.should eq Matrix.new [[1, 2], [3, 4]]
  end

  it ".from" do
    Matrix(Int64).from([1, 2], [3, 4]).should eq Matrix[[1i64, 2i64], [3i64, 4i64]]
    Matrix(Int64).from({1, 2}, {3, 4}).should eq Matrix[[1i64, 2i64], [3i64, 4i64]]
    Matrix(Int32).from(%w[1 2], %w[3 4]).should eq Matrix[[1, 2], [3, 4]]
    Matrix(Int32).from(1..2, 3..4).should eq Matrix[[1, 2], [3, 4]]
  end

  describe ".new" do
    it "receives initial value" do
      Matrix.new(1, 1, 0).should eq Matrix[[0]]
      Matrix.new(2, 3, 0).should eq Matrix[[0, 0, 0], [0, 0, 0]]
      expect_raises(ArgumentError) { Matrix.new(-1, 1, 0) }
      expect_raises(ArgumentError) { Matrix.new(1, -1, 0) }
    end

    it "receives block" do
      Matrix({Int32, Int32}).new(3, 3) { |i, j| {i, j} }.should eq Matrix[
        [{0, 0}, {0, 1}, {0, 2}],
        [{1, 0}, {1, 1}, {1, 2}],
        [{2, 0}, {2, 1}, {2, 2}],
      ]
      expect_raises(ArgumentError) { Matrix(Int32).new(-1, 1) { 0 } }
      expect_raises(ArgumentError) { Matrix(Int32).new(1, -1) { 0 } }
    end

    it "receives Array" do
      Matrix.new([[1, 2], [3, 4]]).should eq Matrix[[1, 2], [3, 4]]
      expect_raises(ArgumentError) { Matrix.new [[1, 2], [3]] }
    end
  end

  it "#fetch(i, j, &block)" do
    i_j_value.each do |i, j, value|
      M.fetch(i, j) { nil }.should eq value
    end
    out_of_range.each do |i, j|
      M.fetch(i, j) { |i, j| [i, j] }.should eq [i, j]
    end
  end

  it "#fetch(i, j, default)" do
    i_j_value.each do |i, j, value|
      M.fetch(i, j, nil).should eq value
    end
    out_of_range.each do |i, j|
      M.fetch(i, j, nil).should be_nil
    end
  end

  it "#[](i, j)" do
    i_j_value.each do |i, j, value|
      M[i, j].should eq value
    end
    out_of_range.each do |i, j|
      expect_raises(IndexError) { M[i, j] }
    end
  end

  it "#[]?(i, j)" do
    i_j_value.each do |i, j, value|
      M[i, j]?.should eq value
    end
    out_of_range.each do |i, j|
      M[i, j]?.should be_nil
    end
  end

  it "#unsafe_fetch(i, j)" do
    M.unsafe_fetch(0, 0).should eq 1
    M.unsafe_fetch(0, 1).should eq 2
    M.unsafe_fetch(1, 0).should eq 3
    M.unsafe_fetch(1, 1).should eq 4
  end

  it "#+(other)" do
    (Matrix[[1, 2], [3, 4]] + Matrix[[5, 6], [7, 8]]).should eq Matrix[[6, 8], [10, 12]]
    expect_raises(IndexError) { Matrix[[1, 2], [3, 4]] + Matrix[[1], [2]] }
  end

  it "#-(other)" do
    (Matrix[[5, 6], [7, 8]] - Matrix[[1, 2], [3, 4]]).should eq Matrix[[4, 4], [4, 4]]
    expect_raises(IndexError) { Matrix[[1, 2], [3, 4]] - Matrix[[1], [2]] }
  end

  it "#*(other)" do
    a = Matrix[[1, 2], [3, 4], [5, 6]]
    b = Matrix[[7, 8, 9], [10, 11, 12]]
    c = Matrix[[27, 30, 33], [61, 68, 75], [95, 106, 117]]
    (a * b).should eq c
    expect_raises(IndexError) { a * a }
    expect_raises(IndexError) { b * b }
  end

  it "#**(k)" do
    a1 = Matrix[[1, 2], [3, 4]]
    (a1 ** 0).should eq Matrix(Int32).identity(2)
    (a1 ** 1).should eq a1
    (a1 ** 2).should eq a1 * a1
    (a1 ** 3).should eq a1 * a1 * a1

    m1 = Matrix(Mint).from [1, 2], [3, 4]
    m2 = Matrix(Mint).from [414846427, 59557274], [89335911, 504182338]
    (m1 ** (10i64**18)).should eq m2
  end

  it "#to_s" do
    M.to_s.should eq "[[1, 2], [3, 4]]"
  end

  it "#inspect" do
    M.inspect.should eq "Matrix[[1, 2], [3, 4]]"
  end
end
Back to top page