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 PyO3 0.21 #72

Merged
merged 1 commit into from
Apr 12, 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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.9.0] - 2024-04-11
### Packaging
- Updated `pyo3` and `numpy` dependencies to 0.21 and adapted to the new `Bound` API.

## [0.8.1] - 2023-10-20
### Packaging
- Un-deprecated the `linalg` module.
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "num-dual"
version = "0.8.1"
version = "0.9.0"
authors = ["Gernot Bauer <bauer@itt.uni-stuttgart.de>",
"Philipp Rehner <prehner@ethz.ch>"]
edition = "2021"
Expand All @@ -19,9 +19,9 @@ name = "num_dual"
[dependencies]
num-traits = "0.2"
nalgebra = "0.32"
pyo3 = { version = "0.20", optional = true, features = ["multiple-pymethods", "extension-module", "abi3", "abi3-py37"] }
pyo3 = { version = "0.21", optional = true, features = ["multiple-pymethods", "extension-module", "abi3", "abi3-py37"] }
ndarray = { version = "0.15", optional = true }
numpy = { version = "0.20", optional = true }
numpy = { version = "0.21", optional = true }
approx = "0.5"
simba = "0.8"

Expand Down
48 changes: 13 additions & 35 deletions src/linalg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,15 @@ mod tests {
#[test]
fn test_solve_dual64() {
let a = arr2(&[
[Dual64::new_scalar(4.0, 3.0), Dual64::new_scalar(3.0, 3.0)],
[Dual64::new_scalar(6.0, 1.0), Dual64::new_scalar(3.0, 2.0)],
]);
let b = arr1(&[
Dual64::new_scalar(10.0, 20.0),
Dual64::new_scalar(12.0, 20.0),
[Dual64::new(4.0, 3.0), Dual64::new(3.0, 3.0)],
[Dual64::new(6.0, 1.0), Dual64::new(3.0, 2.0)],
]);
let b = arr1(&[Dual64::new(10.0, 20.0), Dual64::new(12.0, 20.0)]);
let lu = LU::new(a).unwrap();
let det = lu.determinant();
assert_eq!((det.re, det.eps.unwrap()), (-6.0, -4.0));
assert_eq!((det.re, det.eps), (-6.0, -4.0));
let x = lu.solve(&b);
assert_eq!(
(x[0].re, x[0].eps.unwrap(), x[1].re, x[1].eps.unwrap()),
(1.0, 2.0, 2.0, 1.0)
);
assert_eq!((x[0].re, x[0].eps, x[1].re, x[1].eps), (1.0, 2.0, 2.0, 1.0));
}

#[test]
Expand Down Expand Up @@ -358,8 +352,8 @@ mod tests {
#[test]
fn test_eig_dual64() {
let a = arr2(&[
[Dual64::new_scalar(2.0, 1.0), Dual64::new_scalar(2.0, 2.0)],
[Dual64::new_scalar(2.0, 2.0), Dual64::new_scalar(5.0, 3.0)],
[Dual64::new(2.0, 1.0), Dual64::new(2.0, 2.0)],
[Dual64::new(2.0, 2.0), Dual64::new(5.0, 3.0)],
]);
let (l, v) = jacobi_eigenvalue(a.clone(), 200);
let av = a.dot(&v);
Expand All @@ -368,26 +362,10 @@ mod tests {
assert_abs_diff_eq!(av[(1, 0)].re, (l[0] * v[(1, 0)]).re, epsilon = 1e-14);
assert_abs_diff_eq!(av[(0, 1)].re, (l[1] * v[(0, 1)]).re, epsilon = 1e-14);
assert_abs_diff_eq!(av[(1, 1)].re, (l[1] * v[(1, 1)]).re, epsilon = 1e-14);
assert_abs_diff_eq!(
av[(0, 0)].eps.unwrap(),
(l[0] * v[(0, 0)]).eps.unwrap(),
epsilon = 1e-14
);
assert_abs_diff_eq!(
av[(1, 0)].eps.unwrap(),
(l[0] * v[(1, 0)]).eps.unwrap(),
epsilon = 1e-14
);
assert_abs_diff_eq!(
av[(0, 1)].eps.unwrap(),
(l[1] * v[(0, 1)]).eps.unwrap(),
epsilon = 1e-14
);
assert_abs_diff_eq!(
av[(1, 1)].eps.unwrap(),
(l[1] * v[(1, 1)]).eps.unwrap(),
epsilon = 1e-14
);
assert_abs_diff_eq!(av[(0, 0)].eps, (l[0] * v[(0, 0)]).eps, epsilon = 1e-14);
assert_abs_diff_eq!(av[(1, 0)].eps, (l[0] * v[(1, 0)]).eps, epsilon = 1e-14);
assert_abs_diff_eq!(av[(0, 1)].eps, (l[1] * v[(0, 1)]).eps, epsilon = 1e-14);
assert_abs_diff_eq!(av[(1, 1)].eps, (l[1] * v[(1, 1)]).eps, epsilon = 1e-14);
}

#[test]
Expand All @@ -398,9 +376,9 @@ mod tests {

#[test]
fn test_norm_dual64() {
let v = arr1(&[Dual64::new_scalar(3.0, 1.0), Dual64::new_scalar(4.0, 3.0)]);
let v = arr1(&[Dual64::new(3.0, 1.0), Dual64::new(4.0, 3.0)]);
println!("{}", norm(&v));
assert_eq!(norm(&v).re, 5.0);
assert_eq!(norm(&v).eps.unwrap(), 3.0);
assert_eq!(norm(&v).eps, 3.0);
}
}
7 changes: 3 additions & 4 deletions src/python/dual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use nalgebra::{DVector, SVector};
use numpy::{PyArray, PyReadonlyArrayDyn};
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::Python;

#[pyclass(name = "Dual64")]
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -92,7 +91,7 @@ impl_dual_num!(PyDual64Dyn, DualDVec64, f64);
/// Returns
/// -------
/// function value and first derivative
pub fn first_derivative(f: &PyAny, x: f64) -> PyResult<(f64, f64)> {
pub fn first_derivative(f: &Bound<'_, PyAny>, x: f64) -> PyResult<(f64, f64)> {
let g = |x| {
let res = f.call1((PyDual64::from(x),))?;
if let Ok(res) = res.extract::<PyDual64>() {
Expand Down Expand Up @@ -122,7 +121,7 @@ macro_rules! impl_gradient_and_jacobian {
/// Returns
/// -------
/// function value and gradient
pub fn gradient(f: &PyAny, x: &PyAny) -> PyResult<(f64, Vec<f64>)> {
pub fn gradient(f: &Bound<'_, PyAny>, x: &Bound<'_, PyAny>) -> PyResult<(f64, Vec<f64>)> {
$(
if let Ok(x) = x.extract::<[f64; $n]>() {
let g = |x: SVector<DualSVec64<$n>, $n>| {
Expand Down Expand Up @@ -174,7 +173,7 @@ macro_rules! impl_gradient_and_jacobian {
/// Returns
/// -------
/// function values and Jacobian
pub fn jacobian(f: &PyAny, x: &PyAny) -> PyResult<(Vec<f64>, Vec<Vec<f64>>)> {
pub fn jacobian(f: &Bound<'_, PyAny>, x: &Bound<'_, PyAny>) -> PyResult<(Vec<f64>, Vec<Vec<f64>>)> {
$(
if let Ok(x) = x.extract::<[f64; $n]>() {
let g = |x: SVector<DualSVec64<$n>, $n>| {
Expand Down
4 changes: 2 additions & 2 deletions src/python/dual2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl_dual_num!(PyDual2_64Dyn, Dual2DVec64, f64);
/// Returns
/// -------
/// function value, first derivative, and second derivative
pub fn second_derivative(f: &PyAny, x: f64) -> PyResult<(f64, f64, f64)> {
pub fn second_derivative(f: &Bound<'_, PyAny>, x: f64) -> PyResult<(f64, f64, f64)> {
let g = |x| {
let res = f.call1((PyDual2_64::from(x),))?;
if let Ok(res) = res.extract::<PyDual2_64>() {
Expand Down Expand Up @@ -151,7 +151,7 @@ macro_rules! impl_hessian {
/// Returns
/// -------
/// function value, gradient and Hessian
pub fn hessian(f: &PyAny, x: &PyAny) -> PyResult<(f64, Vec<f64>, Vec<Vec<f64>>)> {
pub fn hessian(f: &Bound<'_, PyAny>, x: &Bound<'_, PyAny>) -> PyResult<(f64, Vec<f64>, Vec<Vec<f64>>)> {
$(
if let Ok(x) = x.extract::<[f64; $n]>() {
let g = |x: SVector<Dual2SVec64<$n>, $n>| {
Expand Down
2 changes: 1 addition & 1 deletion src/python/dual3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl_dual_num!(PyDual3Dual64, Dual3<Dual64, f64>, PyDual64);
/// Returns
/// -------
/// function value, first derivative, second derivative, and third derivative
pub fn third_derivative(f: &PyAny, x: f64) -> PyResult<(f64, f64, f64, f64)> {
pub fn third_derivative(f: &Bound<'_, PyAny>, x: f64) -> PyResult<(f64, f64, f64, f64)> {
let g = |x| {
let res = f.call1((PyDual3_64::from(x),))?;
if let Ok(res) = res.extract::<PyDual3_64>() {
Expand Down
12 changes: 8 additions & 4 deletions src/python/hyperdual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ impl_dual_num!(PyHyperDual64Dyn, HyperDualDVec64, f64);
/// -------
/// function value, first partial derivative w.r.t. x,
/// first parital derivative w.r.t. y, and second partial derivative
pub fn second_partial_derivative(f: &PyAny, x: f64, y: f64) -> PyResult<(f64, f64, f64, f64)> {
pub fn second_partial_derivative(
f: &Bound<'_, PyAny>,
x: f64,
y: f64,
) -> PyResult<(f64, f64, f64, f64)> {
let g = |x, y| {
let res = f.call1((PyHyperDual64::from(x), PyHyperDual64::from(y)))?;
if let Ok(res) = res.extract::<PyHyperDual64>() {
Expand Down Expand Up @@ -159,9 +163,9 @@ macro_rules! impl_partial_hessian {
/// function value, gradient w.r.t. x, gradient w.r.t. y, and partial Hessian
#[allow(clippy::type_complexity)]
pub fn partial_hessian(
f: &PyAny,
x: &PyAny,
y: &PyAny,
f: &Bound<'_, PyAny>,
x: &Bound<'_, PyAny>,
y: &Bound<'_, PyAny>,
) -> PyResult<(f64, Vec<f64>, Vec<f64>, Vec<Vec<f64>>)> {
$(
if let (Ok(x), Ok(y)) = (x.extract::<[f64; $m]>(), y.extract::<[f64; $n]>()) {
Expand Down
4 changes: 2 additions & 2 deletions src/python/hyperhyperdual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl_dual_num!(PyHyperHyperDual64, HyperHyperDual64, f64);
/// third partial derivative
#[allow(clippy::type_complexity)]
pub fn third_partial_derivative(
f: &PyAny,
f: &Bound<'_, PyAny>,
x: f64,
y: f64,
z: f64,
Expand Down Expand Up @@ -130,7 +130,7 @@ pub fn third_partial_derivative(
/// third partial derivative
#[allow(clippy::type_complexity)]
pub fn third_partial_derivative_vec(
f: &PyAny,
f: &Bound<'_, PyAny>,
x: Vec<f64>,
i: usize,
j: usize,
Expand Down
3 changes: 2 additions & 1 deletion src/python/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(non_snake_case)]
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

Expand Down Expand Up @@ -34,7 +35,7 @@ pub use hyperdual::{
pub use hyperhyperdual::PyHyperHyperDual64;

#[pymodule]
pub fn num_dual(_py: Python, m: &PyModule) -> PyResult<()> {
pub fn num_dual(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
m.add_class::<PyDual64>()?;
m.add_class::<PyHyperDual64>()?;
Expand Down
Loading
Loading