[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: simple math operations on arrays

Robert Dober

7/1/2007 10:56:00 AM

On 7/1/07, baptiste Auguié <ba208@exeter.ac.uk> wrote:
Sorry if I have overlooked other issues but
> > longAxis=aspectRatio.collect{ |x| radius.to_f*((x.to_f)**(-2/3)) }
-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 ;)
<snip>
HTH
Robert


--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

5 Answers

baptiste Auguié

7/1/2007 11:08:00 AM

0



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 1 Jul 2007, at 11:55, Robert Dober wrote:

> On 7/1/07, baptiste Auguié <ba208@exeter.ac.uk> wrote:
> Sorry if I have overlooked other issues but
>> > longAxis=aspectRatio.collect{ |x| radius.to_f*((x.to_f)**(-2/3)) }
> -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 ;)
> <snip>
> HTH
> Robert
>


Robert Dober

7/1/2007 11:15:00 AM

0

On 7/1/07, baptiste Auguié <ba208@exeter.ac.uk> 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

Robert Dober

7/1/2007 11:43:00 AM

0

On 7/1/07, baptiste Auguié <ba208@exeter.ac.uk> wrote:
> yep, i do want (-2/3). Oh, i see, is 2/3 == 1 for Ruby?? OMG this is
> mad !
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

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

SonOfLilit

7/1/2007 11:55:00 AM

0

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, Robert Dober <robert.dober@gmail.com> wrote:
> On 7/1/07, baptiste Auguié <ba208@exeter.ac.uk> wrote:
> > yep, i do want (-2/3). Oh, i see, is 2/3 == 1 for Ruby?? OMG this is
> > mad !
> 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
>
> --
> I always knew that one day Smalltalk would replace Java.
> I just didn't know it would be called Ruby
> -- Kent Beck
>
>

Alex LeDonne

7/2/2007 2:30:00 PM

0

On 7/1/07, baptiste Auguié <ba208@exeter.ac.uk> wrote:
> yep, i do want (-2/3). Oh, i see, is 2/3 == 1 for Ruby?? OMG this is
> mad !
>
> thanks, it works now!
>
> <code>
> 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
>
> </code>
>
> 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