mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
The matrix functions read only mValueA to mValueTY from a record passed in. The typename "Matrix" is on the records they give back.
587 lines
23 KiB
Rust
587 lines
23 KiB
Rust
//! The matrix functions of `app`, as the help pages
|
||
//! <https://ai-scripting.docsforadobe.dev/jsobjref/Matrix/> and
|
||
//! <https://ai-scripting.docsforadobe.dev/jsobjref/Application/> describe them. A matrix is a plain
|
||
//! record with the finite numbers `mValueA`, `mValueB`, `mValueC`, `mValueD`, `mValueTX` and
|
||
//! `mValueTY`; a record passed in needs only those six, and each record a function gives also has
|
||
//! the `typename` `"Matrix"`. Each function other than `isEqualMatrix` and `isSingularMatrix` gives
|
||
//! a new record, those two give a boolean, and none changes the records passed to it.
|
||
//!
|
||
//! The record maps a point `(x, y)` to `(a·x + c·y + tx, b·x + d·y + ty)`, as a PDF `cm` matrix
|
||
//! does (ISO 32000-1, 8.3.4), in script coordinates with Y up.
|
||
//!
|
||
//! VectorCraft's own rules, which the help pages do not state:
|
||
//! - `concatenateMatrix(m, n)` gives the matrix that applies `m` and then `n`; the other
|
||
//! `concatenate…` functions apply `m` and then the rotation, scale or translation they make.
|
||
//! - Left-out arguments are 0 for a translation and an angle, and 100 for a horizontal scale; a
|
||
//! left-out vertical scale is the horizontal one.
|
||
//! - Angles are degrees, positive counterclockwise, and quarter turns are exact.
|
||
//! - A matrix whose determinant is 0 is singular, and `invertMatrix` of one throws.
|
||
//! - `isEqualMatrix` compares the six values exactly.
|
||
//! - An argument that is not a finite number, or a matrix with a value that is not one, throws a
|
||
//! `TypeError`; a result with a value too large for a number throws a `RangeError`.
|
||
|
||
use vectorcraft_js::{Data, HostError, MemberInfo};
|
||
|
||
use super::{Answer, Described, Host, Link, arg, method, named, num, returns, text};
|
||
|
||
/// A matrix's six values: `[a, b, c, d, tx, ty]`.
|
||
pub(super) type Matrix = [f64; 6];
|
||
|
||
const KEYS: [&str; 6] = ["mValueA", "mValueB", "mValueC", "mValueD", "mValueTX", "mValueTY"];
|
||
|
||
const IDENTITY: Matrix = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0];
|
||
|
||
/// The matrix functions of `Application`.
|
||
pub(super) fn app_members(link: &Link) -> Vec<Described> {
|
||
let l = link;
|
||
named(vec![
|
||
("getIdentityMatrix", method(l, identity), returns("Matrix", &[])),
|
||
("getTranslationMatrix", method(l, translation), returns("Matrix", &[("deltaX", "number"), ("deltaY", "number")])),
|
||
("getScaleMatrix", method(l, scale), returns("Matrix", &[("scaleX", "number"), ("scaleY", "number")])),
|
||
("getRotationMatrix", method(l, rotation), returns("Matrix", &[("angle", "number")])),
|
||
("concatenateMatrix", method(l, concatenate), returns("Matrix", &[("matrix", "Matrix"), ("secondMatrix", "Matrix")])),
|
||
(
|
||
"concatenateTranslationMatrix",
|
||
method(l, concatenate_translation),
|
||
returns("Matrix", &[("matrix", "Matrix"), ("deltaX", "number"), ("deltaY", "number")]),
|
||
),
|
||
(
|
||
"concatenateScaleMatrix",
|
||
method(l, concatenate_scale),
|
||
returns("Matrix", &[("matrix", "Matrix"), ("scaleX", "number"), ("scaleY", "number")]),
|
||
),
|
||
("concatenateRotationMatrix", method(l, concatenate_rotation), returns("Matrix", &[("matrix", "Matrix"), ("angle", "number")])),
|
||
("invertMatrix", method(l, invert), returns("Matrix", &[("matrix", "Matrix")])),
|
||
("isEqualMatrix", method(l, is_equal), returns("boolean", &[("matrix", "Matrix"), ("secondMatrix", "Matrix")])),
|
||
("isSingularMatrix", method(l, is_singular), returns("boolean", &[("matrix", "Matrix")])),
|
||
])
|
||
.into_iter()
|
||
.map(|(name, member, info)| (name, member, with_defaults(info)))
|
||
.collect()
|
||
}
|
||
|
||
/// `info` with the defaults of its left-out number arguments, which `reflect` reports: 0 for a
|
||
/// translation and an angle, 100 for a horizontal scale. A left-out vertical scale is the
|
||
/// horizontal one, so it has no fixed default and its description says so.
|
||
fn with_defaults(mut info: MemberInfo) -> MemberInfo {
|
||
for a in &mut info.arguments {
|
||
match a.name.as_str() {
|
||
"deltaX" | "deltaY" | "angle" => a.default = Some(num(0.0)),
|
||
"scaleX" => a.default = Some(num(100.0)),
|
||
"scaleY" => a.description = Some("The vertical scale; when it is left out, the horizontal scale.".into()),
|
||
_ => {}
|
||
}
|
||
}
|
||
info
|
||
}
|
||
|
||
/// The record for `m`, or a `RangeError` when a value is not finite.
|
||
fn record(m: Matrix) -> Answer {
|
||
if m.iter().any(|v| !v.is_finite()) {
|
||
return Err(HostError::Range("the matrix has a value too large for a number".into()));
|
||
}
|
||
let mut items: Vec<(String, Data)> = KEYS.iter().zip(m).map(|(k, v)| ((*k).to_string(), num(v))).collect();
|
||
items.push(("typename".into(), text("Matrix")));
|
||
Ok(Data::Object(items))
|
||
}
|
||
|
||
/// The matrix argument `d`.
|
||
pub(super) fn matrix(d: &Data, what: &str) -> std::result::Result<Matrix, HostError> {
|
||
let bad = || HostError::Type(format!("{what} must be a Matrix"));
|
||
let Data::Object(items) = d else { return Err(bad()) };
|
||
let mut m = [0.0; 6];
|
||
for (slot, k) in m.iter_mut().zip(KEYS) {
|
||
match items.iter().find(|(n, _)| n == k) {
|
||
Some((_, Data::Number(v))) if v.is_finite() => *slot = *v,
|
||
_ => return Err(bad()),
|
||
}
|
||
}
|
||
Ok(m)
|
||
}
|
||
|
||
/// A number argument, `default` when it is left out.
|
||
fn number(d: &Data, what: &str, default: f64) -> std::result::Result<f64, HostError> {
|
||
match d {
|
||
Data::Undefined => Ok(default),
|
||
Data::Number(n) if n.is_finite() => Ok(*n),
|
||
_ => Err(HostError::Type(format!("{what} must be a finite number"))),
|
||
}
|
||
}
|
||
|
||
/// The matrix that applies `m` and then `n`. Adding 0 turns -0 into 0.
|
||
fn then(m: Matrix, n: Matrix) -> Matrix {
|
||
let [a, b, c, d, e, f] = m;
|
||
let [a2, b2, c2, d2, e2, f2] = n;
|
||
[
|
||
products_sum(a, a2, b, c2, 0.0) + 0.0,
|
||
products_sum(a, b2, b, d2, 0.0) + 0.0,
|
||
products_sum(c, a2, d, c2, 0.0) + 0.0,
|
||
products_sum(c, b2, d, d2, 0.0) + 0.0,
|
||
products_sum(e, a2, f, c2, e2) + 0.0,
|
||
products_sum(e, b2, f, d2, f2) + 0.0,
|
||
]
|
||
}
|
||
|
||
/// `x·y + z·w + t`. When at most one of the three terms is not 0 and it is finite, that term is the
|
||
/// sum, rounded once. When both products and their sum are exact numbers, adding `t` rounds once.
|
||
/// Otherwise the products are summed past the range of a number and rounded at
|
||
/// the end, so a finite value is not lost to a product too large or too small for a number, to a
|
||
/// sum that rounds twice, or to a rounding error that a later term cancels down to.
|
||
fn products_sum(x: f64, y: f64, z: f64, w: f64, t: f64) -> f64 {
|
||
let (p, q) = (x * y, z * w);
|
||
let terms = [x != 0.0 && y != 0.0, z != 0.0 && w != 0.0, t != 0.0];
|
||
let total = p + q + t;
|
||
if terms.iter().filter(|&&on| on).count() <= 1 && total.is_finite() {
|
||
return total;
|
||
}
|
||
// A product at or above 2^-969 that rounds drops a part a number can hold, so a zero
|
||
// remainder shows the product is exact.
|
||
let exact = |v: f64, a: f64, b: f64| a == 0.0 || b == 0.0 || (v.abs() >= f64::MIN_POSITIVE * 2f64.powi(53) && a.mul_add(b, -v) == 0.0);
|
||
let plain = p + q;
|
||
if total.is_finite() && exact(p, x, y) && exact(q, z, w) && plain - p == q && plain - q == p {
|
||
return total;
|
||
}
|
||
// Each product is split into its rounded value and the part that rounding drops, both exact.
|
||
// The five parts are added one at a time into a list of values that do not overlap, smallest
|
||
// first, whose sum is exact (Shewchuk's expansion).
|
||
let ([p, p_lost], [q, q_lost]) = (product(x, y), product(z, w));
|
||
let exact = [p, p_lost, q, q_lost, Wide::of(t)].into_iter().fold(Parts::EMPTY, grow);
|
||
nearest(exact)
|
||
}
|
||
|
||
/// `a·b` as its rounded value and the part that rounding drops, both exact.
|
||
fn product(a: f64, b: f64) -> [Wide; 2] {
|
||
let (a, b) = (Wide::of(a), Wide::of(b));
|
||
let (hi, e) = (a.m * b.m, a.e.saturating_add(b.e));
|
||
[Wide::new(hi, e), Wide::new(a.m.mul_add(b.m, 0.0 - hi), e)]
|
||
}
|
||
|
||
/// Values that do not overlap, smallest first, held without allocating. Each value added keeps at
|
||
/// most one more, so the five parts of a sum, the two of a comparison and the sixteen of an
|
||
/// inverse's rounding test fit.
|
||
#[derive(Clone, Copy)]
|
||
struct Parts {
|
||
v: [Wide; 16],
|
||
n: usize,
|
||
}
|
||
|
||
impl Parts {
|
||
const EMPTY: Self = Self { v: [Wide { m: 0.0, e: 0 }; 16], n: 0 };
|
||
|
||
fn as_slice(&self) -> &[Wide] {
|
||
&self.v[..self.n]
|
||
}
|
||
}
|
||
|
||
/// `sums` with `part` added: values that do not overlap, smallest first, whose sum is exactly the
|
||
/// sum of `sums` and `part`. The last value is the largest and has the sign of the sum.
|
||
fn grow(sums: Parts, part: Wide) -> Parts {
|
||
let mut carry = part;
|
||
let mut kept = Parts::EMPTY;
|
||
let mut keep = |w: Wide| {
|
||
if w.m != 0.0 && kept.n < kept.v.len() {
|
||
kept.v[kept.n] = w;
|
||
kept.n += 1;
|
||
}
|
||
};
|
||
for &h in sums.as_slice() {
|
||
let (sum, rest) = carry.two_sum(h);
|
||
keep(rest);
|
||
carry = sum;
|
||
}
|
||
keep(carry);
|
||
kept
|
||
}
|
||
|
||
/// The sign of the exact sum of `sums`, `0.0 - v` and `offset`: 1, -1 or 0.
|
||
fn sign_past(sums: Parts, v: f64, offset: Wide) -> i32 {
|
||
let rest = [Wide::of(0.0 - v), offset].into_iter().fold(sums, grow);
|
||
match rest.as_slice().last() {
|
||
Some(w) if w.m > 0.0 => 1,
|
||
Some(w) if w.m < 0.0 => -1,
|
||
_ => 0,
|
||
}
|
||
}
|
||
|
||
/// The number nearest the exact sum of `sums`, ties to the even one, and infinite when the sum
|
||
/// rounds to 2^1024 or past it. The sum of the values from the smallest up comes within a step of
|
||
/// it; the halfway points to the neighbors of that number are then compared with the exact sum, so
|
||
/// the result is rounded once.
|
||
fn nearest(sums: Parts) -> f64 {
|
||
let even = |v: f64| v.to_bits() & 1 == 0;
|
||
let mut v = sums.as_slice().iter().fold(Wide::of(0.0), |total, &h| total.two_sum(h).0).value().clamp(f64::MIN, f64::MAX);
|
||
for _ in 0..4 {
|
||
// Half the step to the next number up, and to the next number down. Past the largest
|
||
// number the step is to 2^1024, which is 2^971 away.
|
||
let up = if v == f64::MAX { f64::INFINITY } else { v.next_up() };
|
||
let down = if v == f64::MIN { f64::NEG_INFINITY } else { v.next_down() };
|
||
let half_up = if up.is_finite() { Wide::new(up - v, -1) } else { Wide::new(1.0, 970) };
|
||
let half_down = if down.is_finite() { Wide::new(v - down, -1) } else { Wide::new(1.0, 970) };
|
||
match sign_past(sums, v, Wide { m: 0.0 - half_up.m, e: half_up.e }) {
|
||
1 => {
|
||
v = up;
|
||
if !v.is_finite() {
|
||
return v;
|
||
}
|
||
continue;
|
||
}
|
||
0 => return if even(v) { v } else { up },
|
||
_ => {}
|
||
}
|
||
match sign_past(sums, v, half_down) {
|
||
-1 => {
|
||
v = down;
|
||
if !v.is_finite() {
|
||
return v;
|
||
}
|
||
}
|
||
0 => return if even(v) { v } else { down },
|
||
_ => return v,
|
||
}
|
||
}
|
||
v
|
||
}
|
||
|
||
pub(super) fn translate(dx: f64, dy: f64) -> Matrix {
|
||
[1.0, 0.0, 0.0, 1.0, dx, dy]
|
||
}
|
||
|
||
/// A scale by `sx` and `sy` percent.
|
||
pub(super) fn scaled(sx: f64, sy: f64) -> Matrix {
|
||
[sx / 100.0, 0.0, 0.0, sy / 100.0, 0.0, 0.0]
|
||
}
|
||
|
||
/// A turn of `degrees` counterclockwise, exact at quarter turns. The remainder keeps the sign of
|
||
/// `degrees` and is exact, so an angle next to a quarter turn is not rounded onto it.
|
||
pub(super) fn rotate(degrees: f64) -> Matrix {
|
||
let turn = degrees % 360.0;
|
||
let (sin, cos) = match turn {
|
||
0.0 => (0.0, 1.0),
|
||
90.0 | -270.0 => (1.0, 0.0),
|
||
180.0 | -180.0 => (0.0, -1.0),
|
||
270.0 | -90.0 => (-1.0, 0.0),
|
||
_ => turn.to_radians().sin_cos(),
|
||
};
|
||
// `0.0 - sin` rather than `-sin`, so a turn of 0 gives 0 and not -0.
|
||
[cos, sin, 0.0 - sin, cos, 0.0, 0.0]
|
||
}
|
||
|
||
fn translation_of(a: &[Data], at: usize) -> std::result::Result<Matrix, HostError> {
|
||
Ok(translate(number(arg(a, at), "deltaX", 0.0)?, number(arg(a, at + 1), "deltaY", 0.0)?))
|
||
}
|
||
|
||
fn scale_of(a: &[Data], at: usize) -> std::result::Result<Matrix, HostError> {
|
||
let sx = number(arg(a, at), "scaleX", 100.0)?;
|
||
Ok(scaled(sx, number(arg(a, at + 1), "scaleY", sx)?))
|
||
}
|
||
|
||
fn rotation_of(a: &[Data], at: usize) -> std::result::Result<Matrix, HostError> {
|
||
Ok(rotate(number(arg(a, at), "angle", 0.0)?))
|
||
}
|
||
|
||
fn identity(_: &Host, _: &[Data]) -> Answer {
|
||
record(IDENTITY)
|
||
}
|
||
|
||
fn translation(_: &Host, a: &[Data]) -> Answer {
|
||
record(translation_of(a, 1)?)
|
||
}
|
||
|
||
fn scale(_: &Host, a: &[Data]) -> Answer {
|
||
record(scale_of(a, 1)?)
|
||
}
|
||
|
||
fn rotation(_: &Host, a: &[Data]) -> Answer {
|
||
record(rotation_of(a, 1)?)
|
||
}
|
||
|
||
fn concatenate(_: &Host, a: &[Data]) -> Answer {
|
||
record(then(matrix(arg(a, 1), "matrix")?, matrix(arg(a, 2), "secondMatrix")?))
|
||
}
|
||
|
||
fn concatenate_translation(_: &Host, a: &[Data]) -> Answer {
|
||
record(then(matrix(arg(a, 1), "matrix")?, translation_of(a, 2)?))
|
||
}
|
||
|
||
fn concatenate_scale(_: &Host, a: &[Data]) -> Answer {
|
||
record(then(matrix(arg(a, 1), "matrix")?, scale_of(a, 2)?))
|
||
}
|
||
|
||
fn concatenate_rotation(_: &Host, a: &[Data]) -> Answer {
|
||
record(then(matrix(arg(a, 1), "matrix")?, rotation_of(a, 2)?))
|
||
}
|
||
|
||
/// A number as `m · 2^e` with `m` 0 or in `[0.5, 1)`, so products and quotients of such numbers
|
||
/// keep their value however far they go past the range of an `f64`.
|
||
#[derive(Clone, Copy)]
|
||
struct Wide {
|
||
m: f64,
|
||
e: i32,
|
||
}
|
||
|
||
/// `2^e`, for `e` from -1022 to 1023.
|
||
fn pow2(e: i32) -> f64 {
|
||
f64::from_bits(u64::from((e + 1023).clamp(1, 2046).unsigned_abs()) << 52)
|
||
}
|
||
|
||
impl Wide {
|
||
/// `x`, a finite number.
|
||
fn of(x: f64) -> Self {
|
||
if x == 0.0 {
|
||
return Self { m: 0.0, e: 0 };
|
||
}
|
||
if x.abs() < f64::MIN_POSITIVE {
|
||
let w = Self::of(x * pow2(54));
|
||
return Self { m: w.m, e: w.e - 54 };
|
||
}
|
||
let bits = x.to_bits();
|
||
let e = i32::try_from((bits >> 52) & 0x7ff).unwrap_or(0) - 1022;
|
||
Self { m: f64::from_bits((bits & !(0x7ff << 52)) | (1022 << 52)), e }
|
||
}
|
||
|
||
/// `m · 2^e`, with `m` any finite number.
|
||
fn new(m: f64, e: i32) -> Self {
|
||
let w = Self::of(m);
|
||
Self { m: w.m, e: w.e.saturating_add(e) }
|
||
}
|
||
|
||
fn div(self, o: Self) -> Self {
|
||
Self::new(self.m / o.m, self.e.saturating_sub(o.e))
|
||
}
|
||
|
||
/// The sum and the part of it that rounding drops, so that `self + o` is exactly their sum. A
|
||
/// number more than 2^110 times smaller than the other is returned whole as the dropped part.
|
||
fn two_sum(self, o: Self) -> (Self, Self) {
|
||
let (big, small) = if self.m == 0.0 || (o.m != 0.0 && o.e > self.e) { (o, self) } else { (self, o) };
|
||
let gap = small.e.saturating_sub(big.e);
|
||
if small.m == 0.0 || gap < -110 {
|
||
return (big, small);
|
||
}
|
||
// Knuth's two-sum: `small` scaled to `big`'s power of 2 is exact, and so is the error.
|
||
let s = small.m * pow2(gap);
|
||
let sum = big.m + s;
|
||
let back = sum - big.m;
|
||
let rest = (big.m - (sum - back)) + (s - back);
|
||
(Self::new(sum, big.e), Self::new(rest, big.e))
|
||
}
|
||
|
||
/// The nearest `f64`: infinite past its range, and 0 or a subnormal number below it.
|
||
fn value(self) -> f64 {
|
||
let (mut m, mut e) = (self.m, self.e);
|
||
while e > 1000 && m.is_finite() {
|
||
m *= pow2(1000);
|
||
e -= 1000;
|
||
}
|
||
while e < -1000 && m != 0.0 {
|
||
m *= pow2(-1000);
|
||
e += 1000;
|
||
}
|
||
m * pow2(e)
|
||
}
|
||
}
|
||
|
||
/// `a·d − b·c` with no product passing the range of a number; its `m` is 0 only when the two
|
||
/// products are equal. The products are taken whole with fused multiply-adds (Kahan's method), so
|
||
/// a difference that is small next to them keeps its value.
|
||
fn products_difference(a: f64, d: f64, b: f64, c: f64) -> Wide {
|
||
let [a, b, c, d] = [a, b, c, d].map(Wide::of);
|
||
let (ad, bc) = (a.e.saturating_add(d.e), b.e.saturating_add(c.e));
|
||
if b.m == 0.0 || c.m == 0.0 {
|
||
return Wide::new(a.m * d.m, ad);
|
||
}
|
||
if a.m == 0.0 || d.m == 0.0 {
|
||
return Wide::new(0.0 - b.m * c.m, bc);
|
||
}
|
||
// `a·d` and `b·c` both lie in [0.25, 1) times a power of 2; one more than 2^120 times the other
|
||
// decides the sign and size alone.
|
||
let gap = bc.saturating_sub(ad);
|
||
if gap < -120 {
|
||
return Wide::new(a.m * d.m, ad);
|
||
}
|
||
if gap > 120 {
|
||
return Wide::new(0.0 - b.m * c.m, bc);
|
||
}
|
||
let b = b.m * pow2(gap);
|
||
let w = b * c.m;
|
||
let err = (0.0 - b).mul_add(c.m, w);
|
||
Wide::new(a.m.mul_add(d.m, 0.0 - w) + err, ad)
|
||
}
|
||
|
||
/// The determinant of `m`'s linear part.
|
||
fn determinant(m: Matrix) -> Wide {
|
||
products_difference(m[0], m[3], m[1], m[2])
|
||
}
|
||
|
||
/// The square root of the size of `m`'s determinant: the factor by which `m` scales lengths on
|
||
/// average. It is infinite only when that factor is too large for a number.
|
||
pub(super) fn area_scale(m: Matrix) -> f64 {
|
||
let d = determinant(m);
|
||
let (mut v, mut e) = (d.m.abs(), d.e);
|
||
if e % 2 != 0 {
|
||
v *= 2.0;
|
||
e -= 1;
|
||
}
|
||
Wide::new(v.sqrt(), e / 2).value()
|
||
}
|
||
|
||
pub(super) fn singular(m: Matrix) -> bool {
|
||
determinant(m).m == 0.0
|
||
}
|
||
|
||
fn invert(_: &Host, a: &[Data]) -> Answer {
|
||
let m = matrix(arg(a, 1), "matrix")?;
|
||
let det = determinant(m);
|
||
if det.m == 0.0 {
|
||
return Err(HostError::Error("the matrix is singular and cannot be inverted".into()));
|
||
}
|
||
let [a, b, c, d, e, f] = m;
|
||
// Each value is divided by the determinant before it is brought back into the range of a
|
||
// number, and the translations are taken whole, so a finite inverse is not lost to an
|
||
// intermediate value. Adding 0 turns -0 into 0.
|
||
let exact_det = || difference_parts(a, d, b, c);
|
||
let zero = Wide::of(0.0);
|
||
let plain = |v: f64| quotient(Wide::of(v), || [Wide::of(v), zero, zero, zero], det, exact_det);
|
||
record([
|
||
plain(d),
|
||
plain(0.0 - b),
|
||
plain(0.0 - c),
|
||
plain(a),
|
||
quotient(products_difference(c, f, d, e), || difference_parts(c, f, d, e), det, exact_det),
|
||
quotient(products_difference(b, e, a, f), || difference_parts(b, e, a, f), det, exact_det),
|
||
])
|
||
}
|
||
|
||
/// `a·d − b·c` as four exact parts.
|
||
fn difference_parts(a: f64, d: f64, b: f64, c: f64) -> [Wide; 4] {
|
||
let neg = |w: Wide| Wide { m: 0.0 - w.m, e: w.e };
|
||
let ([p, p_lost], [q, q_lost]) = (product(a, d), product(b, c));
|
||
[p, p_lost, neg(q), neg(q_lost)]
|
||
}
|
||
|
||
/// The quotient of `num` by `det`, from their rounded values `num` and `det` and their exact parts.
|
||
/// The result is the rounded quotient, which is within a few steps of the exact one, except where
|
||
/// that would decide overflow wrongly. When the rounded quotient overflows or lies within eight
|
||
/// steps of the largest number, the exact quotient is compared with 2^1024 − 2^970, the halfway
|
||
/// point between the largest number and 2^1024: at it or past it the result is infinite. Below it
|
||
/// a finite rounded quotient is kept, and an infinite one is replaced by the number the exact
|
||
/// quotient rounds to, found from the halfway points 2^1024 − j·2^970 (odd `j`) below, ties to the
|
||
/// even one. When the rounded quotient is below 2^-1023, where every number is a multiple of the
|
||
/// smallest number, the result is the multiple whose halfway points to its neighbors hold the exact
|
||
/// quotient, ties to the even one, so a quotient just past half the smallest number is not rounded
|
||
/// to 0. The exact parts are worked out only then. Adding 0 turns -0 into 0.
|
||
fn quotient(num: Wide, exact_num: impl Fn() -> [Wide; 4], det: Wide, exact_det: impl Fn() -> [Wide; 4]) -> f64 {
|
||
if num.m == 0.0 {
|
||
return 0.0;
|
||
}
|
||
let v = num.div(det).value() + 0.0;
|
||
// 2^1024 − 9·2^971, eight steps below the largest number.
|
||
let near_top = f64::MAX - 8.0 * pow2(971);
|
||
let tiny = f64::MIN_POSITIVE / 2.0;
|
||
if v.is_finite() && v.abs() < near_top && v.abs() >= tiny {
|
||
return v;
|
||
}
|
||
let (exact_num, exact_det) = (exact_num(), exact_det());
|
||
let sign = |parts: &[Wide]| match parts.iter().copied().fold(Parts::EMPTY, grow).as_slice().last() {
|
||
Some(w) if w.m < 0.0 => -1.0,
|
||
_ => 1.0,
|
||
};
|
||
let (sn, sd) = (sign(&exact_num), sign(&exact_det));
|
||
if v.abs() < tiny {
|
||
return small_quotient(v, &exact_num, &exact_det, sn, sd);
|
||
}
|
||
// The sign of |num| − (2^1024 − j·2^970)·|det|. Scaling by a power of 2 is exact, and each
|
||
// `j·m` is split into its rounded value and the part that rounding drops.
|
||
let past = |j: f64| {
|
||
let scaled = |w: Wide, s: f64, e: i32| Wide { m: w.m * s, e: w.e.saturating_add(e) };
|
||
let times_j = |w: Wide| {
|
||
let hi = j * w.m * sd;
|
||
let lo = (j * sd).mul_add(w.m, 0.0 - hi);
|
||
[Wide::new(hi, w.e.saturating_add(970)), Wide::new(lo, w.e.saturating_add(970))]
|
||
};
|
||
let sum = exact_num
|
||
.iter()
|
||
.map(|&w| scaled(w, sn, 0))
|
||
.chain(exact_det.iter().map(|&w| scaled(w, 0.0 - sd, 1024)))
|
||
.chain(exact_det.iter().flat_map(|&w| times_j(w)))
|
||
.fold(Parts::EMPTY, grow);
|
||
match sum.as_slice().last() {
|
||
Some(w) if w.m > 0.0 => 1,
|
||
Some(w) if w.m < 0.0 => -1,
|
||
_ => 0,
|
||
}
|
||
};
|
||
if past(1.0) >= 0 {
|
||
return f64::INFINITY;
|
||
}
|
||
if v.is_finite() {
|
||
return v;
|
||
}
|
||
let even = |x: f64| x.to_bits() & 1 == 0;
|
||
let mut top = f64::MAX;
|
||
for k in 1..16u8 {
|
||
let below = top.next_down();
|
||
match past(f64::from(2 * k + 1)) {
|
||
1 => return top * sn * sd,
|
||
0 => return if even(top) { top } else { below } * sn * sd,
|
||
_ => top = below,
|
||
}
|
||
}
|
||
top * sn * sd
|
||
}
|
||
|
||
/// The exact quotient of `exact_num` by `exact_det`, whose signs are `sn` and `sd`, rounded to a
|
||
/// multiple of the smallest number, ties to the even multiple, starting from the rounded quotient
|
||
/// `v`, which is below 2^-1023 and within a few steps of it.
|
||
fn small_quotient(v: f64, exact_num: &[Wide; 4], exact_det: &[Wide; 4], sn: f64, sd: f64) -> f64 {
|
||
let step = f64::from_bits(1);
|
||
// The sign of |num| − j·2^-1075·|det|, with each `j·m` split into its rounded value and the
|
||
// part that rounding drops. `j` is odd and below 2^53, so it is exact.
|
||
let past = |j: f64| {
|
||
let times_j = |w: Wide| {
|
||
let hi = j * w.m * sd;
|
||
let lo = (j * sd).mul_add(w.m, 0.0 - hi);
|
||
[Wide::new(0.0 - hi, w.e.saturating_sub(1075)), Wide::new(0.0 - lo, w.e.saturating_sub(1075))]
|
||
};
|
||
let sum = exact_num.iter().map(|&w| Wide { m: w.m * sn, e: w.e }).chain(exact_det.iter().flat_map(|&w| times_j(w))).fold(Parts::EMPTY, grow);
|
||
match sum.as_slice().last() {
|
||
Some(w) if w.m > 0.0 => 1,
|
||
Some(w) if w.m < 0.0 => -1,
|
||
_ => 0,
|
||
}
|
||
};
|
||
// `k` steps; dividing by the smallest number is exact.
|
||
let mut k = v.abs() / step;
|
||
let even = |k: f64| k % 2.0 == 0.0;
|
||
for _ in 0..8 {
|
||
match past(2.0 * k + 1.0) {
|
||
1 => k += 1.0,
|
||
0 => {
|
||
k = if even(k) { k } else { k + 1.0 };
|
||
break;
|
||
}
|
||
_ => {
|
||
if k == 0.0 {
|
||
break;
|
||
}
|
||
match past(2.0 * k - 1.0) {
|
||
-1 => k -= 1.0,
|
||
0 => {
|
||
k = if even(k) { k } else { k - 1.0 };
|
||
break;
|
||
}
|
||
_ => break,
|
||
}
|
||
}
|
||
}
|
||
}
|
||
k * step * sn * sd + 0.0
|
||
}
|
||
|
||
fn is_equal(_: &Host, a: &[Data]) -> Answer {
|
||
Ok(Data::Bool(matrix(arg(a, 1), "matrix")? == matrix(arg(a, 2), "secondMatrix")?))
|
||
}
|
||
|
||
fn is_singular(_: &Host, a: &[Data]) -> Answer {
|
||
Ok(Data::Bool(singular(matrix(arg(a, 1), "matrix")?)))
|
||
}
|