lib/matrix_extensions.rb in matrix_extensions-0.0.3 vs lib/matrix_extensions.rb in matrix_extensions-0.0.4
- old
+ new
@@ -71,42 +71,39 @@
# create new matrix
self.rows(rows)
end
- # Removes trailing columns from a Matrix.
+ # Removes trailing columns from a Matrix (destructive).
# @param number_of_columns [Integer] number of trailing columns to be removed
- # @return [Matrix] matrix
+ # @return [Matrix] matrix consisting of of dropped columns
# @raise [ErrDimensionMismatch] if Matrix does not have enough columns for operation
def hpop(number_of_columns = 1)
Matrix.Raise ErrDimensionMismatch unless number_of_columns < self.column_count
- columns = []
- last_column_to_be_included = self.column_count - number_of_columns
- self.column_vectors.each_with_index do |column, index|
- break if index + 1 > last_column_to_be_included
- columns << column.to_a
+ dropped_columns = []
+ number_of_columns.times do
+ dropped_column = []
+ @rows.each {|r| dropped_column << r.pop}
+ dropped_columns << dropped_column
end
-
- Matrix.columns(columns)
+ @column_count -= number_of_columns
+
+ Matrix.columns(dropped_columns.reverse)
end
- # Removes trailing rows from a Matrix.
+ # Removes trailing rows from a Matrix (destructive).
# @param number_of_rows [Integer] number of trailing rows to be removed
- # @return [Matrix] matrix
+ # @return [Matrix] matrix consisting of of dropped rows
# @raise [ErrDimensionMismatch] if Matrix does not have enough rows for operation
def vpop(number_of_rows = 1)
Matrix.Raise ErrDimensionMismatch unless number_of_rows < self.row_count
- rows = []
- last_row_to_be_included = self.row_count - number_of_rows
- self.row_vectors.each_with_index do |row, index|
- break if index + 1 > last_row_to_be_included
- rows << row.to_a
- end
-
- Matrix.rows(rows)
- end
+ dropped_rows = []
+ number_of_rows.times { dropped_rows.unshift @rows.pop }
+
+ Matrix.rows(dropped_rows.reverse)
+ end
# Element-wise division.
# @param m [Matrix] matrix
# @return [Matrix] matrix
# @raise [ErrDimensionMismatch] if dimensions don't match
\ No newline at end of file