blob: cbb3b418c8969ad1cb0ba6668732f32c53d38b0a [file] [log] [blame]
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
class Complex {
final double real;
final double imaginary;
const Complex(this.real, this.imaginary);
Complex add(Complex other) {
return new Complex(real + other.real, imaginary + other.imaginary);
}
Complex sub(Complex other) {
return new Complex(real - other.real, imaginary - other.imaginary);
}
Complex negate() {
return new Complex(-real, -imaginary);
}
int get hashCode => real.hashCode * 13 + imaginary.hashCode * 19;
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Complex &&
real == other.real &&
imaginary == other.imaginary;
}
String toString() => 'Complex($real,$imaginary)';
}
extension Operators on Complex {
Complex operator +(Complex other) => add(other);
Complex operator -(Complex other) => sub(other);
Complex operator -() => negate();
}
main() {
Complex c_m2 = new Complex(-2, 2);
Complex c_m1 = new Complex(-1, 1);
Complex c0 = new Complex(0, 0);
Complex c1 = new Complex(1, -1);
Complex c2 = new Complex(2, -2);
expect(c_m2, c0 + c_m2);
expect(c_m2, c_m2 + c0);
expect(c_m2, c_m1 + c_m1);
expect(c_m1, c0 + c_m1);
expect(c_m1, c_m1 + c0);
expect(c0, c_m2 + c2);
expect(c0, c2 + c_m2);
expect(c0, c_m1 + c1);
expect(c0, c1 + c_m1);
expect(c0, c0 + c0);
expect(c1, c0 + c1);
expect(c1, c1 + c0);
expect(c2, c0 + c2);
expect(c2, c2 + c0);
expect(c2, c1 + c1);
expect(c_m2, c0 - c2);
expect(c2, c2 - c0);
expect(c_m2, -c2);
expect(c_m1, c1 - c2);
expect(c1, c2 - c1);
expect(c_m1, c0 - c1);
expect(c1, c1 - c0);
expect(c_m1, -c1);
expect(c0, c2 - c2);
expect(c0, c1 - c1);
expect(c0, c0 - c0);
expect(c0, c_m1 - c_m1);
expect(c0, c_m2 - c_m2);
expect(c0, -c0);
}
expect(expected, actual) {
if (expected != actual) {
throw 'Mismatch: expected=$expected, actual=$actual';
}
}