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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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));

View File

@ -18,7 +18,7 @@ pub fn abs(z: anytype) @TypeOf(z.re) {
const epsilon = 0.0001;
test "complex.cabs" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = abs(a);
try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));
}

View File

@ -13,13 +13,13 @@ const Complex = cmath.Complex;
pub fn acos(z: anytype) Complex(@TypeOf(z.re)) {
const T = @TypeOf(z.re);
const q = cmath.asin(z);
return Complex(T).new(@as(T, math.pi) / 2 - q.re, -q.im);
return Complex(T).init(@as(T, math.pi) / 2 - q.re, -q.im);
}
const epsilon = 0.0001;
test "complex.cacos" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = acos(a);
try testing.expect(math.approxEqAbs(f32, c.re, 0.546975, epsilon));

View File

@ -13,13 +13,13 @@ const Complex = cmath.Complex;
pub fn acosh(z: anytype) Complex(@TypeOf(z.re)) {
const T = @TypeOf(z.re);
const q = cmath.acos(z);
return Complex(T).new(-q.im, q.re);
return Complex(T).init(-q.im, q.re);
}
const epsilon = 0.0001;
test "complex.cacosh" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = acosh(a);
try testing.expect(math.approxEqAbs(f32, c.re, 2.452914, epsilon));

View File

@ -18,7 +18,7 @@ pub fn arg(z: anytype) @TypeOf(z.re) {
const epsilon = 0.0001;
test "complex.carg" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = arg(a);
try testing.expect(math.approxEqAbs(f32, c, 0.540420, epsilon));
}

View File

@ -15,17 +15,17 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re)) {
const x = z.re;
const y = z.im;
const p = Complex(T).new(1.0 - (x - y) * (x + y), -2.0 * x * y);
const q = Complex(T).new(-y, x);
const p = Complex(T).init(1.0 - (x - y) * (x + y), -2.0 * x * y);
const q = Complex(T).init(-y, x);
const r = cmath.log(q.add(cmath.sqrt(p)));
return Complex(T).new(r.im, -r.re);
return Complex(T).init(r.im, -r.re);
}
const epsilon = 0.0001;
test "complex.casin" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = asin(a);
try testing.expect(math.approxEqAbs(f32, c.re, 1.023822, epsilon));

View File

@ -12,15 +12,15 @@ const Complex = cmath.Complex;
/// Returns the hyperbolic arc-sine of z.
pub fn asinh(z: anytype) Complex(@TypeOf(z.re)) {
const T = @TypeOf(z.re);
const q = Complex(T).new(-z.im, z.re);
const q = Complex(T).init(-z.im, z.re);
const r = cmath.asin(q);
return Complex(T).new(r.im, -r.re);
return Complex(T).init(r.im, -r.re);
}
const epsilon = 0.0001;
test "complex.casinh" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = asinh(a);
try testing.expect(math.approxEqAbs(f32, c.re, 2.459831, epsilon));

View File

