std.math.Complex: Change new() to init()

This commit is contained in:
aiz
2021-05-18 02:57:51 +07:00
committed by GitHub
parent 04d95ea419
commit 5414bd48ed
23 changed files with 164 additions and 161 deletions

View File

@@ -38,9 +38,12 @@ pub fn Complex(comptime T: type) type {
/// Imaginary part.
im: T,
/// Deprecated, use init()
pub const new = init;
/// Create a new Complex number from the given real and imaginary parts.
pub fn new(re: T, im: T) Self {
pub fn init(re: T, im: T) Self {
return Self{
.re = re,
.im = im,
@@ -110,32 +113,32 @@ pub fn Complex(comptime T: type) type {
const epsilon = 0.0001;
test "complex.add" {
const a = Complex(f32).new(5, 3);
const b = Complex(f32).new(2, 7);
const a = Complex(f32).init(5, 3);
const b = Complex(f32).init(2, 7);
const c = a.add(b);
try testing.expect(c.re == 7 and c.im == 10);
}
test "complex.sub" {
const a = Complex(f32).new(5, 3);
const b = Complex(f32).new(2, 7);
const a = Complex(f32).init(5, 3);
const b = Complex(f32).init(2, 7);
const c = a.sub(b);
try testing.expect(c.re == 3 and c.im == -4);
}
test "complex.mul" {
const a = Complex(f32).new(5, 3);
const b = Complex(f32).new(2, 7);
const a = Complex(f32).init(5, 3);
const b = Complex(f32).init(2, 7);
const c = a.mul(b);
try testing.expect(c.re == -11 and c.im == 41);
}
test "complex.div" {
const a = Complex(f32).new(5, 3);
const b = Complex(f32).new(2, 7);
const a = Complex(f32).init(5, 3);
const b = Complex(f32).init(2, 7);
const c = a.div(b);
try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 31) / 53, epsilon) and
@@ -143,14 +146,14 @@ test "complex.div" {
}
test "complex.conjugate" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = a.conjugate();
try testing.expect(c.re == 5 and c.im == -3);
}
test "complex.reciprocal" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = a.reciprocal();
try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 5) / 34, epsilon) and
@@ -158,7 +161,7 @@ test "complex.reciprocal" {
}
test "complex.magnitude" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = a.magnitude();
try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));