Skip to content
SANJAY KUMAR edited this page Jul 21, 2024 · 1 revision

Matrix Library

Overview

The Matrix Library is a C library designed for advanced matrix calculations. It provides a range of functions for performing matrix operations, including basic arithmetic operations (addition, subtraction, multiplication) and more advanced calculations such as matrix transposition, inversion, and determinant calculation. This library is intended for use in numerical computing and scientific applications.

Features

  • Matrix Operations: Addition, Subtraction, Multiplication
  • Advanced Operations: Transposition, Inversion, Determinant Calculation
  • Matrix Norm Calculation
  • Identity Matrix Creation
  • Eigenvalue Computation (Placeholder)

Installation

Linux

  1. Clone the Repository

    git clone https://github.com/rubikproxy/matrix.h
    cd matrix.h
  2. Build the Library

    make
  3. Install the Library

    sudo make install
  4. Update Library Cache

    sudo ldconfig

macOS

  1. Clone the Repository

    git clone https://github.com/rubikproxy/matrix.h
    cd matrix.h
  2. Build the Library

    make
  3. Install the Library

    sudo cp libmatrix.a /usr/local/lib/
    sudo cp include/matrix.h /usr/local/include/
  4. Verify Installation

    ls /usr/local/lib/libmatrix.a
    ls /usr/local/include/matrix.h

Windows

  1. Clone the Repository

    git clone https://github.com/rubikproxy/matrix.h
    cd matrix.h
  2. Build the Library

    Use a compatible build system or manually compile the source files using MinGW or Visual Studio.

  3. Install the Library

    Copy the library and header files to appropriate directories:

    • Library: libmatrix.a to C:\Program Files\MatrixLib\lib\
    • Header: matrix.h to C:\Program Files\MatrixLib\include\

Usage

Example Code

Here is an example demonstrating how to use the library:

#include <stdio.h>
#include <matrix.h>

int main() {
    // Create matrices
    Matrix *a = create_matrix(2, 2);
    Matrix *b = create_matrix(2, 2);

    // Initialize matrices
    a->data[0] = 1; a->data[1] = 2;
    a->data[2] = 3; a->data[3] = 4;

    b->data[0] = 5; b->data[1] = 6;
    b->data[2] = 7; b->data[3] = 8;

    // Matrix addition
    Matrix *result_add = matrix_add(a, b);
    printf("Matrix Addition:\n");
    print_matrix(result_add);

    // Matrix multiplication
    Matrix *result_mul = matrix_mul(a, b);
    printf("Matrix Multiplication:\n");
    print_matrix(result_mul);

    // Clean up
    free_matrix(a);
    free_matrix(b);
    free_matrix(result_add);
    free_matrix(result_mul);

    return 0;
}

Example Output

Matrix Addition:
6.000000 8.000000 
10.000000 12.000000 
Matrix Multiplication:
19.000000 22.000000 
43.000000 50.000000 

Function Documentation

Matrix *create_matrix(size_t rows, size_t cols)

Allocates memory for a matrix with the specified number of rows and columns.

void free_matrix(Matrix *matrix)

Frees the memory allocated for the matrix.

void print_matrix(const Matrix *matrix)

Prints the matrix to the standard output.

Matrix *matrix_add(const Matrix *a, const Matrix *b)

Adds two matrices. The matrices must have the same dimensions.

Matrix *matrix_sub(const Matrix *a, const Matrix *b)

Subtracts matrix b from matrix a. The matrices must have the same dimensions.

Matrix *matrix_mul(const Matrix *a, const Matrix *b)

Multiplies two matrices. The number of columns in matrix a must equal the number of rows in matrix b.

Matrix *matrix_transpose(const Matrix *matrix)

Transposes the matrix.

Matrix *matrix_inverse(const Matrix *matrix)

Calculates the inverse of a square matrix. Returns NULL if the matrix is not invertible.

double matrix_determinant(const Matrix *matrix)

Calculates the determinant of a square matrix.

Matrix *matrix_identity(size_t size)

Creates an identity matrix of the given size.

double matrix_norm(const Matrix *matrix)

Calculates the Frobenius norm of the matrix.

void matrix_eigenvalues(const Matrix *matrix, double *real, double *imag)

Computes the eigenvalues of a square matrix. The real and imaginary parts are stored in the provided arrays.

License

This library is licensed under the MIT License. See the LICENSE file for more details.

External Links