情報学部 | 菅沼ホーム | 目次 | 索引 |
require 'matrix'
require 'matrix' x = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] a = Matrix[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] # a = Matrix[[],[]]; a = x b = Matrix.rows(x) print "a ", a, "\n" # => a Matrix[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] b[1,0] = 7.0 # b[1][0] は許されない print "b ", b, "\n" # => b Matrix[[1.0, 2.0, 3.0], [7.0, 5.0, 6.0]]
require 'matrix' a = Matrix.diagonal(1, 2, 3) print a, "\n" # => Matrix[[1, 0, 0], [0, 2, 0], [0, 0, 3]] b = Matrix.scalar(3, 1.0) print b, "\n" # => Matrix[[1.0, 0, 0], [0, 1.0, 0], [0, 0, 1.0]]
require 'matrix' a = Matrix.I(3) print a, "\n" # => Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
require 'matrix' a = Matrix.zero(3) print a, "\n" # => Matrix[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
require 'matrix' a = Matrix[[1.0, 1.0], [1.0, 2.0]] b = Matrix[[2.0, -1.0], [-1.0, 1.0]] print a, "\n" # => Matrix[[1.0, 1.0], [1.0, 2.0]] print b, "\n" # => Matrix[[2.0, -1.0], [-1.0, 1.0]] print "a+b ", a+b, "\n" # => a+b Matrix[[3.0, 0.0], [0.0, 3.0]] print "a-b ", a-b, "\n" # => a-b Matrix[[-1.0, 2.0], [2.0, 1.0]] print "a*b ", a*b, "\n" # => a*b Matrix[[1.0, 0.0], [0.0, 1.0]] print "a/a ", a/a, "\n" # => a/a Matrix[[1.0, 0.0], [0.0, 1.0]] print "a*2.0 ", a*2.0, "\n" # => a*2.0 Matrix[[2.0, 2.0], [2.0, 4.0]] print "a/2.0 ", a/2.0, "\n" # => a/2.0 Matrix[[0.5, 0.5], [0.5, 1.0]]
require 'matrix' a = Matrix[[1.0, 1.0], [1.0, 2.0]] print a, "\n" # => Matrix[[1.0, 1.0], [1.0, 2.0]] print a.inv, "\n" # => Matrix[[2.0, -1.0], [-1.0, 1.0]]
require 'matrix' a = Matrix[[1.0, 1.0], [1.0, 2.0]] print a, "\n" # => Matrix[[1.0, 1.0], [1.0, 2.0]] print a ** 2, "\n" # => Matrix[[2.0, 3.0], [3.0, 5.0]]
require 'matrix' a = Matrix[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] print a, "\n" # => Matrix[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] print "row ", a.row_size, " column ", a.column_size, "\n" # => row 2 column 3
require 'matrix' a = Matrix[[1.0, 2.0], [3.0, 4.0]] print a, "\n" # => Matrix[[1.0, 2.0], [3.0, 4.0]] print "det ", a.det, " rank ", a.rank, " trace ", a.tr, "\n" # => det -2.0 rank 2 trace 5.0
require 'matrix' a = Matrix[[1.0, 2.0], [3.0, 4.0]] print a, "\n" # => Matrix[[1.0, 2.0], [3.0, 4.0]] print a.t, "\n" # => Matrix[[1.0, 3.0], [2.0, 4.0]]
情報学部 | 菅沼ホーム | 目次 | 索引 |