use super::trig::{TERMS, fill, reciprocal_factorials};
use crate::structures::numerics::culvert::constants::PI;
use crate::structures::numerics::culvert::{Arithmetic, BarrelClient};
use numr::dtype::DType;
use numr::ops::{
BinaryOps, CompareOps, ConditionalOps, NextRepresentableOps, ScalarOps, ShapeOps, UnaryOps,
};
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::{ArithmeticResult, Interval};
const UNIT: f64 = 1.0 / 9_007_199_254_740_992.0;
const MARGIN: f64 = 1.02;
const HALF_PI_ERROR: f64 = 6.2e-17;
const TAIL: f64 = 3e-24;
const COEFFICIENT_SUM: f64 = 2.6;
const EXACT_FACTORIAL: usize = 18;
fn coefficient(n: usize, bounds: &[[f64; 2]]) -> f64 {
let magnitude = if n <= EXACT_FACTORIAL {
let factorial: u64 = (1..=n as u64).product();
1.0 / factorial as f64
} else {
bounds.get(n).map_or(0.0, |pair| pair[0])
};
if (n / 2).is_multiple_of(2) {
magnitude
} else {
-magnitude
}
}
type Values<R> = [Tensor<R>; 4];
fn float_sin_cos<R>(c: &R::Client, e: &Tensor<R>) -> numr::error::Result<Values<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let bounds = reciprocal_factorials(2 * TERMS);
let x = c.add_scalar(e, -std::f64::consts::FRAC_PI_2)?;
let z = c.mul(&x, &x)?;
let horner = |offset: usize| -> numr::error::Result<[Tensor<R>; 2]> {
let mut value = fill(e, coefficient(offset + 2 * (TERMS - 1), &bounds))?;
let mut running = fill(e, 0.0)?;
for k in (0..TERMS - 1).rev() {
let product = c.mul(&z, &value)?;
let sum = c.add_scalar(&product, coefficient(offset + 2 * k, &bounds))?;
let carried = c.add(&c.mul(&z, &running)?, &c.abs(&product)?)?;
running = c.add(&carried, &c.abs(&sum)?)?;
value = sum;
}
Ok([value, running])
};
let series = |running: &Tensor<R>| -> numr::error::Result<Tensor<R>> {
let inner = c.add_scalar(&c.add(running, &c.mul_scalar(&z, 0.51)?)?, COEFFICIENT_SUM)?;
c.mul_scalar(&inner, UNIT * MARGIN)
};
let size = c.abs(&x)?;
let shift = c.add_scalar(&c.mul_scalar(&size, 1.01 * UNIT)?, HALF_PI_ERROR)?;
let [cos_x, cos_running] = horner(0)?;
let cos_error = c.add_scalar(&c.add(&series(&cos_running)?, &shift)?, TAIL)?;
let [odd, odd_running] = horner(1)?;
let sin_x = c.mul(&x, &odd)?;
let odd_error = c.mul(&size, &series(&odd_running)?)?;
let product_error = c.mul_scalar(&c.abs(&sin_x)?, UNIT * MARGIN)?;
let sin_error = c.add_scalar(&c.add(&c.add(&odd_error, &product_error)?, &shift)?, TAIL)?;
let widen = |value: &Tensor<R>| c.mul_scalar(value, MARGIN);
Ok([
cos_x,
c.neg(&sin_x)?,
widen(&cos_error)?,
widen(&sin_error)?,
])
}
pub(crate) fn sin_cos_ends<R>(
a: &mut Arithmetic<'_, R>,
phi: &Interval<R>,
) -> ArithmeticResult<[Interval<R>; 2]>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let (lo, hi) = (phi.lower(), phi.upper());
let n = lo.numel();
let ends = c.cat(&[lo, hi], 0)?;
let [sine, cosine, sine_error, cosine_error] = float_sin_cos(c, &ends)?;
let half = |value: &Tensor<R>, index: usize| value.narrow(0, index * n, n);
let range = |value: &Tensor<R>, error: &Tensor<R>| -> numr::error::Result<[Tensor<R>; 2]> {
let (at_lo, at_hi) = (half(value, 0)?, half(value, 1)?);
let (err_lo, err_hi) = (half(error, 0)?, half(error, 1)?);
let lower = c.minimum(&c.sub(&at_lo, &err_lo)?, &c.sub(&at_hi, &err_hi)?)?;
let upper = c.maximum(&c.add(&at_lo, &err_lo)?, &c.add(&at_hi, &err_hi)?)?;
Ok([c.next_down(&lower)?, c.next_up(&upper)?])
};
let [sin_lower, sin_upper] = range(&sine, &sine_error)?;
let [cos_lower, cos_upper] = range(&cosine, &cosine_error)?;
let one = fill(lo, 1.0)?;
let minus_one = fill(lo, -1.0)?;
let pi_low = f64::from_bits(PI[0]);
let pi_high = f64::from_bits(PI[1]);
let peak = c.mul(
&c.le(lo, &fill(lo, pi_high * 0.5)?)?,
&c.ge(hi, &fill(lo, pi_low * 0.5)?)?,
)?;
let sin_upper = c.where_cond(&peak, &one, &sin_upper)?;
let zero = fill(lo, 0.0)?;
let top = c.mul(&c.le(lo, &zero)?, &c.ge(hi, &zero)?)?;
let bottom = c.mul(
&c.le(lo, &fill(lo, pi_high)?)?,
&c.ge(hi, &fill(lo, pi_low)?)?,
)?;
let cos_lower = c.where_cond(&bottom, &minus_one, &cos_lower)?;
let cos_upper = c.where_cond(&top, &one, &cos_upper)?;
Ok([
clip(a, &sin_lower, &sin_upper)?,
clip(a, &cos_lower, &cos_upper)?,
])
}
pub(crate) fn sin_cos_near<R>(
a: &mut Arithmetic<'_, R>,
phi: &Interval<R>,
) -> ArithmeticResult<[Interval<R>; 2]>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let (lo, hi) = (phi.lower(), phi.upper());
let middle = c.add(lo, &c.mul_scalar(&c.sub(hi, lo)?, 0.5)?)?;
let middle = c.minimum(&c.maximum(&middle, lo)?, hi)?;
let reach = c.next_up(&c.maximum(&c.sub(&middle, lo)?, &c.sub(hi, &middle)?)?)?;
let [sine, cosine, sine_error, cosine_error] = float_sin_cos(c, &middle)?;
let around = |value: &Tensor<R>, error: &Tensor<R>| -> numr::error::Result<[Tensor<R>; 2]> {
let spread = c.next_up(&c.add(error, &reach)?)?;
Ok([
c.next_down(&c.sub(value, &spread)?)?,
c.next_up(&c.add(value, &spread)?)?,
])
};
let [sin_lower, sin_upper] = around(&sine, &sine_error)?;
let [cos_lower, cos_upper] = around(&cosine, &cosine_error)?;
Ok([
clip(a, &sin_lower, &sin_upper)?,
clip(a, &cos_lower, &cos_upper)?,
])
}
fn clip<R>(
a: &mut Arithmetic<'_, R>,
lower: &Tensor<R>,
upper: &Tensor<R>,
) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let one = fill(lower, 1.0)?;
let minus_one = fill(lower, -1.0)?;
let lower = c.minimum(&c.maximum(lower, &minus_one)?, &one)?;
let upper = c.maximum(&c.minimum(upper, &one)?, &minus_one)?;
a.bounds(&lower, &upper)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::structures::StructureError;
use numr::runtime::cpu::{CpuDevice, CpuRuntime};
use solvr::common::arithmetic::with_interval_arithmetic;
type Bounds = [Vec<[f64; 2]>; 2];
type Form = fn(
&mut Arithmetic<'_, CpuRuntime>,
&Interval<CpuRuntime>,
) -> ArithmeticResult<[Interval<CpuRuntime>; 2]>;
fn evaluate(form: Form, lower: &[f64], upper: &[f64]) -> Result<Bounds, StructureError> {
let device = CpuDevice::new();
let client = CpuRuntime::default_client(&device);
let tensor =
|values: &[f64]| Tensor::<CpuRuntime>::from_slice(values, &[values.len()], &device);
let (lower, upper) = (tensor(lower)?, tensor(upper)?);
let read = |value: &Interval<CpuRuntime>| -> numr::error::Result<Vec<[f64; 2]>> {
let low = value.lower().try_to_vec::<f64>()?;
let high = value.upper().try_to_vec::<f64>()?;
Ok(low.into_iter().zip(high).map(|(l, h)| [l, h]).collect())
};
Ok(with_interval_arithmetic(
&client,
DType::F64,
"series",
|a| {
let phi = a.bounds(&lower, &upper)?;
let [s, k] = form(a, &phi)?;
Ok([read(&s)?, read(&k)?])
},
)?)
}
#[test]
fn point_enclosures_hold_library_values_within_a_few_ulps() -> Result<(), StructureError> {
let angles = [
-0.02,
0.0,
1e-3,
0.5,
1.0,
std::f64::consts::FRAC_PI_2,
2.5,
3.1,
std::f64::consts::PI,
3.16,
];
for form in [sin_cos_ends as Form, sin_cos_near as Form] {
let [sine, cosine] = evaluate(form, &angles, &angles)?;
for ((angle, s), k) in angles.iter().zip(&sine).zip(&cosine) {
for ([low, high], value) in [(*s, angle.sin()), (*k, angle.cos())] {
assert!(
low <= value.next_up() && value.next_down() <= high,
"angle={angle}"
);
let ulps = (high - low) / f64::EPSILON;
assert!(ulps <= 16.0, "angle={angle} width={ulps} eps");
}
}
}
Ok(())
}
#[test]
fn wide_intervals_hold_every_sample_and_both_peaks() -> Result<(), StructureError> {
let lower = [0.0, 1.0, 2.0, -0.02];
let upper = [1.0, 2.0, 3.17, 3.17];
for form in [sin_cos_ends as Form, sin_cos_near as Form] {
let [sine, cosine] = evaluate(form, &lower, &upper)?;
let rows = lower.iter().zip(&upper).zip(sine.iter().zip(&cosine));
for ((low, high), (s, k)) in rows {
for step in 0..=64 {
let t = low + (high - low) * f64::from(step) / 64.0;
assert!(s[0] <= t.sin() && t.sin() <= s[1], "t={t}");
assert!(k[0] <= t.cos() && t.cos() <= k[1], "t={t}");
}
}
}
let [sine, cosine] = evaluate(sin_cos_ends, &lower, &upper)?;
assert_eq!(sine.get(1).map(|s| s[1]), Some(1.0));
assert_eq!(cosine.get(3).map(|k| k[0]), Some(-1.0));
Ok(())
}
}