Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to nalgebra 0.33.0 and simba 0.9.0 #75

Merged
merged 4 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ name = "num_dual"

[dependencies]
num-traits = "0.2"
nalgebra = "0.32"
nalgebra = "0.33"
pyo3 = { version = "0.21", optional = true, features = ["multiple-pymethods", "extension-module", "abi3", "abi3-py37"] }
ndarray = { version = "0.15", optional = true }
numpy = { version = "0.21", optional = true }
approx = "0.5"
simba = "0.8"
simba = "0.9"
serde = { version = "1.0", features = ["derive"], optional = true }

[profile.release]
Expand Down
66 changes: 31 additions & 35 deletions src/derivative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Derivative<T: DualNum<F>, F, R: Dim, C: Dim>(
PhantomData<F>,
)
where
DefaultAllocator: Allocator<T, R, C>;
DefaultAllocator: Allocator<R, C>;

impl<T: DualNum<F> + Copy, F: Copy, const R: usize, const C: usize> Copy
for Derivative<T, F, Const<R>, Const<C>>
Expand All @@ -24,7 +24,7 @@ impl<T: DualNum<F> + Copy, F: Copy, const R: usize, const C: usize> Copy

impl<T: DualNum<F>, F, R: Dim, C: Dim> Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
pub fn new(derivative: Option<OMatrix<T, R, C>>) -> Self {
Self(derivative, PhantomData)
Expand All @@ -41,7 +41,7 @@ where
pub(crate) fn map<T2, F2>(&self, f: impl FnMut(T) -> T2) -> Derivative<T2, F2, R, C>
where
T2: DualNum<F2>,
DefaultAllocator: Allocator<T2, R, C>,
DefaultAllocator: Allocator<R, C>,
{
let opt = self.0.as_ref().map(|eps| eps.map(f));
Derivative::new(opt)
Expand All @@ -59,7 +59,7 @@ where
) -> Derivative<T2, F2, R, C>
where
T2: DualNum<F2>,
DefaultAllocator: Allocator<T2, R, C>,
DefaultAllocator: Allocator<R, C>,
{
let opt = self.0.as_ref().map(move |eps| {
let (nrows, ncols) = eps.shape_generic();
Expand Down Expand Up @@ -88,7 +88,7 @@ where
) -> Option<Derivative<T2, F2, R, C>>
where
T2: DualNum<F2>,
DefaultAllocator: Allocator<T2, R, C>,
DefaultAllocator: Allocator<R, C>,
{
self.0
.as_ref()
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<T: DualNum<F>, F> Derivative<T, F, U1, U1> {

impl<T: DualNum<F>, F, R: Dim, C: Dim> Mul<T> for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Self;

Expand All @@ -169,7 +169,7 @@ where

impl<'a, T: DualNum<F>, F, R: Dim, C: Dim> Mul<T> for &'a Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Derivative<T, F, R, C>;

Expand All @@ -181,7 +181,7 @@ where
impl<'a, 'b, T: DualNum<F>, F, R: Dim, C: Dim, R2: Dim, C2: Dim> Mul<&'b Derivative<T, F, R2, C2>>
for &'a Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C> + Allocator<T, R2, C2> + Allocator<T, R, C2>,
DefaultAllocator: Allocator<R, C> + Allocator<R2, C2> + Allocator<R, C2>,
ShapeConstraint: SameNumberOfRows<C, R2>,
{
type Output = Derivative<T, F, R, C2>;
Expand All @@ -193,7 +193,7 @@ where

impl<T: DualNum<F>, F, R: Dim, C: Dim> Div<T> for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Self;

Expand All @@ -204,7 +204,7 @@ where

impl<'a, T: DualNum<F>, F, R: Dim, C: Dim> Div<T> for &'a Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Derivative<T, F, R, C>;

Expand All @@ -215,14 +215,14 @@ where

impl<T: DualNum<F>, F, R: Dim, C: Dim> Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
pub fn tr_mul<R2: Dim, C2: Dim>(
&self,
rhs: &Derivative<T, F, R2, C2>,
) -> Derivative<T, F, C, C2>
where
DefaultAllocator: Allocator<T, R2, C2> + Allocator<T, C, C2>,
DefaultAllocator: Allocator<R2, C2> + Allocator<C, C2>,
ShapeConstraint: SameNumberOfRows<R, R2>,
{
Derivative::new(
Expand All @@ -236,7 +236,7 @@ where

impl<T: DualNum<F>, F, R: Dim, C: Dim> Add for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Self;

Expand All @@ -253,7 +253,7 @@ where
impl<'a, T: DualNum<F>, F, R: Dim, C: Dim> Add<&'a Derivative<T, F, R, C>>
for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Derivative<T, F, R, C>;

Expand All @@ -269,7 +269,7 @@ where

impl<'a, T: DualNum<F>, F, R: Dim, C: Dim> Add for &'a Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Derivative<T, F, R, C>;

Expand All @@ -285,7 +285,7 @@ where

impl<T: DualNum<F>, F, R: Dim, C: Dim> Sub for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Self;

Expand All @@ -302,7 +302,7 @@ where
impl<'a, T: DualNum<F>, F, R: Dim, C: Dim> Sub<&'a Derivative<T, F, R, C>>
for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Derivative<T, F, R, C>;

Expand All @@ -318,7 +318,7 @@ where

impl<'a, T: DualNum<F>, F, R: Dim, C: Dim> Sub for &'a Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Derivative<T, F, R, C>;

Expand All @@ -334,7 +334,7 @@ where

impl<'a, T: DualNum<F>, F, R: Dim, C: Dim> Neg for &'a Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Derivative<T, F, R, C>;

Expand All @@ -345,7 +345,7 @@ where

impl<T: DualNum<F>, F, R: Dim, C: Dim> Neg for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
type Output = Self;

Expand All @@ -356,7 +356,7 @@ where

impl<T: DualNum<F>, F, R: Dim, C: Dim> AddAssign for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
fn add_assign(&mut self, rhs: Self) {
match (&mut self.0, rhs.0) {
Expand All @@ -369,7 +369,7 @@ where

impl<T: DualNum<F>, F, R: Dim, C: Dim> SubAssign for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
fn sub_assign(&mut self, rhs: Self) {
match (&mut self.0, rhs.0) {
Expand All @@ -382,7 +382,7 @@ where

impl<T: DualNum<F>, F, R: Dim, C: Dim> MulAssign<T> for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
fn mul_assign(&mut self, rhs: T) {
match &mut self.0 {
Expand All @@ -394,7 +394,7 @@ where

impl<T: DualNum<F>, F, R: Dim, C: Dim> DivAssign<T> for Derivative<T, F, R, C>
where
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<R, C>,
{
fn div_assign(&mut self, rhs: T) {
match &mut self.0 {
Expand All @@ -406,18 +406,15 @@ where

impl<T, R: Dim, C: Dim> nalgebra::SimdValue for Derivative<T, T::Element, R, C>
where
DefaultAllocator: Allocator<T, R, C> + Allocator<T::Element, R, C>,
DefaultAllocator: Allocator<R, C>,
T: DualNum<T::Element> + SimdValue + Scalar,
T::Element: DualNum<T::Element> + Scalar + Zero,
{
type Element = Derivative<T::Element, T::Element, R, C>;

type SimdBool = T::SimdBool;

#[inline]
fn lanes() -> usize {
T::lanes()
}
const LANES: usize = T::LANES;

#[inline]
fn splat(val: Self::Element) -> Self {
Expand Down Expand Up @@ -541,12 +538,11 @@ impl<TSuper, FSuper, T, F, R: Dim, C: Dim> SubsetOf<Derivative<TSuper, FSuper, R
where
TSuper: DualNum<FSuper> + SupersetOf<T>,
T: DualNum<F>,
DefaultAllocator: Allocator<T, R, C>,
DefaultAllocator: Allocator<TSuper, R, C>,
// DefaultAllocator: Allocator<TSuper, D>
// + Allocator<TSuper, U1, D>
// + Allocator<TSuper, D, U1>
// + Allocator<TSuper, D, D>,
DefaultAllocator: Allocator<R, C>,
// DefaultAllocator: Allocator<D>
// + Allocator<U1, D>
// + Allocator<D, U1>
// + Allocator<D, D>,
{
#[inline(always)]
fn to_superset(&self) -> Derivative<TSuper, FSuper, R, C> {
Expand Down
4 changes: 2 additions & 2 deletions src/derivatives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ macro_rules! impl_derivatives {
($deriv:ident, $nderiv:expr, $struct:ident, [$($im:ident),*]$(, [$($dim:tt),*])?) => {
impl<T: DualNum<F>, F: DualNumFloat$($(, $dim: Dim)*)?> DualNum<F> for $struct<T, F$($(, $dim)*)?>
where
$($(DefaultAllocator: Allocator<T, $dim> + Allocator<T, U1, $dim> + Allocator<T, $dim, $dim>,)*
DefaultAllocator: Allocator<T$(, $dim)*>)?
$($(DefaultAllocator: Allocator<$dim> + Allocator<U1, $dim> + Allocator<$dim, $dim>,)*
DefaultAllocator: Allocator<$($dim,)*>)?
{
const NDERIV: usize = T::NDERIV + $nderiv;

Expand Down
7 changes: 3 additions & 4 deletions src/dual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ where
type Element = Dual<T::Element, T::Element>;
type SimdBool = T::SimdBool;

#[inline]
fn lanes() -> usize {
T::lanes()
}
const LANES: usize = T::LANES;

#[inline]
fn splat(val: Self::Element) -> Self {
Expand Down Expand Up @@ -383,6 +380,7 @@ where
T: DualNum<T::Element> + SupersetOf<T> + AbsDiffEq<Epsilon = T> + Sync + Send,
T::Element: DualNum<T::Element> + Scalar + DualNumFloat + Sync + Send,
T: SupersetOf<T::Element>,
T: SupersetOf<f32>,
T: SupersetOf<f64>,
T: SimdPartialOrd + PartialOrd,
T: SimdValue<Element = T, SimdBool = bool>,
Expand Down Expand Up @@ -638,6 +636,7 @@ where
T: DualNum<T::Element> + SupersetOf<T> + Sync + Send,
T::Element: DualNum<T::Element> + Scalar + DualNumFloat,
T: SupersetOf<T::Element>,
T: SupersetOf<f32>,
T: SupersetOf<f64>,
T: SimdPartialOrd + PartialOrd,
T: RelativeEq + AbsDiffEq<Epsilon = T>,
Expand Down
24 changes: 9 additions & 15 deletions src/dual2_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::ops::{
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Dual2Vec<T: DualNum<F>, F, D: Dim>
where
DefaultAllocator: Allocator<T, U1, D> + Allocator<T, D, D>,
DefaultAllocator: Allocator<U1, D> + Allocator<D, D>,
{
/// Real part of the second order dual number
pub re: T,
Expand All @@ -36,7 +36,7 @@ pub type Dual2DVec64 = Dual2Vec<f64, f64, Dyn>;

impl<T: DualNum<F>, F, D: Dim> Dual2Vec<T, F, D>
where
DefaultAllocator: Allocator<T, U1, D> + Allocator<T, D, D>,
DefaultAllocator: Allocator<U1, D> + Allocator<D, D>,
{
/// Create a new second order dual number from its fields.
#[inline]
Expand All @@ -52,7 +52,7 @@ where

impl<T: DualNum<F>, F, D: Dim> Dual2Vec<T, F, D>
where
DefaultAllocator: Allocator<T, U1, D> + Allocator<T, D, D>,
DefaultAllocator: Allocator<U1, D> + Allocator<D, D>,
{
/// Create a new second order dual number from the real part.
#[inline]
Expand Down Expand Up @@ -83,10 +83,7 @@ pub fn hessian<G, T: DualNum<F>, F: DualNumFloat, D: Dim>(
) -> (T, OVector<T, D>, OMatrix<T, D, D>)
where
G: FnOnce(OVector<Dual2Vec<T, F, D>, D>) -> Dual2Vec<T, F, D>,
DefaultAllocator: Allocator<T, D>
+ Allocator<T, U1, D>
+ Allocator<T, D, D>
+ Allocator<Dual2Vec<T, F, D>, D>,
DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
try_hessian(|x| Ok::<_, Infallible>(g(x)), x).unwrap()
}
Expand All @@ -99,10 +96,7 @@ pub fn try_hessian<G, T: DualNum<F>, F: DualNumFloat, E, D: Dim>(
) -> Result<(T, OVector<T, D>, OMatrix<T, D, D>), E>
where
G: FnOnce(OVector<Dual2Vec<T, F, D>, D>) -> Result<Dual2Vec<T, F, D>, E>,
DefaultAllocator: Allocator<T, D>
+ Allocator<T, U1, D>
+ Allocator<T, D, D>
+ Allocator<Dual2Vec<T, F, D>, D>,
DefaultAllocator: Allocator<D> + Allocator<U1, D> + Allocator<D, D>,
{
let mut x = x.map(Dual2Vec::from_re);
let (r, c) = x.shape_generic();
Expand All @@ -121,7 +115,7 @@ where
/* chain rule */
impl<T: DualNum<F>, F: Float, D: Dim> Dual2Vec<T, F, D>
where
DefaultAllocator: Allocator<T, U1, D> + Allocator<T, D, D>,
DefaultAllocator: Allocator<U1, D> + Allocator<D, D>,
{
#[inline]
fn chain_rule(&self, f0: T, f1: T, f2: T) -> Self {
Expand All @@ -136,7 +130,7 @@ where
/* product rule */
impl<'a, 'b, T: DualNum<F>, F: Float, D: Dim> Mul<&'a Dual2Vec<T, F, D>> for &'b Dual2Vec<T, F, D>
where
DefaultAllocator: Allocator<T, U1, D> + Allocator<T, D, D>,
DefaultAllocator: Allocator<U1, D> + Allocator<D, D>,
{
type Output = Dual2Vec<T, F, D>;
#[inline]
Expand All @@ -155,7 +149,7 @@ where
/* quotient rule */
impl<'a, 'b, T: DualNum<F>, F: Float, D: Dim> Div<&'a Dual2Vec<T, F, D>> for &'b Dual2Vec<T, F, D>
where
DefaultAllocator: Allocator<T, U1, D> + Allocator<T, D, D>,
DefaultAllocator: Allocator<U1, D> + Allocator<D, D>,
{
type Output = Dual2Vec<T, F, D>;
#[inline]
Expand All @@ -179,7 +173,7 @@ where
/* string conversions */
impl<T: DualNum<F>, F: fmt::Display, D: Dim> fmt::Display for Dual2Vec<T, F, D>
where
DefaultAllocator: Allocator<T, U1, D> + Allocator<T, D, D>,
DefaultAllocator: Allocator<U1, D> + Allocator<D, D>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.re)?;
Expand Down
Loading
Loading