zig/lib/std/math/complex/proj.zig
youdi323323 cea89d54fe final
2025-09-07 23:29:36 +09:00

40 lines
964 B
Zig

const std = @import("../../std.zig");
const testing = std.testing;
const math = std.math;
const Complex = math.Complex;
/// 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;
const T = @TypeOf(x, y);
if (math.isInf(x) or math.isInf(y))
return .init(
math.inf(T),
math.copysign(@as(T, 0), x),
);
return .init(x, y);
}
test proj {
{ // Finite complex number
const a: Complex(f32) = .init(5, 3);
const a_proj = proj(a);
try testing.expectEqual(5, a_proj.re);
try testing.expectEqual(3, a_proj.im);
}
{ // Infinity complex number
const inf = math.inf(f32);
const a: Complex(f32) = .init(inf, 2);
const a_proj = proj(a);
try testing.expectEqual(inf, a_proj.re);
try testing.expectEqual(0, a_proj.im);
}
}