Skip to content

Commit

Permalink
Add Interaction with other classes
Browse files Browse the repository at this point in the history
  • Loading branch information
desmond60 committed Mar 22, 2023
1 parent f2df60e commit 4ba4226
Show file tree
Hide file tree
Showing 6 changed files with 512 additions and 8 deletions.
47 changes: 47 additions & 0 deletions Numerics/ComplexMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,52 @@ public SquareComplexMatrix ToSquareComplexMatrix() {
}

// % ***** Interaction with other classes ***** % //
//: Overloading multiplication by a ComplexVector
public static ComplexVector operator *(ComplexMatrix mat, ComplexVector vec) {

if (vec.Length != mat.Columns) throw new InvalidOperationException();

var result = new ComplexVector(mat.Rows);
for (int i = 0; i < mat.Rows; i++)
for (int j = 0; j < mat.Columns; j++)
result[i] += (dynamic)mat[i,j] * vec[j];
return result;
}

//: Overloading multiplication by a SquareComplexMatrix
public static ComplexMatrix operator *(ComplexMatrix mat1, SquareComplexMatrix mat2) {

if (mat1.Columns != mat2.Dim) throw new InvalidOperationException();

var result = new ComplexMatrix(mat1.Rows, mat2.Dim);
for (int i = 0; i < mat1.Rows; i++)
for (int j = 0; j < mat2.Dim; j++)
for (int k = 0; k < mat2.Dim; k++)
result[i,j] += mat1[i, k] * mat2[k, j];
return result;
}

//: Overloading addition by a SquareComplexMatrix
public static SquareComplexMatrix operator +(ComplexMatrix mat1, SquareComplexMatrix mat2) {

if (mat1.Columns != mat2.Dim || mat2.Dim != mat1.Rows) throw new InvalidOperationException();

var result = new SquareComplexMatrix(mat2.Dim);
for (int i = 0; i < mat2.Dim; i++)
for (int j = 0; j < mat2.Dim; j++)
result[i,j] = mat1[i, j] + mat2[i, j];
return result;
}

//: Overloading subtraction by a SquareComplexMatrix
public static SquareComplexMatrix operator -(ComplexMatrix mat1, SquareComplexMatrix mat2) {

if (mat1.Columns != mat2.Dim || mat2.Dim != mat1.Rows) throw new InvalidOperationException();

var result = new SquareComplexMatrix(mat2.Dim);
for (int i = 0; i < mat2.Dim; i++)
for (int j = 0; j < mat2.Dim; j++)
result[i,j] = mat1[i, j] - mat2[i, j];
return result;
}
}
4 changes: 1 addition & 3 deletions Numerics/ComplexVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Numerics;
// % ***** Class ComplexVector ***** //
public class ComplexVector : IEnumerable, ICloneable
{

public Complex[] vector; /// Vector
public int Length => vector.Length; /// Length vector

Expand Down Expand Up @@ -205,7 +206,4 @@ public static int IndexOf(ComplexVector vec, Complex val) {
public static int LastIndexOf(ComplexVector vec, Complex val) {
return Array.LastIndexOf((Complex[])vec, val);
}

// % ***** Interaction with other classes ***** % //

}
187 changes: 186 additions & 1 deletion Numerics/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,190 @@ public SquareMatrix<T> ToSquareMatrix() {
}

// % ***** Interaction with other classes ***** % //

//: Overloading multiplication by a Vector
public static Vector<T> operator *(Matrix<T> mat, Vector<T> vec) {

if (vec.Length != mat.Columns) throw new InvalidOperationException();

var result = new Vector<T>(mat.Rows);
for (int i = 0; i < mat.Rows; i++)
for (int j = 0; j < mat.Columns; j++)
result[i] += mat[i,j] * vec[j];
return result;
}

//: Overloading multiplication by a ComplexVector
public static ComplexVector operator *(Matrix<T> mat, ComplexVector vec) {

if (vec.Length != mat.Columns) throw new InvalidOperationException();

var result = new ComplexVector(mat.Rows);
for (int i = 0; i < mat.Rows; i++)
for (int j = 0; j < mat.Columns; j++)
result[i] += (dynamic)mat[i,j] * vec[j];
return result;
}

//: Overloading multiplication by a SquareMatrix
public static Matrix<T> operator *(Matrix<T> mat1, SquareMatrix<T> mat2) {

if (mat1.Columns != mat2.Dim) throw new InvalidOperationException();

var result = new Matrix<T>(mat1.Rows, mat2.Dim);
for (int i = 0; i < mat1.Rows; i++)
for (int j = 0; j < mat2.Dim; j++)
for (int k = 0; k < mat2.Dim; k++)
result[i,j] += mat1[i, k] * mat2[k, j];
return result;
}

//: Overloading multiplication by a ComplexMatrix
public static ComplexMatrix operator *(Matrix<T> mat1, ComplexMatrix mat2) {

if (mat1.Columns != mat2.Rows) throw new InvalidOperationException();

var result = new ComplexMatrix(mat1.Rows, mat2.Columns);;
for (int i = 0; i < mat1.Rows; i++)
for (int j = 0; j < mat2.Columns; j++)
for (int k = 0; k < mat1.Columns; k++)
result[i,j] += mat1[i, k] * (dynamic)mat2[k, j];
return result;
}

//: Overloading multiplication by a ComplexMatrix
public static ComplexMatrix operator *(ComplexMatrix mat1, Matrix<T> mat2) {

if (mat1.Columns != mat2.Rows) throw new InvalidOperationException();

var result = new ComplexMatrix(mat1.Rows, mat2.Columns);;
for (int i = 0; i < mat1.Rows; i++)
for (int j = 0; j < mat2.Columns; j++)
for (int k = 0; k < mat1.Columns; k++)
result[i,j] += mat1[i, k] * (dynamic)mat2[k, j];
return result;
}

//: Overloading multiplication by a SquareComplexMatrix
public static ComplexMatrix operator *(Matrix<T> mat1, SquareComplexMatrix mat2) {

if (mat1.Columns != mat2.Dim) throw new InvalidOperationException();

var result = new ComplexMatrix(mat1.Rows, mat2.Dim);
for (int i = 0; i < mat1.Rows; i++)
for (int j = 0; j < mat2.Dim; j++)
for (int k = 0; k < mat2.Dim; k++)
result[i,j] += mat1[i, k] * (dynamic)mat2[k, j];
return result;
}

//: Overloading multiplication by a SquareComplexMatrix
public static ComplexMatrix operator *(SquareComplexMatrix mat1, Matrix<T> mat2) {

if (mat1.Dim != mat2.Rows) throw new InvalidOperationException();

var result = new ComplexMatrix(mat1.Dim, mat2.Columns);
for (int i = 0; i < mat1.Dim; i++)
for (int j = 0; j < mat2.Columns; j++)
for (int k = 0; k < mat1.Dim; k++)
result[i,j] += (dynamic)mat1[i, k] * mat2[k, j];
return result;
}

//: Overloading addition by a SquareMatrix
public static SquareMatrix<T> operator +(Matrix<T> mat1, SquareMatrix<T> mat2) {

if (mat1.Columns != mat2.Dim || mat2.Dim != mat1.Rows) throw new InvalidOperationException();

var result = new SquareMatrix<T>(mat2.Dim);
for (int i = 0; i < mat2.Dim; i++)
for (int j = 0; j < mat2.Dim; j++)
result[i,j] = mat1[i, j] + mat2[i, j];
return result;
}

//: Overloading addition by a ComplexMatrix
public static ComplexMatrix operator +(Matrix<T> mat1, ComplexMatrix mat2) {

if (mat1.Columns != mat2.Columns || mat2.Rows != mat1.Rows) throw new InvalidOperationException();

var result = new ComplexMatrix(mat1.Rows, mat1.Columns);
for (int i = 0; i < mat1.Rows; i++)
for (int j = 0; j < mat1.Columns; j++)
result[i,j] = mat1[i, j] + (dynamic)mat2[i, j];
return result;
}
public static ComplexMatrix operator +(ComplexMatrix mat1, Matrix<T> mat2) => mat2 + mat1;

//: Overloading addition by a SquareComplexMatrix
public static SquareComplexMatrix operator +(Matrix<T> mat1, SquareComplexMatrix mat2) {

if (mat1.Columns != mat2.Dim || mat2.Dim != mat1.Rows) throw new InvalidOperationException();

var result = new SquareComplexMatrix(mat2.Dim);
for (int i = 0; i < mat2.Dim; i++)
for (int j = 0; j < mat2.Dim; j++)
result[i,j] = mat1[i, j] + (dynamic)mat2[i, j];
return result;
}
public static SquareComplexMatrix operator +(SquareComplexMatrix mat1, Matrix<T> mat2) => mat2 + mat1;

//: Overloading subtraction by a SquareComplexMatrix
public static SquareMatrix<T> operator -(Matrix<T> mat1, SquareMatrix<T> mat2) {

if (mat1.Columns != mat2.Dim || mat2.Dim != mat1.Rows) throw new InvalidOperationException();

var result = new SquareMatrix<T>(mat2.Dim);
for (int i = 0; i < mat2.Dim; i++)
for (int j = 0; j < mat2.Dim; j++)
result[i,j] = mat1[i, j] - mat2[i, j];
return result;
}

//: Overloading subtraction by a ComplexMatrix
public static ComplexMatrix operator -(Matrix<T> mat1, ComplexMatrix mat2) {

if (mat1.Columns != mat2.Columns || mat2.Rows != mat1.Rows) throw new InvalidOperationException();

var result = new ComplexMatrix(mat1.Rows, mat1.Columns);
for (int i = 0; i < mat1.Rows; i++)
for (int j = 0; j < mat1.Columns; j++)
result[i,j] = mat1[i, j] - (dynamic)mat2[i, j];
return result;
}

//: Overloading subtraction by a ComplexMatrix
public static ComplexMatrix operator -(ComplexMatrix mat1, Matrix<T> mat2) {

if (mat1.Columns != mat2.Columns || mat2.Rows != mat1.Rows) throw new InvalidOperationException();

var result = new ComplexMatrix(mat1.Rows, mat1.Columns);
for (int i = 0; i < mat1.Rows; i++)
for (int j = 0; j < mat1.Columns; j++)
result[i,j] = mat1[i, j] - (dynamic)mat2[i, j];
return result;
}

//: Overloading subtraction by a SquareComplexMatrix
public static SquareComplexMatrix operator -(Matrix<T> mat1, SquareComplexMatrix mat2) {

if (mat1.Columns != mat2.Dim || mat2.Dim != mat1.Rows) throw new InvalidOperationException();

var result = new SquareComplexMatrix(mat2.Dim);
for (int i = 0; i < mat2.Dim; i++)
for (int j = 0; j < mat2.Dim; j++)
result[i,j] = mat1[i, j] - (dynamic)mat2[i, j];
return result;
}

//: Overloading subtraction by a SquareComplexMatrix
public static SquareComplexMatrix operator -(SquareComplexMatrix mat1, Matrix<T> mat2) {

if (mat1.Dim != mat2.Columns || mat2.Rows != mat1.Dim) throw new InvalidOperationException();

var result = new SquareComplexMatrix(mat1.Dim);
for (int i = 0; i < mat1.Dim; i++)
for (int j = 0; j < mat1.Dim; j++)
result[i,j] = mat1[i, j] - (dynamic)mat2[i, j];
return result;
}
}
43 changes: 42 additions & 1 deletion Numerics/SquareComplexMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public SquareComplexMatrix(int _dim) {

//: Constructor (with double array)
public SquareComplexMatrix(Complex[,] _mat) {

if (_mat.GetUpperBound(0) + 1 != _mat.GetUpperBound(1) + 1) throw new InvalidOperationException();

int dim = _mat.GetUpperBound(0) + 1;
matrix = new Complex[dim, dim];
for (int i = 0; i < dim; i++)
Expand Down Expand Up @@ -191,5 +194,43 @@ public static SquareComplexMatrix Pow(SquareComplexMatrix mat, int degree) {
public ComplexMatrix ToComplexMatrix() => new ComplexMatrix(matrix);

// % ***** Interaction with other classes ***** % //

//: Overloading multiplication by a ComplexVector
public static ComplexVector operator *(SquareComplexMatrix mat, ComplexVector vec) {

if (vec.Length != mat.Dim) throw new InvalidOperationException();

var result = new ComplexVector(vec.Length);
for (int i = 0; i < mat.Dim; i++)
for (int j = 0; j < mat.Dim; j++)
result[i] += (dynamic)mat[i,j] * vec[j];
return result;
}

//: Overloading multiplication by a ComplexMatrix
public static ComplexMatrix operator *(SquareComplexMatrix mat1, ComplexMatrix mat2) {

if (mat1.Dim != mat2.Rows) throw new InvalidOperationException();

var result = new ComplexMatrix(mat1.Dim, mat2.Columns);
for (int i = 0; i < mat1.Dim; i++)
for (int j = 0; j < mat2.Columns; j++)
for (int k = 0; k < mat1.Dim; k++)
result[i,j] += mat1[i, k] * mat2[k, j];
return result;
}

//: Overloading addition by a ComplexMatrix
public static SquareComplexMatrix operator +(SquareComplexMatrix mat1, ComplexMatrix mat2) => mat2 + mat1;

//: Overloading subtraction by a SquareComplexMatrix
public static SquareComplexMatrix operator -(SquareComplexMatrix mat1, ComplexMatrix mat2) {

if (mat1.Dim != mat2.Rows || mat2.Columns != mat1.Dim) throw new InvalidOperationException();

var result = new SquareComplexMatrix(mat1.Dim);
for (int i = 0; i < mat1.Dim; i++)
for (int j = 0; j < mat1.Dim; j++)
result[i,j] = mat1[i, j] - mat2[i, j];
return result;
}
}
Loading

0 comments on commit 4ba4226

Please sign in to comment.