Native rational and complex number implementations?

Hi -talk,

I believe it would be beneficial to have Rational and Complex
as built-in classes.

Comments?

Gavin

Could you tell some concrete expectations from making them built-in, i.e.
the reasons why you think it would be beneficial?

All that I would like to have built in is a ruby literal for specifying
complex numbers (a literal for purely imaginary numbers would be
sufficient, of course) because it’s tedious to write

Complex(2,3)

if what you want is

2+3i

(Btw, it would be nice if ruby’s parser were dynamically extensible to
allow adding features like this)

I know I could define a constant I and write

2+3*I

but it’s uglier. Also, Complex.to_s would then produce output valid for
ruby’s parser to read.

I don’t think significant performance gains are possible by making Complex
built-in because of the templated nature of the Complex class (real and
imaginary part can be any Numeric except Complex itself).

T

···

On Tue, 4 Feb 2003, Gavin Sinclair wrote:

I believe it would be beneficial to have Rational and Complex
as built-in classes.

What do people think of making “Math.sqrt(-1)” return “Complex(0,1)”?

···


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

I believe it would be beneficial to have Rational and Complex
as built-in classes.

Could you tell some concrete expectations from making them built-in, i.e.
the reasons why you think it would be beneficial?

Small benefit, really. It would be easier to document because
everything is designed to interoperate up-front. The current state
is: you require some files which butcher the existing classes. Thus
the API documentation is split. Not that I expect anything to change
for that reason; I’m just curious, really.

I do think it would be elegant to include them, and that’s a good, but
not sufficient, reason.

All that I would like to have built in is a ruby literal for specifying
complex numbers (a literal for purely imaginary numbers would be
sufficient, of course) because it’s tedious to write
Complex(2,3) if what you want is 2+3i

True, but how many literals do you really define? Numbers tend to be
kept internal to algorithms.

Also, alias C Complex :slight_smile:

I don’t think significant performance gains are possible by making Complex
built-in because of the templated nature of the Complex class (real and
imaginary part can be any Numeric except Complex itself).

Probably not, but if a method is creating lots of complex #s then what
would you prefer: Ruby methods to create them, or machine code?

Again, just curious to see if there are good reasons to keep them out,
apart from the “too busy; leave me alone” reason, which is very good.

Gavin

···

On Tuesday, February 4, 2003, 11:33:10 PM, Tobias wrote:

On Tue, 4 Feb 2003, Gavin Sinclair wrote:

This is prettier:

require ‘complex’

class Numeric
def i
Complex(0,self)
end
end

p 2+3.i

martin

···

Tobias Peters tpeters@invalid.uni-oldenburg.de wrote:

I know I could define a constant I and write

2+3*I

but it’s uglier. Also, Complex.to_s would then produce output valid for
ruby’s parser to read.

Daniel Carrera wrote:

What do people think of making “Math.sqrt(-1)” return “Complex(0,1)”?

It already does.

[kentda@v052a kentda]$ irb
irb(main):001:0> Math.sqrt(-1)
ArgumentError: square root for negative number
from (irb):1:in `sqrt’
from (irb):1
irb(main):002:0> require ‘complex’
true
irb(main):003:0> Math.sqrt(-1)
Complex(0, 1.0)
irb(main):004:0>

This little trinket seemed to catch the attention when I tried it at a
small Ruby course last year.

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Several hundreds to a few thousands, I guess :wink:

Ok, that source code was machine generated. It’s unit tests that check
whether parts of a C++ module wrapped with swig generate the same output
for a given test input as older Matlab code.

Machine generated, but still ugly to look at when scrolling through the
file to add more tests.

T

···

On Tue, 4 Feb 2003, Gavin Sinclair wrote:

because it’s tedious to write
Complex(2,3) if what you want is 2+3i

True, but how many literals do you really define? Numbers tend to be
kept internal to algorithms.

That’s really cool!

Do you know how it does it? Does the ‘require’ set a variable or
something? I ask because I want to make a Math::Polynomial class that can
be used both for real and complex polynomials.

Thanks.

···

On Wed, Feb 05, 2003 at 04:46:29PM +0900, Kent Dahl wrote:

Daniel Carrera wrote:

What do people think of making “Math.sqrt(-1)” return “Complex(0,1)”?

It already does.

[kentda@v052a kentda]$ irb
irb(main):001:0> Math.sqrt(-1)
ArgumentError: square root for negative number
from (irb):1:in `sqrt’
from (irb):1
irb(main):002:0> require ‘complex’
true
irb(main):003:0> Math.sqrt(-1)
Complex(0, 1.0)
irb(main):004:0>

This little trinket seemed to catch the attention when I tried it at a
small Ruby course last year.


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

That’s really cool!

We seem to have lost some context.

Do you know how it does it? Does the ‘require’ set a variable or
something? I ask because I want to make a Math::Polynomial class that can
be used both for real and complex polynomials.

You can look at the complex.rb source - it’s easy to understand.

Spoiler: complex.rb redefines Math.sqrt.

As for your Polynomial class, just make it work for reals, then
require ‘complex.rb’, and see if it works for complex numbers. It
probably will. If it doesn’t, you’ll probably be able to spot a few
extra methods that are needed, or something.

Gavin

···

On Thursday, February 6, 2003, 1:31:39 AM, Daniel wrote: