Hi,
I am looking for the suitability of ruby for numerical applications. I am currently using MATLAB and Octave for this purpose (see www.octave.org, www.mathworks.com).
For vectors and matrices a single "*" or "/" operator is not sufficient to implement the necessary matrix/vector arithmetic in a convinient and consistent operator notation. In OOPS languages like C++ which support operator overloading, Matrix class libraries usually resort to a mixed functional/operator notation and assign the matrix product to the "*" and implement the dot-product by a function e.g as dot(A, B).
Besides product and division operators for matrix/vector operations it is necessary to introduce a transpose and hermite operator which is of no interest in scalar arithmetic. In MATLAB the transpose of a matrix X is expressed as X.' and the conjugate transpose is X'.
A simple and compact example of how this notation can be used is in computing the 2-norm of a vector:
x = 1:1:10 % Vector counting from 1 to 10
norm2 = sqrt( x*x' ) % use matrix multiplication to compute the squared sum
norm2 = sqrt(sum(x .* x)) % use elementwise multiplication
norm2 = sqrt(sum(x .^ 2)) % square element by element
In standart programming languages with an OOPS library this might look like:
x = [1 2 3 4 ... 10] ;
norm2 = sqrt( x*x.transpose()) ; // use matrix multiplication
norm2 = sqrt(sum(dot(x, x))) ; // use dot-product auxiliary function
While for such a simple example, there is not so much difference in readability, but for real world examples, the matrix operator notation makes the code more compact and readable. Conceptionally it lacks consistency, if the maybe more often used dot-product is implemented as function and the matrix product as operator.
My assumption is that is might be possible to extend ruby with reasonable effort to add the additional ".*" "./" and ".^2" binary operators and the transpose / conjugate transpose operators. For the standard ruby this function would just do the same as their scalar conterparts, however, when implementing a matrix class these additional operators can become alive and be used to implement the elementwise dot operations.
Aside from the operators a second concept is quite essential for a scripting matrix and vector class. This requires a new data-type to express strides.
Suppose one wants to access only every second element of a vector or reverse a vector, then usually this has to be done in a loop. Since looping over elements is prohibitively expensive in a scripting language, the preferred solution for doing such common indexing operation is to use strides. For ruby, it would be possible to define a stride class, which stores the index of the first, last and stepsize. Reversing a vector might look like:
vector x=stride(1, 3) // use a stride to construct a new vector from 1 to 3.
reverse_x = x( stride(3, 1, -1) )
More elegantly, if strides are natively supported by the scripting language, then the example could be implemented as
x = 1:3 ;
reverse_x = x(3:-1:1) ;
Has anybody implemented a patch to include dot-operators or index strides to ruby? Are there any plans / discussion to include MATLAB/Octave-alike features in the future?
Regards,
よろしくおねがいします
Roderick Koehle
References:
* http://en.wikipedia.org/wiki/Matlab
* http://en.wikipedia.org/wiki/IDL_programming_language
···
---------------------------------
Telefonieren Sie ohne weitere Kosten mit Ihren Freunden von PC zu PC!
Jetzt Yahoo! Messenger installieren!