authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-16 21:32:53-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-16 21:32:53-04:00
log06a26f0965deff3d752da3d448b34872010d80f3
tree0e41e31ae4370f43011ba1eadfe1d2e4fb6493b2
parent751518787ae9772eb063a4911a10bf30b2c2a19c

std.Complex: use better arg passing convention and fix a TODO


2 files changed, 11 insertions(+), 12 deletions(-)

std/math/complex/index.zig+7-7
...@@ -37,28 +37,28 @@ pub fn Complex(comptime T: type) type {...@@ -37,28 +37,28 @@ pub fn Complex(comptime T: type) type {
37 };37 };
38 }38 }
3939
40 pub fn add(self: *const Self, other: *const Self) Self {40 pub fn add(self: Self, other: Self) Self {
41 return Self{41 return Self{
42 .re = self.re + other.re,42 .re = self.re + other.re,
43 .im = self.im + other.im,43 .im = self.im + other.im,
44 };44 };
45 }45 }
4646
47 pub fn sub(self: *const Self, other: *const Self) Self {47 pub fn sub(self: Self, other: Self) Self {
48 return Self{48 return Self{
49 .re = self.re - other.re,49 .re = self.re - other.re,
50 .im = self.im - other.im,50 .im = self.im - other.im,
51 };51 };
52 }52 }
5353
54 pub fn mul(self: *const Self, other: *const Self) Self {54 pub fn mul(self: Self, other: Self) Self {
55 return Self{55 return Self{
56 .re = self.re * other.re - self.im * other.im,56 .re = self.re * other.re - self.im * other.im,
57 .im = self.im * other.re + self.re * other.im,57 .im = self.im * other.re + self.re * other.im,
58 };58 };
59 }59 }
6060
61 pub fn div(self: *const Self, other: *const Self) Self {61 pub fn div(self: Self, other: Self) Self {
62 const re_num = self.re * other.re + self.im * other.im;62 const re_num = self.re * other.re + self.im * other.im;
63 const im_num = self.im * other.re - self.re * other.im;63 const im_num = self.im * other.re - self.re * other.im;
64 const den = other.re * other.re + other.im * other.im;64 const den = other.re * other.re + other.im * other.im;
...@@ -69,14 +69,14 @@ pub fn Complex(comptime T: type) type {...@@ -69,14 +69,14 @@ pub fn Complex(comptime T: type) type {
69 };69 };
70 }70 }
7171
72 pub fn conjugate(self: *const Self) Self {72 pub fn conjugate(self: Self) Self {
73 return Self{73 return Self{
74 .re = self.re,74 .re = self.re,
75 .im = -self.im,75 .im = -self.im,
76 };76 };
77 }77 }
7878
79 pub fn reciprocal(self: *const Self) Self {79 pub fn reciprocal(self: Self) Self {
80 const m = self.re * self.re + self.im * self.im;80 const m = self.re * self.re + self.im * self.im;
81 return Self{81 return Self{
82 .re = self.re / m,82 .re = self.re / m,
...@@ -84,7 +84,7 @@ pub fn Complex(comptime T: type) type {...@@ -84,7 +84,7 @@ pub fn Complex(comptime T: type) type {
84 };84 };
85 }85 }
8686
87 pub fn magnitude(self: *const Self) T {87 pub fn magnitude(self: Self) T {
88 return math.sqrt(self.re * self.re + self.im * self.im);88 return math.sqrt(self.re * self.re + self.im * self.im);
89 }89 }
90 };90 };
std/math/complex/sqrt.zig+4-5
...@@ -4,18 +4,17 @@ const math = std.math;...@@ -4,18 +4,17 @@ const math = std.math;
4const cmath = math.complex;4const cmath = math.complex;
5const Complex = cmath.Complex;5const Complex = cmath.Complex;
66
7// TODO when #733 is solved this can be @typeOf(z) instead of Complex(@typeOf(z.re))7pub fn sqrt(z: var) @typeOf(z) {
8pub fn sqrt(z: var) Complex(@typeOf(z.re)) {
9 const T = @typeOf(z.re);8 const T = @typeOf(z.re);
109
11 return switch (T) {10 return switch (T) {
12 f32 => sqrt32(z),11 f32 => sqrt32(z),
13 f64 => sqrt64(z),12 f64 => sqrt64(z),
14 else => @compileError("sqrt not implemented for " ++ @typeName(z)),13 else => @compileError("sqrt not implemented for " ++ @typeName(T)),
15 };14 };
16}15}
1716
18fn sqrt32(z: *const Complex(f32)) Complex(f32) {17fn sqrt32(z: Complex(f32)) Complex(f32) {
19 const x = z.re;18 const x = z.re;
20 const y = z.im;19 const y = z.im;
2120
...@@ -57,7 +56,7 @@ fn sqrt32(z: *const Complex(f32)) Complex(f32) {...@@ -57,7 +56,7 @@ fn sqrt32(z: *const Complex(f32)) Complex(f32) {
57 }56 }
58}57}
5958
60fn sqrt64(z: *const Complex(f64)) Complex(f64) {59fn sqrt64(z: Complex(f64)) Complex(f64) {
61 // may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2))60 // may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2))
62 const threshold = 0x1.a827999fcef32p+1022;61 const threshold = 0x1.a827999fcef32p+1022;
6362