ruby picture

RCR 194: adding notation for complex numbers

Submitted by discordantus (Sat Jan 17 06:01:49 UTC 2004)

Abstract

Support for the 'i' suffix to indicate imaginary numbers, for use in complex mathematics.

Problem

The current method for creation of complex numbers is somewhat unwieldy, and not as readable and compact as it could be.

Proposal

I propose the addition of a suffix that may be added to the end of a numeric literal, to indicate that it is an imaginary number. The standard notation that is used for imaginary numbers is (a+bi), where 'a' and 'b' are real numbers. A complex number specified in this way would look like this: (2.3+4.2i)

Analysis

This change will enhance readability of code, by allowing complex numbers that are entered in the code to look the same as they do when you use the numbers in a formula. For example, defining a series of complex numbers would look like this: values = [ 1+1i, 2+2i, 3+3i, 4+4i ] rather than this: values = [ Complex.new(1,1), Complex.new(2,2), Complex.new(3,3), Complex.new(4,4) ]

There is some precedent for this style of notation, in C and Java, the 'f' suffix is built into the language for specifying floats. In Python, the 'j' suffix specifies an imaginary number.

This change would not break any features, and it would make the language more attractive for people working with complex numbers.

Implementation

This would need to be implemented in C, due to the syntax modifications that would be necessary. However, it could be simulated in an eval function like this:
def ieval(code)
    require 'complex'
    eval code.gsub(/[^\d]\d*\.?\d+i/){|m|"#{m[0..0]}Complex.new(0,#{m[1..-2]})"}
end
puts ieval("(2+3i)*(5-7i)")
ruby picture
Comments Current voting

I realize now that I failed to mention treatment of variables (and methods, for that matter) in this RCR, and perhaps that made the proposal less clear

Dealing with both variables and methods, it would be unlikely that this notation could be implemented for them without breaking things. However, there would be many times that you would want to construct a complex number in this readable fashion, but using variables with values set at runtime. This could be implemented in both the aforementioned manner:

a, b=2.3, 4.2
c = a+b.to_c
or, it could be done in this way, without implementing an additional method:
a, b=2.3, 4.2
c = a+b*1i
Example one would require a new method (of class Numeric?) that converts a real number into an imaginary one, and example two is just built in.

I'm not sure about whether it would be logical to call the method #to_c, since it's not simply converting it to a complex number, it's multiplying the value by 'i', that is, converting it to an imaginary number. Then again, I think #to_i (to imaginary) is already taken by something else... :) - marc


[quote]Example one would require a new method (of class Numeric?) that converts a real number into an imaginary one, and example two is just built in.[/quote]

example 1 is present already in ruby:

-- iepiep


Strongly opposed 0
Opposed 0
Neutral 8
In favor 8
Strongly advocate 4
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 .