Skip to content

IliaFarzi/matrix-operations-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Matrix Operations Library

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.


Features

1. Matrix-to-Matrix Operations

  • 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);

2. Scalar-to-Matrix Operations

  • 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);

3. Matrix Properties

  • 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 type double).

    double determinant = matrixA.Determinant();

Decoupled Design

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
);

Getting Started

1. Prerequisites

To check if .NET is installed:

dotnet --version

2. Run the Example

  1. Navigate to the Project Directory:

    cd /path/to/matrix-operations-library
  2. Run the Example:

    Use the following command to run program.cs, which demonstrates basic matrix operations:

    dotnet run program.cs

3. Example Output

Dot Product: 70
Matrix Sum:
6 8 
10 12 
Scalar Multiplication:
2 4 
6 8 

Project Structure

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

Extending the Library

To add a new matrix operation:

  1. Define an Interface in the interfaces folder (e.g., IMatrixInverseAlgorithm.cs).
  2. Implement the Algorithm in the algorithms folder (e.g., MatrixInverseAlgorithm.cs).
  3. Inject the Implementation into MatrixOperationsService.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages