module scheme import math import math.complex // Components are immutable numeric handles. Inexact payloads contain only two // inline floats, using V's standard complex representation without arrays. @[heap] struct ExactComplex { re Value im Value } @[heap] struct InexactComplex { value complex.Complex } @[inline] fn is_nonreal(value Value) bool { return value.tag in [.exact_complex, .inexact_complex] } fn is_real_number(value Value) bool { return is_number(value) && !is_nonreal(value) } fn is_inexact_number(value Value) bool { return value.tag in [.real, .inexact_complex] } fn (value Value) exact_complex_pointer() &ExactComplex { return unsafe { &ExactComplex(value.pointer()) } } fn (value Value) inexact_complex_pointer() &InexactComplex { return unsafe { &InexactComplex(value.pointer()) } } fn complex_parts(value Value) (Value, Value) { if value.tag == .exact_complex { p := value.exact_complex_pointer() return p.re, p.im } if value.tag == .inexact_complex { z := value.inexact_complex_pointer().value return real(z.re), real(z.im) } return value, if value.tag == .real { real(0) } else { integer(0) } } fn inexact_complex(value Value) !complex.Complex { if value.tag == .inexact_complex { return value.inexact_complex_pointer().value } x, y := complex_parts(value) return complex.complex(x.as_real()!, y.as_real()!) } fn owned_inexact_complex(value complex.Complex) Value { // This dialect follows R5RS's real? classification and one upper-axis cut: // imaginary signed zero is deliberately lost by scalar canonicalization. if value.im == 0 { return real(value.re) } return payload_value(.inexact_complex, &InexactComplex{value}) } fn owned_complex(re Value, im Value) !Value { if !is_real_number(re) || !is_real_number(im) { return failure('complex components must be real numbers') } if re.tag == .real || im.tag == .real { return owned_inexact_complex(complex.complex(re.as_real()!, im.as_real()!)) } if exact_sign(im) == 0 { return re } return payload_value(.exact_complex, &ExactComplex{re, im}) } fn snapshot_real(value Value) !Value { match value.tag { .integer, .real { return value } .rational { r := value.rational_pointer() return rational(r.numerator, r.denominator) } .big_integer { return integer_from_big(value.big_integer_pointer().value) } .big_rational { r := value.big_rational_pointer() return rational_from_big(r.numerator, r.denominator) } else { return failure('expected a real number') } } } // complex_value snapshots boxed components in the current allocator. Pop a // source region before constructing a complex value that must outlive it. pub fn complex_value(re Value, im Value) !Value { if !is_real_number(re) || !is_real_number(im) { return failure('complex components must be real numbers') } if re.tag == .real || im.tag == .real { return owned_complex(re, im) } return owned_complex(snapshot_real(re)!, snapshot_real(im)!) } // as_complex_parts exports independently owned real component snapshots into // the current allocator, also for scalar numbers (whose imaginary part is zero). pub fn (value Value) as_complex_parts() !(Value, Value) { if !is_number(value) { return failure('expected a number') } x, y := complex_parts(value) return snapshot_real(x)!, snapshot_real(y)! } fn complex_binary(a Value, b Value, operation BigOperation) !Value { if is_inexact_number(a) || is_inexact_number(b) { x, y := inexact_complex(a)!, inexact_complex(b)! return owned_inexact_complex(match operation { .add { x + y } .subtract { x - y } .multiply { finite_complex_product(x, y)! } .divide { scaled_complex_divide(x, y)! } }) } ar, ai := complex_parts(a) br, bi := complex_parts(b) match operation { .add { return owned_complex(numeric_add(ar, br)!, numeric_add(ai, bi)!) } .subtract { return owned_complex(numeric_subtract(ar, br)!, numeric_subtract(ai, bi)!) } .multiply { return owned_complex(numeric_subtract(numeric_multiply(ar, br)!, numeric_multiply(ai, bi)!)!, numeric_add(numeric_multiply(ar, bi)!, numeric_multiply(ai, br)!)!) } .divide { norm := numeric_add(numeric_multiply(br, br)!, numeric_multiply(bi, bi)!)! return owned_complex(numeric_divide(numeric_add(numeric_multiply(ar, br)!, numeric_multiply(ai, bi)!)!, norm)!, numeric_divide(numeric_subtract(numeric_multiply(ai, br)!, numeric_multiply(ar, bi)!)!, norm)!) } } } // Scale both operands independently by powers of two before norm/products. // Bounded mantissas avoid spurious norm overflow/underflow, including subnormals. // A lost tiny component of a highly unbalanced operand is recovered by exact // binary-rational arithmetic below, rather than by a naive Smith-ratio formula. fn scaled_complex_divide(a complex.Complex, b complex.Complex) !complex.Complex { if b.re == 0 && b.im == 0 { return failure('division by zero') } if !complex_finite(a) || !complex_finite(b) { return nonfinite_complex_divide(a, b) } if a.re == 0 && a.im == 0 { return complex.complex(0, 0) } _, ae := math.frexp(math.max(math.abs(a.re), math.abs(a.im))) _, be := math.frexp(math.max(math.abs(b.re), math.abs(b.im))) ar, ai := math.scalbn(a.re, -ae), math.scalbn(a.im, -ae) br, bi := math.scalbn(b.re, -be), math.scalbn(b.im, -be) if (a.re != 0 && ar == 0) || (a.im != 0 && ai == 0) || (b.re != 0 && br == 0) || (b.im != 0 && bi == 0) { return exact_binary_complex_divide(a, b) } if tiny_product(ar, br, ar * br) || tiny_product(ai, bi, ai * bi) || tiny_product(ai, br, ai * br) || tiny_product(ar, bi, ar * bi) { return exact_binary_complex_divide(a, b) } re := compensated_product_sum(ar, br, ai, bi) im := compensated_product_sum(ai, br, -ar, bi) d := br * br + bi * bi exponent := ae - be answer := complex.complex(math.scalbn(re / d, exponent), math.scalbn(im / d, exponent)) if exponent < 0 && ((re != 0 && math.abs(answer.re) < 2.2250738585072014e-308) || (im != 0 && math.abs(answer.im) < 2.2250738585072014e-308)) { // Even normal mantissa products can double-round: division rounds // first, then restoring the exponent rounds again into the subnormal // range. An exact binary quotient decides the final underflow tie. return exact_binary_complex_divide(a, b) } return answer } fn exact_binary_complex_divide(a complex.Complex, b complex.Complex) !complex.Complex { x := owned_complex(to_exact(real(a.re))!, to_exact(real(a.im))!)! y := owned_complex(to_exact(real(b.re))!, to_exact(real(b.im))!)! return ieee_complex_result(complex_binary(x, y, .divide)!) } fn nonfinite_complex_divide(a complex.Complex, b complex.Complex) complex.Complex { if complex_finite(a) && (math.is_inf(b.re, 0) || math.is_inf(b.im, 0)) { br := math.copysign(if math.is_inf(b.re, 0) { 1.0 } else { 0.0 }, b.re) bi := math.copysign(if math.is_inf(b.im, 0) { 1.0 } else { 0.0 }, b.im) return complex.complex(0.0 * (a.re * br + a.im * bi), 0.0 * (a.im * br - a.re * bi)) } return a / b } fn ieee_exact_component(value Value) !f64 { return value.as_real() or { if err is EvalError && err.message == 'inexact result exceeds supported range' { return math.copysign(math.inf(1), f64(exact_sign(value))) } return err } } fn ieee_complex_result(value Value) !complex.Complex { x, y := complex_parts(value) return complex.complex(ieee_exact_component(x)!, ieee_exact_component(y)!) } @[inline] fn tiny_product(x f64, y f64, product f64) bool { return x != 0 && y != 0 && math.abs(product) < 2.2250738585072014e-308 } fn finite_complex_product(a complex.Complex, b complex.Complex) !complex.Complex { ac, bd, ad, bc := a.re * b.re, a.im * b.im, a.re * b.im, a.im * b.re re := compensated_product_sum(a.re, b.re, -a.im, b.im) im := compensated_product_sum(a.re, b.im, a.im, b.re) z := complex.complex(re, im) if complex_finite(a) && complex_finite(b) && (!complex_finite(z) || tiny_product(a.re, b.re, ac) || tiny_product(a.im, b.im, bd) || tiny_product(a.re, b.im, ad) || tiny_product(a.im, b.re, bc)) { x := owned_complex(to_exact(real(a.re))!, to_exact(real(a.im))!)! y := owned_complex(to_exact(real(b.re))!, to_exact(real(b.im))!)! return ieee_complex_result(complex_binary(x, y, .multiply)!) } return z } fn complex_finite(z complex.Complex) bool { return !math.is_nan(z.re) && !math.is_nan(z.im) && !math.is_inf(z.re, 0) && !math.is_inf(z.im, 0) } fn numeric_equal(a Value, b Value) bool { if is_nonreal(a) || is_nonreal(b) { if !is_nonreal(a) || !is_nonreal(b) { return false } ar, ai := complex_parts(a) br, bi := complex_parts(b) return numeric_equal(ar, br) && numeric_equal(ai, bi) } return numeric_compare(a, b) or { return false } == 0 } fn complex_eqv(a Value, b Value) bool { ar, ai := complex_parts(a) br, bi := complex_parts(b) return eqv(ar, br) && eqv(ai, bi) } fn complex_to_exact(value Value) !Value { if value.tag != .inexact_complex { return value } x, y := complex_parts(value) return owned_complex(to_exact(x)!, to_exact(y)!) } fn exact_perfect_root(value Value) ?Value { if exact_sign(value) < 0 { return none } if is_big_exact(value) { n, d := big_exact_parts(value) or { return none } nr, dr := n.isqrt_checked() or { return none }, d.isqrt_checked() or { return none } if nr * nr == n && dr * dr == d { return reduced_big_rational(nr, dr, false) or { return none } } return none } n, d := value.as_rational() or { return none } nr, dr := integer_sqrt(u64(n)), integer_sqrt(u64(d)) if nr * nr == u64(n) && dr * dr == u64(d) { return rational(i64(nr), i64(dr)) or { return none } } return none } fn complex_magnitude(value Value) !Value { if !is_nonreal(value) { if value.tag == .real { return real(math.abs(value.floating())) } return if exact_sign(value) < 0 { numeric_negate(value)! } else { value } } x, y := complex_parts(value) if value.tag == .exact_complex { norm := numeric_add(numeric_multiply(x, x)!, numeric_multiply(y, y)!)! if root := exact_perfect_root(norm) { return root } } return real(inexact_complex(value)!.abs()) } fn complex_angle(value Value) !Value { x, y := complex_parts(value) if !is_inexact_number(value) { if exact_sign(x) == 0 && exact_sign(y) == 0 { return failure('angle is undefined at zero') } ax := if exact_sign(x) < 0 { numeric_negate(x)! } else { x } ay := if exact_sign(y) < 0 { numeric_negate(y)! } else { y } if numeric_compare(ax, ay)! >= 0 { mut a := math.atan(numeric_divide(y, ax)!.as_real()!) if exact_sign(x) < 0 { a = if exact_sign(y) > 0 { math.pi - a } else { -math.pi - a } } return real(if exact_sign(x) < 0 && exact_sign(y) == 0 { math.pi } else { a }) } a := math.atan(numeric_divide(x, ay)!.as_real()!) return real(if exact_sign(y) > 0 { math.pi / 2 - a } else { -math.pi / 2 + a }) } z := inexact_complex(value)! if z.re == 0 && z.im == 0 { return failure('angle is undefined at zero') } a := z.angle() return real(if z.re < 0 && z.im == 0 { math.pi } else { a }) }