-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
901 additions
and
678 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,92 @@ | ||
use super::BlockRange; | ||
use nalgebra::Const; | ||
use sophus_autodiff::linalg::VecF64; | ||
|
||
/// Block gradient vector | ||
#[derive(Debug, Clone)] | ||
pub struct BlockGradient<const NUM: usize, const NUM_ARGS: usize> { | ||
/// vector storage | ||
pub vec: nalgebra::SVector<f64, NUM>, | ||
/// ranges, one for each block | ||
pub ranges: [BlockRange; NUM_ARGS], | ||
} | ||
|
||
impl<const NUM: usize, const NUM_ARGS: usize> BlockGradient<NUM, NUM_ARGS> { | ||
/// create a new block vector | ||
pub fn new(dims: &[usize; NUM_ARGS]) -> Self { | ||
debug_assert!(!dims.is_empty()); | ||
|
||
let num_blocks = dims.len(); | ||
|
||
let mut ranges = [BlockRange::default(); NUM_ARGS]; | ||
|
||
let mut num_rows: usize = 0; | ||
|
||
for i in 0..num_blocks { | ||
let dim = dims[i]; | ||
ranges[i] = BlockRange { | ||
index: num_rows as i64, | ||
dim, | ||
}; | ||
num_rows += dim; | ||
} | ||
Self { | ||
vec: nalgebra::SVector::zeros(), | ||
ranges, | ||
} | ||
} | ||
|
||
/// Number of blocks | ||
pub fn num_blocks(&self) -> usize { | ||
self.ranges.len() | ||
} | ||
|
||
/// set the ith block | ||
pub fn set_block<const R: usize>(&mut self, ith: usize, v: VecF64<R>) { | ||
debug_assert!(ith < self.num_blocks()); | ||
debug_assert_eq!(R, self.ranges[ith].dim); | ||
self.mut_block::<R>(ith).copy_from(&v); | ||
} | ||
|
||
/// get the ith block | ||
pub fn block( | ||
&self, | ||
ith: usize, | ||
) -> nalgebra::Matrix< | ||
f64, | ||
nalgebra::Dyn, | ||
nalgebra::Const<1>, | ||
nalgebra::ViewStorage< | ||
'_, | ||
f64, | ||
nalgebra::Dyn, | ||
nalgebra::Const<1>, | ||
nalgebra::Const<1>, | ||
Const<NUM>, | ||
>, | ||
> { | ||
let idx = self.ranges[ith].index as usize; | ||
self.vec.rows(idx, self.ranges[ith].dim) | ||
} | ||
|
||
/// mutable reference to the ith block | ||
pub fn mut_block<const ROWS: usize>( | ||
&mut self, | ||
ith: usize, | ||
) -> nalgebra::Matrix< | ||
f64, | ||
nalgebra::Const<ROWS>, | ||
nalgebra::Const<1>, | ||
nalgebra::ViewStorageMut< | ||
'_, | ||
f64, | ||
nalgebra::Const<ROWS>, | ||
nalgebra::Const<1>, | ||
nalgebra::Const<1>, | ||
Const<NUM>, | ||
>, | ||
> { | ||
let idx = self.ranges[ith].index as usize; | ||
self.vec.fixed_rows_mut::<ROWS>(idx) | ||
} | ||
} |
Oops, something went wrong.