Struct Vector

Source
pub struct Vector<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> Vector<'a>

Source

pub fn create(ceed: &Ceed, n: usize) -> Result<Self>

Source

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");
}
Source

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");
Source

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");
Source

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");
Source

pub fn len(&self) -> usize

Returns the length of a Vector

let vec = ceed.vector(10)?;
assert_eq!(vec.len(), 10, "Incorrect length");
Source

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");
}
Source

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");
}
Source

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");
}
Source

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");
}
Source

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..]);
Source

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");
Source

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");
Source

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");
}
Source

pub fn axpy(self, alpha: Scalar, x: &Vector<'_>) -> Result<Self>

Compute y = alpha x + y for a pair of Vectors

§arguments
  • alpha - scaling factor
  • x - 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");
}
Source

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 factor
  • beta - second scaling factor
  • x - 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");
}
Source

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 product
  • y - 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");
}
Source

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");
}
Source

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<'a> Debug for Vector<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Display for Vector<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

View a Vector

let vec = libceed::vector::Vector::from_slice(&ceed, &[1., 2., 3.])?;
assert_eq!(
    vec.to_string(),
    "CeedVector length 3
    1.00000000
    2.00000000
    3.00000000
"
);
Source§

impl<'a> Drop for Vector<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<&Vector<'_>> for CeedVector

Source§

fn from(vec: &Vector<'_>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a Vector<'_>> for VectorOpt<'a>

Construct a VectorOpt reference from a Vector reference

Source§

fn from(vec: &'a Vector<'_>) -> Self

Converts to this type from the input type.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.