pub fn Complex(comptime T: type) type re: TReal part.
im: TImaginary part.
pub fn init(re: T, im: T) SelfCreate a new Complex number from the given real and imaginary parts.
pub fn add(self: Self, other: Self) SelfReturns the sum of two complex numbers.
pub fn sub(self: Self, other: Self) SelfReturns the subtraction of two complex numbers.
pub fn mul(self: Self, other: Self) SelfReturns the product of two complex numbers.
pub fn div(self: Self, other: Self) SelfReturns the quotient of two complex numbers.
pub fn conjugate(self: Self) SelfReturns the complex conjugate of a number.
pub fn neg(self: Self) SelfReturns the negation of a complex number.
pub fn mulbyi(self: Self) SelfReturns the product of complex number and i=sqrt(-1)
pub fn reciprocal(self: Self) SelfReturns the reciprocal of a complex number.
pub fn magnitude(self: Self) TReturns the magnitude of a complex number.
pub fn squaredMagnitude(self: Self) T27
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123pub fn Complex(comptime T: type) type {
return struct {
const Self = @This();
/// Real part.
re: T,
/// Imaginary part.
im: T,
/// Create a new Complex number from the given real and imaginary parts.
pub fn init(re: T, im: T) Self {
return Self{
.re = re,
.im = im,
};
}
/// Returns the sum of two complex numbers.
pub fn add(self: Self, other: Self) Self {
return Self{
.re = self.re + other.re,
.im = self.im + other.im,
};
}
/// Returns the subtraction of two complex numbers.
pub fn sub(self: Self, other: Self) Self {
return Self{
.re = self.re - other.re,
.im = self.im - other.im,
};
}
/// Returns the product of two complex numbers.
pub fn mul(self: Self, other: Self) Self {
return Self{
.re = self.re * other.re - self.im * other.im,
.im = self.im * other.re + self.re * other.im,
};
}
/// Returns the quotient of two complex numbers.
pub fn div(self: Self, other: Self) Self {
const re_num = self.re * other.re + self.im * other.im;
const im_num = self.im * other.re - self.re * other.im;
const den = other.re * other.re + other.im * other.im;
return Self{
.re = re_num / den,
.im = im_num / den,
};
}
/// Returns the complex conjugate of a number.
pub fn conjugate(self: Self) Self {
return Self{
.re = self.re,
.im = -self.im,
};
}
/// Returns the negation of a complex number.
pub fn neg(self: Self) Self {
return Self{
.re = -self.re,
.im = -self.im,
};
}
/// Returns the product of complex number and i=sqrt(-1)
pub fn mulbyi(self: Self) Self {
return Self{
.re = -self.im,
.im = self.re,
};
}
/// Returns the reciprocal of a complex number.
pub fn reciprocal(self: Self) Self {
const m = self.re * self.re + self.im * self.im;
return Self{
.re = self.re / m,
.im = -self.im / m,
};
}
/// Returns the magnitude of a complex number.
pub fn magnitude(self: Self) T {
return @sqrt(self.re * self.re + self.im * self.im);
}
pub fn squaredMagnitude(self: Self) T {
return self.re * self.re + self.im * self.im;
}
};
}
A complex number consisting of a real an imaginary part. T must be a floating-point value.