@ -50,14 +50,14 @@ fn atan32(z: Complex(f32)) Complex(f32) {
if ((x == 0.0) and (y > 1.0)) {
// overflow
return Complex(f32).new(maxnum, maxnum);
return Complex(f32).init(maxnum, maxnum);
}
const x2 = x * x;
var a = 1.0 - x2 - (y * y);
if (a == 0.0) {
// overflow
return Complex(f32).new(maxnum, maxnum);
return Complex(f32).init(maxnum, maxnum);
}
var t = 0.5 * math.atan2(f32, 2.0 * x, a);
@ -67,12 +67,12 @@ fn atan32(z: Complex(f32)) Complex(f32) {
a = x2 + t * t;
if (a == 0.0) {
// overflow
return Complex(f32).new(maxnum, maxnum);
return Complex(f32).init(maxnum, maxnum);
}
t = y + 1.0;
a = (x2 + (t * t)) / a;
return Complex(f32).new(w, 0.25 * math.ln(a));
return Complex(f32).init(w, 0.25 * math.ln(a));
}
fn redupif64(x: f64) f64 {
@ -99,14 +99,14 @@ fn atan64(z: Complex(f64)) Complex(f64) {
if ((x == 0.0) and (y > 1.0)) {
// overflow
return Complex(f64).new(maxnum, maxnum);
return Complex(f64).init(maxnum, maxnum);
}
const x2 = x * x;
var a = 1.0 - x2 - (y * y);
if (a == 0.0) {
// overflow
return Complex(f64).new(maxnum, maxnum);
return Complex(f64).init(maxnum, maxnum);
}
var t = 0.5 * math.atan2(f64, 2.0 * x, a);
@ -116,18 +116,18 @@ fn atan64(z: Complex(f64)) Complex(f64) {
a = x2 + t * t;
if (a == 0.0) {
// overflow
return Complex(f64).new(maxnum, maxnum);
return Complex(f64).init(maxnum, maxnum);
}
t = y + 1.0;
a = (x2 + (t * t)) / a;
return Complex(f64).new(w, 0.25 * math.ln(a));
return Complex(f64).init(w, 0.25 * math.ln(a));
}
const epsilon = 0.0001;
test "complex.catan32" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = atan(a);
try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon));
@ -135,7 +135,7 @@ test "complex.catan32" {
}
test "complex.catan64" {
const a = Complex(f64).new(5, 3);
const a = Complex(f64).init(5, 3);
const c = atan(a);
try testing.expect(math.approxEqAbs(f64, c.re, 1.423679, epsilon));

View File

@ -12,15 +12,15 @@ const Complex = cmath.Complex;
/// Returns the hyperbolic arc-tangent of z.
pub fn atanh(z: anytype) Complex(@TypeOf(z.re)) {
const T = @TypeOf(z.re);
const q = Complex(T).new(-z.im, z.re);
const q = Complex(T).init(-z.im, z.re);
const r = cmath.atan(q);
return Complex(T).new(r.im, -r.re);
return Complex(T).init(r.im, -r.re);
}
const epsilon = 0.0001;
test "complex.catanh" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = atanh(a);
try testing.expect(math.approxEqAbs(f32, c.re, 0.146947, epsilon));

View File

@ -12,11 +12,11 @@ const Complex = cmath.Complex;
/// Returns the complex conjugate of z.
pub fn conj(z: anytype) Complex(@TypeOf(z.re)) {
const T = @TypeOf(z.re);
return Complex(T).new(z.re, -z.im);
return Complex(T).init(z.re, -z.im);
}
test "complex.conj" {
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);

View File

@ -12,14 +12,14 @@ const Complex = cmath.Complex;
/// Returns the cosine of z.
pub fn cos(z: anytype) Complex(@TypeOf(z.re)) {
const T = @TypeOf(z.re);
const p = Complex(T).new(-z.im, z.re);
const p = Complex(T).init(-z.im, z.re);
return cmath.cosh(p);
}
const epsilon = 0.0001;
test "complex.ccos" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = cos(a);
try testing.expect(math.approxEqAbs(f32, c.re, 2.855815, epsilon));

View File

@ -40,55 +40,55 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
if (ix < 0x7f800000 and iy < 0x7f800000) {
if (iy == 0) {
return Complex(f32).new(math.cosh(x), y);
return Complex(f32).init(math.cosh(x), y);
}
// small x: normal case
if (ix < 0x41100000) {
return Complex(f32).new(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y));
return Complex(f32).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y));
}
// |x|>= 9, so cosh(x) ~= exp(|x|)
if (ix < 0x42b17218) {
// x < 88.7: exp(|x|) won't overflow
const h = math.exp(math.fabs(x)) * 0.5;
return Complex(f32).new(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y));
return Complex(f32).init(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y));
}
// x < 192.7: scale to avoid overflow
else if (ix < 0x4340b1e7) {
const v = Complex(f32).new(math.fabs(x), y);
const v = Complex(f32).init(math.fabs(x), y);
const r = ldexp_cexp(v, -1);
return Complex(f32).new(r.re, r.im * math.copysign(f32, 1, x));
return Complex(f32).init(r.re, r.im * math.copysign(f32, 1, x));
}
// x >= 192.7: result always overflows
else {
const h = 0x1p127 * x;
return Complex(f32).new(h * h * math.cos(y), h * math.sin(y));
return Complex(f32).init(h * h * math.cos(y), h * math.sin(y));
}
}
if (ix == 0 and iy >= 0x7f800000) {
return Complex(f32).new(y - y, math.copysign(f32, 0, x * (y - y)));
return Complex(f32).init(y - y, math.copysign(f32, 0, x * (y - y)));
}
if (iy == 0 and ix >= 0x7f800000) {
if (hx & 0x7fffff == 0) {
return Complex(f32).new(x * x, math.copysign(f32, 0, x) * y);
return Complex(f32).init(x * x, math.copysign(f32, 0, x) * y);
}
return Complex(f32).new(x, math.copysign(f32, 0, (x + x) * y));
return Complex(f32).init(x, math.copysign(f32, 0, (x + x) * y));
}
if (ix < 0x7f800000 and iy >= 0x7f800000) {
return Complex(f32).new(y - y, x * (y - y));
return Complex(f32).init(y - y, x * (y - y));
}
if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) {
if (iy >= 0x7f800000) {
return Complex(f32).new(x * x, x * (y - y));
return Complex(f32).init(x * x, x * (y - y));
}
return Complex(f32).new((x * x) * math.cos(y), x * math.sin(y));
return Complex(f32).init((x * x) * math.cos(y), x * math.sin(y));
}
return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y));
return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));
}
fn cosh64(z: Complex(f64)) Complex(f64) {
@ -108,61 +108,61 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
// nearly non-exceptional case where x, y are finite
if (ix < 0x7ff00000 and iy < 0x7ff00000) {
if (iy | ly == 0) {
return Complex(f64).new(math.cosh(x), x * y);
return Complex(f64).init(math.cosh(x), x * y);
}
// small x: normal case
if (ix < 0x40360000) {
return Complex(f64).new(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y));
return Complex(f64).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y));
}
// |x|>= 22, so cosh(x) ~= exp(|x|)
if (ix < 0x40862e42) {
// x < 710: exp(|x|) won't overflow
const h = math.exp(math.fabs(x)) * 0.5;
return Complex(f64).new(h * math.cos(y), math.copysign(f64, h, x) * math.sin(y));
return Complex(f64).init(h * math.cos(y), math.copysign(f64, h, x) * math.sin(y));
}
// x < 1455: scale to avoid overflow
else if (ix < 0x4096bbaa) {
const v = Complex(f64).new(math.fabs(x), y);
const v = Complex(f64).init(math.fabs(x), y);
const r = ldexp_cexp(v, -1);
return Complex(f64).new(r.re, r.im * math.copysign(f64, 1, x));
return Complex(f64).init(r.re, r.im * math.copysign(f64, 1, x));
}
// x >= 1455: result always overflows
else {
const h = 0x1p1023;
return Complex(f64).new(h * h * math.cos(y), h * math.sin(y));
return Complex(f64).init(h * h * math.cos(y), h * math.sin(y));
}
}
if (ix | lx == 0 and iy >= 0x7ff00000) {
return Complex(f64).new(y - y, math.copysign(f64, 0, x * (y - y)));
return Complex(f64).init(y - y, math.copysign(f64, 0, x * (y - y)));
}
if (iy | ly == 0 and ix >= 0x7ff00000) {
if ((hx & 0xfffff) | lx == 0) {
return Complex(f64).new(x * x, math.copysign(f64, 0, x) * y);
return Complex(f64).init(x * x, math.copysign(f64, 0, x) * y);
}
return Complex(f64).new(x * x, math.copysign(f64, 0, (x + x) * y));
return Complex(f64).init(x * x, math.copysign(f64, 0, (x + x) * y));
}
if (ix < 0x7ff00000 and iy >= 0x7ff00000) {
return Complex(f64).new(y - y, x * (y - y));
return Complex(f64).init(y - y, x * (y - y));
}
if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) {
if (iy >= 0x7ff00000) {
return Complex(f64).new(x * x, x * (y - y));
return Complex(f64).init(x * x, x * (y - y));
}
return Complex(f64).new(x * x * math.cos(y), x * math.sin(y));
return Complex(f64).init(x * x * math.cos(y), x * math.sin(y));
}
return Complex(f64).new((x * x) * (y - y), (x + x) * (y - y));
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
}
const epsilon = 0.0001;
test "complex.ccosh32" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = cosh(a);
try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon));
@ -170,7 +170,7 @@ test "complex.ccosh32" {
}
test "complex.ccosh64" {
const a = Complex(f64).new(5, 3);
const a = Complex(f64).init(5, 3);
const c = cosh(a);
try testing.expect(math.approxEqAbs(f64, c.re, -73.467300, epsilon));

View File

@ -39,25 +39,25 @@ fn exp32(z: Complex(f32)) Complex(f32) {
const hy = @bitCast(u32, y) & 0x7fffffff;
// cexp(x + i0) = exp(x) + i0
if (hy == 0) {
return Complex(f32).new(math.exp(x), y);
return Complex(f32).init(math.exp(x), y);
}
const hx = @bitCast(u32, x);
// cexp(0 + iy) = cos(y) + isin(y)
if ((hx & 0x7fffffff) == 0) {
return Complex(f32).new(math.cos(y), math.sin(y));
return Complex(f32).init(math.cos(y), math.sin(y));
}
if (hy >= 0x7f800000) {
// cexp(finite|nan +- i inf|nan) = nan + i nan
if ((hx & 0x7fffffff) != 0x7f800000) {
return Complex(f32).new(y - y, y - y);
return Complex(f32).init(y - y, y - y);
} // cexp(-inf +- i inf|nan) = 0 + i0
else if (hx & 0x80000000 != 0) {
return Complex(f32).new(0, 0);
return Complex(f32).init(0, 0);
} // cexp(+inf +- i inf|nan) = inf + i nan
else {
return Complex(f32).new(x, y - y);
return Complex(f32).init(x, y - y);
}
}
@ -70,7 +70,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {
// - x = nan
else {
const exp_x = math.exp(x);
return Complex(f32).new(exp_x * math.cos(y), exp_x * math.sin(y));
return Complex(f32).init(exp_x * math.cos(y), exp_x * math.sin(y));
}
}
@ -87,7 +87,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {
// cexp(x + i0) = exp(x) + i0
if (hy | ly == 0) {
return Complex(f64).new(math.exp(x), y);
return Complex(f64).init(math.exp(x), y);
}
const fx = @bitCast(u64, x);
@ -96,19 +96,19 @@ fn exp64(z: Complex(f64)) Complex(f64) {
// cexp(0 + iy) = cos(y) + isin(y)
if ((hx & 0x7fffffff) | lx == 0) {
return Complex(f64).new(math.cos(y), math.sin(y));
return Complex(f64).init(math.cos(y), math.sin(y));
}
if (hy >= 0x7ff00000) {
// cexp(finite|nan +- i inf|nan) = nan + i nan
if (lx != 0 or (hx & 0x7fffffff) != 0x7ff00000) {
return Complex(f64).new(y - y, y - y);
return Complex(f64).init(y - y, y - y);
} // cexp(-inf +- i inf|nan) = 0 + i0
else if (hx & 0x80000000 != 0) {
return Complex(f64).new(0, 0);
return Complex(f64).init(0, 0);
} // cexp(+inf +- i inf|nan) = inf + i nan
else {
return Complex(f64).new(x, y - y);
return Complex(f64).init(x, y - y);
}
}
@ -121,14 +121,14 @@ fn exp64(z: Complex(f64)) Complex(f64) {
// - x = nan
else {
const exp_x = math.exp(x);
return Complex(f64).new(exp_x * math.cos(y), exp_x * math.sin(y));
return Complex(f64).init(exp_x * math.cos(y), exp_x * math.sin(y));
}
}
const epsilon = 0.0001;
test "complex.cexp32" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = exp(a);
try testing.expect(math.approxEqAbs(f32, c.re, -146.927917, epsilon));
@ -136,7 +136,7 @@ test "complex.cexp32" {
}
test "complex.cexp64" {
const a = Complex(f64).new(5, 3);
const a = Complex(f64).init(5, 3);
const c = exp(a);
try testing.expect(math.approxEqAbs(f64, c.re, -146.927917, epsilon));

View File

@ -48,7 +48,7 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
const half_expt2 = exptf - half_expt1;
const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);
return Complex(f32).new(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2);
return Complex(f32).init(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2);
}
fn frexp_exp64(x: f64, expt: *i32) f64 {
@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
const half_expt2 = exptf - half_expt1;
const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20);
return Complex(f64).new(
return Complex(f64).init(
math.cos(z.im) * exp_x * scale1 * scale2,
math.sin(z.im) * exp_x * scale1 * scale2,
);

View File

@ -15,13 +15,13 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re)) {
const r = cmath.abs(z);
const phi = cmath.arg(z);
return Complex(T).new(math.ln(r), phi);
return Complex(T).init(math.ln(r), phi);
}
const epsilon = 0.0001;
test "complex.clog" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = log(a);
try testing.expect(math.approxEqAbs(f32, c.re, 1.763180, epsilon));

View File

@ -19,8 +19,8 @@ pub fn pow(comptime T: type, z: T, c: T) T {
const epsilon = 0.0001;
test "complex.cpow" {
const a = Complex(f32).new(5, 3);
const b = Complex(f32).new(2.3, -1.3);
const a = Complex(f32).init(5, 3);
const b = Complex(f32).init(2.3, -1.3);
const c = pow(Complex(f32), a, b);
try testing.expect(math.approxEqAbs(f32, c.re, 58.049110, epsilon));

View File

@ -14,16 +14,16 @@ pub fn proj(z: anytype) Complex(@TypeOf(z.re)) {
const T = @TypeOf(z.re);
if (math.isInf(z.re) or math.isInf(z.im)) {
return Complex(T).new(math.inf(T), math.copysign(T, 0, z.re));
return Complex(T).init(math.inf(T), math.copysign(T, 0, z.re));
}
return Complex(T).new(z.re, z.im);
return Complex(T).init(z.re, z.im);
}
const epsilon = 0.0001;
test "complex.cproj" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = proj(a);
try testing.expect(c.re == 5 and c.im == 3);

View File

@ -12,15 +12,15 @@ const Complex = cmath.Complex;
/// Returns the sine of z.
pub fn sin(z: anytype) Complex(@TypeOf(z.re)) {
const T = @TypeOf(z.re);
const p = Complex(T).new(-z.im, z.re);
const p = Complex(T).init(-z.im, z.re);
const q = cmath.sinh(p);
return Complex(T).new(q.im, -q.re);
return Complex(T).init(q.im, -q.re);
}
const epsilon = 0.0001;
test "complex.csin" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = sin(a);
try testing.expect(math.approxEqAbs(f32, c.re, -9.654126, epsilon));

View File

@ -40,55 +40,55 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
if (ix < 0x7f800000 and iy < 0x7f800000) {
if (iy == 0) {
return Complex(f32).new(math.sinh(x), y);
return Complex(f32).init(math.sinh(x), y);
}
// small x: normal case
if (ix < 0x41100000) {
return Complex(f32).new(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y));
return Complex(f32).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y));
}
// |x|>= 9, so cosh(x) ~= exp(|x|)
if (ix < 0x42b17218) {
// x < 88.7: exp(|x|) won't overflow
const h = math.exp(math.fabs(x)) * 0.5;
return Complex(f32).new(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y));
return Complex(f32).init(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y));
}
// x < 192.7: scale to avoid overflow
else if (ix < 0x4340b1e7) {
const v = Complex(f32).new(math.fabs(x), y);
const v = Complex(f32).init(math.fabs(x), y);
const r = ldexp_cexp(v, -1);
return Complex(f32).new(r.re * math.copysign(f32, 1, x), r.im);
return Complex(f32).init(r.re * math.copysign(f32, 1, x), r.im);
}
// x >= 192.7: result always overflows
else {
const h = 0x1p127 * x;
return Complex(f32).new(h * math.cos(y), h * h * math.sin(y));
return Complex(f32).init(h * math.cos(y), h * h * math.sin(y));
}
}
if (ix == 0 and iy >= 0x7f800000) {
return Complex(f32).new(math.copysign(f32, 0, x * (y - y)), y - y);
return Complex(f32).init(math.copysign(f32, 0, x * (y - y)), y - y);
}
if (iy == 0 and ix >= 0x7f800000) {
if (hx & 0x7fffff == 0) {
return Complex(f32).new(x, y);
return Complex(f32).init(x, y);
}
return Complex(f32).new(x, math.copysign(f32, 0, y));
return Complex(f32).init(x, math.copysign(f32, 0, y));
}
if (ix < 0x7f800000 and iy >= 0x7f800000) {
return Complex(f32).new(y - y, x * (y - y));
return Complex(f32).init(y - y, x * (y - y));
}
if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) {
if (iy >= 0x7f800000) {
return Complex(f32).new(x * x, x * (y - y));
return Complex(f32).init(x * x, x * (y - y));
}
return Complex(f32).new(x * math.cos(y), math.inf_f32 * math.sin(y));
return Complex(f32).init(x * math.cos(y), math.inf_f32 * math.sin(y));
}
return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y));
return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));
}
fn sinh64(z: Complex(f64)) Complex(f64) {
@ -107,61 +107,61 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
if (ix < 0x7ff00000 and iy < 0x7ff00000) {
if (iy | ly == 0) {
return Complex(f64).new(math.sinh(x), y);
return Complex(f64).init(math.sinh(x), y);
}
// small x: normal case
if (ix < 0x40360000) {
return Complex(f64).new(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y));
return Complex(f64).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y));
}
// |x|>= 22, so cosh(x) ~= exp(|x|)
if (ix < 0x40862e42) {
// x < 710: exp(|x|) won't overflow
const h = math.exp(math.fabs(x)) * 0.5;
return Complex(f64).new(math.copysign(f64, h, x) * math.cos(y), h * math.sin(y));
return Complex(f64).init(math.copysign(f64, h, x) * math.cos(y), h * math.sin(y));
}
// x < 1455: scale to avoid overflow
else if (ix < 0x4096bbaa) {
const v = Complex(f64).new(math.fabs(x), y);
const v = Complex(f64).init(math.fabs(x), y);
const r = ldexp_cexp(v, -1);
return Complex(f64).new(r.re * math.copysign(f64, 1, x), r.im);
return Complex(f64).init(r.re * math.copysign(f64, 1, x), r.im);
}
// x >= 1455: result always overflows
else {
const h = 0x1p1023 * x;
return Complex(f64).new(h * math.cos(y), h * h * math.sin(y));
return Complex(f64).init(h * math.cos(y), h * h * math.sin(y));
}
}
if (ix | lx == 0 and iy >= 0x7ff00000) {
return Complex(f64).new(math.copysign(f64, 0, x * (y - y)), y - y);
return Complex(f64).init(math.copysign(f64, 0, x * (y - y)), y - y);
}
if (iy | ly == 0 and ix >= 0x7ff00000) {
if ((hx & 0xfffff) | lx == 0) {
return Complex(f64).new(x, y);
return Complex(f64).init(x, y);
}
return Complex(f64).new(x, math.copysign(f64, 0, y));
return Complex(f64).init(x, math.copysign(f64, 0, y));
}
if (ix < 0x7ff00000 and iy >= 0x7ff00000) {
return Complex(f64).new(y - y, x * (y - y));
return Complex(f64).init(y - y, x * (y - y));
}
if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) {
if (iy >= 0x7ff00000) {
return Complex(f64).new(x * x, x * (y - y));
return Complex(f64).init(x * x, x * (y - y));
}
return Complex(f64).new(x * math.cos(y), math.inf_f64 * math.sin(y));
return Complex(f64).init(x * math.cos(y), math.inf_f64 * math.sin(y));
}
return Complex(f64).new((x * x) * (y - y), (x + x) * (y - y));
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
}
const epsilon = 0.0001;
test "complex.csinh32" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = sinh(a);
try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon));
@ -169,7 +169,7 @@ test "complex.csinh32" {
}
test "complex.csinh64" {
const a = Complex(f64).new(5, 3);
const a = Complex(f64).init(5, 3);
const c = sinh(a);
try testing.expect(math.approxEqAbs(f64, c.re, -73.460617, epsilon));

View File

@ -32,15 +32,15 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
const y = z.im;
if (x == 0 and y == 0) {
return Complex(f32).new(0, y);
return Complex(f32).init(0, y);
}
if (math.isInf(y)) {
return Complex(f32).new(math.inf(f32), y);
return Complex(f32).init(math.inf(f32), y);
}
if (math.isNan(x)) {
// raise invalid if y is not nan
const t = (y - y) / (y - y);
return Complex(f32).new(x, t);
return Complex(f32).init(x, t);
}
if (math.isInf(x)) {
// sqrt(inf + i nan) = inf + nan i
@ -48,9 +48,9 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
// sqrt(-inf + i nan) = nan +- inf i
// sqrt(-inf + iy) = 0 + inf i
if (math.signbit(x)) {
return Complex(f32).new(math.fabs(x - y), math.copysign(f32, x, y));
return Complex(f32).init(math.fabs(x - y), math.copysign(f32, x, y));
} else {
return Complex(f32).new(x, math.copysign(f32, y - y, y));
return Complex(f32).init(x, math.copysign(f32, y - y, y));
}
}
@ -62,13 +62,13 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
if (dx >= 0) {
const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
return Complex(f32).new(
return Complex(f32).init(
@floatCast(f32, t),
@floatCast(f32, dy / (2.0 * t)),
);
} else {
const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
return Complex(f32).new(
return Complex(f32).init(
@floatCast(f32, math.fabs(y) / (2.0 * t)),
@floatCast(f32, math.copysign(f64, t, y)),
);
@ -83,15 +83,15 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
var y = z.im;
if (x == 0 and y == 0) {
return Complex(f64).new(0, y);
return Complex(f64).init(0, y);
}
if (math.isInf(y)) {
return Complex(f64).new(math.inf(f64), y);
return Complex(f64).init(math.inf(f64), y);
}
if (math.isNan(x)) {
// raise invalid if y is not nan
const t = (y - y) / (y - y);
return Complex(f64).new(x, t);
return Complex(f64).init(x, t);
}
if (math.isInf(x)) {
// sqrt(inf + i nan) = inf + nan i
@ -99,9 +99,9 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
// sqrt(-inf + i nan) = nan +- inf i
// sqrt(-inf + iy) = 0 + inf i
if (math.signbit(x)) {
return Complex(f64).new(math.fabs(x - y), math.copysign(f64, x, y));
return Complex(f64).init(math.fabs(x - y), math.copysign(f64, x, y));
} else {
return Complex(f64).new(x, math.copysign(f64, y - y, y));
return Complex(f64).init(x, math.copysign(f64, y - y, y));
}
}
@ -118,10 +118,10 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
var result: Complex(f64) = undefined;
if (x >= 0) {
const t = math.sqrt((x + math.hypot(f64, x, y)) * 0.5);
result = Complex(f64).new(t, y / (2.0 * t));
result = Complex(f64).init(t, y / (2.0 * t));
} else {
const t = math.sqrt((-x + math.hypot(f64, x, y)) * 0.5);
result = Complex(f64).new(math.fabs(y) / (2.0 * t), math.copysign(f64, t, y));
result = Complex(f64).init(math.fabs(y) / (2.0 * t), math.copysign(f64, t, y));
}
if (scale) {
@ -135,7 +135,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
const epsilon = 0.0001;
test "complex.csqrt32" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = sqrt(a);
try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon));
@ -143,7 +143,7 @@ test "complex.csqrt32" {
}
test "complex.csqrt64" {
const a = Complex(f64).new(5, 3);
const a = Complex(f64).init(5, 3);
const c = sqrt(a);
try testing.expect(math.approxEqAbs(f64, c.re, 2.3271175190399496, epsilon));

View File

@ -12,15 +12,15 @@ const Complex = cmath.Complex;
/// Returns the tanget of z.
pub fn tan(z: anytype) Complex(@TypeOf(z.re)) {
const T = @TypeOf(z.re);
const q = Complex(T).new(-z.im, z.re);
const q = Complex(T).init(-z.im, z.re);
const r = cmath.tanh(q);
return Complex(T).new(r.im, -r.re);
return Complex(T).init(r.im, -r.re);
}
const epsilon = 0.0001;
test "complex.ctan" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = tan(a);
try testing.expect(math.approxEqAbs(f32, c.re, -0.002708233, epsilon));

View File

@ -36,22 +36,22 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
if (ix >= 0x7f800000) {
if (ix & 0x7fffff != 0) {
const r = if (y == 0) y else x * y;
return Complex(f32).new(x, r);
return Complex(f32).init(x, r);
}
const xx = @bitCast(f32, hx - 0x40000000);
const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y);
return Complex(f32).new(xx, math.copysign(f32, 0, r));
return Complex(f32).init(xx, math.copysign(f32, 0, r));
}
if (!math.isFinite(y)) {
const r = if (ix != 0) y - y else x;
return Complex(f32).new(r, y - y);
return Complex(f32).init(r, y - y);
}
// x >= 11
if (ix >= 0x41300000) {
const exp_mx = math.exp(-math.fabs(x));
return Complex(f32).new(math.copysign(f32, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx);
return Complex(f32).init(math.copysign(f32, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx);
}
// Kahan's algorithm
@ -61,7 +61,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
const rho = math.sqrt(1 + s * s);
const den = 1 + beta * s * s;
return Complex(f32).new((beta * rho * s) / den, t / den);
return Complex(f32).init((beta * rho * s) / den, t / den);
}
fn tanh64(z: Complex(f64)) Complex(f64) {
@ -78,23 +78,23 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
if (ix >= 0x7ff00000) {
if ((ix & 0x7fffff) | lx != 0) {
const r = if (y == 0) y else x * y;
return Complex(f64).new(x, r);
return Complex(f64).init(x, r);
}
const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx);
const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y);
return Complex(f64).new(xx, math.copysign(f64, 0, r));
return Complex(f64).init(xx, math.copysign(f64, 0, r));
}
if (!math.isFinite(y)) {
const r = if (ix != 0) y - y else x;
return Complex(f64).new(r, y - y);
return Complex(f64).init(r, y - y);
}
// x >= 22
if (ix >= 0x40360000) {
const exp_mx = math.exp(-math.fabs(x));
return Complex(f64).new(math.copysign(f64, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx);
return Complex(f64).init(math.copysign(f64, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx);
}
// Kahan's algorithm
@ -104,13 +104,13 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
const rho = math.sqrt(1 + s * s);
const den = 1 + beta * s * s;
return Complex(f64).new((beta * rho * s) / den, t / den);
return Complex(f64).init((beta * rho * s) / den, t / den);
}
const epsilon = 0.0001;
test "complex.ctanh32" {
const a = Complex(f32).new(5, 3);
const a = Complex(f32).init(5, 3);
const c = tanh(a);
try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon));
@ -118,7 +118,7 @@ test "complex.ctanh32" {
}
test "complex.ctanh64" {
const a = Complex(f64).new(5, 3);
const a = Complex(f64).init(5, 3);
const c = tanh(a);
try testing.expect(math.approxEqAbs(f64, c.re, 0.999913, epsilon));

View File

@ -1608,13 +1608,13 @@ test "zig fmt: if-else with comment before else" {
\\comptime {
\\ // cexp(finite|nan +- i inf|nan) = nan + i nan
\\ if ((hx & 0x7fffffff) != 0x7f800000) {
\\ return Complex(f32).new(y - y, y - y);
\\ return Complex(f32).init(y - y, y - y);
\\ } // cexp(-inf +- i inf|nan) = 0 + i0
\\ else if (hx & 0x80000000 != 0) {
\\ return Complex(f32).new(0, 0);
\\ return Complex(f32).init(0, 0);
\\ } // cexp(+inf +- i inf|nan) = inf + i nan
\\ else {
\\ return Complex(f32).new(x, y - y);
\\ return Complex(f32).init(x, y - y);
\\ }
\\}
\\
@ -2267,16 +2267,16 @@ test "zig fmt: line comment between if block and else keyword" {
\\test "aoeu" {
\\ // cexp(finite|nan +- i inf|nan) = nan + i nan
\\ if ((hx & 0x7fffffff) != 0x7f800000) {
\\ return Complex(f32).new(y - y, y - y);
\\ return Complex(f32).init(y - y, y - y);
\\ }
\\ // cexp(-inf +- i inf|nan) = 0 + i0
\\ else if (hx & 0x80000000 != 0) {
\\ return Complex(f32).new(0, 0);
\\ return Complex(f32).init(0, 0);
\\ }
\\ // cexp(+inf +- i inf|nan) = inf + i nan
\\ // another comment
\\ else {
\\ return Complex(f32).new(x, y - y);
\\ return Complex(f32).init(x, y - y);
\\ }
\\}
\\