Skip to content

Commit

Permalink
Add 'ones' and 'full' constructors for owned Col, Row, and Matrix.
Browse files Browse the repository at this point in the history
  • Loading branch information
pleepleus authored and sarah committed May 4, 2024
1 parent 3489353 commit 879de55
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/col/colown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ impl<E: Entity> Col<E> {
Self::from_fn(nrows, |_| unsafe { core::mem::zeroed() })
}

/// Returns a new matrix with number of rows `nrows`, filled with ones.
///
/// # Panics
/// The function panics if the total capacity in bytes exceeds `isize::MAX`.
#[inline]
pub fn ones(nrows: usize) -> Self
where
E: ComplexField,
{
Self::from_fn(nrows, |_| E::faer_one())
}

/// Returns a new matrix with number of rows `nrows`, filled with a constant value.
///
/// # Panics
/// The function panics if the total capacity in bytes exceeds `isize::MAX`.
#[inline]
pub fn full(nrows: usize, constant: E) -> Self
where
E: ComplexField,
{
Self::from_fn(nrows, |_| constant)
}

/// Returns the number of rows of the column.
#[inline(always)]
pub fn nrows(&self) -> usize {
Expand Down
24 changes: 24 additions & 0 deletions src/mat/matown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ impl<E: Entity> Mat<E> {
Self::from_fn(nrows, ncols, |_, _| unsafe { core::mem::zeroed() })
}

/// Returns a new matrix with dimensions `(nrows, ncols)`, filled with ones.
///
/// # Panics
/// The function panics if the total capacity in bytes exceeds `isize::MAX`.
#[inline]
pub fn ones(nrows: usize, ncols: usize) -> Self
where
E: ComplexField,
{
Self::from_fn(nrows, ncols, |_, _| E::faer_one())
}

/// Returns a new matrix with dimensions `(nrows, ncols)`, filled with a constant value.
///
/// # Panics
/// The function panics if the total capacity in bytes exceeds `isize::MAX`.
#[inline]
pub fn full(nrows: usize, ncols: usize, constant: E) -> Self
where
E: ComplexField,
{
Self::from_fn(nrows, ncols, |_, _| constant)
}

/// Returns a new matrix with dimensions `(nrows, ncols)`, filled with zeros, except the main
/// diagonal which is filled with ones.
///
Expand Down
24 changes: 24 additions & 0 deletions src/row/rowown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ impl<E: Entity> Row<E> {
Self::from_fn(ncols, |_| unsafe { core::mem::zeroed() })
}

/// Returns a new matrix with number of columns `ncols`, filled with ones.
///
/// # Panics
/// The function panics if the total capacity in bytes exceeds `isize::MAX`.
#[inline]
pub fn ones(ncols: usize) -> Self
where
E: ComplexField,
{
Self::from_fn(ncols, |_| E::faer_one())
}

/// Returns a new matrix with number of columns `ncols`, filled with a constant value.
///
/// # Panics
/// The function panics if the total capacity in bytes exceeds `isize::MAX`.
#[inline]
pub fn full(ncols: usize, constant: E) -> Self
where
E: ComplexField,
{
Self::from_fn(ncols, |_| constant)
}

/// Returns the number of rows of the row. This is always equal to `1`.
#[inline(always)]
pub fn nrows(&self) -> usize {
Expand Down

0 comments on commit 879de55

Please sign in to comment.