In order to handle linear algerbra in ruby, I create a package/library called "RubyAlg" to create matrix and handle common matrix operations through the created Matrix class. Below will introduce how to use this package and introduce basic operations among created Matrix object.
Matrix Creation
There are multiple ways to create Matrix object through alg/math/LinearAlgebra. Let's check a few examples:
- Create Matrix Object (1)
Or you can just do translation from Array object to Matrix object:
- Create Matrix Object (2)
There are some advanced ways to create Matrix object:
- Create Matrix Object (3)
Basic Matrix Operations
There are a number of basic operations that can be applied to modify matrices, called matrix addition, scalar multiplication, transposition, matrix multiplication, row operations, and submatrix.
- Addition, scalar multiplication and transposition
Main articles: Matrix addition, Scalar multiplication and Transpose. Let's check how to do them in Matrix object:
- Matrix multiplication
Multiplication of two matrices is defined if and only if the number of columns of the left matrix is the same as the number of rows of the right matrix. If A is an m-by-n matrix and B is an n-by-p matrix, then their matrix product AB is the m-by-p matrix whose entries are given by dot product of the corresponding row of A and the corresponding column of B:
Let's check how to do them in Matrix object:
- Submatrix
A submatrix of a matrix is obtained by deleting any collection of rows and/or columns. For example, for the following 3-by-4 matrix, we can construct a 2-by-3 submatrix by removing row 3 and column 2:
Let's check how to do them in Matrix object:
Performance Concern
So far this package doesn't do any optimization (Multipthreading etc). Maybe in the future I will try to focus on this part when I have time. But it is only a PoC and help me to do some matrix operations more directly and easily.
Supplement
* SciRuby - Tools for Scientific Computing in Ruby
沒有留言:
張貼留言