Operator Overloading. Complex Numbers in paxJavaScript, paxPascal, paxBasic and paxC.
paxJavaScript
function Complex(re, im)
{
this.re = re;
this.im = im;
this.op_Addition = function (x, y){ return new Complex(x.re + y.re, x.im + y.im); }
this.op_Subtraction = function (x, y){ return new Complex(x.re - y.re, x.im - y.im); }
this.op_Multiply = function (x, y){ return new Complex(x.re * y.re - x.im * y.im,
x.re * y.im + y.re * x.im); }
this.op_Division = function (x, y)
{
result = new Complex(0, 0);
d = y.re * y.re + y.im * y.im;
result.re = (x.re * y.re + x.im * y.im) / d;
result.im = (x.im * y.re - x.re * y.im) / d;
return result;
}
this.op_Negation = function (x){ return new Complex(- x.re, - x.im); }
this.op_Equality = function op_Equality(x, y) { return (x.re == y.re) && (x.im == y.im); }
this.op_Inequality = function op_Inequality(x, y) { return (x.re != y.re) || (x.im != y.im); }
}
c1 = new Complex(10, 20);
c2 = new Complex(30, 40);
c3 = c1 + c2;
println c3.re, " ", c3.im;
c3 = c1 - c2;
println c3.re, " ", c3.im;
c3 = c1 * c2;
println c3.re, " ", c3.im;
c3 = c1 / c2;
println c3.re, " ", c3.im;
c3 = - c3;
println c3.re, " ", c3.im;
println c1 == c2;
println c1 != c2;
paxPascal
type
Complex = record
re, im: Double;
static const i: Complex = (0.0, 1.0);
// +
operator + (x, y: Complex): Complex;
begin
result.re := x.re + y.re;
result.im := x.im + y.im;
end;
operator + (x: Complex; y: Double): Complex;
begin
result.re := x.re + y;
result.im := x.im;
end;
operator + (x: Double; y: Complex): Complex;
begin
result.re := x + y.re;
result.im := y.im;
end;
// -
operator - (x, y: Complex): Complex;
begin
result.re := x.re - y.re;
result.im := x.im - y.im;
end;
operator - (x: Complex; y: Double): Complex;
begin
result.re := x.re - y;
result.im := x.im;
end;
operator - (x: Double; y: Complex): Complex;
begin
result.re := x - y.re;
result.im := - y.im;
end;
// unary -
operator - (x: Complex): Complex;
begin
result.re := -x.re;
result.im := -x.im;
end;
// *
operator * (x, y: Complex): Complex;
begin
result.re := x.re * y.re - x.im * y.im;
result.im := x.re * y.im + y.re * x.im;
end;
operator * (x: Complex; y: Double): Complex;
begin
result.re := x.re * y;
result.im := x.im * y;
end;
operator * (x: Double; y: Complex): Complex;
begin
result.re := y.re * x;
result.im := y.im * x;
end;
// /
operator / (x, y: Complex): Complex;
var
d: Double;
begin
d := y.re * y.re + y.im * y.im;
result.re := (x.re * y.re + x.im * y.im) / d;
result.im := (x.im * y.re - x.re * y.im) / d;
end;
operator / (x: Complex; y: Double): Complex;
begin
result.re := x.re / y;
result.im := x.im / y;
end;
operator / (x: Double; y: Complex): Complex;
var
d: Double;
begin
d := y.re * y.re + y.im * y.im;
result.re := x * y.re / d;
result.im := - x * y.im / d;
end;
// =
operator = (x, y: Complex): Boolean;
begin
result := (x.re = y.re) and (x.im = y.im);
end;
operator = (x: Double; y: Complex): Boolean;
begin
result := (x = y.re) and (0.0 = y.im);
end;
operator = (x: Complex; y: Double): Boolean;
begin
result := (x.re = y) and (x.im = 0.0);
end;
// <>
operator <> (x, y: Complex): Boolean;
begin
result := (x.re <> y.re) or (x.im <> y.im);
end;
operator <> (x: Double; y: Complex): Boolean;
begin
result := (x <> y.re) or (0.0 <> y.im);
end;
operator <> (x: Complex; y: Double): Boolean;
begin
result := (x.re <> y) or (x.im <> 0.0);
end;
end;
var
x, y, z: Complex;
begin
println Complex.i.re;
println Complex.i.im;
x.re := 10.3;
x.im := 100.2;
y.re := 5.3;
y.im := 5.5;
println x = y;
println x <> y;
z := x + y;
println z.re;
println z.im;
z := z + 5.5;
println z.re;
println z.im;
z := 7 + z;
println z.re;
println z.im;
z := -z;
println z.re;
println z.im;
end.
paxBasic
Structure Complex
Dim re As Double
Dim im As Double
Shared Dim i As Complex = (0.0, 1.0)
' +
Operator + (x As Complex, y As Complex) As Complex
Dim result As Complex = (x.re + y.re, x.im + y.im)
return result
End Operator
Operator + (x As Double, y As Complex) As Complex
Dim result As Complex = (x + y.re, y.im)
return result
End Operator
Operator + (x As Complex, y As Double) As Complex
Dim result As Complex = (x.re + y, x.im)
return result
End Operator
' -
Operator - (x As Complex, y As Complex) As Complex
Dim result As Complex = (x.re - y.re, x.im - y.im)
return result
End Operator
Operator - (x As Double, y As Complex) As Complex
Dim result As Complex = (x - y.re, - y.im)
return result
End Operator
Operator - (x As Complex, y As Double) As Complex
Dim result As Complex = (x.re - y, x.im)
return result
End Operator
' unary -
Operator - (x As Complex) As Complex
Dim result As Complex = (-x.re, -x.im)
return result
End Operator
' *
Operator * (x As Complex, y As Complex) As Complex
Dim result As Complex = (x.re * y.re - x.im * y.im, x.re * y.im + y.re * x.im)
return result
End Operator
Operator * (x As Complex, y As Double) As Complex
Dim result As Complex = (x.re * y, x.im * y)
return result
End Operator
Operator * (x As Double, y As Complex) As Complex
Dim result As Complex = (y.re * x, y.im * x)
return result
End Operator
' /
Operator / (x As Complex, y As Complex) As Complex
Dim result As Complex
Dim d As Double = y.re * y.re + y.im * y.im
result.re = (x.re * y.re + x.im * y.im) / d
result.im = (x.im * y.re - x.re * y.im) / d
return result
End Operator
Operator / (x As Complex, y As Double) As Complex
Dim result As Complex = (x.re / y, x.im / y)
return result
End Operator
Operator / (x As Double, y As Complex) As Complex
Dim result As Complex
Dim d As Double = y.re * y.re + y.im * y.im
result.re = x * y.re / d
result.im = - x * y.im / d
return result
End Operator
' =
Operator = (x As Complex, y As Complex) As Boolean
return (x.re = y.re) and (x.im = y.im)
End Operator
Operator = (x As Double, y As Complex) As Boolean
return (x = y.re) and (0.0 = y.im)
End Operator
Operator = (x As Complex, y As Double) As Boolean
return (x.re = y) and (x.im = 0.0)
End Operator
' <>
Operator <> (x As Complex, y As Complex) As Boolean
return (x.re <> y.re) or (x.im <> y.im)
End Operator
Operator <> (x As Double, y As Complex) As Boolean
return (x <> y.re) or (0.0 <> y.im)
End Operator
Operator <> (x As Complex, y As Double) As Boolean
return (x.re <> y) or (x.im <> 0.0)
End Operator
End Structure
Dim x As Complex, y As Complex, z As Complex
println Complex.i.re
x.re = 56.7
x.im = 8
y.re = 12
y.im = 22.2
z = x + y
println x, y, z
Grammar
structure Complex {
double re, im;
static Complex i = (0.0, 1.0);
// +
Complex operator + (Complex x, Complex y) {
Complex result = (x.re + y.re, x.im + y.im);
return result;
}
Complex operator + (Complex x, double y) {
Complex result = (x.re + y, x.im);
return result;
}
Complex operator + (double x, Complex y) {
Complex result = (x + y.re, y.im);
return result;
}
// -
Complex operator - (Complex x, Complex y) {
Complex result = (x.re - y.re, x.im - y.im);
return result;
}
Complex operator - (Complex x, double y) {
Complex result = (x.re - y, x.im);
return result;
}
Complex operator - (double x, Complex y) {
Complex result = (x - y.re, - y.im);
return result;
}
// unary -
Complex operator - (Complex x) {
Complex result = (-x.re, -x.im);
return result;
}
// *
Complex operator * (Complex x, Complex y) {
Complex result = (x.re * y.re - x.im * y.im, x.re * y.im + y.re * x.im);
return result;
}
Complex operator * (Complex x, double y) {
Complex result = (x.re * y, x.im * y);
return result;
}
Complex operator * (double x, Complex y) {
Complex result = (y.re * x, y.im * x);
return result;
}
// /
Complex operator / (Complex x, Complex y) {
Complex result;
double d;
d = y.re * y.re + y.im * y.im;
result.re = (x.re * y.re + x.im * y.im) / d;
result.im = (x.im * y.re - x.re * y.im) / d;
return result;
}
Complex operator / (Complex x, double y) {
Complex result;
result.re = x.re / y;
result.im = x.im / y;
return result;
}
Complex operator / (double x, Complex y) {
Complex result;
double d;
d = y.re * y.re + y.im * y.im;
result.re = x * y.re / d;
result.im = - x * y.im / d;
return result;
}
// ==
bool operator == (Complex x, Complex y) {
return (x.re == y.re) && (x.im == y.im);
}
bool operator == (Complex x, double y) {
return (x.re == y) && (x.im == 0.0);
}
bool operator == (double x, Complex y) {
return (x == y.re) && (0.0 == y.im);
}
// !=
bool operator != (Complex x, Complex y) {
return (x.re != y.re) || (x.im != y.im);
}
bool operator != (Complex x, double y) {
return (x.re != y) || (x.im != 0.0);
}
bool operator != (double x, Complex y) {
return (x != y.re) || (0.0 != y.im);
}
}
Complex x, y, z;
println Complex.i.re;
x.re = 16.3;
x.im = 7.5;
y.re = 106.7;
y.im = 13.4;
z = x + y;
println z.re;
println z.im;
println x == y;
Copyright © 1999-2008
VIRT Laboratory. All rights reserved.