项目作者: mejszin

项目描述 :
Ruby implementation of a matrix and vector library
高级语言: Ruby
项目地址: git://github.com/mejszin/matrices.git
创建时间: 2019-05-24T13:03:31Z
项目社区:https://github.com/mejszin/matrices

开源协议:

下载


matrices

Usage

  1. require 'matrices.rb'
  1. a = Matrix.new [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  2. b = Matrix.new [[1, 2, 3], [4, 5, 6]]
  3. c = Matrix.new [[7, 8], [9, 10], [11, 12]]
  4. d = Matrix.new [[1, -2, 3], [-4, 5, 6], [7, 8, -9]]
  5. e = Matrix.new [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
  1. u = Vector.new [8, -7]
  2. v = Vector.new [5, -2, 18]
  3. w = Vector.new [8, 13, -7]
  4. x = Vector.new [0, 0.2, 0.8, 0]
  5. y = Vector.new [0.4, 0.3, 0, 0]
  6. z = Vector.new [0, 1, 0]
  1. a.add(c.multiply(b))
  2. # => [[40, 56, 72], [53, 73, 93], [66, 90, 114]]
  1. c.multiply(b).subtract(a)
  2. # => [[38, 52, 66], [45, 63, 81], [52, 74, 96]]
  1. b.multiply(c)
  2. # => [[58, 64], [139, 154]]
  1. b.scale(3.5)
  2. # => [[3.5, 7.0, 10.5], [14.0, 17.5, 21.0]]
  1. d.determinant()
  2. # => -306.0
  1. a.submatrix(1, 0)
  2. # => [[2, 3], [8, 9]]
  1. a.minor(1, 0)
  2. # => -6.0
  1. a.adjoint()
  2. # => [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
  1. c.transpose()
  2. # => [[7, 9, 11], [8, 10, 12]]
  1. e.minors()
  2. # => [[2.0, 2.0, 2.0], [-2.0, 3.0, 3.0], [0.0, -10.0, 0.0]]
  1. e.cofactors()
  2. # => [[2.0, -2.0, 2.0], [2.0, 3.0, -3.0], [0.0, 10.0, 0.0]]
  1. e.inverse()
  2. # => [[0.2, 0.2, 0.0], [-0.2, 0.3, 1.0], [0.2, -0.3, 0.0]]
  1. a.swap_rows(1, 2)
  2. # => [[1, 2, 3], [7, 8, 9], [4, 5, 6]]
  1. a.trace()
  2. # => 15
  1. d.leading_diagonal()
  2. # => -45
  1. a.is_square?
  2. # => true
  1. empty_matrix(4, 3)
  2. # => [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
  1. unit_matrix(3)
  2. # => [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
  1. empty_vector(3)
  2. # => [0, 0, 0]
  1. x.is_vector?
  2. # => true
  1. x.is_stochastic?
  2. # => true
  3. y.is_stochastic?
  4. # => false
  1. a.multiply(z)
  2. # => [[2, 5, 8]]
  1. u.magnitude.round(2)
  2. # => 10.63
  1. u.direction.round(2)
  2. # => -0.72
  1. v.dimension()
  2. # => 3
  1. v.dot(w)
  2. # => -112
  1. v.similarity(w).round(3)
  2. # => -0.355
  1. v.cross(w)
  2. # => [-220, -179, 30]
  1. u.parallel?(Vector.new [4,-3.5])
  2. # => true
  1. y.unit()
  2. # => [0.8, 0.6, 0, 0]