From: Panagiotis Atmatzidis Date: 2012-09-09T02:39:25+09:00 Subject: Re: Using ruby for scientific computing Hello, On 8 Σεπ 2012, at 18:06 , Olivier Saut wrote: > Hi all, > > For pedagogic purposes, I am trying to solve simple partial differential > equations (say a diffusion equation on a regular mesh in 2D) with Ruby. > Using the NArray gem, I have built a satisfactory tool from an interface > (and ease of use) point of view but the performance are suboptimal. Is the code available on a public repo? > I am now wondering what is the right way to improve my code > performance-wise. I am doing a lot of matrix-vector and vector-vector > products. > > From 2 2D NArrays describing two scalar fields, dot-product is implemented as : > > def dot_product(vec_a, vec_b) > dot_product=(vec_a*vec_b">vec_a*vec_b).sum > end > > and as I am working with sparse matrices, (for the time being) a matrix > is an Array of Hashes. Each key of the hash contains the index of the > non-vanishing coefficients (the value of this coefficient is the value > of the Hash) of the matrix. For the identity matrix it gives > > def identity(sizeX, sizeY) > mat=[] > (0..sizeX*sizeY">sizeX*sizeY).each { |ind| > mat << {ind=>1.0} > } > mat > end > > > Matrix vector product is then defined as > > def matrix_vector_product(mat, vector) > ff=NArray.float(100,100) > (0..100*100).each { |ind| > matrix[ind].each do |key,value| > ff[[ind]]+=value*vector[[key]][0] > end > } > ff > end > > I am wondering what is the best way to tackle this performance problem : > 1. Improve the ruby code (and how? :) Should I avoid using hashes? > 2. Write an extension to perform these operations in C. BTW is there a > way to access directly NArray data (without copy) from a C extension? > 3. Use a wrapper like Ruby-lapack > ( > https://rubyforge.org/projects/ruby-lapack/ > ). > > What do you think? > > Thanks for your help ! > > -- > Olivier Saut > > osaut@airpost.net > - Olivier > > Panagiotis Atmatzidis ----------------------------- Pharmacy Student at VFU email4lists: ml@convalesco.org More info: http://about.me/atmosx The wise man said: "Never argue with an idiot, he brings you down to his level and beat you with experience."