137 
137 
rows.each do row

138 
138 
Matrix.Raise ErrDimensionMismatch, "element size differs (#{row.size} should be #{size})" unless row.size == size

139 
139 
end

140 

new rows


140 
new rows, size

141 
141 
end

142 
142 

143 
143 
#

...  ...  
223 
223 
#

224 
224 
def Matrix.column_vector(column)

225 
225 
column = Matrix.convert_to_array(column)

226 

new [column].transpose


226 
new [column].transpose, 1

227 
227 
end

228 
228 

229 
229 
#

230 
230 
# Matrix.new is private; use Matrix.rows, columns, [], etc... to create.

231 
231 
#

232 

def initialize(rows)


232 
def initialize(rows, column_size = rows[0].size)

233 
233 
# No checking is done at this point. rows must be an Array of Arrays.


234 
# column_size must be the size of the first row, if there is one,


235 
# otherwise it *must* be specified and can be any integer >= 0

234 
236 
@rows = rows


237 
@column_size = column_size

235 
238 
end

236 
239 

237 

def new(rows) # :nodoc:

238 

Matrix.send(:new, rows) # bypass privacy of Matrix.new


240 
def new(rows, column_size = rows[0].size) # :nodoc:


241 
Matrix.send(:new, rows, column_size) # bypass privacy of Matrix.new

239 
242 
end

240 
243 
private :new

241 
244 

...  ...  
265 
268 
#

266 
269 
# Returns the number of columns.

267 
270 
#

268 

def column_size

269 

@rows[0].size

270 

end


271 
attr_reader :column_size

271 
272 

272 
273 
#

273 
274 
# Returns row vector number +i+ of the matrix as a Vector (starting at 0 like

...  ...  
308 
309 
#

309 
310 
def collect(&block) # :yield: e

310 
311 
rows = @rows.collect{row row.collect(&block)}

311 

new(rows)


312 
new rows, column_size

312 
313 
end

313 
314 
alias map collect

314 
315 

...  ...  
339 
340 
rows = @rows[from_row, size_row].collect{row

340 
341 
row[from_col, size_col]

341 
342 
}

342 

new(rows)


343 
new rows, column_size  from_col

343 
344 
end

344 
345 

345 
346 
#

...  ...  
403 
404 
# There should be no good reason to do this since Matrices are immutable.

404 
405 
#

405 
406 
def clone

406 

new(@rows.map{row row.dup})


407 
new @rows.map{row row.dup}, column_size

407 
408 
end

408 
409 

409 
410 
#

...  ...  
431 
432 
e * m

432 
433 
}

433 
434 
}

434 

return new(rows)


435 
return new rows, column_size

435 
436 
when Vector

436 
437 
m = Matrix.column_vector(m)

437 
438 
r = self * m

...  ...  
446 
447 
end

447 
448 
}

448 
449 
}

449 

return new(rows)


450 
return new rows, m.column_size

450 
451 
else

451 
452 
x, y = m.coerce(self)

452 
453 
return x * y

...  ...  
478 
479 
self[i, j] + m[i, j]

479 
480 
}

480 
481 
}

481 

new(rows)


482 
new rows, column_size

482 
483 
end

483 
484 

484 
485 
#

...  ...  
506 
507 
self[i, j]  m[i, j]

507 
508 
}

508 
509 
}

509 

new(rows)


510 
new rows, column_size

510 
511 
end

511 
512 

512 
513 
#

...  ...  
523 
524 
e / other

524 
525 
}

525 
526 
}

526 

return new(rows)


527 
return new rows, column_size

527 
528 
when Matrix

528 
529 
return self * other.inverse

529 
530 
else

...  ...  
822 
823 
# 2 4 6

823 
824 
#

824 
825 
def transpose

825 

new @rows.transpose


826 
new @rows.transpose, row_size

826 
827 
end

827 
828 
alias t transpose

828 
829 

...  ...  
887 
888 
# Overrides Object#to_s

888 
889 
#

889 
890 
def to_s

890 

"Matrix[" + @rows.collect{row

891 

"[" + row.collect{e e.to_s}.join(", ") + "]"

892 

}.join(", ")+"]"

893 

end

894 


895 

#

896 

# Overrides Object#inspect

897 

#

898 

def inspect

899 

"Matrix"+@rows.inspect


891 
if row_size == 0 && column_size > 0


892 
"Matrix.columns(#{Array.new(column_size, [])})"


893 
else


894 
"Matrix#{@rows}"


895 
end

900 
896 
end


897 
alias_method :inspect, :to_s

901 
898 

902 
899 
#

903 
900 
# Converts the obj to an Array. If copy is set to true
