Function numpy::dot_bound

source ·
pub fn dot_bound<'py, T, DIN1, DIN2, OUT>(
    array1: &Bound<'py, PyArray<T, DIN1>>,
    array2: &Bound<'py, PyArray<T, DIN2>>
) -> PyResult<OUT>
where T: Element, DIN1: Dimension, DIN2: Dimension, OUT: ArrayOrScalar<'py, T>,
Expand description

Return the dot product of two arrays.

NumPy’s documentation has the details.

§Examples

Note that this function can either return an array…

use pyo3::{Python, Bound};
use ndarray::array;
use numpy::{dot_bound, pyarray_bound, PyArray2, PyArrayMethods};

Python::with_gil(|py| {
    let matrix = pyarray_bound![py, [1, 0], [0, 1]];
    let another_matrix = pyarray_bound![py, [4, 1], [2, 2]];

    let result: Bound<'_, PyArray2<_>> = dot_bound(&matrix, &another_matrix).unwrap();

    assert_eq!(
        result.readonly().as_array(),
        array![[4, 1], [2, 2]]
    );
});

…or a scalar depending on its arguments.

use pyo3::Python;
use numpy::{dot_bound, pyarray_bound, PyArray0};

Python::with_gil(|py| {
    let vector = pyarray_bound![py, 1.0, 2.0, 3.0];
    let result: f64 = dot_bound(&vector, &vector).unwrap();
    assert_eq!(result, 14.0);
});