This commit is contained in:
youdi323323 2025-09-07 23:29:36 +09:00
parent 29f38a384e
commit cea89d54fe
21 changed files with 34 additions and 34 deletions

View file

@ -22,7 +22,7 @@ pub const sqrt = @import("complex/sqrt.zig").sqrt;
pub const tanh = @import("complex/tanh.zig").tanh;
pub const tan = @import("complex/tan.zig").tan;
/// A complex number consisting of a real an imaginary part.
/// A complex number consisting of a real and imaginary part.
/// T must be a floating-point value.
pub fn Complex(comptime T: type) type {
return struct {
@ -97,7 +97,7 @@ pub fn Complex(comptime T: type) type {
};
}
/// Calculates the product of complex number and imaginary unit.
/// Calculates the product of a complex number and imaginary unit.
/// You should not manually does ".mul(.i, *)" instead of using this,
/// as its consumes more operations than this.
pub fn mulByI(self: Self) Self {
@ -107,7 +107,7 @@ pub fn Complex(comptime T: type) type {
};
}
/// Calculates the product of complex number and negation of imaginary unit,
/// Calculates the product of a complex number and negation of imaginary unit,
/// thus this rotates 90 degrees clockwise on the complex plane.
/// You should not manually does "*.mul(.i).neg()" (or "*.neg().mul(.i)") instead of using this,
/// as its consumes more operations than this.
@ -133,7 +133,7 @@ pub fn Complex(comptime T: type) type {
return @sqrt(self.squaredMagnitude());
}
/// Calculates the squared magnitude.
/// Calculates the squared magnitude of a complex number.
pub fn squaredMagnitude(self: Self) T {
return self.re * self.re + self.im * self.im;
}
@ -212,10 +212,10 @@ test "multiplication by i yields same result as mulByI" {
const a: TestingComplex = .init(5, 3);
const i_a_natural = a.mulByI();
const i_a_intentional: TestingComplex = .mul(.i, a);
const i_a_unnatural: TestingComplex = .mul(.i, a);
try testing.expectEqual(i_a_intentional.re, i_a_natural.re);
try testing.expectEqual(i_a_intentional.im, i_a_natural.im);
try testing.expectEqual(i_a_unnatural.re, i_a_natural.re);
try testing.expectEqual(i_a_unnatural.im, i_a_natural.im);
}
test "mulByMinusI" {
@ -230,10 +230,10 @@ test "multiplication by negation of i yields same result as mulByMinusI" {
const a: TestingComplex = .init(5, 3);
const minus_i_a_natural = a.mulByMinusI();
const minus_i_a_intentional: TestingComplex = a.mul(.i).neg(); // x.mul(.i).neg() -> -ix
const minus_i_a_unnatural: TestingComplex = a.mul(.i).neg(); // x.mul(.i).neg() -> -ix
try testing.expectEqual(minus_i_a_intentional.re, minus_i_a_natural.re);
try testing.expectEqual(minus_i_a_intentional.im, minus_i_a_natural.im);
try testing.expectEqual(minus_i_a_unnatural.re, minus_i_a_natural.re);
try testing.expectEqual(minus_i_a_unnatural.im, minus_i_a_natural.im);
}
test "i^2 equals to -1" {

View file

@ -3,7 +3,7 @@ const testing = std.testing;
const math = std.math;
const Complex = math.Complex;
/// Calculates the absolute value (modulus) of complex number.
/// Calculates the absolute value (modulus) of a complex number.
pub fn abs(z: anytype) @TypeOf(z.re, z.im) {
return math.hypot(z.re, z.im);
}

View file

@ -5,7 +5,7 @@ const Complex = math.Complex;
const asin = @import("asin.zig").asin;
/// Calculates the arc-cosine of complex number.
/// Calculates the arc-cosine of a complex number.
pub fn acos(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);

View file

@ -5,7 +5,7 @@ const Complex = math.Complex;
const acos = @import("acos.zig").acos;
/// Calculates the hyperbolic arc-cosine of complex number.
/// Calculates the hyperbolic arc-cosine of a complex number.
pub fn acosh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const q = acos(z);

View file

@ -3,7 +3,7 @@ const testing = std.testing;
const math = std.math;
const Complex = math.Complex;
/// Calculates the angular component (in radians) of complex number.
/// Calculates the angular component (in radians) of a complex number.
pub fn arg(z: anytype) @TypeOf(z.re, z.im) {
return math.atan2(z.im, z.re);
}

View file

@ -6,12 +6,12 @@ const Complex = math.Complex;
const sqrt = @import("sqrt.zig").sqrt;
const log = @import("log.zig").log;
/// Calculates the arc-sine of complex number.
/// Calculates the arc-sine of a complex number.
pub fn asin(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const x = z.re;
const y = z.im;
const T = @TypeOf(x);
const T = @TypeOf(x, y);
const p: Complex(T) = .init(
1 - (x - y) * (x + y),
@ -26,8 +26,8 @@ test asin {
const epsilon = math.floatEps(f32);
const a: Complex(f32) = .init(5, 3);
const b = asin(a);
const asin_a = asin(a);
try testing.expectApproxEqAbs(1.0238227, b.re, epsilon);
try testing.expectApproxEqAbs(2.4529128, b.im, epsilon);
try testing.expectApproxEqAbs(1.0238227, asin_a.re, epsilon);
try testing.expectApproxEqAbs(2.4529128, asin_a.im, epsilon);
}

View file

@ -5,7 +5,7 @@ const Complex = math.Complex;
const asin = @import("asin.zig").asin;
/// Calculates the hyperbolic arc-sine of complex number.
/// Calculates the hyperbolic arc-sine of a complex number.
pub fn asinh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return asin(z.mulByI()).mulByMinusI();
}

View file

@ -9,7 +9,7 @@ const testing = std.testing;
const math = std.math;
const Complex = math.Complex;
/// Calculates the arc-tangent of complex number.
/// Calculates the arc-tangent of a complex number.
pub fn atan(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);

View file

@ -5,7 +5,7 @@ const Complex = math.Complex;
const atan = @import("atan.zig").atan;
/// Calculates the hyperbolic arc-tangent of complex number.
/// Calculates the hyperbolic arc-tangent of a complex number.
pub fn atanh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return atan(z.mulByI()).mulByMinusI();
}

View file

@ -5,7 +5,7 @@ const Complex = math.Complex;
const cosh = @import("cosh.zig").cosh;
/// Calculates the cosine of complex number.
/// Calculates the cosine of a complex number.
pub fn cos(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return cosh(z.mulByI());
}

View file

@ -11,7 +11,7 @@ const Complex = math.Complex;
const ldexp = @import("ldexp.zig").ldexp;
/// Calculates the hyperbolic arc-cosine of complex number.
/// Calculates the hyperbolic arc-cosine of a complex number.
pub fn cosh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);

View file

@ -11,7 +11,7 @@ const Complex = math.Complex;
const ldexp = @import("ldexp.zig").ldexp;
/// Calculates e raised to the power of complex number.
/// Calculates e raised to the power of a complex number.
pub fn exp(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);

View file

@ -10,7 +10,7 @@ const math = std.math;
const testing = std.testing;
const Complex = math.Complex;
/// Calculates scaled exp of complex number to avoid overflow.
/// Calculates scaled exp of a complex number to avoid overflow.
pub fn ldexp(z: anytype, expt: i32) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);

View file

@ -6,7 +6,7 @@ const Complex = math.Complex;
const abs = @import("abs.zig").abs;
const arg = @import("arg.zig").arg;
/// Calculates the natural logarithm of complex number.
/// Calculates the natural logarithm of a complex number.
pub fn log(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return .init(@log(abs(z)), arg(z));
}

View file

@ -6,7 +6,7 @@ const Complex = math.Complex;
const exp = @import("exp.zig").exp;
const log = @import("log.zig").log;
/// Calculates z raised to the complex power of complex number.
/// Calculates z raised to the complex power of a complex number.
pub fn pow(z: anytype, s: anytype) Complex(@TypeOf(z.re, z.im, s.re, s.im)) {
return exp(log(z).mul(s));
}

View file

@ -3,7 +3,7 @@ const testing = std.testing;
const math = std.math;
const Complex = math.Complex;
/// Calculates the projection of complex number onto the riemann sphere.
/// Calculates the projection of a complex number onto the riemann sphere.
pub fn proj(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const x = z.re;
const y = z.im;

View file

@ -5,7 +5,7 @@ const Complex = math.Complex;
const sinh = @import("sinh.zig").sinh;
/// Calculates the sine of complex number.
/// Calculates the sine of a complex number.
pub fn sin(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return sinh(z.mulByI()).mulByMinusI();
}

View file

@ -11,7 +11,7 @@ const Complex = math.Complex;
const ldexp = @import("ldexp.zig").ldexp;
/// Calculates the hyperbolic sine of complex number.
/// Calculates the hyperbolic sine of a complex number.
pub fn sinh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);

View file

@ -9,7 +9,7 @@ const testing = std.testing;
const math = std.math;
const Complex = math.Complex;
/// Calculates the square root of complex number. The real and imaginary parts of the result have the same sign
/// Calculates the square root of a complex number. The real and imaginary parts of the result have the same sign
/// as the imaginary part of complex number.
pub fn sqrt(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);

View file

@ -5,7 +5,7 @@ const Complex = math.Complex;
const tanh = @import("tanh.zig").tanh;
/// Calculates the tangent of complex number.
/// Calculates the tangent of a complex number.
pub fn tan(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return tanh(z.mulByI()).mulByMinusI();
}

View file

@ -9,7 +9,7 @@ const testing = std.testing;
const math = std.math;
const Complex = math.Complex;
/// Calculates the hyperbolic tangent of complex number.
/// Calculates the hyperbolic tangent of a complex number.
pub fn tanh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);