0% found this document useful (0 votes)
20 views

Diagonal Matrix

Uploaded by

nandinimani2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Diagonal Matrix

Uploaded by

nandinimani2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Diagonal Matrix

template <class T>


class diagonalMatrix
{
public:
diagonalMatrix(int size = 10);
~diagonalMatrix() { delete[] element; }

T get(int i, int j) const;


void set(int i, int j, const T& newValue);

private:
int n;
T* element;
};

template <class T>


diagonalMatrix<T>::diagonalMatrix(int size)
{
if (size < 1)
throw illegalParameterValue("Matrix size must be > 0");
n = size;
element = new T[n]();
}

template <class T>


T diagonalMatrix<T>::get(int i, int j) const
{
if (i < 1 || j < 1 || i > n || j > n)
throw matrixIndexOutOfBounds();
if (i == j)
return element[i - 1];
else
return 0;
}

template <class T>


void diagonalMatrix<T>::set(int i, int j, const T& newValue)
{
if (i < 1 || j < 1 || i > n || j > n)
throw matrixIndexOutOfBounds();
if (i == j)
element[i - 1] = newValue;
else if (newValue != 0)
throw illegalParameterValue("Non-diagonal elements must be zero");
}

int main() {

diagonalMatrix<int> mat(5);

mat.set(1, 1, 10);
mat.set(2, 2, 20);
mat.set(3, 3, 30);
mat.set(4, 4, 40);
mat.set(5, 5, 50);

cout << "Diagonal Matrix:" << endl;


for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
cout << mat.get(i, j) << " ";
cout << endl;
}
mat.set(1, 2, 5);
return 0;
}
Tridiagonal Matrix

template <class T>


class tridiagonalMatrix {
public:
tridiagonalMatrix(int size = 10);
~tridiagonalMatrix()
{
delete[] element;
}

T get(int i, int j) const;


void set(int i, int j, const T& newValue);

private:
int n;
T* element;
};

template <class T>


tridiagonalMatrix<T>::tridiagonalMatrix(int size) {
if (size < 1)
throw illegalParameterValue("Matrix size must be > 0");
n = size;
element = new T[3 * n - 2](); // Only 3n - 2 elements are needed
}

T tridiagonalMatrix<T>::get(int i, int j) const


{
if (i < 1 || j < 1 || i > n || j > n)
throw matrixIndexOutOfBounds();

switch (i - j) {
case 1: // Lower diagonal
return element[i - 2];
case 0: // Main diagonal
return element[n - 1 + i - 1];
case -1: // Upper diagonal
return element[2 * n - 1 + i - 1];
default:
return 0;
}
}

template <class T>


void tridiagonalMatrix<T>::set(int i, int j, const T& newValue) {
if (i < 1 || j < 1 || i > n || j > n)
throw matrixIndexOutOfBounds();

switch (i - j) {
case 1:
element[i - 2] = newValue;
break;
case 0:
element[n - 1 + i - 1] = newValue;
break;
case -1:
element[2 * n - 1 + i - 1] = newValue;
break;
default:
if (newValue != 0)
throw illegalParameterValue("Non-tridiagonal elements must be zero");
}

}
int main()
{
tridiagonalMatrix<int> mat(5);
mat.set(1, 1, 10);
mat.set(2, 2, 20);
mat.set(3, 3, 30);
mat.set(4, 4, 40);
mat.set(5, 5, 50);

mat.set(1, 2, 5);
mat.set(2, 3, 6);
mat.set(3, 4, 7);
mat.set(4, 5, 8);

mat.set(2, 1, 1);
mat.set(3, 2, 2);
mat.set(4, 3, 3);
mat.set(5, 4, 4);

cout << "Tridiagonal Matrix:" << endl;


for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
cout << mat.get(i, j) << " ";
cout << endl;
}

return 0;
}
Lower Triangle Matrix

#include <iostream>
#include <stdexcept>
using namespace std;

template <class T>


class lowerTriangularMatrix
{
public:
lowerTriangularMatrix(int size = 10);
~lowerTriangularMatrix() { delete[] element; }

T get(int i, int j) const;


void set(int i, int j, const T& newValue);

private:
int n;
T* element;

template <class T>


lowerTriangularMatrix<T>::lowerTriangularMatrix(int size) {
if (size < 1)
throw illegalParameterValue("Matrix size must be > 0");
n = size;
element = new T[n * (n + 1) / 2]();
}

template <class T>


T lowerTriangularMatrix<T>::get(int i, int j) const {
if (i < 1 || j < 1 || i > n || j > n)
throw matrixIndexOutOfBounds();
if (i >= j) // Element is within lower triangle
return element[i * (i - 1) / 2 + j - 1];
else // Element is not in lower triangle
return 0;
}

template <class T>


void lowerTriangularMatrix<T>::set(int i, int j, const T& newValue)
{
if (i < 1 || j < 1 || i > n || j > n)
throw matrixIndexOutOfBounds();

if (i >= j)
element[i * (i - 1) / 2 + j - 1] = newValue;
else
if (newValue != 0)
throw illegalParameterValue("Elements not in lower triangle must be zero");
}

#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;

class SymmetricMatrix {
public:
SymmetricMatrix(int size) : n(size) {
if (size <= 0)
throw invalid_argument("Matrix size must be posiive.");
elements.resize(size * (size + 1) / 2); // Only store the lower triangle
}

// Geker for (i, j) element


int get(int i, int j) const {
checkBounds(i, j);
if (i >= j)
return elements[index(i, j)];
else
return elements[index(j, i)]; // Leverage symmetry
}

// Seker for (i, j) element


void set(int i, int j, int value) {
checkBounds(i, j);
if (i >= j)
elements[index(i, j)] = value;
else
elements[index(j, i)] = value; // Leverage symmetry
}

// Funcion to display the matrix


void display() const {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << get(i, j) << " ";
}
cout << endl;
}
}

private:
int n; // Matrix dimension
vector<int> elements; // Store only lower triangle elements

// Check if indices are within bounds


void checkBounds(int i, int j) const {
if (i < 1 || j < 1 || i > n || j > n)
throw out_of_range("Matrix indices out of bounds.");
}

// Calculate 1D index for element (i, j) in lower triangle


int index(int i, int j) const {
return i * (i - 1) / 2 + (j - 1); // 1D indexing for lower triangle storage
}
};

int main() {
try {
SymmetricMatrix matrix(3);

// Set some values


matrix.set(1, 1, 1);
matrix.set(2, 1, 2);
matrix.set(2, 2, 3);
matrix.set(3, 1, 4);
matrix.set(3, 2, 5);
matrix.set(3, 3, 6);

cout << "Symmetric Matrix:" << endl;


matrix.display();
}
catch (const excepion& e)
{
cerr << "Error: " << endl;
}

return 0;
}

You might also like