2021-02-03 12:13:09 +01:00
|
|
|
// Helpers.hpp
|
|
|
|
// Some usefull helper functions to support the Maponi algorithm.
|
|
|
|
#include <iostream>
|
|
|
|
#include <cmath>
|
|
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
unsigned int getMaxIndex(T *vector, unsigned int size) {
|
2021-02-09 13:40:52 +01:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int max = vector[0];
|
2021-02-03 12:13:09 +01:00
|
|
|
unsigned int maxi = 0;
|
|
|
|
for (i = 1; i < size; i++) {
|
|
|
|
if (vector[i] > max) {
|
|
|
|
max = vector[i];
|
|
|
|
maxi = i;
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 13:40:52 +01:00
|
|
|
return maxi;
|
2021-02-03 12:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void showScalar(T scalar, string name) {
|
|
|
|
cout << name << " = " << scalar << endl << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2021-02-03 14:49:13 +01:00
|
|
|
void showVector(T *vector, unsigned int size, string name) {
|
2021-02-03 12:13:09 +01:00
|
|
|
cout << name << " = " << endl;
|
|
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
|
|
cout << "[ " << vector[i] << " ]" << endl;
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2021-02-09 13:40:52 +01:00
|
|
|
void showMatrix(T *matrix, unsigned int M, string name) {
|
2021-02-03 12:13:09 +01:00
|
|
|
cout << name << " = " << endl;
|
2021-02-09 13:40:52 +01:00
|
|
|
for (unsigned int i = 0; i < M; i++) {
|
2021-02-03 12:13:09 +01:00
|
|
|
cout << "[ ";
|
2021-02-09 13:40:52 +01:00
|
|
|
for (unsigned int j = 0; j < M; j++) {
|
|
|
|
cout << matrix[i*M+j] << " ";
|
2021-02-03 12:13:09 +01:00
|
|
|
}
|
|
|
|
cout << " ]" << endl;
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2021-02-03 14:49:13 +01:00
|
|
|
void showMatrixT(T **matrix, unsigned int size, string name) {
|
2021-02-03 12:13:09 +01:00
|
|
|
cout << name << " = " << endl;
|
|
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
|
|
cout << "[ ";
|
|
|
|
for (unsigned int j = 0; j < size; j++) {
|
|
|
|
cout << matrix[j][i] << " ";
|
|
|
|
}
|
|
|
|
cout << " ]" << endl;
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2021-02-09 13:40:52 +01:00
|
|
|
T *matMul(T *A, T *B, unsigned int M) {
|
|
|
|
T *C = new T[M*M];
|
|
|
|
for (unsigned int i = 0; i < M; i++) {
|
|
|
|
for (unsigned int j = 0; j < M; j++) {
|
|
|
|
for (unsigned int k = 0; k < M; k++) {
|
|
|
|
C[i*M+j] += A[i*M+k] * B[k*M+j];
|
2021-02-06 18:59:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-03 12:13:09 +01:00
|
|
|
template<typename T1, typename T2>
|
2021-02-09 13:40:52 +01:00
|
|
|
T1 *outProd(T1 *vec1, T2 *vec2, unsigned int M) {
|
|
|
|
T1 *C = new T1[M*M];
|
|
|
|
for (unsigned int i = 0; i < M; i++) {
|
|
|
|
for (unsigned int j = 0; j < M; j++) {
|
|
|
|
C[i*M+j] = vec1[i+1] * vec2[j];
|
2021-02-03 12:13:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2021-02-03 14:49:13 +01:00
|
|
|
T matDet(T **A, unsigned int M) {
|
2021-02-03 12:13:09 +01:00
|
|
|
int det = 0, p, h, k, i, j;
|
2021-02-03 14:49:13 +01:00
|
|
|
T **temp = new T*[M];
|
2021-02-03 12:13:09 +01:00
|
|
|
for (int i = 0; i < M; i++) temp[i] = new T[M];
|
|
|
|
if(M == 1) {
|
|
|
|
return A[0][0];
|
|
|
|
}
|
|
|
|
else if(M == 2) {
|
|
|
|
det = (A[0][0] * A[1][1] - A[0][1] * A[1][0]);
|
|
|
|
return det;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(p = 0; p < M; p++) {
|
|
|
|
h = 0;
|
|
|
|
k = 0;
|
|
|
|
for(i = 1; i < M; i++) {
|
|
|
|
for( j = 0; j < M; j++) {
|
|
|
|
if(j == p) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
temp[h][k] = A[i][j];
|
|
|
|
k++;
|
|
|
|
if(k == M-1) {
|
|
|
|
h++;
|
|
|
|
k = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
det = det + A[0][p] * pow(-1, p) * matDet(temp, M-1);
|
|
|
|
}
|
|
|
|
return det;
|
|
|
|
}
|
|
|
|
delete [] temp;
|
2021-02-09 13:40:52 +01:00
|
|
|
}
|
2021-02-16 10:49:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool is_identity(T *A, unsigned int M, double tolerance) {
|
|
|
|
for (unsigned int i = 0; i < M; i++) {
|
|
|
|
for (unsigned int j = 0; j < M; j++) {
|
|
|
|
if (i==j && fabs(A[i*M+j]-1) > tolerance) return false;
|
|
|
|
if (i!=j && fabs(A[i*M+j]) > tolerance) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|