#include <iostream>
#include <complex>
void print(const std::complex<double> (&matrix)[2][2])
{
for(const auto& row: matrix) {
for(const auto& c: row)
std::cout << c << ' ';
std::cout << '\n';
}
}
void multiply(std::complex<double> (&lhs)[2][2], const std::complex<double> (&rhs)[2][2])
{
std::complex<double> result[2][2];
for(int i = 0; i < 2; ++i) for(int j = 0; j < 2; ++j)
result[i][j] = lhs[i][0]*rhs[0][j] + lhs[i][1]*rhs[1][j];
for(int i = 0; i < 2; ++i) for(int j = 0; j < 2; ++j)
lhs[i][j] = result[i][j];
}
void power(std::complex<double> (&matrix)[2][2], unsigned n)
{
std::complex<double> result[2][2] = {{1, 0}, {0, 1}};
for(int i = 0; i < n; ++i)
multiply(result, matrix);
for(int i = 0; i < 2; ++i) for(int j = 0; j < 2; ++j)
matrix[i][j] = result[i][j];
}
int main()
{
using namespace std::complex_literals;
std::complex<double> matrix[2][2] =
{{ 1.0 + 0i, 0.0 + 1i},
{-1.0 + 0i, 0.0 - 1i}};
print(matrix);
std::cout << '\n';
power(matrix, 1);
print(matrix);
std::cout << '\n';
power(matrix, 2);
print(matrix);
std::cout << '\n';
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y29tcGxleD4KCnZvaWQgcHJpbnQoY29uc3Qgc3RkOjpjb21wbGV4PGRvdWJsZT4gKCZtYXRyaXgpWzJdWzJdKQp7Cglmb3IoY29uc3QgYXV0byYgcm93OiBtYXRyaXgpIHsKCQlmb3IoY29uc3QgYXV0byYgYzogcm93KQoJCQlzdGQ6OmNvdXQgPDwgYyA8PCAnICc7CgkJc3RkOjpjb3V0IDw8ICdcbic7Cgl9Cn0KCnZvaWQgbXVsdGlwbHkoc3RkOjpjb21wbGV4PGRvdWJsZT4gKCZsaHMpWzJdWzJdLCBjb25zdCBzdGQ6OmNvbXBsZXg8ZG91YmxlPiAoJnJocylbMl1bMl0pCnsKICAgIHN0ZDo6Y29tcGxleDxkb3VibGU+IHJlc3VsdFsyXVsyXTsKICAgIGZvc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