Skip to content

A collection of efficient matrix classes in C++ for diagonal, lower triangular, upper triangular, symmetric, tridiagonal, and Toeplitz matrices, optimized for memory usage and element access.

Notifications You must be signed in to change notification settings

khalidalam980/EfficientMatrixClasses

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

EfficientMatrixClasses

A collection of efficient matrix classes in C++ for diagonal, lower triangular, upper triangular, symmetric, tridiagonal, and Toeplitz matrices, optimized for memory usage and element access. The above code is a C++ program that defines several classes for different types of matrices and demonstrates their usage. Here's a summary of each class and its purpose:

  1. Diagonal: Represents a diagonal matrix. It has a constructor to create a diagonal matrix with a given size, a destructor to free memory, and methods to set and get elements of the matrix. The Display method prints the matrix.

  2. LTMatrix: Represents a lower triangular matrix. It has similar functionalities as the Diagonal class but uses a different formula to store and access elements. The formula used is based on the row-major ordering. The Display method prints the matrix.

  3. UTMatrix: Represents an upper triangular matrix. It is similar to the LTMatrix class but uses a different formula based on column-major ordering. The Display method prints the matrix.

  4. SymMatrix: Represents a symmetric matrix. It uses the row-major formula of the lower triangular matrix to store elements. If A[i][j] equals A[j][i], the matrix is symmetric. It has methods to set and get elements, as well as a Display method to print the matrix.

  5. TrMatrix: Represents a tridiagonal matrix. It stores nonzero elements only on the main diagonal, the diagonal above the main diagonal, and the diagonal below the main diagonal. It provides methods to set and get elements, as well as a Display method to print the matrix.

  6. ToeplitzMatrix: Represents a Toeplitz matrix. It is a matrix where each descending diagonal from left to right is constant. It has methods to set and get elements, as well as a Display method to print the matrix.

The main function demonstrates the usage of the ToeplitzMatrix class. It prompts the user for the dimensions of the matrix, reads the elements from the user, sets them in the matrix using the Set method, and finally displays the matrix using the Display method.

The code provided demonstrates several advantages in terms of time and space efficiency:

  1. Optimized Memory Usage: The classes for different types of matrices (Diagonal, LTMatrix, UTMatrix, SymMatrix, TrMatrix, ToeplitzMatrix) are designed to use memory efficiently. They allocate memory dynamically based on the size of the matrix, resulting in memory usage proportional to the number of non-zero elements. This is particularly beneficial for sparse matrices, where a significant amount of memory can be saved by only storing non-zero elements.

  2. Efficient Access: The classes provide methods (Set and Get) to access matrix elements in an optimized manner. They use specific formulas to calculate the index of the elements based on the matrix type (e.g., row-major or column-major ordering for lower and upper triangular matrices). This eliminates the need for a two-dimensional array and allows direct access to elements, resulting in faster access times.

  3. Time Complexity: The time complexity of accessing or setting an element in these matrix classes is generally O(1) since the index calculation is based on simple formulas rather than searching through a two-dimensional array. This provides efficient element manipulation and is especially advantageous when dealing with large matrices.

  4. Space Complexity: The space complexity of these matrix classes is reduced compared to a traditional two-dimensional array. They only store the non-zero elements, resulting in lower space requirements, especially for sparse matrices. For example, the TrMatrix class only stores the elements on the main diagonal and its adjacent diagonals, resulting in significant memory savings.

  5. Reusability: The code provides reusable and modular classes for different types of matrices. Each class encapsulates the logic and operations specific to a matrix type, making it easier to maintain, modify, and extend the code. This reusability promotes code organization and reduces the likelihood of errors when working with different matrix types.

Overall, the code is designed to optimize memory usage and provide efficient element access, resulting in improved time and space complexity compared to traditional approaches using two-dimensional arrays for all matrix types.

About

A collection of efficient matrix classes in C++ for diagonal, lower triangular, upper triangular, symmetric, tridiagonal, and Toeplitz matrices, optimized for memory usage and element access.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages