Skip to content
Open
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
15 changes: 15 additions & 0 deletions src/linalg/impl_linalg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ where S: Data<Elem = A>
}
self.dot_generic(rhs)
}

/// Outer product of two 1D arrays.
///
/// The outer product of two vectors a (of dimension M) and b (of dimension N)
/// is defined as an (M*N)-dimensional matrix whose ij-th element is a_i * b_j.
/// This implementation essentially calls `dot` by reshaping the vectors.
pub fn outer<S2>(&self, b: &ArrayBase<S2, Ix1>) -> Array<A, Ix2>
where
S2: Data<Elem = A>,
A: LinalgScalar,
{
let (size_a, size_b) = (self.shape()[0], b.shape()[0]);
let b_reshaped = b.view().into_shape((1, size_b)).unwrap();
self.view().into_shape((size_a, 1)).unwrap().dot(&b_reshaped)
}
}

/// Return a pointer to the starting element in BLAS's view.
Expand Down
7 changes: 7 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ fn test_mat_mul()
assert_eq!(c.dot(&a), a);
}

#[test]
fn test_outer_product() {
let a: Array1<f64> = array![2., 4.];
let b: Array1<f64> = array![3., 5.];
assert_eq!(a.outer(&b), array![[6., 10.], [12., 20.]]);
}

#[deny(unsafe_code)]
#[test]
fn test_slice()
Expand Down