forked from romanbsd/rb-gsl
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
= examples/vector/*.rb, examples/matrix/*.rb
* Modified vector and matrix example scripts to use irb/xmp "example printer" to better show usage and results. * Added comments to examples scripts to provide better annotation of examples. = examples/matrix/a.dat, examples/matrix/b.dat * Added data files a.dat and b.dat as created by examples/matrix/write.rb and used by examples/matrix/read.rb. git-svn-id: http://rb-gsl.rubyforge.org/svn/trunk/rb-gsl@81 6e764f74-f39f-46f8-8c54-8307d62afe8d
- Loading branch information
davidm
committed
Nov 14, 2008
1 parent
9ba482a
commit 760ef8c
Showing
47 changed files
with
1,093 additions
and
375 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -1,23 +1,45 @@ | ||
#!/usr/bin/env ruby | ||
require("gsl") | ||
# Turn on warnings | ||
$-w = true | ||
|
||
require 'irb/xmp' | ||
require 'gsl' | ||
|
||
# Apparently, IRB::Frame has a bug that prevents the defaults from working, so | ||
# an XMP instance must be created explicitly this way instead of using the | ||
# otherwise convenient xmp method. | ||
XMP.new(IRB::Frame.top(-1)).puts <<END | ||
# Create to 3x3 matrices | ||
a = GSL::Matrix.alloc([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 3) | ||
b = GSL::Matrix.alloc([6, 7, 8], [2, 3, 4], [3, 4, 5]) | ||
p a + b | ||
# Add together to create new matrix | ||
a + b | ||
# Add b to a, modifying a | ||
a += b | ||
p a | ||
# Show that a is modified | ||
a | ||
# Subtract b from a, modifying a | ||
a -= b | ||
p a | ||
p a + 2 | ||
# Add 2 to all elements of a, creating new matrix | ||
a + 2 | ||
p 2 + a | ||
# Another way to add 2 to all elements of a, creating new matrix | ||
2 + a | ||
# Add 2 to all elements of a, modifying a | ||
a += 2 | ||
p a | ||
# Show that a is modified | ||
a | ||
# Subtract 2 from all elements of a, modifying a | ||
a -= 2 | ||
p a | ||
# Show that a is modified | ||
a | ||
END |
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,4 @@ | ||
5 | ||
6 | ||
7 | ||
8 |
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 |
---|---|---|
@@ -1,13 +1,31 @@ | ||
#!/usr/bin/env ruby | ||
require("gsl") | ||
# Turn on warnings | ||
$-w = true | ||
|
||
require 'irb/xmp' | ||
require 'gsl' | ||
|
||
# Apparently, IRB::Frame has a bug that prevents the defaults from working, so | ||
# an XMP instance must be created explicitly this way instead of using the | ||
# otherwise convenient xmp method. | ||
XMP.new(IRB::Frame.top(-1)).puts <<END | ||
# Create a 2x2 Matrix::Int and a 2x3 Matrix::Int | ||
a = GSL::Matrix::Int[1..4, 2, 2] | ||
b = GSL::Matrix::Int[5..10, 2, 3] | ||
p a.horzcat(b) | ||
p GSL::Matrix::Int.horzcat(a, b) | ||
# Concatenate them horizontally using Matrix::Int#horzcat | ||
a.horzcat(b) | ||
# Concatenate them horizontally using Matrix::Int.horzcat | ||
GSL::Matrix::Int.horzcat(a, b) | ||
# Create a 2x2 Matrix::Int and a 3x2 Matrix::Int | ||
a = GSL::Matrix::Int[1..4, 2, 2] | ||
b = GSL::Matrix::Int[5..10, 3, 2] | ||
p b | ||
p a.vertcat(b) | ||
p GSL::Matrix::Int.vertcat(a, b) | ||
# Concatenate them vertically using Matrix::Int#vertcat | ||
a.vertcat(b) | ||
# Concatenate them vertically using Matrix::Int.vertcat | ||
GSL::Matrix::Int.vertcat(a, b) | ||
END |
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 |
---|---|---|
@@ -1,13 +1,24 @@ | ||
#!/usr/bin/env ruby | ||
require("gsl") | ||
# Turn on warnings | ||
$-w = true | ||
|
||
require 'irb/xmp' | ||
require 'gsl' | ||
|
||
# Apparently, IRB::Frame has a bug that prevents the defaults from working, so | ||
# an XMP instance must be created explicitly this way instead of using the | ||
# otherwise convenient xmp method. | ||
XMP.new(IRB::Frame.top(-1)).puts <<END | ||
# Create two 3x3 matrices | ||
m = GSL::Matrix.alloc(1..9, 3, 3) | ||
n = GSL::Matrix.alloc(11..19, 3, 3) | ||
# Get Vector::Col::View for column 1 of Matrix a | ||
a = m.col(1) | ||
# Get Vector::Col::View for column 2 of Matrix b | ||
b = n.col(2) | ||
# Create new Matrix from Vector::Col::Views | ||
c = GSL::Matrix[a, b] | ||
p m | ||
p n | ||
p c | ||
END |
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 |
---|---|---|
@@ -1,29 +1,41 @@ | ||
#!/usr/bin/env ruby | ||
require("gsl") | ||
# Turn on warnings | ||
$-w = true | ||
|
||
m = GSL::Matrix::Complex.alloc(3, 3) | ||
require 'irb/xmp' | ||
require 'gsl' | ||
|
||
m.set(1, 2, 3, 5.6) | ||
# Apparently, IRB::Frame has a bug that prevents the defaults from working, so | ||
# an XMP instance must be created explicitly this way instead of using the | ||
# otherwise convenient xmp method. | ||
XMP.new(IRB::Frame.top(-1)).puts <<END | ||
# Create 3x3 Matrix::Complex mz | ||
mz = GSL::Matrix::Complex.alloc(3, 3) | ||
m.print | ||
# Set element at row 1, column 2 to 3+5.6i | ||
mz.set(1, 2, GSL::Complex[3, 5.6]) | ||
a = m.get(1, 2) | ||
p a | ||
p a.class | ||
# Get element at row 1, column 2 | ||
a = mz.get(1, 2) | ||
m2 = m.submatrix(1, 1, 2, 2) | ||
p m2 | ||
m2.print | ||
# Create Matrix::Complex::View of mz | ||
# starting at row 1, column 1 and | ||
# spanning 2 rows and 2 columns | ||
mzv = mz.submatrix(1, 1, 2, 2) | ||
row = m.row(1) | ||
p row | ||
col = m.col(2) | ||
p col | ||
# Create a Vector::Complex::View for row 1 of mz | ||
row = mz.row(1) | ||
m.each_row do |v| | ||
# Create a Vector::Complex::Col::View for column 2 of mz | ||
col = mz.col(2) | ||
# Iterate through rows of mz | ||
mz.each_row do |v| | ||
p v | ||
end | ||
m.each_col do |v| | ||
# Iterate through columns of mz | ||
mz.each_col do |v| | ||
p v | ||
end | ||
END |
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 |
---|---|---|
@@ -1,9 +1,29 @@ | ||
#!/usr/bin/env ruby | ||
require("gsl") | ||
p m = GSL::Matrix[[1, 2, 3, 4, 5, 6, 7, 8 ,0], 3, 3] | ||
puts("m.det = #{m.det}") | ||
puts("m.trace = #{m.trace}") | ||
# Turn on warnings | ||
$-w = true | ||
|
||
p m.to_complex.det | ||
p m.to_complex.trace | ||
require 'irb/xmp' | ||
require 'gsl' | ||
|
||
# Apparently, IRB::Frame has a bug that prevents the defaults from working, so | ||
# an XMP instance must be created explicitly this way instead of using the | ||
# otherwise convenient xmp method. | ||
XMP.new(IRB::Frame.top(-1)).puts <<END | ||
# Crete test matrix m | ||
m = GSL::Matrix[[1, 2, 3, 4, 5, 6, 7, 8 ,0], 3, 3] | ||
# Calculate determinant of m | ||
m.det | ||
# Calculate trace of m (sum of diagonal elements) | ||
m.trace | ||
# Convert to Matrix::Complex mz | ||
mz = m.to_complex | ||
# Calulate determinant of mz | ||
mz.det | ||
# Calculate trace of mz (sum of diagonal elements) | ||
mz.trace | ||
END |
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 |
---|---|---|
@@ -1,6 +1,23 @@ | ||
#!/usr/bin/env ruby | ||
require("gsl") | ||
p GSL::Matrix.diagonal(1..3) | ||
p GSL::Matrix.diagonal([1, 2, 3]) | ||
p GSL::Matrix.diagonal([1, 2, 3].to_gv) | ||
p GSL::Matrix.diagonal(1, 2, 3) | ||
# Turn on warnings | ||
$-w = true | ||
|
||
require 'irb/xmp' | ||
require 'gsl' | ||
|
||
# Apparently, IRB::Frame has a bug that prevents the defaults from working, so | ||
# an XMP instance must be created explicitly this way instead of using the | ||
# otherwise convenient xmp method. | ||
XMP.new(IRB::Frame.top(-1)).puts <<END | ||
# Create matrix with diagonal given by Range | ||
GSL::Matrix.diagonal(1..3) | ||
# Create matrix with diagonal given by Array | ||
GSL::Matrix.diagonal([1, 2, 3]) | ||
# Create matrix with diagonal given by GSL::Vector | ||
GSL::Matrix.diagonal(GSL::Vector.indgen(3,1)) | ||
# Create matrix with diagonal given by individual elements | ||
GSL::Matrix.diagonal(1, 2, 3) | ||
END |
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 |
---|---|---|
@@ -1,14 +1,31 @@ | ||
#!/usr/bin/env ruby | ||
require("gsl") | ||
# Turn on warnings | ||
$-w = true | ||
|
||
require 'irb/xmp' | ||
require 'gsl' | ||
|
||
# Apparently, IRB::Frame has a bug that prevents the defaults from working, so | ||
# an XMP instance must be created explicitly this way instead of using the | ||
# otherwise convenient xmp method. | ||
XMP.new(IRB::Frame.top(-1)).puts <<END | ||
# Create 3x3 Hilbert matrix m | ||
m = GSL::Matrix.hilbert(3) | ||
p m | ||
# Compute inverse of m | ||
invm = m.inv | ||
# Create inverse of 3x3 Hilbert matrix directly | ||
invm2 = GSL::Matrix.invhilbert(3) | ||
p invm | ||
# Show that both inverse matrices are inverses of m | ||
m*invm | ||
m*invm2 | ||
# Show that the two inverse matrices are equal | ||
# to absolute accuracy eps = 1e-10 | ||
invm == invm2 | ||
p m*invm | ||
p m*invm2 | ||
p invm == invm2 | ||
# Show that they may not be exactly equal | ||
invm - invm2 | ||
END |
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 |
---|---|---|
@@ -1,9 +1,19 @@ | ||
#!/usr/bin/env ruby | ||
require("gsl") | ||
# Turn on warnings | ||
$-w = true | ||
|
||
require 'irb/xmp' | ||
require 'gsl' | ||
|
||
# Apparently, IRB::Frame has a bug that prevents the defaults from working, so | ||
# an XMP instance must be created explicitly this way instead of using the | ||
# otherwise convenient xmp method. | ||
XMP.new(IRB::Frame.top(-1)).puts <<END | ||
# Create test matrix | ||
m = GSL::Matrix.alloc([1, 2, 3, 4, 5, 6, 7, 8 ,9], 3, 3) | ||
# Iterate through columns | ||
m.each_col do |v| | ||
p v | ||
end | ||
|
||
END |
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 |
---|---|---|
@@ -1,45 +1,57 @@ | ||
#!/usr/bin/env ruby | ||
require("gsl") | ||
# Turn on warnings | ||
$-w = true | ||
|
||
m = GSL::Matrix.alloc([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 3) | ||
require 'irb/xmp' | ||
require 'gsl' | ||
|
||
p m | ||
# Apparently, IRB::Frame has a bug that prevents the defaults from working, so | ||
# an XMP instance must be created explicitly this way instead of using the | ||
# otherwise convenient xmp method. | ||
XMP.new(IRB::Frame.top(-1)).puts <<END | ||
# Create test matrix m | ||
m = GSL::Matrix.alloc([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 3) | ||
m.set([6,5, 6], [4, 5,7], [8, 5, 21]) | ||
m.print | ||
# Set rows of m from Arrays | ||
m.set([6, 5, 6], [4, 5, 7], [8, 5, 21]) | ||
m.set_col(1, [12, 3, 55].to_gv) | ||
#m.print | ||
# Set column 1 of m from GSL::Vector | ||
m.set_col(1, GSL::Vector[12, 3, 55]) | ||
# Create transpose of m | ||
m2 = m.transpose | ||
m.print | ||
m2.print | ||
# Swap rows 1 and 2 of m | ||
m.swap_rows(1, 2) | ||
m.print | ||
# Create Vector::Col::View for column 0 of m | ||
v = m.col(0) | ||
p v.to_a | ||
# Create Vector::View of diagonal of m | ||
m.diagonal | ||
# Create Array containing diagonal elements of m | ||
m.diagonal.to_a | ||
# Create another test matrix m | ||
m = GSL::Matrix.alloc([1, 2, 3], [6, 5, 4], [7, 8, 1]) | ||
p m.get(1, 2) | ||
m.print | ||
m3 = m.LU_decomp | ||
p m3 | ||
# Get element at row 1, column 2 | ||
m.get(1, 2) | ||
# Perform LU decomposition of m | ||
lu, perm, sign = m.LU_decomp | ||
m3 = GSL::Matrix.alloc(5, 5) | ||
# Create 5x5 zero matrix m5 | ||
m5 = GSL::Matrix.alloc(5, 5) | ||
# Initialize elements of m5 | ||
for i in 0...5 do | ||
for j in 0...5 do | ||
val = 0.5*(i+0.4)*(j+1.2) | ||
m3.set(i, j, val) | ||
m5[i, j] = 0.5*(i+0.4)*(j+1.2) | ||
end | ||
end | ||
m3.print | ||
|
||
|
||
|
||
# Show m5 | ||
m5 | ||
END |
Oops, something went wrong.