Method: Rust::Matrix#initialize
- Defined in:
- lib/rust/core/types/matrix.rb
#initialize(data, row_names = nil, column_names = nil) ⇒ Matrix
Creates a new matrix with the given data (Ruby Matrix). Optionally, row_names and column_names can be specified.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rust/core/types/matrix.rb', line 38 def initialize(data, row_names = nil, column_names = nil) @data = data.clone @row_names = row_names @column_names = column_names if @data.is_a?(::Matrix) @data = @data.row_vectors.map { |v| v.to_a } end if self.flatten.size == 0 raise "Empty matrices are not allowed" else raise TypeError, "Expected array of array" unless @data.is_a?(Array) || @data[0].is_a?(Array) raise TypeError, "Only numeric matrices are supported" unless self.flatten.all? { |e| e.is_a?(Numeric) } raise "All the rows must have the same size" unless @data.map { |row| row.size }.uniq.size == 1 raise ArgumentError, "Expected row names #@row_names to match the number of rows in #{self.inspect}" if @row_names && @row_names.size != self.rows raise ArgumentError, "Expected column names #@column_names to match the number of columns in #{self.inspect}" if @column_names && @column_names.size != self.cols end end |