-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra_operator_overloading.dart
82 lines (63 loc) · 2.14 KB
/
extra_operator_overloading.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import 'dart:core';
import 'dart:math';
class Complex {
final double real;
final double imaginary;
Complex conjugate() {
return Complex(real: this.real, imaginary: -this.imaginary);
}
Complex({this.real = 0, this.imaginary = 0});
Complex.ri(this.real, this.imaginary);
Complex operator +(Complex b) {
return Complex(
real: this.real + b.real, imaginary: this.imaginary + b.imaginary);
}
Complex operator -(Complex b) {
return Complex(
real: this.real - b.real, imaginary: this.imaginary - b.imaginary);
}
Complex operator *(Complex b) {
return Complex(
real: this.real * b.real - this.imaginary * b.imaginary,
imaginary: this.real * b.imaginary + this.imaginary * b.real);
}
Complex operator /(Complex b) {
// https://stackoverflow.com/a/41146661/6846888
var conjugation = b.conjugate();
var denominatorRes = b * conjugation;
// denominator has only real part
var denominator = denominatorRes.real;
var nominator = this * conjugation;
return Complex(
real: nominator.real / denominator,
imaginary: nominator.imaginary / denominator);
}
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Complex &&
other.real == real &&
other.imaginary == imaginary;
}
@override
int get hashCode => real.hashCode ^ imaginary.hashCode;
double get magnitude => sqrt(real * real + imaginary * imaginary);
// Complex get conjugate => Complex(real: real, imaginary: -imaginary);
Complex scale(double factor) =>
Complex(real: real * factor, imaginary: imaginary * factor);
@override
String toString() {
return 'Complex(real: ${real}, imaginary: ${imaginary})';
}
}
void main(List<String> args) {
final complex1 = Complex.ri(3, 4); // 3 + 4i
final complex2 = Complex.ri(1, 2); // 1 + 2i
final sum = complex1 + complex2;
final difference = complex1 - complex2;
final product = complex1 * complex2;
final quotient = complex1 / complex2;
print('Sum : $sum');
print('Difference: $difference');
print('Product : $product');
print('Quotient : $quotient');
}