| 1 | module scheme |
| 2 | |
| 3 | import math |
| 4 | import math.complex |
| 5 | |
| 6 | // Components are immutable numeric handles. Inexact payloads contain only two |
| 7 | // inline floats, using V's standard complex representation without arrays. |
| 8 | @[heap] |
| 9 | struct ExactComplex { |
| 10 | re Value |
| 11 | im Value |
| 12 | } |
| 13 | |
| 14 | @[heap] |
| 15 | struct InexactComplex { |
| 16 | value complex.Complex |
| 17 | } |
| 18 | |
| 19 | @[inline] |
| 20 | fn is_nonreal(value Value) bool { return value.tag in [.exact_complex, .inexact_complex] } |
| 21 | |
| 22 | fn is_real_number(value Value) bool { return is_number(value) && !is_nonreal(value) } |
| 23 | |
| 24 | fn is_inexact_number(value Value) bool { return value.tag in [.real, .inexact_complex] } |
| 25 | |
| 26 | fn (value Value) exact_complex_pointer() &ExactComplex { |
| 27 | return unsafe { &ExactComplex(value.pointer()) } |
| 28 | } |
| 29 | |
| 30 | fn (value Value) inexact_complex_pointer() &InexactComplex { |
| 31 | return unsafe { &InexactComplex(value.pointer()) } |
| 32 | } |
| 33 | |
| 34 | fn complex_parts(value Value) (Value, Value) { |
| 35 | if value.tag == .exact_complex { |
| 36 | p := value.exact_complex_pointer() |
| 37 | return p.re, p.im |
| 38 | } |
| 39 | if value.tag == .inexact_complex { |
| 40 | z := value.inexact_complex_pointer().value |
| 41 | return real(z.re), real(z.im) |
| 42 | } |
| 43 | return value, if value.tag == .real { real(0) } else { integer(0) } |
| 44 | } |
| 45 | |
| 46 | fn inexact_complex(value Value) !complex.Complex { |
| 47 | if value.tag == .inexact_complex { return value.inexact_complex_pointer().value } |
| 48 | x, y := complex_parts(value) |
| 49 | return complex.complex(x.as_real()!, y.as_real()!) |
| 50 | } |
| 51 | |
| 52 | fn owned_inexact_complex(value complex.Complex) Value { |
| 53 | // This dialect follows R5RS's real? classification and one upper-axis cut: |
| 54 | // imaginary signed zero is deliberately lost by scalar canonicalization. |
| 55 | if value.im == 0 { return real(value.re) } |
| 56 | return payload_value(.inexact_complex, &InexactComplex{value}) |
| 57 | } |
| 58 | |
| 59 | fn owned_complex(re Value, im Value) !Value { |
| 60 | if !is_real_number(re) || !is_real_number(im) { |
| 61 | return failure('complex components must be real numbers') |
| 62 | } |
| 63 | if re.tag == .real || im.tag == .real { |
| 64 | return owned_inexact_complex(complex.complex(re.as_real()!, im.as_real()!)) |
| 65 | } |
| 66 | if exact_sign(im) == 0 { return re } |
| 67 | return payload_value(.exact_complex, &ExactComplex{re, im}) |
| 68 | } |
| 69 | |
| 70 | fn snapshot_real(value Value) !Value { |
| 71 | match value.tag { |
| 72 | .integer, .real { return value } |
| 73 | .rational { |
| 74 | r := value.rational_pointer() |
| 75 | return rational(r.numerator, r.denominator) |
| 76 | } |
| 77 | .big_integer { return integer_from_big(value.big_integer_pointer().value) } |
| 78 | .big_rational { |
| 79 | r := value.big_rational_pointer() |
| 80 | return rational_from_big(r.numerator, r.denominator) |
| 81 | } |
| 82 | else { return failure('expected a real number') } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // complex_value snapshots boxed components in the current allocator. Pop a |
| 87 | // source region before constructing a complex value that must outlive it. |
| 88 | pub fn complex_value(re Value, im Value) !Value { |
| 89 | if !is_real_number(re) || !is_real_number(im) { |
| 90 | return failure('complex components must be real numbers') |
| 91 | } |
| 92 | if re.tag == .real || im.tag == .real { return owned_complex(re, im) } |
| 93 | return owned_complex(snapshot_real(re)!, snapshot_real(im)!) |
| 94 | } |
| 95 | |
| 96 | // as_complex_parts exports independently owned real component snapshots into |
| 97 | // the current allocator, also for scalar numbers (whose imaginary part is zero). |
| 98 | pub fn (value Value) as_complex_parts() !(Value, Value) { |
| 99 | if !is_number(value) { return failure('expected a number') } |
| 100 | x, y := complex_parts(value) |
| 101 | return snapshot_real(x)!, snapshot_real(y)! |
| 102 | } |
| 103 | |
| 104 | fn complex_binary(a Value, b Value, operation BigOperation) !Value { |
| 105 | if is_inexact_number(a) || is_inexact_number(b) { |
| 106 | x, y := inexact_complex(a)!, inexact_complex(b)! |
| 107 | return owned_inexact_complex(match operation { |
| 108 | .add { x + y } |
| 109 | .subtract { x - y } |
| 110 | .multiply { finite_complex_product(x, y)! } |
| 111 | .divide { scaled_complex_divide(x, y)! } |
| 112 | }) |
| 113 | } |
| 114 | ar, ai := complex_parts(a) |
| 115 | br, bi := complex_parts(b) |
| 116 | match operation { |
| 117 | .add { return owned_complex(numeric_add(ar, br)!, numeric_add(ai, bi)!) } |
| 118 | .subtract { return owned_complex(numeric_subtract(ar, br)!, numeric_subtract(ai, bi)!) } |
| 119 | .multiply { |
| 120 | return owned_complex(numeric_subtract(numeric_multiply(ar, br)!, numeric_multiply(ai, bi)!)!, |
| 121 | numeric_add(numeric_multiply(ar, bi)!, numeric_multiply(ai, br)!)!) |
| 122 | } |
| 123 | .divide { |
| 124 | norm := numeric_add(numeric_multiply(br, br)!, numeric_multiply(bi, bi)!)! |
| 125 | return owned_complex(numeric_divide(numeric_add(numeric_multiply(ar, br)!, numeric_multiply(ai, bi)!)!, norm)!, |
| 126 | numeric_divide(numeric_subtract(numeric_multiply(ai, br)!, numeric_multiply(ar, bi)!)!, norm)!) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Scale both operands independently by powers of two before norm/products. |
| 132 | // Bounded mantissas avoid spurious norm overflow/underflow, including subnormals. |
| 133 | // A lost tiny component of a highly unbalanced operand is recovered by exact |
| 134 | // binary-rational arithmetic below, rather than by a naive Smith-ratio formula. |
| 135 | fn scaled_complex_divide(a complex.Complex, b complex.Complex) !complex.Complex { |
| 136 | if b.re == 0 && b.im == 0 { return failure('division by zero') } |
| 137 | if !complex_finite(a) || !complex_finite(b) { |
| 138 | return nonfinite_complex_divide(a, b) |
| 139 | } |
| 140 | if a.re == 0 && a.im == 0 { return complex.complex(0, 0) } |
| 141 | _, ae := math.frexp(math.max(math.abs(a.re), math.abs(a.im))) |
| 142 | _, be := math.frexp(math.max(math.abs(b.re), math.abs(b.im))) |
| 143 | ar, ai := math.scalbn(a.re, -ae), math.scalbn(a.im, -ae) |
| 144 | br, bi := math.scalbn(b.re, -be), math.scalbn(b.im, -be) |
| 145 | if (a.re != 0 && ar == 0) || (a.im != 0 && ai == 0) || (b.re != 0 && br == 0) || (b.im != 0 && bi == 0) { |
| 146 | return exact_binary_complex_divide(a, b) |
| 147 | } |
| 148 | if tiny_product(ar, br, ar * br) || tiny_product(ai, bi, ai * bi) |
| 149 | || tiny_product(ai, br, ai * br) || tiny_product(ar, bi, ar * bi) { |
| 150 | return exact_binary_complex_divide(a, b) |
| 151 | } |
| 152 | re := compensated_product_sum(ar, br, ai, bi) |
| 153 | im := compensated_product_sum(ai, br, -ar, bi) |
| 154 | d := br * br + bi * bi |
| 155 | exponent := ae - be |
| 156 | answer := complex.complex(math.scalbn(re / d, exponent), math.scalbn(im / d, exponent)) |
| 157 | if exponent < 0 && ((re != 0 && math.abs(answer.re) < 2.2250738585072014e-308) |
| 158 | || (im != 0 && math.abs(answer.im) < 2.2250738585072014e-308)) { |
| 159 | // Even normal mantissa products can double-round: division rounds |
| 160 | // first, then restoring the exponent rounds again into the subnormal |
| 161 | // range. An exact binary quotient decides the final underflow tie. |
| 162 | return exact_binary_complex_divide(a, b) |
| 163 | } |
| 164 | return answer |
| 165 | } |
| 166 | |
| 167 | fn exact_binary_complex_divide(a complex.Complex, b complex.Complex) !complex.Complex { |
| 168 | x := owned_complex(to_exact(real(a.re))!, to_exact(real(a.im))!)! |
| 169 | y := owned_complex(to_exact(real(b.re))!, to_exact(real(b.im))!)! |
| 170 | return ieee_complex_result(complex_binary(x, y, .divide)!) |
| 171 | } |
| 172 | |
| 173 | fn nonfinite_complex_divide(a complex.Complex, b complex.Complex) complex.Complex { |
| 174 | if complex_finite(a) && (math.is_inf(b.re, 0) || math.is_inf(b.im, 0)) { |
| 175 | br := math.copysign(if math.is_inf(b.re, 0) { 1.0 } else { 0.0 }, b.re) |
| 176 | bi := math.copysign(if math.is_inf(b.im, 0) { 1.0 } else { 0.0 }, b.im) |
| 177 | return complex.complex(0.0 * (a.re * br + a.im * bi), 0.0 * (a.im * br - a.re * bi)) |
| 178 | } |
| 179 | return a / b |
| 180 | } |
| 181 | |
| 182 | fn ieee_exact_component(value Value) !f64 { |
| 183 | return value.as_real() or { |
| 184 | if err is EvalError && err.message == 'inexact result exceeds supported range' { |
| 185 | return math.copysign(math.inf(1), f64(exact_sign(value))) |
| 186 | } |
| 187 | return err |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | fn ieee_complex_result(value Value) !complex.Complex { |
| 192 | x, y := complex_parts(value) |
| 193 | return complex.complex(ieee_exact_component(x)!, ieee_exact_component(y)!) |
| 194 | } |
| 195 | |
| 196 | @[inline] |
| 197 | fn tiny_product(x f64, y f64, product f64) bool { |
| 198 | return x != 0 && y != 0 && math.abs(product) < 2.2250738585072014e-308 |
| 199 | } |
| 200 | |
| 201 | fn finite_complex_product(a complex.Complex, b complex.Complex) !complex.Complex { |
| 202 | ac, bd, ad, bc := a.re * b.re, a.im * b.im, a.re * b.im, a.im * b.re |
| 203 | re := compensated_product_sum(a.re, b.re, -a.im, b.im) |
| 204 | im := compensated_product_sum(a.re, b.im, a.im, b.re) |
| 205 | z := complex.complex(re, im) |
| 206 | if complex_finite(a) && complex_finite(b) && (!complex_finite(z) |
| 207 | || tiny_product(a.re, b.re, ac) || tiny_product(a.im, b.im, bd) |
| 208 | || tiny_product(a.re, b.im, ad) || tiny_product(a.im, b.re, bc)) { |
| 209 | x := owned_complex(to_exact(real(a.re))!, to_exact(real(a.im))!)! |
| 210 | y := owned_complex(to_exact(real(b.re))!, to_exact(real(b.im))!)! |
| 211 | return ieee_complex_result(complex_binary(x, y, .multiply)!) |
| 212 | } |
| 213 | return z |
| 214 | } |
| 215 | |
| 216 | fn complex_finite(z complex.Complex) bool { |
| 217 | return !math.is_nan(z.re) && !math.is_nan(z.im) && !math.is_inf(z.re, 0) && !math.is_inf(z.im, 0) |
| 218 | } |
| 219 | |
| 220 | fn numeric_equal(a Value, b Value) bool { |
| 221 | if is_nonreal(a) || is_nonreal(b) { |
| 222 | if !is_nonreal(a) || !is_nonreal(b) { return false } |
| 223 | ar, ai := complex_parts(a) |
| 224 | br, bi := complex_parts(b) |
| 225 | return numeric_equal(ar, br) && numeric_equal(ai, bi) |
| 226 | } |
| 227 | return numeric_compare(a, b) or { return false } == 0 |
| 228 | } |
| 229 | |
| 230 | fn complex_eqv(a Value, b Value) bool { |
| 231 | ar, ai := complex_parts(a) |
| 232 | br, bi := complex_parts(b) |
| 233 | return eqv(ar, br) && eqv(ai, bi) |
| 234 | } |
| 235 | |
| 236 | fn complex_to_exact(value Value) !Value { |
| 237 | if value.tag != .inexact_complex { return value } |
| 238 | x, y := complex_parts(value) |
| 239 | return owned_complex(to_exact(x)!, to_exact(y)!) |
| 240 | } |
| 241 | |
| 242 | fn exact_perfect_root(value Value) ?Value { |
| 243 | if exact_sign(value) < 0 { return none } |
| 244 | if is_big_exact(value) { |
| 245 | n, d := big_exact_parts(value) or { return none } |
| 246 | nr, dr := n.isqrt_checked() or { return none }, d.isqrt_checked() or { return none } |
| 247 | if nr * nr == n && dr * dr == d { |
| 248 | return reduced_big_rational(nr, dr, false) or { return none } |
| 249 | } |
| 250 | return none |
| 251 | } |
| 252 | n, d := value.as_rational() or { return none } |
| 253 | nr, dr := integer_sqrt(u64(n)), integer_sqrt(u64(d)) |
| 254 | if nr * nr == u64(n) && dr * dr == u64(d) { |
| 255 | return rational(i64(nr), i64(dr)) or { return none } |
| 256 | } |
| 257 | return none |
| 258 | } |
| 259 | |
| 260 | fn complex_magnitude(value Value) !Value { |
| 261 | if !is_nonreal(value) { |
| 262 | if value.tag == .real { return real(math.abs(value.floating())) } |
| 263 | return if exact_sign(value) < 0 { numeric_negate(value)! } else { value } |
| 264 | } |
| 265 | x, y := complex_parts(value) |
| 266 | if value.tag == .exact_complex { |
| 267 | norm := numeric_add(numeric_multiply(x, x)!, numeric_multiply(y, y)!)! |
| 268 | if root := exact_perfect_root(norm) { return root } |
| 269 | } |
| 270 | return real(inexact_complex(value)!.abs()) |
| 271 | } |
| 272 | |
| 273 | fn complex_angle(value Value) !Value { |
| 274 | x, y := complex_parts(value) |
| 275 | if !is_inexact_number(value) { |
| 276 | if exact_sign(x) == 0 && exact_sign(y) == 0 { return failure('angle is undefined at zero') } |
| 277 | ax := if exact_sign(x) < 0 { numeric_negate(x)! } else { x } |
| 278 | ay := if exact_sign(y) < 0 { numeric_negate(y)! } else { y } |
| 279 | if numeric_compare(ax, ay)! >= 0 { |
| 280 | mut a := math.atan(numeric_divide(y, ax)!.as_real()!) |
| 281 | if exact_sign(x) < 0 { a = if exact_sign(y) > 0 { math.pi - a } else { -math.pi - a } } |
| 282 | return real(if exact_sign(x) < 0 && exact_sign(y) == 0 { math.pi } else { a }) |
| 283 | } |
| 284 | a := math.atan(numeric_divide(x, ay)!.as_real()!) |
| 285 | return real(if exact_sign(y) > 0 { math.pi / 2 - a } else { -math.pi / 2 + a }) |
| 286 | } |
| 287 | z := inexact_complex(value)! |
| 288 | if z.re == 0 && z.im == 0 { return failure('angle is undefined at zero') } |
| 289 | a := z.angle() |
| 290 | return real(if z.re < 0 && z.im == 0 { math.pi } else { a }) |
| 291 | } |
| 292 | |