From: Ryan Davis Date: 2012-10-03T05:14:22+09:00 Subject: Re: 2D array strategies On Oct 2, 2012, at 11:49 , Thomas Luedeke wrote: > Since Ruby only does 1D arrays, is there a trick for an array of arrays? > Or is this hash territory? How do I do the 2D equivalent of using the > .push array command to add on more data to the original array? Are > there are any particular Ruby resources that I need to go read up on and > go practice in IRB? Unless your dimensions are quite huge, you might want to try out Matrix: % ri Matrix = MMaattrriixx  <<  OObbjjeecctt (from ruby core) ------------------------------------------------------------------------------ The Matrix class represents a mathematical matrix, and provides methods for creating special-case matrices (zero, identity, diagonal, singular, vector), operating on them arithmetically and algebraically, and determining their mathematical properties (trace, rank, inverse, determinant). Note that although matrices should theoretically be rectangular, this is not enforced by the class. Also note that the determinant of integer matrices may be incorrectly calculated unless you also require 'mathn'. This may be fixed in the future. == MMeetthhoodd  CCaattaalloogguuee To create a matrix: * Matrix[*rows] * Matrix.[](*rows) * Matrix.rows(rows, copy = true) * Matrix.columns(columns) * Matrix.diagonal(*values) * Matrix.scalar(n, value) * Matrix.scalar(n, value) * Matrix.identity(n) * Matrix.unit(n) * Matrix.I(n) * Matrix.zero(n) * Matrix.row_vector(row) * Matrix.column_vector(column) To access Matrix elements/columns/rows/submatrices/properties: * [](i, j) * #row_size * #column_size * #row(i) * #column(j) * #collect * #map * #minor(*param) Properties of a matrix: * #regular? * #singular? * #square? Matrix arithmetic: * *(m) * +(m) * -(m) * #/(m) * #inverse * #inv * ** Matrix functions: * #determinant * #det * #rank * #trace * #tr * #transpose * #t Conversion to other data types: * #coerce(other) * #row_vectors * #column_vectors * #to_a String representations: * #to_s * #inspect ------------------------------------------------------------------------------ = CCllaassss  mmeetthhooddss:: I [] column_vector columns diagonal identity new row_vector rows scalar unit zero = IInnssttaannccee  mmeetthhooddss:: * ** + - / == [] clone coerce collect column column_size column_vectors compare_by_row_vectors det determinant eql? hash init_rows inspect inv inverse inverse_from map minor rank regular? row row_size row_vectors singular? square? t to_a to_s tr trace transpose