A flexible and extensible C# SDK for performing matrix operations. This library supports matrix-to-matrix operations, scalar-to-matrix operations, and matrix properties like transposition and determinant calculation. The design follows SOLID principles, ensuring that the logic is decoupled from the implementation of algorithms, allowing for easy swapping or extending of functionality.
-
Dot Product:
Calculates the dot product of two matrices.double dotProduct = matrixService.DotProduct(matrixA, matrixB);
-
Cross Product:
Calculates the cross product of two 3D vectors represented as matrices.var crossProduct = matrixService.CrossProduct(matrixA, matrixB);
-
Matrix Sum:
Adds two matrices element-wise.var sumResult = matrixService.Sum(matrixA, matrixB);
-
Scalar Multiplication:
Multiplies every element in a matrix by a scalar value.var scalarMultiplyResult = matrixService.ScalarMultiply(matrixA, 2);
-
Scalar Addition:
Adds a scalar value to every element in the matrix.var scalarAddResult = matrixService.ScalarAdd(matrixA, 5);
-
Transpose:
Transposes a given matrix (flips rows and columns).var transposedMatrix = matrixA.Transpose();
-
Determinant:
Calculates the determinant of a square matrix (only for matrices of typedouble
).double determinant = matrixA.Determinant();
This library is designed following the Dependency Inversion Principle from SOLID. All matrix operations are defined via interfaces, and the concrete algorithm implementations are injected into the service layer. This means:
- You can easily swap algorithms without modifying the core logic.
- Extend functionality by adding new algorithms implementing the provided interfaces.
- Keep the business logic clean and separate from specific implementation details.
Example:
To switch to a custom dot product algorithm, simply implement the IMatrixDotProductAlgorithm<T>
interface and inject it into the MatrixOperationsService<T>
.
var customDotProductAlgorithm = new CustomDotProductAlgorithm();
var matrixService = new MatrixOperationsService<double>(
customDotProductAlgorithm,
crossProductAlgorithm,
sumAlgorithm,
scalarOperationsAlgorithm
);
- .NET 6 SDK or higher
To check if .NET is installed:
dotnet --version
-
Navigate to the Project Directory:
cd /path/to/matrix-operations-library
-
Run the Example:
Use the following command to run
program.cs
, which demonstrates basic matrix operations:dotnet run program.cs
Dot Product: 70
Matrix Sum:
6 8
10 12
Scalar Multiplication:
2 4
6 8
matrix-operations-library/
├── src/
│ ├── algorithms/ # Algorithm implementations for matrix operations
│ ├── interfaces/ # Interfaces defining matrix operations
│ ├── services/ # Service layer exposing matrix functionalities
│ ├── types/ # Matrix types (e.g., 2D matrices)
│ └── MatrixOperationsLibrary.csproj
├── doc/
│ └── conventions.md # Coding conventions for the project
├── program.cs # Example demonstrating matrix operations
└── README.md # This file
To add a new matrix operation:
- Define an Interface in the
interfaces
folder (e.g.,IMatrixInverseAlgorithm.cs
). - Implement the Algorithm in the
algorithms
folder (e.g.,MatrixInverseAlgorithm.cs
). - Inject the Implementation into
MatrixOperationsService
.
This project is licensed under the MIT License - see the LICENSE file for details.