ruby-changes:58964
From: Hiroshi <ko1@a...>
Date: Sat, 30 Nov 2019 08:01:00 +0900 (JST)
Subject: [ruby-changes:58964] d82c541ae4 (master): Use simple exception classes instead of e2mmap
https://git.ruby-lang.org/ruby.git/commit/?id=d82c541ae4 From d82c541ae439b3c28ec71e9d6b839c2f304df273 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA <hsbt@r...> Date: Wed, 27 Nov 2019 11:48:04 +0900 Subject: Use simple exception classes instead of e2mmap diff --git a/lib/matrix.rb b/lib/matrix.rb index 6b3a2be..690f98f 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -12,19 +12,44 @@ https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L12 # Original Documentation:: Gavin Sinclair (sourced from <i>Ruby in a Nutshell</i> (Matsumoto, O'Reilly)) ## -require "e2mmap" - require_relative "matrix/version" module ExceptionForMatrix # :nodoc: - extend Exception2MessageMapper - def_e2message(TypeError, "wrong argument type %s (expected %s)") - def_e2message(ArgumentError, "Wrong # of arguments(%d for %d)") - - def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch") - def_exception("ErrNotRegular", "Not Regular Matrix") - def_exception("ErrOperationNotDefined", "Operation(%s) can\\'t be defined: %s op %s") - def_exception("ErrOperationNotImplemented", "Sorry, Operation(%s) not implemented: %s op %s") + class TypeError + def initialize(val) + "wrong argument type #{val} (expected #{val})" + end + end + + class ArgumentError + def initialize(val) + "Wrong # of arguments(#{val} for #{val})" + end + end + + class ErrDimensionMismatch < StandardError + def initialize + super("\#{self.name} dimension mismatch") + end + end + + class ErrNotRegular < StandardError + def initialize + super("Not Regular Matrix") + end + end + + class ErrOperationNotDefined < StandardError + def initialize(val) + super("Operation(#{val}) can\\'t be defined: #{val} op #{val}") + end + end + + class ErrOperationNotImplemented < StandardError + def initialize(val) + super("Sorry, Operation(#{val}) not implemented: #{val} op #{val}") + end + end end # @@ -265,7 +290,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L290 matrices.map!(&CoercionHelper.method(:coerce_to_matrix)) x = matrices.first matrices.each do |m| - Matrix.Raise ErrDimensionMismatch unless x.row_count == m.row_count && x.column_count == m.column_count + raise ErrDimensionMismatch unless x.row_count == m.row_count && x.column_count == m.column_count end rows = Array.new(x.row_count) do |i| @@ -374,12 +399,12 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L399 private def set_row_range(row_range, col, value) if value.is_a?(Vector) - Matrix.Raise ErrDimensionMismatch unless row_range.size == value.size + raise ErrDimensionMismatch unless row_range.size == value.size set_column_vector(row_range, col, value) elsif value.is_a?(Matrix) - Matrix.Raise ErrDimensionMismatch unless value.column_count == 1 + raise ErrDimensionMismatch unless value.column_count == 1 value = value.column(0) - Matrix.Raise ErrDimensionMismatch unless row_range.size == value.size + raise ErrDimensionMismatch unless row_range.size == value.size set_column_vector(row_range, col, value) else @rows[row_range].each{|e| e[col] = value } @@ -397,12 +422,12 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L422 value = if value.is_a?(Vector) value.to_a elsif value.is_a?(Matrix) - Matrix.Raise ErrDimensionMismatch unless value.row_count == 1 + raise ErrDimensionMismatch unless value.row_count == 1 value.row(0).to_a else Array.new(col_range.size, value) end - Matrix.Raise ErrDimensionMismatch unless col_range.size == value.size + raise ErrDimensionMismatch unless col_range.size == value.size @rows[row][col_range] = value end @@ -740,7 +765,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L765 # def cofactor(row, column) raise RuntimeError, "cofactor of empty matrix is not defined" if empty? - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? det_of_minor = first_minor(row, column).determinant det_of_minor * (-1) ** (row + column) @@ -754,7 +779,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L779 # -3 7 # def adjugate - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? Matrix.build(row_count, column_count) do |row, column| cofactor(column, row) end @@ -777,7 +802,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L802 raise ArgumentError, "exactly one the row or column arguments must be specified" end - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? raise RuntimeError, "laplace_expansion of empty matrix is not defined" if empty? unless 0 <= num && num < row_count @@ -800,7 +825,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L825 # Raises an error if matrix is not square. # def diagonal? - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? each(:off_diagonal).all?(&:zero?) end @@ -817,7 +842,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L842 # Raises an error if matrix is not square. # def hermitian? - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? each_with_index(:upper).all? do |e, row, col| e == rows[col][row].conj end @@ -835,7 +860,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L860 # Raises an error if matrix is not square. # def normal? - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? rows.each_with_index do |row_i, i| rows.each_with_index do |row_j, j| s = 0 @@ -853,7 +878,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L878 # Raises an error if matrix is not square. # def orthogonal? - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? rows.each_with_index do |row, i| column_count.times do |j| s = 0 @@ -871,7 +896,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L896 # Raises an error if matrix is not square. # def permutation? - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? cols = Array.new(column_count) rows.each_with_index do |row, i| found = false @@ -921,7 +946,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L946 # Raises an error if matrix is not square. # def symmetric? - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? each_with_index(:strict_upper) do |e, row, col| return false if e != rows[col][row] end @@ -933,7 +958,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L958 # Raises an error if matrix is not square. # def antisymmetric? - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? each_with_index(:upper) do |e, row, col| return false unless e == -rows[col][row] end @@ -946,7 +971,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L971 # Raises an error if matrix is not square. # def unitary? - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? rows.each_with_index do |row, i| column_count.times do |j| s = 0 @@ -1029,7 +1054,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1054 r = self * m return r.column(0) when Matrix - Matrix.Raise ErrDimensionMismatch if column_count != m.row_count + raise ErrDimensionMismatch if column_count != m.row_count rows = Array.new(row_count) {|i| Array.new(m.column_count) {|j| @@ -1053,7 +1078,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1078 def +(m) case m when Numeric - Matrix.Raise ErrOperationNotDefined, "+", self.class, m.class + raise ErrOperationNotDefined, "+", self.class, m.class when Vector m = self.class.column_vector(m) when Matrix @@ -1061,7 +1086,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1086 return apply_through_coercion(m, __method__) end - Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count + raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count rows = Array.new(row_count) {|i| Array.new(column_count) {|j| @@ -1080,7 +1105,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1105 def -(m) case m when Numeric - Matrix.Raise ErrOperationNotDefined, "-", self.class, m.class + raise ErrOperationNotDefined, "-", self.class, m.class when Vector m = self.class.column_vector(m) when Matrix @@ -1088,7 +1113,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1113 return apply_through_coercion(m, __method__) end - Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count + raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count rows = Array.new(row_count) {|i| Array.new(column_count) {|j| @@ -1136,7 +1161,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1161 # 0 -1 # def inverse - Matrix.Raise ErrDimensionMismatch unless square? + raise ErrDimensionMismatch unless square? self.class.I(row_count).send(:inverse_from, self) end alias_method (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/