use extendr_api::*;
use faer::traits::{ComplexField, RealField};
use faer_entity::SimpleEntity;
use num_traits::float::TotalOrder;
use num_traits::{Float, FromPrimitive, ToPrimitive};
use std::fmt::Display;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, Sub, SubAssign};
pub trait BixverseFloat:
Float
+ FromPrimitive
+ ToPrimitive
+ ComplexField
+ Copy
+ 'static
+ RealField
+ AddAssign
+ DivAssign
+ SubAssign
+ Div
+ Display
+ Sub
+ Mul
+ Div
+ TotalOrder
+ Clone
{
}
impl<T> BixverseFloat for T where
T: Float
+ FromPrimitive
+ ToPrimitive
+ ComplexField
+ Copy
+ 'static
+ RealField
+ AddAssign
+ DivAssign
+ SubAssign
+ Div
+ Display
+ Sub
+ Mul
+ Div
+ TotalOrder
+ Clone
{
}
pub trait BixverseNumeric:
Clone
+ Default
+ Copy
+ Sync
+ Send
+ Add<Output = Self>
+ Mul<Output = Self>
+ Sub<Output = Self>
+ AddAssign
+ PartialEq
+ Sub
+ DivAssign
{
}
impl<T> BixverseNumeric for T where
T: Clone
+ Default
+ Copy
+ Sync
+ Send
+ Add<Output = Self>
+ Mul<Output = Self>
+ Sub<Output = Self>
+ AddAssign
+ PartialEq
+ Sub
+ DivAssign
{
}
pub trait VecConvert<U> {
fn r_int_convert(self) -> Vec<U>;
fn r_int_convert_shift(self) -> Vec<U>;
}
impl VecConvert<i32> for Vec<usize> {
fn r_int_convert(self) -> Vec<i32> {
self.into_iter().map(|x| x as i32).collect()
}
fn r_int_convert_shift(self) -> Vec<i32> {
self.into_iter().map(|x| (x as i32) + 1).collect()
}
}
impl VecConvert<i32> for &[&usize] {
fn r_int_convert(self) -> Vec<i32> {
self.iter().map(|&&x| x as i32).collect()
}
fn r_int_convert_shift(self) -> Vec<i32> {
self.iter().map(|&&x| (x as i32) + 1).collect()
}
}
impl VecConvert<i32> for Vec<u32> {
fn r_int_convert(self) -> Vec<i32> {
self.into_iter().map(|x| x as i32).collect()
}
fn r_int_convert_shift(self) -> Vec<i32> {
self.into_iter().map(|x| (x as i32) + 1).collect()
}
}
impl VecConvert<usize> for Vec<i32> {
fn r_int_convert(self) -> Vec<usize> {
self.into_iter().map(|x| x as usize).collect()
}
fn r_int_convert_shift(self) -> Vec<usize> {
self.into_iter().map(|x| (x - 1) as usize).collect()
}
}
impl VecConvert<i32> for &[usize] {
fn r_int_convert(self) -> Vec<i32> {
self.iter().map(|&x| x as i32).collect()
}
fn r_int_convert_shift(self) -> Vec<i32> {
self.iter().map(|&x| (x as i32) + 1).collect()
}
}
impl VecConvert<usize> for &[i32] {
fn r_int_convert(self) -> Vec<usize> {
self.iter().map(|&x| x as usize).collect()
}
fn r_int_convert_shift(self) -> Vec<usize> {
self.iter().map(|&x| (x - 1) as usize).collect()
}
}
pub trait VecFloatConvert<U> {
fn r_float_convert(self) -> Vec<U>;
}
impl VecFloatConvert<f32> for Vec<f64> {
fn r_float_convert(self) -> Vec<f32> {
self.into_iter().map(|x| x as f32).collect()
}
}
impl VecFloatConvert<f64> for Vec<f32> {
fn r_float_convert(self) -> Vec<f64> {
self.into_iter().map(|x| x as f64).collect()
}
}
impl VecFloatConvert<f64> for &[f32] {
fn r_float_convert(self) -> Vec<f64> {
self.iter().map(|x| *x as f64).collect()
}
}
impl VecFloatConvert<f32> for &[f64] {
fn r_float_convert(self) -> Vec<f32> {
self.iter().map(|&x| x as f32).collect()
}
}
pub trait FaerRType: SimpleEntity + Copy + Clone + 'static {
type RType: Copy + Clone;
fn to_r_matrix(x: faer::MatRef<Self>) -> extendr_api::RArray<Self::RType, [usize; 2]>;
}
impl FaerRType for f64 {
type RType = f64;
fn to_r_matrix(x: faer::MatRef<Self>) -> extendr_api::RArray<Self, [usize; 2]> {
let nrow = x.nrows();
let ncol = x.ncols();
RArray::new_matrix(nrow, ncol, |row, column| x[(row, column)])
}
}
impl FaerRType for i32 {
type RType = i32;
fn to_r_matrix(x: faer::MatRef<Self>) -> extendr_api::RArray<Self, [usize; 2]> {
let nrow = x.nrows();
let ncol = x.ncols();
RArray::new_matrix(nrow, ncol, |row, column| x[(row, column)])
}
}
impl FaerRType for f32 {
type RType = f64;
fn to_r_matrix(x: faer::MatRef<Self>) -> extendr_api::RArray<f64, [usize; 2]> {
let nrow = x.nrows();
let ncol = x.ncols();
RArray::new_matrix(nrow, ncol, |row, column| x[(row, column)] as f64)
}
}