pub struct Vector<'a> { /* private fields */ }
Implementations§
Source§impl<'a> Vector<'a>
impl<'a> Vector<'a>
pub fn create(ceed: &Ceed, n: usize) -> Result<Self>
Sourcepub fn copy_from(&mut self, vec_source: &Vector<'_>) -> Result<i32>
pub fn copy_from(&mut self, vec_source: &Vector<'_>) -> Result<i32>
Copy the array of self into vec_copy
§arguments
vec_source
- vector to copy array values from
let a = ceed.vector_from_slice(&[1., 2., 3.])?;
let mut b = ceed.vector(3)?;
b.copy_from(&a)?;
for (i, v) in b.view()?.iter().enumerate() {
assert_eq!(*v, (i + 1) as Scalar, "Copy contents not set correctly");
}
Sourcepub fn from_slice(ceed: &Ceed, v: &[Scalar]) -> Result<Self>
pub fn from_slice(ceed: &Ceed, v: &[Scalar]) -> Result<Self>
Create a Vector from a slice
§arguments
slice
- values to initialize vector with
let vec = vector::Vector::from_slice(&ceed, &[1., 2., 3.])?;
assert_eq!(vec.length(), 3, "Incorrect length from slice");
Sourcepub fn from_array(ceed: &Ceed, v: &mut [Scalar]) -> Result<Self>
pub fn from_array(ceed: &Ceed, v: &mut [Scalar]) -> Result<Self>
Create a Vector from a mutable array reference
§arguments
slice
- values to initialize vector with
let mut rust_vec = vec![1., 2., 3.];
let vec = libceed::vector::Vector::from_array(&ceed, &mut rust_vec)?;
assert_eq!(vec.length(), 3, "Incorrect length from slice");
Sourcepub fn length(&self) -> usize
pub fn length(&self) -> usize
Returns the length of a Vector
let vec = ceed.vector(10)?;
let n = vec.length();
assert_eq!(n, 10, "Incorrect length");
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of a Vector
let vec = ceed.vector(10)?;
assert_eq!(vec.len(), 10, "Incorrect length");
Sourcepub fn set_value(&mut self, value: Scalar) -> Result<i32>
pub fn set_value(&mut self, value: Scalar) -> Result<i32>
Set the Vector to a constant value
§arguments
val
- Value to be used
let len = 10;
let mut vec = ceed.vector(len)?;
let val = 42.0;
vec.set_value(val)?;
for v in vec.view()?.iter() {
assert_eq!(*v, val, "Value not set correctly");
}
Sourcepub fn set_slice(&mut self, slice: &[Scalar]) -> Result<i32>
pub fn set_slice(&mut self, slice: &[Scalar]) -> Result<i32>
Set values from a slice of the same length
§arguments
slice
- values to into self; length must match
let mut vec = ceed.vector(4)?;
vec.set_slice(&[10., 11., 12., 13.])?;
for (i, v) in vec.view()?.iter().enumerate() {
assert_eq!(*v, 10. + i as Scalar, "Slice not set correctly");
}
Sourcepub fn wrap_slice_mut<'b>(
&mut self,
slice: &'b mut [Scalar],
) -> Result<VectorSliceWrapper<'b>>
pub fn wrap_slice_mut<'b>( &mut self, slice: &'b mut [Scalar], ) -> Result<VectorSliceWrapper<'b>>
Wrap a mutable slice in a Vector of the same length
§arguments
slice
- values to wrap in self; length must match
let mut vec = ceed.vector(4)?;
let mut array = [10., 11., 12., 13.];
{
// `wrapper` holds a mutable reference to the wrapped slice
// that is dropped when `wrapper` goes out of scope
let wrapper = vec.wrap_slice_mut(&mut array)?;
for (i, v) in vec.view()?.iter().enumerate() {
assert_eq!(*v, 10. + i as Scalar, "Slice not set correctly");
}
// This line will not compile, as the `wrapper` holds mutable
// access to the `array`
// array[0] = 5.0;
// Changes here are reflected in the `array`
vec.set_value(5.0)?;
for v in vec.view()?.iter() {
assert_eq!(*v, 5.0 as Scalar, "Value not set correctly");
}
}
// 'array' remains changed
for v in array.iter() {
assert_eq!(*v, 5.0 as Scalar, "Array not mutated correctly");
}
// While changes to `vec` no longer affect `array`
vec.set_value(6.0)?;
for v in array.iter() {
assert_eq!(*v, 5.0 as Scalar, "Array mutated without permission");
}
Sourcepub fn sync(&self, mtype: MemType) -> Result<i32>
pub fn sync(&self, mtype: MemType) -> Result<i32>
Sync the Vector to a specified memtype
§arguments
mtype
- Memtype to be synced
let len = 10;
let mut vec = ceed.vector(len)?;
let val = 42.0;
vec.set_value(val);
vec.sync(MemType::Host)?;
for v in vec.view()?.iter() {
assert_eq!(*v, val, "Value not set correctly");
}
Sourcepub fn view(&self) -> Result<VectorView<'_>>
pub fn view(&self) -> Result<VectorView<'_>>
Create an immutable view
let vec = ceed.vector_from_slice(&[10., 11., 12., 13.])?;
let v = vec.view()?;
assert_eq!(v[0..2], [10., 11.]);
// It is valid to have multiple immutable views
let w = vec.view()?;
assert_eq!(v[1..], w[1..]);
Sourcepub fn view_mut(&mut self) -> Result<VectorViewMut<'_>>
pub fn view_mut(&mut self) -> Result<VectorViewMut<'_>>
Create an mutable view
let mut vec = ceed.vector_from_slice(&[10., 11., 12., 13.])?;
{
let mut v = vec.view_mut()?;
v[2] = 9.;
}
let w = vec.view()?;
assert_eq!(w[2], 9., "View did not mutate data");
Sourcepub fn norm(&self, ntype: NormType) -> Result<Scalar>
pub fn norm(&self, ntype: NormType) -> Result<Scalar>
Return the norm of a Vector
§arguments
ntype
- Norm type One, Two, or Max
let vec = ceed.vector_from_slice(&[1., 2., 3., 4.])?;
let max_norm = vec.norm(NormType::Max)?;
assert_eq!(max_norm, 4.0, "Incorrect Max norm");
let l1_norm = vec.norm(NormType::One)?;
assert_eq!(l1_norm, 10., "Incorrect L1 norm");
let l2_norm = vec.norm(NormType::Two)?;
assert!((l2_norm - 5.477) < 1e-3, "Incorrect L2 norm");
Sourcepub fn scale(self, alpha: Scalar) -> Result<Self>
pub fn scale(self, alpha: Scalar) -> Result<Self>
Compute x = alpha x for a Vector
§arguments
alpha
- scaling factor
let mut vec = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
vec = vec.scale(-1.0)?;
for (i, v) in vec.view()?.iter().enumerate() {
assert_eq!(*v, -(i as Scalar), "Value not set correctly");
}
Sourcepub fn axpy(self, alpha: Scalar, x: &Vector<'_>) -> Result<Self>
pub fn axpy(self, alpha: Scalar, x: &Vector<'_>) -> Result<Self>
Compute y = alpha x + y for a pair of Vectors
§arguments
alpha
- scaling factorx
- second vector, must be different than self
let x = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
let mut y = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
y = y.axpy(-0.5, &x)?;
for (i, y) in y.view()?.iter().enumerate() {
assert_eq!(*y, (i as Scalar) / 2.0, "Value not set correctly");
}
Sourcepub fn axpby(self, alpha: Scalar, beta: Scalar, x: &Vector<'_>) -> Result<Self>
pub fn axpby(self, alpha: Scalar, beta: Scalar, x: &Vector<'_>) -> Result<Self>
Compute y = alpha x + beta y for a pair of Vectors
§arguments
alpha
- first scaling factorbeta
- second scaling factorx
- second vector, must be different than self
let x = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
let mut y = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
y = y.axpby(-0.5, 1.0, &x)?;
for (i, y) in y.view()?.iter().enumerate() {
assert_eq!(*y, (i as Scalar) * 1.5, "Value not set correctly");
}
Sourcepub fn pointwise_mult(self, x: &Vector<'_>, y: &Vector<'_>) -> Result<Self>
pub fn pointwise_mult(self, x: &Vector<'_>, y: &Vector<'_>) -> Result<Self>
Compute the pointwise multiplication w = x .* y for Vectors
§arguments
x
- first vector for producty
- second vector for product
let mut w = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
let x = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
let y = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
w = w.pointwise_mult(&x, &y)?;
for (i, w) in w.view()?.iter().enumerate() {
assert_eq!(*w, (i as Scalar).powf(2.0), "Value not set correctly");
}
Sourcepub fn pointwise_scale(self, x: &Vector<'_>) -> Result<Self>
pub fn pointwise_scale(self, x: &Vector<'_>) -> Result<Self>
Compute the pointwise multiplication w = w .* x for Vectors
§arguments
x
- second vector for product
let mut w = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
let x = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
w = w.pointwise_scale(&x)?;
for (i, w) in w.view()?.iter().enumerate() {
assert_eq!(*w, (i as Scalar).powf(2.0), "Value not set correctly");
}
Sourcepub fn pointwise_square(self) -> Result<Self>
pub fn pointwise_square(self) -> Result<Self>
Compute the pointwise multiplication w = w .* w for a Vector
let mut w = ceed.vector_from_slice(&[0., 1., 2., 3., 4.])?;
w = w.pointwise_square()?;
for (i, w) in w.view()?.iter().enumerate() {
assert_eq!(*w, (i as Scalar).powf(2.0), "Value not set correctly");
}
Trait Implementations§
Source§impl From<&Vector<'_>> for CeedVector
impl From<&Vector<'_>> for CeedVector
Auto Trait Implementations§
impl<'a> Freeze for Vector<'a>
impl<'a> RefUnwindSafe for Vector<'a>
impl<'a> !Send for Vector<'a>
impl<'a> !Sync for Vector<'a>
impl<'a> Unpin for Vector<'a>
impl<'a> UnwindSafe for Vector<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more