[#351786] Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 — Raul Parolari <raulparolari@...>

In porting some automation code from 1.8.7 to 1.9.1, I find that in ruby

16 messages 2009/12/01
[#351787] Re: Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 — Raul Parolari <raulparolari@...> 2009/12/01

[ Sorry for the horrible indentation. I repeat the text renouncing to

[#351816] Re: Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 — Rick DeNatale <rick.denatale@...> 2009/12/01

On Tue, Dec 1, 2009 at 12:53 AM, Raul Parolari <raulparolari@gmail.com> wro=

[#351818] Re: Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 — Raul Parolari <raulparolari@...> 2009/12/01

Rick Denatale wrote:

[#351822] Re: Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 — Robert Klemme <shortcutter@...> 2009/12/01

2009/12/1 Raul Parolari <raulparolari@gmail.com>:

[#351823] Re: Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 — Raul Parolari <raulparolari@...> 2009/12/01

Robert Klemme wrote:

[#351878] Graphics mode — Teodor Carstea <teodorcarstea@...>

Hi all! I'm a beginner.

24 messages 2009/12/02

[#351881] duda sobre nuevo metodo — "David J,nas" <acidburg@...>

mi routers.rb es

18 messages 2009/12/02
[#351886] Re: duda sobre nuevo metodo — Rob Biedenharn <Rob@...> 2009/12/02

On Dec 2, 2009, at 10:18 AM, David J,nas wrote:

[#351894] Re: duda sobre nuevo metodo — "David J,nas" <acidburg@...> 2009/12/02

Rob Biedenharn wrote:

[#351900] Re: duda sobre nuevo metodo — Rob Biedenharn <Rob@...> 2009/12/02

On Dec 2, 2009, at 11:17 AM, David J,nas wrote:

[#351903] Re: duda sobre nuevo metodo — "David J,nas" <acidburg@...> 2009/12/02

esta solucionado gracias

[#351950] Encoding/decoding a image as Base64 (fails under Ruby1.9 but works under Ruby1.8) — Iñaki Baz Castillo <ibc@...>

Hi, the folowing code encodes and decodes a image file as Base64:

8 messages 2009/12/03

[#352043] Graphics mode again — Teodor Carstea <teodorcarstea@...>

>Hi, all! here is my prog: it calculates the trajectory of a cannon shell:

15 messages 2009/12/04

[#352051] gems installation - invalid gem format — Serguei Cambour <s.cambour@...>

No matter on which OS you are, - XP Family, XP Pro or Ubuntu 9.10. No

10 messages 2009/12/04

[#352101] how about ruby's threads? — Ruby Newbee <rubynewbee@...>

Hello,

19 messages 2009/12/05
[#352106] Re: how about ruby's threads? — David Masover <ninja@...> 2009/12/05

On Friday 04 December 2009 09:39:33 pm Ruby Newbee wrote:

[#352149] Re: how about ruby's threads? — Ruby Newbee <rubynewbee@...> 2009/12/06

Well, I asked this because Perl thread documentation warns that

[#352170] Re: how about ruby's threads? — David Masover <ninja@...> 2009/12/06

First, it's just a preference, but I think most on the list agree to me --

[#352190] Re: how about ruby's threads? — Brian Candler <b.candler@...> 2009/12/07

David Masover wrote:

[#352122] ruby 1.9 and collect — Raul Jara <raul.c.jara@...>

Under ruby 1.8.6, running

13 messages 2009/12/05

[#352138] Why doesn't Ruby "compile" strings? — Iñaki Baz Castillo <ibc@...>

Hi, the following code:

12 messages 2009/12/06

[#352150] IRB GUI on OSX? — Sophie <itsme213@...>

Is there a good GUI IRB on OSX? Preferably one that lets me easily

14 messages 2009/12/06

[#352192] Reading Images — Alexandro Kez <alexandro.mail@...>

Hei there, ruby coders,

14 messages 2009/12/07
[#352193] Re: Reading Images — Robert Klemme <shortcutter@...> 2009/12/07

2009/12/7 Alexandro Kez <alexandro.mail@gmail.com>

[#352206] Wordpress Port — hjast <hjast89@...>

Has there any been an effort to make a ruby Wordpress port? I am a

30 messages 2009/12/07

[#352220] convert string into a variable object — Ad Ad <codetest123@...>

Hi,

16 messages 2009/12/07
[#352221] Re: convert string into a variable object — Aldric Giacomoni <aldric@...> 2009/12/07

Ad Ad wrote:

[#352225] Re: convert string into a variable object — Ad Ad <codetest123@...> 2009/12/07

Aldric Giacomoni wrote:

[#352390] Mysql::Error: Data too long for column — "rabarama" <profpelosotoglimi@...>

Pleas i need help:

12 messages 2009/12/09

[#352459] human-readable listing of array elements — Aldric Giacomoni <aldric@...>

This took me less than a minute to write, but I don't know if it's as

22 messages 2009/12/10

[#352463] Re: Redirecting standard output — Roger Pack <rogerpack2005@...>

Omar Campos wrote:

17 messages 2009/12/10
[#352509] Re: Redirecting standard output — Omar Campos <hypermeister@...> 2009/12/11

Thanks, that's exactly what I was looking for! Nice gem by the way. I

[#352510] Re: Redirecting standard output — Robert Gleeson <rob@...> 2009/12/11

Omar Campos wrote:

[#352492] syntax issue — Sig Dx <sigbackup@...>

Hello guys,

17 messages 2009/12/10

[#352556] program: a small ball in a window — Teodor Carstea <teodorcarstea@...>

Please help, I realy need help!

11 messages 2009/12/11

[#352607] Is it possible to force a Ruby program to run as a proc name different than "ruby"? — Iñaki Baz Castillo <ibc@...>

Hi, I have a Python software called "py_program". It runs as daemon in Linu=

28 messages 2009/12/11
[#352612] Re: Is it possible to force a Ruby program to run as a proc name different than "ruby"? — David Masover <ninja@...> 2009/12/11

On Friday 11 December 2009 04:34:04 pm I=C3=B1aki Baz Castillo wrote:

[#352616] Re: Is it possible to force a Ruby program to run as a proc name different than "ruby"? — Iñaki Baz Castillo <ibc@...> 2009/12/11

El S=C3=A1bado, 12 de Diciembre de 2009, David Masover escribi=C3=B3:

[#352618] Re: Is it possible to force a Ruby program to run as a proc name different than "ruby"? — David Masover <ninja@...> 2009/12/12

On Friday 11 December 2009 05:33:06 pm I=C3=B1aki Baz Castillo wrote:

[#352631] Re: Is it possible to force a Ruby program to run as a proc name different than "ruby"? — Iñaki Baz Castillo <ibc@...> 2009/12/12

El S=C3=A1bado, 12 de Diciembre de 2009, David Masover escribi=C3=B3:

[#352641] Re: Is it possible to force a Ruby program to run as a proc name different than "ruby"? — Brian Candler <b.candler@...> 2009/12/12

I単aki Baz Castillo wrote:

[#352642] Re: Is it possible to force a Ruby program to run as a proc name different than "ruby"? — Brian Candler <b.candler@...> 2009/12/12

Brian Candler wrote:

[#352647] Re: Is it possible to force a Ruby program to run as a proc name different than "ruby"? — Iñaki Baz Castillo <ibc@...> 2009/12/12

El S=C3=A1bado, 12 de Diciembre de 2009, Brian Candler escribi=C3=B3:

[#352653] How to make a ruby program tu run unser other user:group different than root — Iñaki Baz Castillo <ibc@...>

Hi, many daemons allow an argument "-u uid" which makes the program to run=

12 messages 2009/12/12

[#352694] Code block for element comparison in an array? — Derek Cannon <novellterminator@...>

I'm new to Ruby and I can't think of how to do this! I would like each

14 messages 2009/12/13

[#352736] Poll: Significant Indentation — David Masover <ninja@...>

I've just re-read the "Beating a Dead Horse" thread. I will bring that up

30 messages 2009/12/13

[#352922] Using threads to show progress — Aldric Giacomoni <aldric@...>

I want to do this in a Rake task, but the concept is Ruby. I would like

22 messages 2009/12/15
[#352925] Re: Using threads to show progress — Aldric Giacomoni <aldric@...> 2009/12/15

Aldric Giacomoni wrote:

[#352941] Re: Using threads to show progress — David Masover <ninja@...> 2009/12/16

On Tuesday 15 December 2009 01:53:21 pm Aldric Giacomoni wrote:

[#352943] Re: Using threads to show progress — Piyush Ranjan <piyush.pr@...> 2009/12/16

How about using a queue ?

[#353056] Encapsulating Information and Behavior without State — Intransition <transfire@...>

This should prove an interesting topic. I am currently debating two

9 messages 2009/12/17

[#353070] How to fund an open-source project? — Eleanor McHugh <eleanor@...>

In the recent thread on Ruby VMs I mentioned my newly launched =

23 messages 2009/12/17
[#353071] Re: How to fund an open-source project? — Gregory Brown <gregory.t.brown@...> 2009/12/17

On Thu, Dec 17, 2009 at 6:19 PM, Eleanor McHugh

[#353233] Re: How to fund an open-source project? — Eleanor McHugh <eleanor@...> 2009/12/20

On 17 Dec 2009, at 23:28, Gregory Brown wrote:

[#353258] Re: How to fund an open-source project? — Robert Klemme <shortcutter@...> 2009/12/20

On 20.12.2009 02:28, Eleanor McHugh wrote:

[#353088] Suggestion of Array#=== which improves case/when behaviour — Dmitry Vazhov <dmitryelastic@...>

Hello,

19 messages 2009/12/18
[#353090] Re: Suggestion of Array#=== which improves case/when behavio — Dmitry Vazhov <dmitryelastic@...> 2009/12/18

"Set" class has meaning close to "Range" class. If we will define

[#353173] Re: Suggestion of Array#=== which improves case/when behavio — Caleb Clausen <vikkous@...> 2009/12/18

> Dmitry Vazhov wrote:

[#353198] Re: Suggestion of Array#=== which improves case/when behavio — Tony Arcieri <tony@...> 2009/12/19

I would absolutely love if Array recursively performed #=== on its

[#353200] Re: Suggestion of Array#=== which improves case/when behavio — "David A. Black" <dblack@...> 2009/12/19

Hi --

[#353089] Math errors — jzakiya <jzakiya@...>

(-3)**3 => -27

19 messages 2009/12/18

[#353151] Question about sum of fibonacci sequene [PROJECT EULER] — Panagiotis Atmatzidis <atma@...>

-----BEGIN PGP SIGNED MESSAGE-----

10 messages 2009/12/18

[#353319] Help with gem ruby mysql error — Andrew Ting <drikting@...>

Hi,

11 messages 2009/12/21

[#353322] Ruby's implementation of Fixnum-assignment — RichardOnRails <RichardDummyMailbox58407@...>

Hi,

19 messages 2009/12/21

[#353339] promiscuous mode — Marco Biscetti <bisciasia@...>

Hi guys. i have a question. i am new in a ruby world.

13 messages 2009/12/21

[#353353] Strange behavior of unary +@ for Fixnum? — Alexandre Mutel <alexandre_mutel@...>

It seems that it's not possible to use the unary +@ operator for Fixnum

14 messages 2009/12/21

[#353357] Symbols vs. constants? — Sonja Elen Kisa <sonja@...>

How are symbols and constant (capitalized) strings similar or

35 messages 2009/12/22
[#353359] Re: Symbols vs. constants? — Gennady Bystritsky <Gennady.Bystritsky@...> 2009/12/22

Constants are variables that can be assigned to any object (only once). Whi=

[#353360] Re: Symbols vs. constants? — "Young H." <armywide@...> 2009/12/22

On Tue, Dec 22, 2009 at 9:07 AM, Gennady Bystritsky

[#353396] integers and floats — Rajinder Yadav <devguy.ca@...>

Can someone explain to me why Ruby treats integers and floating point

16 messages 2009/12/22
[#353398] Re: integers and floats — Paul Smith <paul@...> 2009/12/22

On Tue, Dec 22, 2009 at 1:01 PM, Rajinder Yadav <devguy.ca@gmail.com> wrote:

[#353402] newbie: if is not null else... — Alfonso Caponi <alfonso.caponi@...>

Hi forum,

16 messages 2009/12/22

[#353453] Suggestions for a distributed job queue — Tony Arcieri <tony@...>

I'm looking at replacing our homebrew job queue with something better, and

15 messages 2009/12/22
[#353457] Re: Suggestions for a distributed job queue — "Walton Hoops" <walton@...> 2009/12/22

> -----Original Message-----

[#353477] Trig value errors — jzakiya <jzakiya@...>

Hardware: 32-bit Intel P4 cpu

58 messages 2009/12/23

[#353482] awk print $4 in ruby — Derek Smith <derekbellnersmith@...>

Hi All,

17 messages 2009/12/23

[#353690] pasing data between 2 win XP boxes via internet — Dave Lilley <dglnz2@...>

Hi folks,

23 messages 2009/12/26

[#353733] How to truncate the spaces in the front of a line — Milo Luo <lxybhbh@...>

Hi, guys

13 messages 2009/12/27
[#353735] Re: How to truncate the spaces in the front of a line — Phillip Gawlowski <pg@...> 2009/12/27

On 27.12.2009 04:22, Milo Luo wrote:

[#353739] building ruby from source — Rajinder Yadav <devguy.ca@...>

I removed my older ruby package from my ubuntu, built ruby from source

14 messages 2009/12/27

[#353823] au3 0.1.1 released — Marvin Gülker <sutniuq@...>

au3 0.1.1 has been released. au3 is a library that allows you to

23 messages 2009/12/28
[#353837] Re: [ANN] au3 0.1.1 released — Roger Pack <rogerpack2005@...> 2009/12/28

Marvin G端lker wrote:

[#353842] Re: [ANN] au3 0.1.1 released — Marvin Gülker <sutniuq@...> 2009/12/28

Roger Pack wrote:

[#353877] Re: [ANN] au3 0.1.1 released — Edward Middleton <emiddleton@...> 2009/12/29

Marvin G端lker wrote:

[#353909] Re: au3 0.1.1 released — Marvin Gülker <sutniuq@...> 2009/12/29

Edward Middleton wrote:

[#353854] Creating my own method for sorting an array — Joe User <gctaylor2004-rubyforum@...>

Hi,

12 messages 2009/12/28

[#353856] ? about Search in google,com and Show Result — Saji Jaooon <seyyedsajjad1363@...>

hi every body

13 messages 2009/12/28

[#353872] A Ruby appliance: What would you include? — Phillip Gawlowski <pg@...>

Hello, list!

37 messages 2009/12/29
[#353923] Re: A Ruby appliance: What would you include? — Aldric Giacomoni <aldric@...> 2009/12/29

Phillip Gawlowski wrote:

[#353933] Re: A Ruby appliance: What would you include? — Phillip Gawlowski <pg@...> 2009/12/29

On 29.12.2009 14:25, Aldric Giacomoni wrote:

[#353957] Re: A Ruby appliance: What would you include? — Marnen Laibow-Koser <marnen@...> 2009/12/29

Aldric Giacomoni wrote:

[#353962] Re: A Ruby appliance: What would you include? — Phillip Gawlowski <pg@...> 2009/12/29

On 29.12.2009 17:51, Marnen Laibow-Koser wrote:

[#353878] Using #include at the instance level? — Intransition <transfire@...>

I would like to use #include at an instance level, such that it

22 messages 2009/12/29
[#353901] Re: Using #include at the instance level? — Robert Klemme <shortcutter@...> 2009/12/29

2009/12/29 Intransition <transfire@gmail.com>:

[#353927] Re: Using #include at the instance level? — Intransition <transfire@...> 2009/12/29

[#353935] Re: Using #include at the instance level? — Robert Klemme <shortcutter@...> 2009/12/29

2009/12/29 Intransition <transfire@gmail.com>:

[#353955] Re: Using #include at the instance level? — Intransition <transfire@...> 2009/12/29

[#353961] Re: Using #include at the instance level? — Robert Klemme <shortcutter@...> 2009/12/29

2009/12/29 Intransition <transfire@gmail.com>:

[#353992] Re: Using #include at the instance level? — Intransition <transfire@...> 2009/12/29

[#353880] PLEASE HELP...dup is not working correctly in the following code — timr <timrandg@...>

#dup creates a copy of an object with a different object_id. As

10 messages 2009/12/29

[#354040] How to pass a function as parameter? — Fritz Trapper <ajfrenzel@...>

I want to pass a reference to function as parameter to another function

15 messages 2009/12/30
[#354042] Re: How to pass a function as parameter? — Jes俍 Gabriel y Gal疣 <jgabrielygalan@...> 2009/12/30

On Wed, Dec 30, 2009 at 12:42 PM, Fritz Trapper <ajfrenzel@web.de> wrote:

[#354077] Where to find a description of yaml for ruby? — Fritz Trapper <ajfrenzel@...>

Where to find a description of yaml for ruby?

12 messages 2009/12/30

[#354081] Test::unit assertion pass scenario — John Smith <ks1911shooter@...>

When using the test::unit assertion, such as assert_equal, the script

13 messages 2009/12/30

[#354098] Why can't I use "or" here? — Albert Schlef <albertschlef@...>

(The problem is in both Ruby 1.8 and Ruby 1.9)

41 messages 2009/12/31
[#354106] Re: Why can't I use "or" here? — botp <botpena@...> 2009/12/31

On Thu, Dec 31, 2009 at 10:24 AM, Albert Schlef <albertschlef@gmail.com> wrote:

[#354114] Re: Why can't I use "or" here? — Albert Schlef <albertschlef@...> 2009/12/31

botp wrote:

[#354162] Automated testing of visual library (ncurses) — "(rkumar) Sentinel" <sentinel.2001@...>

As i work more on a ncurses widget library, I am wondering how does one

10 messages 2009/12/31

[#354187] Delete elements in array, break, and keep changes? — Joe Buck <semle2000@...>

I have an array of a lot elements that I need to cluster (they are

12 messages 2009/12/31

Re: Roots Module

From: jzakiya <jzakiya@...>
Date: 2009-12-26 20:40:26 UTC
List: ruby-talk #353718
On Dec 26, 2:33=A0am, jzakiya <jzak...@gmail.com> wrote:
> On Dec 26, 2:18=A0am, jzakiya <jzak...@gmail.com> wrote:
>
>
>
> > On Dec 25, 5:13=A0pm, jzakiya <jzak...@mail.com> wrote:
>
> > > On Dec 25, 4:42=A0pm, jzakiya <jzak...@mail.com> wrote:
>
> > > > As a Christmas/Holiday present to the Ruby community I finally
> > > > completed creating a versatile module which will find the accurate
> > > > real and complex roots of +|- real values.
>
> > > > This work evolved from concerns raised in this thread:
>
> > > >http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/9=
b...
>
> > > > from which I expanded on this work and incorporated some suggestion=
s.
>
> > > > I have tested this mixed in this module to classes Float, Integer, =
and
> > > > BigDecimal successfully.
>
> > > > I tested this on Ruby versions 1.9.1p243, 1.8.7p174 and
> > > > Ruby Enterprise Edition 1.8.6 20090610.
>
> > > > Constructive =A0feedback is welcome.
>
> > > > File: roots.rb
> > > > ------------------------------------------------------
> > > > module Roots
> > > > =3Dbegin
> > > > Mixin Roots into Integer, BigDecimal, Float to add
> > > > methods root & roots, to find the real|complex roots
> > > > of +|- real values.
>
> > > > Use syntax: =A0val.root(n,{k}}
> > > > root(n,k=3D0) n is root (1/n) exponent, integer > 0,
> > > > =A0 =A0 =A0 =A0 =A0 =A0 k is nth root 1..n , integer >=3D0
> > > > If k not given default root returned, which are:
> > > > for +val =3D> real root |val**(1.0/n)|
> > > > for -val =3D> real root -|val**(1.0/n)| when n is odd
> > > > for -val =3D> first complex root -val =A0 when n is even
> > > > for any val =3D> first ccw root real|complex when k > 0
>
> > > > 9.root(2); 8.root(3,1), -32.root(5,3), -100.43.root 6,6
>
> > > > Use syntax: =A0val.roots(n,{opt}}
> > > > roots(n,opt=3D0) n is root (1/n) exponent, integer > 0,
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0opt area optional input options belo=
w
> > > > 0 =A0 =A0 =A0: default (no input), return array of n ccw root vals
> > > > 'c'|'C': complex, return array of complex roots, nil if none
> > > > 'e'|'E': even, return array even numbered roots, nil if none
> > > > 'o'|'O': odd , return array odd =A0numbered roots, nil if none
> > > > 'i'|'I': imag, return array of imaginary roots, nil if none
> > > > 'r'|'R': real, return array of real roots, nil if none
>
> > > > 293481349432.roots(9); -892.roots(4,'real'); 22.2.roots 3,'i'
> > > > =3Dend
>
> > > > =A0 require 'complex'
> > > > =A0 include Math
>
> > > > =A0 def root(n,k=3D0) # k=3D1..n; k=3D0 for default root
> > > > =A0 =A0 raise "Root not integer >0" unless n.kind_of?(Integer) && n=
>=3D1
> > > > =A0 =A0 =A0 =A0 raise "Index k not an integer" unless k.kind_of?(In=
teger)
> > > > =A0 =A0 return self if n =3D=3D 1 || self =3D=3D 0
> > > > =A0 =A0 mag =3D self.abs**n**-1
> > > > =A0 =A0 =A0 =A0 return rootn(mag,k-1,PI/n) if k>0 # nth root, k =3D=
 1..n, of any real
> > > > =A0 =A0 =A0 =A0 return mag if self > 0 =A0 =A0 =A0 =A0 =A0 =A0# pos=
 real default
> > > > =A0 =A0 =A0 =A0 return -1*mag if n&1 =3D=3D 1 =A0 =A0 =A0 =A0 # neg=
 real default, n odd
> > > > =A0 =A0 =A0 =A0 return rootn(mag,0,PI/n) =A0 =A0 =A0 =A0 =A0# neg r=
eal default, n even
> > > > =A0 end
>
> > > > =A0 def roots(n,opt=3D0) # =A0returns an array of values, or nil if=
 option
> > > > not valid
> > > > =A0 =A0 raise "Root not integer >0" unless n.kind_of?(Integer) && n=
>=3D1
> > > > =A0 =A0 =A0 =A0 raise "Invalid option" unless opt =3D=3D 0 || opt =
=3D~ /^(c|e|i|o|r|C|E|I|O|
> > > > R)/
> > > > =A0 =A0 return [self] if n =3D=3D 1 || self =3D=3D 0
> > > > =A0 =A0 mag =3D self.abs**n**-1
> > > > =A0 =A0 =A0 =A0 roots =3D []; theta =3D PI/n
> > > > =A0 =A0 =A0 =A0 case opt
> > > > =A0 =A0 =A0 =A0 =A0 when /^(o|O)/ =A0# even roots 2,4,6...
> > > > =A0 =A0 =A0 =A0 =A0 =A0 0.step(n-1,2) {|k| roots << rootn(mag,k,the=
ta)}
> > > > =A0 =A0 =A0 =A0 =A0 when /^(e|E)/ =A0# odd roots 1,3,5...
> > > > =A0 =A0 =A0 =A0 =A0 =A0 1.step(n-1,2) {|k| roots << rootn(mag,k,the=
ta)}
> > > > =A0 =A0 =A0 =A0 =A0 when /^(r|R)/ =A0# real roots =A0Complex(x,0) =
=3D(x+i0)
> > > > =A0 =A0 =A0 =A0 =A0 =A0 n.times {|k|
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 x=3Drootn(mag,k,theta); roots <=
< x if x.imag =3D=3D 0}
> > > > =A0 =A0 =A0 =A0 =A0 when /^(i|I)/ =A0# imaginry roots =A0Complex(0,=
x) =3D (0+ix)
> > > > =A0 =A0 =A0 =A0 =A0 =A0 n.times {|k|
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 x=3Drootn(mag,k,theta); roots <=
< x if x.real =3D=3D 0}
> > > > =A0 =A0 =A0 =A0 =A0 when /^(c|C)/ =A0# complex roots Complex(x,y) =
=3D (x+iy)
> > > > =A0 =A0 =A0 =A0 =A0 =A0 n.times {|k|
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 x=3Drootn(mag,k,theta); roots <=
< x unless
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 x.imag =3D=3D 0 || x.real =3D=
=3D 0}
> > > > =A0 =A0 =A0 =A0 =A0 else
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 n.times {|k| roots << rootn(mag,k,theta=
)}
> > > > =A0 =A0 end
> > > > =A0 =A0 =A0 =A0 return roots.empty? ? nil : roots
> > > > =A0 end
>
> > > > =3Dbegin
> > > > Ruby currently produces incorrect values for x|y axis angles.
> > > > cos PI/2 =A0 =3D> =A06.12303176911189e-17
> > > > sin PI =A0 =A0 =3D> =A01.22460635382238e-16
> > > > cos 3*PI/2 =3D> -1.83690953073357e-16
> > > > sin 2*PI =A0 =3D> -2.44921970764475e-16
>
> > > > These all should be 0.0, which causes incorrect root values there.
> > > > I 'fix' these errors by clipping absolute values less than a value
> > > > I call TRiG-EPSILON so they produces the correct results.
>
> > > > Extract this code into separate file and 'require' into your apps
> > > > to get correct|exact results for x|y axis angles and still get same
> > > > accurrcay for extremely small delta angles to the x|y axis.
> > > > cosine(89.9999999999*PI/180) =3D> =A01.74534333112399e-11
> > > > cosine(90.0*PI/180) =A0 =A0 =A0 =A0 =A0=3D> =A00.0
> > > > cosine(90.0000000001*PI/180) =3D> -1.74543140899798e-12
> > > > =3Dend
>
> > > > =A0 protected
> > > > =A0 TRIG_EPSILON =3D 1e-15
> > > > =A0 def sine(x); y=3Dsin(x); y.abs < TRIG_EPSILON ? 0.0:y end
> > > > =A0 def cosine(x); y=3Dcos(x); y.abs<TRIG_EPSILON ? 0.0:y end
> > > > =A0 def tangent(x); sine(x)/cosine(x) end # not used here but more
> > > > correct
>
> > > > =A0 def angle(k,theta) # roots 1..n --> k =3D 0..n-1
> > > > =A0 =A0 angle =3D self > 0.0 ? 2*(k+1)*theta : (2*k+1)*theta
> > > > =A0 end
>
> > > > =A0 def rootn(mag,k,theta)
> > > > =A0 =A0 a =3D angle(k,theta); mag*Complex(cosine(a),sine(a))
> > > > =A0 end
> > > > end
>
> > > Corrections:
> > > change access control from 'protected' to 'private'
> > > as those definitions shouldn't show up as methods in
> > > the mixedin classes.
>
> > > Also put at top of comment:
> > > Author: Jabari Zakiya, 2009-12-25
>
> > Correction for method conflict in Float:
>
> > In 1.9.1 there is conflict in Float with method 'angle'
> > thus, rename my private definitions as such:
>
> > =A0 def angle_n(k,theta) # roots 1..n --> k =3D 0..n-1
> > =A0 =A0 self > 0 ? 2*(k+1)*theta : (2*k+1)*theta
> > =A0 end
>
> > =A0 def rootn(mag,k,theta) # nth root of a real value
> > =A0 =A0 a =3D angle_n(k,theta); mag*Complex(cosine(a),sine(a))
> > =A0 end
>
> Actually, just eliminate 'def angle_n' since it was only
> use in rootn, and just have rootn be this:
>
> =A0 def rootn(mag,k,theta) # nth root of a real value
> =A0 =A0 angle_n =3D self > 0 ? 2*(k+1)*theta : (2*k+1)*theta
> =A0 =A0 =A0 =A0 mag*Complex(cosine(angle_n),sine(angle_n))
> =A0 end

OK, here's the final (for now) Roots module, all
cleaned up and tweaked.

New changes:
I made roots method return just an array in all cases.
So now, if an option doesn't have members, it returns
an empty array, [], instead of nil.

This allows you to now consistently do things like:
How many real roots for x? -- x.roots(n,'real').size
If there are none, you get '0', instead of exception
error of NoMethod for Nilclass for size.

I also set TRIG-EPSILON =3D 2.5e-16 to get it as close
to the largest incorrect value for cos|sin for the x|y
axis angles, which is sin(2*PI)=3D-2.44921970764475e-16
on my Intel P4 cpu system. Set for equivalent results
for your cpu (32|64-bit) environment (AMD, PPC, etc).

I also noticed when Roots is mixedin to Integer it will
also take care of Bignums too, but you have to mixin to
Float separately to take care of them too.

Finally, for those who don't know|care|believe this
stuff matters, or has any uses, check out:

Project Euler -- http://projecteuler.net/

Ruby users and aficionados have a pretty strong throng
there (along with other languages), and to get the
"correct answers" exactness matters.

So here is the current final Roots module, cleaned up
with (hopefully) no typos, and formatted here to cleanly
fit within the column limitations for posting here.

If/when I get the desire, I may create a gem out of it,
with DOC markup, and all the rest.

Finally, today is December 26, the first day of Kwanzaa,
which celebrates the principle of Umoja -- unity.

Peace and Blessings to All.

File roots.rb
--------------------------------
module Roots
=3Dbegin
Author: Jabari Zakiya,   Original: 2009-12-25
Revision-1: 2009-12-26

Mixin Roots into Integer, BigDecimal, Float to add
methods root & roots, to find the real|complex roots
of +|- real values.

Use syntax:  val.root(n,{k})
root(n,k=3D0) n is root 1/n exponent,  integer > 0
            k is nth ccw root 1..n , integer >=3D0
If k not given default root returned, which are:
for +val =3D> real root  |val**(1.0/n)|
for -val =3D> real root -|val**(1.0/n)| when n is odd
for -val =3D> first ccw complex root    when n is even

94835805813.root(2); -32.root(5,3), -100.43.root 6,6

Use syntax:  val.roots(n,{opt})
roots(n,opt=3D0) n is root (1/n) exponent, integer > 0
               opt, optional string input, are:
   0   : default (no input), return array of n ccw roots
'c'|'C': complex, return array of complex roots
'e'|'E': even, return array even numbered roots
'o'|'O': odd , return array odd  numbered roots
'i'|'I': imag, return array of imaginary roots
'r'|'R': real, return array of real roots
An empty array is returned for an opt with no members.

9334943.roots(9); -89.roots(4,'real'); 2.2.roots 3,'Im'

Can ask: How many real roots of: x.roots(n,'real').size

Ruby currently gives incorrect values for x|y axis angles
cos PI/2   =3D>  6.12303176911189e-17
sin PI     =3D>  1.22460635382238e-16
cos 3*PI/2 =3D> -1.83690953073357e-16
sin 2*PI   =3D> -2.44921970764475e-16

These all should be 0.0, causing wrong root values there.
I 'fix' by clipping absolute values less than a value
I call TRIG-EPSILON so they produces the correct results.

Put trig code into separate file and 'require' into apps
to get correct|exact results for x|y axis angles and get
same accuracy for very small angles close to each axis.
cosine(89.9999999999*PI/180) =3D>  1.74534333112399e-11
cosine(90.0*PI/180)          =3D>  0.0
cosine(90.0000000001*PI/180) =3D> -1.74543140899798e-12
=3Dend

  require 'complex'
  include Math

  def root(n,k=3D0) # return nth root k=3D1.n, or default k=3D0
    raise "Root not integer >0" unless
                             n.kind_of?(Integer) && n>=3D1
    raise "Index k not an integer" unless
                             k.kind_of?(Integer)
    return self if n =3D=3D 1 || self =3D=3D 0
    mag =3D self.abs**n**-1
    return rootn(mag,k-1,PI/n) if k>0 # nth root any real
    return mag if self > 0     # pos real default
    return -1*mag if n&1 =3D=3D 1  # neg real default, n odd
    return rootn(mag,0,PI/n)   # neg real default, n even
  end

  def roots(n,opt=3D0) # return array of roots, or empty
    raise "Root not integer >0" unless
                           n.kind_of?(Integer) && n>=3D1
    raise "Invalid option" unless opt =3D=3D 0 ||
                           opt =3D~ ^(c|e|i|o|r|C|E|I|O|R)/
    return [self] if n =3D=3D 1 || self =3D=3D 0
    mag =3D self.abs**n**-1
    roots =3D []; theta =3D PI/n
    case opt
    when /^(o|O)/  # even roots 2,4,6...
      0.step(n-1,2) {|k| roots << rootn(mag,k,theta)}
    when /^(e|E)/  # odd roots 1,3,5...
      1.step(n-1,2) {|k| roots << rootn(mag,k,theta)}
    when /^(r|R)/  # real roots Complex(x,0) =3D(x+i0)
      n.times {|k|
        x=3Drootn(mag,k,theta); roots << x if x.imag =3D=3D 0}
    when /^(i|I)/  # imaginry roots Complex(0,x) =3D (0+ix)
      n.times {|k|
        x=3Drootn(mag,k,theta); roots << x if x.real =3D=3D 0}
    when /^(c|C)/  # complex roots  Complex(x,y) =3D (x+iy)
      n.times {|k|
        x=3Drootn(mag,k,theta); roots << x unless
        x.imag =3D=3D 0 || x.real =3D=3D 0}
    else           # all n roots
      n.times {|k| roots << rootn(mag,k,theta)}
    end
    return roots
  end

  private # don't show as methods in mixin class
  TRIG_EPSILON =3D 2.5e-16
  def sine(x); y=3Dsin(x); y.abs < TRIG_EPSILON ? 0.0:y end
  def cosine(x); y=3Dcos(x); y.abs<TRIG_EPSILON ? 0.0:y end
  def tangent(x); sine(x)/cosine(x) end # better than tan

  def rootn(mag,k,theta) # nth root of a real value
    angle_n =3D self > 0 ? 2*(k+1)*theta : (2*k+1)*theta
    mag*Complex(cosine(angle_n),sine(angle_n))
  end
end

In This Thread