ruby picture

RCR 193: built-in complex numbers

Submitted by discordantus (Thu Jan 15 04:01:56 UTC 2004)

Abstract

Implementing fast built-in complex numbers in the standard form (a+bi)

Problem

Currently, complex number mathematics is very slow. For example, a program I wrote which made heavy use of complex numbers was made three times faster by using separate variables for imaginary and real parts, and doing the complex math using those parts.
Also, creation of complex numbers is not as intuitive as it could be. Complex numbers are usually written out in the form "(a+bi)", where a and b are real numbers. In Ruby, you must currently create complex numbers using "Complex.new(a,b)", which is less readable from a mathematics viewpoint.

Proposal

A new feature could be added to the language, creating a new literal. Any number followed immediately by the letter 'i' would be considered imaginary. For example, "2.3i" would be the same as "Complex.new(0,2.3)". Addition would enable the simple creation of complex numbers: "(2.3+4.2i)" == "(2.3+Complex.new(0,4.2))" == "Complex.new(2.3,4.2)".

Analysis

This issue cannot be solved satisfactorily without a language modification. Because of the syntax of standard notation for imaginary numbers, it is not possible to simply extend Ruby *in Ruby* to use that notation.

Also, implementing complex numbers as built-ins would speed complex calculations to a much more usable rate.

Making the changes suggested here would not break any existing code.

There is precedent for this in other programming languages. In Java and C, you can add the 'f' suffix to a number to assure that it is a float. In Python, adding the 'j' suffix (why 'j'?!?) will do something exactly like what is proposed here.

Implementation

It would seem this would have to be implemented in C. Adding the syntax required for the literals is not possible, that I know of, using just Ruby. Also, scientific and mathematical programs would benefit greatly from the increased speed of compiled code.
ruby picture
Comments Current voting

To me, this sounds like two change requests in one RCR, though the proposal only explicitly indicates one change:

  1. Implement the Complex class in C instead of in Ruby
  2. Change the parser so that a number followed by 'i' indicates the imaginary portion of a Complex number.

The former change seems more imporatant to me than the latter.

Also, note that some fields use 'j' to indicate an imaginary number. I don't know whether we'd want to support both or whether we should just pick one.

-- Paul Brannan


Paul is right. Those two proposal have two different impacts on the language, which need to be disucussed separetaedly. discordantus, can you separate two proposals and resubmit?

-matz.


I see what you mean. I will withdraw this and resubmit. - marc


Strongly opposed 0
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .