aboutsummaryrefslogtreecommitdiff
path: root/libpsi/src/maths/vector.rs
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2024-09-22 14:24:27 +0200
committerhachem <im@hachem.wtf>2024-09-22 14:24:27 +0200
commitae8abfa8335edf856f423e7a60c5bb830a79d5a7 (patch)
tree621e34eef728b971428c20e7bf946289c7dedc67 /libpsi/src/maths/vector.rs
parent1a9ef4a7f0cbd4ed848ccf0903ebbbd4fdab6adf (diff)
Refactor and implement other gates
Diffstat (limited to 'libpsi/src/maths/vector.rs')
-rw-r--r--libpsi/src/maths/vector.rs144
1 files changed, 20 insertions, 124 deletions
diff --git a/libpsi/src/maths/vector.rs b/libpsi/src/maths/vector.rs
index 243061d..a21cf36 100644
--- a/libpsi/src/maths/vector.rs
+++ b/libpsi/src/maths/vector.rs
@@ -96,6 +96,26 @@ impl<T: Float> RowVector<T> {
}
}
+impl<T: Float> VectorMatrix<T> for RowVector<T> {
+ fn to_matrix(&self) -> Matrix<T> {
+ Matrix::new(1, self.size(), self.0.clone())
+ }
+
+ fn from_matrix(matrix: &Matrix<T>) -> Self {
+ Self::new(matrix.data.clone())
+ }
+}
+
+impl<T: Float> VectorMatrix<T> for ColumnVector<T> {
+ fn to_matrix(&self) -> Matrix<T> {
+ Matrix::new(self.size(), 1, self.0.clone())
+ }
+
+ fn from_matrix(matrix: &Matrix<T>) -> Self {
+ Self::new(matrix.data.clone())
+ }
+}
+
impl<T: Float, const ROWS: usize, const COLS: usize> Vector<T> for VectorImpl<T, ROWS, COLS> {
fn new(data: Vec<T>) -> Self {
Self(data)
@@ -211,130 +231,6 @@ impl<T: Float, const ROWS: usize, const COLS: usize> ops::IndexMut<usize>
}
}
-impl<T: Float, const ROWS: usize, const COLS: usize> ops::Add<&VectorImpl<T, ROWS, COLS>>
- for VectorImpl<T, ROWS, COLS>
-{
- type Output = Option<VectorImpl<T, ROWS, COLS>>;
-
- fn add(self, other: &VectorImpl<T, ROWS, COLS>) -> Self::Output {
- self.add_to(other)
- }
-}
-
-impl<T: Float, const ROWS: usize, const COLS: usize> ops::Sub<&VectorImpl<T, ROWS, COLS>>
- for VectorImpl<T, ROWS, COLS>
-{
- type Output = Option<VectorImpl<T, ROWS, COLS>>;
-
- fn sub(self, other: &VectorImpl<T, ROWS, COLS>) -> Self::Output {
- self.subtract(other)
- }
-}
-
-impl<T: Float, const ROWS: usize, const COLS: usize> ops::Mul<T> for VectorImpl<T, ROWS, COLS> {
- type Output = VectorImpl<T, ROWS, COLS>;
-
- fn mul(self, scalar: T) -> Self::Output {
- self.scale(scalar)
- }
-}
-
-impl<T: Float, const ROWS: usize, const COLS: usize> ops::Div<T> for VectorImpl<T, ROWS, COLS> {
- type Output = VectorImpl<T, ROWS, COLS>;
-
- fn div(self, scalar: T) -> Self::Output {
- self.scale(T::one() / scalar)
- }
-}
-
-impl<T: Float, const ROWS: usize, const COLS: usize> ops::AddAssign<VectorImpl<T, ROWS, COLS>>
- for VectorImpl<T, ROWS, COLS>
-{
- fn add_assign(&mut self, other: VectorImpl<T, ROWS, COLS>) {
- if let Some(result) = self.add_to(&other) {
- *self = result;
- }
- }
-}
-
-impl<T: Float, const ROWS: usize, const COLS: usize> ops::SubAssign<VectorImpl<T, ROWS, COLS>>
- for VectorImpl<T, ROWS, COLS>
-{
- fn sub_assign(&mut self, other: VectorImpl<T, ROWS, COLS>) {
- if let Some(result) = self.subtract(&other) {
- *self = result;
- }
- }
-}
-
-impl<T: Float, const ROWS: usize, const COLS: usize> ops::MulAssign<T>
- for VectorImpl<T, ROWS, COLS>
-{
- fn mul_assign(&mut self, scalar: T) {
- *self = self.scale(scalar);
- }
-}
-
-impl<T: Float, const ROWS: usize, const COLS: usize> ops::DivAssign<T>
- for VectorImpl<T, ROWS, COLS>
-{
- fn div_assign(&mut self, scalar: T) {
- *self = self.scale(T::one() / scalar);
- }
-}
-
-impl<T: Float> VectorMatrix<T> for RowVector<T> {
- fn to_matrix(&self) -> Matrix<T> {
- Matrix::new(1, self.size(), self.0.clone())
- }
-
- fn from_matrix(matrix: &Matrix<T>) -> Self {
- Self::new(matrix.data.clone())
- }
-}
-
-impl<T: Float> VectorMatrix<T> for ColumnVector<T> {
- fn to_matrix(&self) -> Matrix<T> {
- Matrix::new(self.size(), 1, self.0.clone())
- }
-
- fn from_matrix(matrix: &Matrix<T>) -> Self {
- Self::new(matrix.data.clone())
- }
-}
-
-impl<T: Float> ops::Mul<&Matrix<T>> for RowVector<T> {
- type Output = Option<RowVector<T>>;
-
- fn mul(self, matrix: &Matrix<T>) -> Self::Output {
- self.mul_matrix(matrix)
- }
-}
-
-impl<T: Float> ops::Mul<&Matrix<T>> for ColumnVector<T> {
- type Output = Option<ColumnVector<T>>;
-
- fn mul(self, matrix: &Matrix<T>) -> Self::Output {
- self.mul_matrix(matrix)
- }
-}
-
-impl<T: Float> ops::MulAssign<&Matrix<T>> for RowVector<T> {
- fn mul_assign(&mut self, matrix: &Matrix<T>) {
- if let Some(result) = self.mul_matrix(matrix) {
- *self = result;
- }
- }
-}
-
-impl<T: Float> ops::MulAssign<&Matrix<T>> for ColumnVector<T> {
- fn mul_assign(&mut self, matrix: &Matrix<T>) {
- if let Some(result) = self.mul_matrix(matrix) {
- *self = result;
- }
- }
-}
-
impl<T: Float + fmt::Debug> fmt::Debug for RowVector<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "RowVector({:?})", self.0)