# Simple math operations on arrays

Hi,

Again a very basic problem, I need to create arrays with numerical
values, and do very simple math operations on each elements. I’ve
tried to install ruby-gsl as suggested but I don’t have admin rights
on the machine. Here is my try:

puts longAxis
puts shortAxis

the result is wrong, and it all seems over-sophisticated to me. I
must have missed a basic technique.

I need three arrays: ‘aspectRatio’ is just [0.5, 0.6, 0.7, 0.8, 0.9,
1.0], but I want to construct it as a range (call me lazy). The
second is constructed doing the same calculation on every element of
‘aspectRatio’. The third one needs to access elements of the same
index from the previous two arrays, and multiply them together to
form the new array.

Here is the result I’d expect:

0.035717
0.037954
0.039956
0.041774
0.043447
0.045000

and the result I get:

0.045
0.045
0.045

Best regards

baptiste

On 7/1/07, baptiste Auguié [email protected] wrote:
Sorry if I have overlooked other issues but

-2/3 is not what you expect it to be, I’d say, cause you’d probably
written -1 than or just divided radius by x.

radius and x seem to be floats already, you might save some keystrokes
there

HTH
Robert

The names may have been confusing without the context. I believe the
formula is right (as Octave’s results show). long/short axis refer to
a 3D ellipsoid of constant volume, radius referring to the degenerate
case of a sphere. Thanks for the .to_f thing, I seem to be overdoing
many things

thanks,

baptiste

On 7/1/07, baptiste Auguié [email protected] wrote:

The names may have been confusing without the context. I believe the
formula is right (as Octave’s results show).
You mean **(-2/3) is what you want???
Maybe I was not clear enough - as I rarely are :(.
-2/3 ==> -1

Robert

yep, i do want (-2/3). Oh, i see, is 2/3 == 1 for Ruby?? OMG this is

thanks, it works now!

``` radius=0.045 aspectRatio=[] longAxis=[] shortAxis=[] 0.5.step(1, 0.1) { |x| aspectRatio << x.to_s } longAxis=aspectRatio.collect{ |x| radius*((x.to_f)**(-2/3.to_f)) } aspectRatio.each_index { |x| shortAxis << longAxis[x].to_f*aspectRatio [x].to_f} puts longAxis puts shortAxis ```

any easier / more elegant way to do all this ?

thanks,

baptiste

On 1 Jul 2007, at 12:15, Robert D. wrote:

Physics Department
University of Exeter
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

Have a look at matrix.rb (require matrix, ri Vector) and at the narray
gem (gem install narray, require ‘narray’, ‘nmatrix’, read the docs
that come with it for NVector).

The Vector and NVector datatypes seem to be fit for what you do, and
you can always .to_a them.

Aur

On 7/1/07, baptiste Auguié [email protected] wrote:

yep, i do want (-2/3). Oh, i see, is 2/3 == 1 for Ruby?? OMG this is
No 0 I am a declared egghead already, but it was the issue nonetheless.

It is not mad for Computer Scientists only for real Scientists
You have heard the prime number joke before, have you not?

Well here it goes spamming the group, but it really explains the
problem of expectation and behavior:

Theorem: All odd numbers are prime.
Proof
Theoretical Physician: 1 is odd but not prime, theorem fails!
I spare you 42 disciplines here
Computer Scientist (that is me folks do not holler ;): 1 prime, 2
prime, 3 prime Quod Erat Demonstrandum.

Seriously now, in many fields 2/3 ==> 0 is much more useful than ==>
0.66666…, not in yours I guess. BTW Lua and Perl do it your way;
Phyton does it our way.

Cheers
Robert

On 7/1/07, baptiste Auguié [email protected] wrote:

any easier / more elegant way to do all this ?

thanks,

baptiste

Instead of (-2/3.to_f) , you could use (-2.0/3). By using a float
literal rather than an integer literal, you will get float division
rather than integer division.

-A