-
Notifications
You must be signed in to change notification settings - Fork 0
Implement Tensor struct #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
521dad9
feat: add tensor struct
siliconlad 3b6dffa
feat: implement more tensor methods
siliconlad 918cf6d
feat: add tensor add/sub
siliconlad eba8f49
feat: add matrix multiplication
siliconlad fd4ecfe
feat: add tensor product
siliconlad 34dee9a
feat: add ones method
siliconlad eebe35f
feat: add ShapeError
siliconlad e6e9b93
feat!: rename shape len to order
siliconlad 6035f12
feat: add Coordinate and DynamicStorage
siliconlad 44fa364
feat: add tests
siliconlad da4f2f8
feat: collapse uses
siliconlad a8bd42f
feat!: make dims private in Shape
siliconlad dcaf8d2
feat: add one more test case
siliconlad cc91cbd
feat: move tensor to separate file
siliconlad 03cfc0c
feat: add matrix and vector structs
siliconlad bf3ae68
feat: display method for tensor
siliconlad 251f5d7
fix: display method
siliconlad 6bcc07a
feat: add pow method
siliconlad d12849d
feat: add eye method to matrix
siliconlad c9459b8
style: run fmt
siliconlad 9b9783b
feat!: prevent empty coord
siliconlad e4d91e1
feat!: rename len to order
siliconlad d4a0fae
style: fix clippy warnings
siliconlad 0df55fe
build: remove license-file to remove build warning
siliconlad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
pub type Axes = Vec<usize>; |
This file contains hidden or 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,131 @@ | ||
use std::fmt; | ||
use std::ops::{Index, IndexMut}; | ||
|
||
use crate::error::ShapeError; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct Coordinate { | ||
indices: Vec<usize>, | ||
} | ||
|
||
impl Coordinate { | ||
pub fn new(indices: Vec<usize>) -> Result<Self, ShapeError> { | ||
if indices.is_empty() { | ||
return Err(ShapeError::new("Coordinate cannot be empty")); | ||
} | ||
Ok(Self { indices }) | ||
} | ||
|
||
pub fn order(&self) -> usize { | ||
self.indices.len() | ||
} | ||
|
||
pub fn iter(&self) -> std::slice::Iter<'_, usize> { | ||
self.indices.iter() | ||
} | ||
|
||
pub fn insert(&self, index: usize, axis: usize) -> Self { | ||
let mut new_indices = self.indices.clone(); | ||
new_indices.insert(index, axis); | ||
Self { | ||
indices: new_indices, | ||
} | ||
} | ||
} | ||
|
||
impl Index<usize> for Coordinate { | ||
type Output = usize; | ||
|
||
fn index(&self, index: usize) -> &Self::Output { | ||
&self.indices[index] | ||
} | ||
} | ||
|
||
impl IndexMut<usize> for Coordinate { | ||
fn index_mut(&mut self, index: usize) -> &mut Self::Output { | ||
&mut self.indices[index] | ||
} | ||
} | ||
|
||
impl fmt::Display for Coordinate { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
use itertools::Itertools; | ||
let idxs = self.indices.iter().map(|&x| format!("{}", x)).join(", "); | ||
write!(f, "({})", idxs) | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! coord { | ||
($($index:expr),*) => { | ||
{ | ||
use $crate::coordinate::Coordinate; | ||
Coordinate::new(vec![$($index),*]) | ||
} | ||
}; | ||
|
||
($index:expr; $count:expr) => { | ||
{ | ||
use $crate::coordinate::Coordinate; | ||
Coordinate::new(vec![$index; $count]) | ||
} | ||
}; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_order() { | ||
let coord = coord![1, 2, 3].unwrap(); | ||
assert_eq!(coord.order(), 3); | ||
} | ||
|
||
#[test] | ||
fn test_iter() { | ||
let coord = coord![1, 2, 3].unwrap(); | ||
let mut iter = coord.iter(); | ||
assert_eq!(iter.next(), Some(&1)); | ||
assert_eq!(iter.next(), Some(&2)); | ||
assert_eq!(iter.next(), Some(&3)); | ||
assert_eq!(iter.next(), None); | ||
} | ||
|
||
#[test] | ||
fn test_insert() { | ||
let coord = coord![1, 2, 3].unwrap(); | ||
let new_coord = coord.insert(1, 4); | ||
assert_eq!(new_coord, coord![1, 4, 2, 3].unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_index() { | ||
let coord = coord![1, 2, 3].unwrap(); | ||
assert_eq!(coord[0], 1); | ||
assert_eq!(coord[1], 2); | ||
assert_eq!(coord[2], 3); | ||
} | ||
|
||
#[test] | ||
fn test_index_mut() { | ||
let mut coord = coord![1, 2, 3].unwrap(); | ||
coord[1] = 4; | ||
assert_eq!(coord[1], 4); | ||
} | ||
|
||
#[test] | ||
fn test_display() { | ||
let coord = coord![1, 2, 3].unwrap(); | ||
assert_eq!(format!("{}", coord), "(1, 2, 3)"); | ||
} | ||
|
||
#[test] | ||
fn test_coord_macro() { | ||
let coord = coord![1, 2, 3].unwrap(); | ||
assert_eq!(coord, Coordinate::new(vec![1, 2, 3]).unwrap()); | ||
|
||
let coord_repeated = coord![1; 3].unwrap(); | ||
assert_eq!(coord_repeated, Coordinate::new(vec![1, 1, 1]).unwrap()); | ||
} | ||
} |
This file contains hidden or 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,20 @@ | ||
use std::fmt; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ShapeError { | ||
reason: String, | ||
} | ||
|
||
impl ShapeError { | ||
pub fn new(reason: &str) -> Self { | ||
ShapeError { | ||
reason: reason.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for ShapeError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "ShapeError: {}", self.reason) | ||
} | ||
} |
This file contains hidden or 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,88 @@ | ||
use crate::coord; | ||
use crate::coordinate::Coordinate; | ||
use crate::shape::Shape; | ||
use std::cmp::max; | ||
|
||
pub struct IndexIterator { | ||
shape: Shape, | ||
current: Coordinate, | ||
done: bool, | ||
} | ||
|
||
impl IndexIterator { | ||
pub fn new(shape: &Shape) -> Self { | ||
// (shape.order() == 0) => `next` returns None before `current` is used | ||
let current = coord![0; max(shape.order(), 1)].unwrap(); | ||
IndexIterator { | ||
shape: shape.clone(), | ||
current, | ||
done: false, | ||
} | ||
} | ||
} | ||
|
||
impl Iterator for IndexIterator { | ||
type Item = Coordinate; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.done || self.shape.order() == 0 { | ||
return None; | ||
} | ||
|
||
let result = self.current.clone(); | ||
|
||
for i in (0..self.shape.order()).rev() { | ||
if self.current[i] + 1 < self.shape[i] { | ||
self.current[i] += 1; | ||
break; | ||
} else { | ||
self.current[i] = 0; | ||
if i == 0 { | ||
self.done = true; | ||
} | ||
} | ||
} | ||
|
||
Some(result) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::shape; | ||
|
||
#[test] | ||
fn test_index_iterator() { | ||
let shape = shape![2, 3].unwrap(); | ||
let mut iter = IndexIterator::new(&shape); | ||
|
||
assert_eq!(iter.next(), Some(coord![0, 0].unwrap())); | ||
assert_eq!(iter.next(), Some(coord![0, 1].unwrap())); | ||
assert_eq!(iter.next(), Some(coord![0, 2].unwrap())); | ||
assert_eq!(iter.next(), Some(coord![1, 0].unwrap())); | ||
assert_eq!(iter.next(), Some(coord![1, 1].unwrap())); | ||
assert_eq!(iter.next(), Some(coord![1, 2].unwrap())); | ||
assert_eq!(iter.next(), None); | ||
} | ||
|
||
#[test] | ||
fn test_index_iterator_single_dimension() { | ||
let shape = shape![4].unwrap(); | ||
let mut iter = IndexIterator::new(&shape); | ||
|
||
assert_eq!(iter.next(), Some(coord![0].unwrap())); | ||
assert_eq!(iter.next(), Some(coord![1].unwrap())); | ||
assert_eq!(iter.next(), Some(coord![2].unwrap())); | ||
assert_eq!(iter.next(), Some(coord![3].unwrap())); | ||
assert_eq!(iter.next(), None); | ||
} | ||
|
||
#[test] | ||
fn test_index_iterator_empty_tensor() { | ||
let shape = shape![].unwrap(); | ||
let mut iter = IndexIterator::new(&shape); | ||
|
||
assert_eq!(iter.next(), None); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,14 +1,9 @@ | ||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
} | ||
pub mod axes; | ||
pub mod coordinate; | ||
pub mod error; | ||
pub mod iter; | ||
pub mod matrix; | ||
pub mod shape; | ||
pub mod storage; | ||
pub mod tensor; | ||
pub mod vector; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.