How to resolve the algorithm Matrix-exponentiation operator step by step in the C++ programming language
How to resolve the algorithm Matrix-exponentiation operator step by step in the C++ programming language
Table of Contents
Problem Statement
Most programming languages have a built-in implementation of exponentiation for integers and reals only.
Demonstrate how to implement matrix exponentiation as an operator.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Matrix-exponentiation operator step by step in the C++ programming language
The code provided is a C++ implementation of a square matrix class, SqMx, which can be used to perform matrix operations such as multiplication and exponentiation. The matrix class is templated on the size of the matrix and the type of elements it contains. In the code provided, the matrix is templated on the size 3 and the type complex
The matrix class has a private data member a, which is an array of size MSize x MSize. The constructor for the matrix class takes an array of the appropriate size and initializes the matrix with the values in the array. The matrix class also has a static method identity() which returns the identity matrix of the appropriate size.
The matrix class has a method operator*() which overloads the multiplication operator for matrices. The method takes another matrix as an argument and returns the product of the two matrices. The method operator^() overloads the exponentiation operator for matrices. The method takes an integer as an argument and returns the matrix raised to the power of the integer.
The main() function in the code provided creates a 3x3 matrix of complex numbers and then raises the matrix to the power of 23 using the operator^() method. The result is printed to the console.
Source code in the cpp programming language
#include <complex>
#include <cmath>
#include <iostream>
using namespace std;
template<int MSize = 3, class T = complex<double> >
class SqMx {
typedef T Ax[MSize][MSize];
typedef SqMx<MSize, T> Mx;
private:
Ax a;
SqMx() { }
public:
SqMx(const Ax &_a) { // constructor with pre-defined array
for (int r = 0; r < MSize; r++)
for (int c = 0; c < MSize; c++)
a[r][c] = _a[r][c];
}
static Mx identity() {
Mx m;
for (int r = 0; r < MSize; r++)
for (int c = 0; c < MSize; c++)
m.a[r][c] = (r == c ? 1 : 0);
return m;
}
friend ostream &operator<<(ostream& os, const Mx &p)
{ // ugly print
for (int i = 0; i < MSize; i++) {
for (int j = 0; j < MSize; j++)
os << p.a[i][j] << ',';
os << endl;
}
return os;
}
Mx operator*(const Mx &b) {
Mx d;
for (int r = 0; r < MSize; r++)
for (int c = 0; c < MSize; c++) {
d.a[r][c] = 0;
for (int k = 0; k < MSize; k++)
d.a[r][c] += a[r][k] * b.a[k][c];
}
return d;
}
// C++ does not have a ** operator, instead, ^ (bitwise Xor) is used.
Mx operator^(int n) {
if (n < 0)
throw "Negative exponent not implemented";
Mx d = identity();
for (Mx sq = *this; n > 0; sq = sq * sq, n /= 2)
if (n % 2 != 0)
d = d * sq;
return d;
}
};
typedef SqMx<> M3;
typedef complex<double> creal;
int main() {
double q = sqrt(0.5);
creal array[3][3] = { { { q, 0 }, { q, 0 }, { 0, 0 } },
{ { 0, -q }, { 0, q }, { 0, 0 } },
{ { 0, 0 }, { 0, 0 }, { 0, 1 } } };
M3 m(array);
cout << "m ^ 23=" << endl
<< (m ^ 23) << endl;
return 0;
}
You may also check:How to resolve the algorithm Smarandache-Wellin primes step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Factorions step by step in the Swift programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Miranda programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Joystick position step by step in the Julia programming language