-
Notifications
You must be signed in to change notification settings - Fork 330
Add dot product support for ArrayD #1483
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
Merged
akern40
merged 13 commits into
rust-ndarray:master
from
NewBornRustacean:feature-dot-for-arrayd
Mar 18, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e34747c
impl and tests added
NewBornRustacean 3adf2e3
add docstring and exmples
NewBornRustacean a5f78a9
remove duplicated length check
NewBornRustacean 1ca9409
cargo fmt
NewBornRustacean 18ca5d5
Revert "cargo fmt"
NewBornRustacean 3748a3b
cargo nigthly fmt
NewBornRustacean ac7de36
not to exectue example for CI
NewBornRustacean da24369
add missing use block(vec macro)
NewBornRustacean 26a0230
Merge branch 'master' into feature-dot-for-arrayd
NewBornRustacean 5841891
correct example
NewBornRustacean 28cc3b0
make sure example runs OK, apply fmt
NewBornRustacean ba3c029
move tests into blas-tests
NewBornRustacean e577db3
apply nightly formatter
NewBornRustacean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
extern crate blas_src; | ||
use ndarray::{linalg::Dot, Array1, Array2, ArrayD, Ix1, Ix2}; | ||
|
||
#[test] | ||
fn test_arrayd_dot_2d() | ||
{ | ||
let mat1 = ArrayD::from_shape_vec(vec![3, 2], vec![3.0; 6]).unwrap(); | ||
let mat2 = ArrayD::from_shape_vec(vec![2, 3], vec![1.0; 6]).unwrap(); | ||
|
||
let result = mat1.dot(&mat2); | ||
|
||
// Verify the result is correct | ||
assert_eq!(result.ndim(), 2); | ||
assert_eq!(result.shape(), &[3, 3]); | ||
|
||
// Compare with Array2 implementation | ||
let mat1_2d = Array2::from_shape_vec((3, 2), vec![3.0; 6]).unwrap(); | ||
let mat2_2d = Array2::from_shape_vec((2, 3), vec![1.0; 6]).unwrap(); | ||
let expected = mat1_2d.dot(&mat2_2d); | ||
|
||
assert_eq!(result.into_dimensionality::<Ix2>().unwrap(), expected); | ||
} | ||
|
||
#[test] | ||
fn test_arrayd_dot_1d() | ||
{ | ||
// Test 1D array dot product | ||
let vec1 = ArrayD::from_shape_vec(vec![3], vec![1.0, 2.0, 3.0]).unwrap(); | ||
let vec2 = ArrayD::from_shape_vec(vec![3], vec![4.0, 5.0, 6.0]).unwrap(); | ||
|
||
let result = vec1.dot(&vec2); | ||
|
||
// Verify scalar result | ||
assert_eq!(result.ndim(), 0); | ||
assert_eq!(result.shape(), &[]); | ||
assert_eq!(result[[]], 32.0); // 1*4 + 2*5 + 3*6 | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "Dot product for ArrayD is only supported for 1D and 2D arrays")] | ||
fn test_arrayd_dot_3d() | ||
{ | ||
// Test that 3D arrays are not supported | ||
let arr1 = ArrayD::from_shape_vec(vec![2, 2, 2], vec![1.0; 8]).unwrap(); | ||
let arr2 = ArrayD::from_shape_vec(vec![2, 2, 2], vec![1.0; 8]).unwrap(); | ||
|
||
let _result = arr1.dot(&arr2); // Should panic | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "ndarray: inputs 2 × 3 and 4 × 5 are not compatible for matrix multiplication")] | ||
fn test_arrayd_dot_incompatible_dims() | ||
{ | ||
// Test arrays with incompatible dimensions | ||
let arr1 = ArrayD::from_shape_vec(vec![2, 3], vec![1.0; 6]).unwrap(); | ||
let arr2 = ArrayD::from_shape_vec(vec![4, 5], vec![1.0; 20]).unwrap(); | ||
|
||
let _result = arr1.dot(&arr2); // Should panic | ||
} | ||
|
||
#[test] | ||
fn test_arrayd_dot_matrix_vector() | ||
{ | ||
// Test matrix-vector multiplication | ||
let mat = ArrayD::from_shape_vec(vec![3, 2], vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap(); | ||
let vec = ArrayD::from_shape_vec(vec![2], vec![1.0, 2.0]).unwrap(); | ||
|
||
let result = mat.dot(&vec); | ||
|
||
// Verify result | ||
assert_eq!(result.ndim(), 1); | ||
assert_eq!(result.shape(), &[3]); | ||
|
||
// Compare with Array2 implementation | ||
let mat_2d = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap(); | ||
let vec_1d = Array1::from_vec(vec![1.0, 2.0]); | ||
let expected = mat_2d.dot(&vec_1d); | ||
|
||
assert_eq!(result.into_dimensionality::<Ix1>().unwrap(), expected); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.