-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added the Array.each_slice(n, {...}) method (same as in Ruby)
- Various optimizations inside the Array class.
- Loading branch information
trizen
committed
Dec 17, 2015
1 parent
6bc3074
commit 5e94052
Showing
3 changed files
with
175 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Reduced_row_echelon_form | ||
# | ||
|
||
func rref (Array m) { | ||
m.is_empty && return; | ||
var (lead, rows, cols) = (0, m.len, m[0].len); | ||
|
||
rows.range.each { |r| | ||
lead >= cols && return m; | ||
var i = r; | ||
|
||
while (!m[i][lead]) { | ||
++i == rows || next; | ||
i = r; | ||
++lead == cols && return m; | ||
} | ||
|
||
m[i, r] = m[r, i]; | ||
var lv = m[r][lead]; | ||
m[r] = (m[r] »/» lv); | ||
|
||
rows.range.each { |n| | ||
n == r && next; | ||
m[n] = (m[n] »-« (m[r] «*« m[n][lead])) | ||
} | ||
++lead; | ||
} | ||
return m | ||
} | ||
|
||
func say_it (message, array) { | ||
say "\n#{message}"; | ||
array.each { |row| | ||
say row.map { |n| " %5s" % n }.join | ||
} | ||
} | ||
|
||
var M = [ | ||
[ # base test case | ||
[ 1, 2, -1, -4 ], | ||
[ 2, 3, -1, -11 ], | ||
[ -2, 0, -3, 22 ], | ||
], | ||
[ # mix of number styles | ||
[ 3, 0, -3, 1 ], | ||
[ .5, 3/2, -3, -2 ], | ||
[ .2, 4/5, -1.6, .3 ], | ||
], | ||
[ # degenerate case | ||
[ 1, 2, 3, 4, 3, 1], | ||
[ 2, 4, 6, 2, 6, 2], | ||
[ 3, 6, 18, 9, 9, -6], | ||
[ 4, 8, 12, 10, 12, 4], | ||
[ 5, 10, 24, 11, 15, -4], | ||
], | ||
]; | ||
|
||
M.each { |matrix| | ||
var rat_matrix = matrix.map{.map{.to_r}}; | ||
say_it('Original Matrix', rat_matrix); | ||
say_it('Reduced Row Echelon Form Matrix', rref(rat_matrix)); | ||
say ''; | ||
} |