use crate::Float;
use crate::test_util::common::rug_float_significant_bits;
use crate::test_util::float::arithmetic::sum::naive_sum_prec_round;
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_base::num::logic::traits::SignificantBits;
use malachite_base::rounding_modes::RoundingMode::{self, *};
use rug::float::Round;
use rug::ops::AssignRound;
use std::cmp::Ordering;
pub fn naive_dot_prec_round(
xs: &[Float],
ys: &[Float],
prec: u64,
rm: RoundingMode,
) -> (Float, Ordering) {
assert_eq!(xs.len(), ys.len());
let terms: Vec<Float> = xs
.iter()
.zip(ys.iter())
.map(|(x, y)| {
x.mul_prec_round_ref_ref(y, x.significant_bits() + y.significant_bits(), Exact)
.0
})
.collect();
naive_sum_prec_round(&terms, prec, rm)
}
#[inline]
pub fn naive_dot_prec(xs: &[Float], ys: &[Float], prec: u64) -> (Float, Ordering) {
naive_dot_prec_round(xs, ys, prec, Nearest)
}
fn naive_max_prec(xs: &[Float], ys: &[Float]) -> u64 {
xs.iter()
.chain(ys.iter())
.map(SignificantBits::significant_bits)
.max()
.unwrap_or(1)
}
#[inline]
pub fn naive_dot_round(xs: &[Float], ys: &[Float], rm: RoundingMode) -> (Float, Ordering) {
naive_dot_prec_round(xs, ys, naive_max_prec(xs, ys), rm)
}
#[inline]
pub fn naive_dot(xs: &[Float], ys: &[Float]) -> Float {
naive_dot_prec_round(xs, ys, naive_max_prec(xs, ys), Nearest).0
}
pub fn rug_dot_prec_round(
xs: &[rug::Float],
ys: &[rug::Float],
prec: u64,
rm: Round,
) -> (rug::Float, Ordering) {
let mut dot = rug::Float::with_val(u32::exact_from(prec), 0);
let o = dot.assign_round(rug::Float::dot(xs.iter().zip(ys.iter())), rm);
(dot, o)
}
#[inline]
pub fn rug_dot_prec(xs: &[rug::Float], ys: &[rug::Float], prec: u64) -> (rug::Float, Ordering) {
rug_dot_prec_round(xs, ys, prec, Round::Nearest)
}
fn rug_max_prec(xs: &[rug::Float], ys: &[rug::Float]) -> u64 {
xs.iter()
.chain(ys.iter())
.map(rug_float_significant_bits)
.max()
.unwrap_or(1)
}
#[inline]
pub fn rug_dot_round(xs: &[rug::Float], ys: &[rug::Float], rm: Round) -> (rug::Float, Ordering) {
rug_dot_prec_round(xs, ys, rug_max_prec(xs, ys), rm)
}
pub fn rug_dot(xs: &[rug::Float], ys: &[rug::Float]) -> rug::Float {
rug_dot_prec_round(xs, ys, rug_max_prec(xs, ys), Round::Nearest).0
}