1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <math.h>
void randomVector(int N, double* vector){
for(int i = 0; i < N; i++) {
vector[i] = ((double) rand()) / RAND_MAX;
}
}
void sortZeile(int M, int* nonZeros, int* perm){
for(int i = 0; i < M; i++){
int maxIndex = i;
for(int j = i + 1; j < M; j++){
if(nonZeros[maxIndex] < nonZeros[j]){
maxIndex = j;
}
}
std::swap(perm[maxIndex], perm[i]);
std::swap(nonZeros[maxIndex], nonZeros[i]);
}
}
void fullenMatrix(int M,int L, int* nonZeros,int* perm, int* rowIndex, double* jdiag,double* matrix,int* col_ind, int* columnIndex, int* begin){
int nonZerosCol = 0;
int jdiagIndex = 0;
for(int i = 0, l = 0, h = 0; h < nonZeros[0]; ++h, i+=M){
for(int k = 0, m = 0; k < M; k++){
for(int j = 0; j < L; j++){
if(perm[k] == rowIndex[j] && m++ == l){
jdiag[jdiagIndex] = matrix[j];
col_ind[jdiagIndex++] = columnIndex[j];
nonZerosCol++;
break;
}
}
m = 0;
}
begin[l + 1] = nonZerosCol + 1;
l++;
}
}
void jdsMult(int M, int* nonZeros,int* begin, double* jdiag, double* vector, int* col_ind, int* perm, double* resultVector ){
for(int i = 0; i < M; i++){
int offset = i;
double sum = 0;
for(int j = 0; j < nonZeros[i]; j++){
offset = begin[j] - 1 + i;
sum += jdiag[offset] * vector[col_ind[offset] - 1];
}
resultVector[perm[i] - 1] = sum;
}
}
void normMult(int M, int N, double* vector, double* matrixWithZeros, double* normResult){
for(int i = 0; i < M; i++){
double sum = 0;
for(int j = 0; j < N; j++){
sum += vector[j] * matrixWithZeros[i*N+j];
}
normResult[i] = sum;
}
}
void residual(int M, double* resultVector2, double* resultVector){
double* k = new double[M];
double accum = 0.0;
for (int i = 0; i < M; ++i) {
k[i]=pow(fabs(resultVector2[i]-resultVector[i]),2);
accum+=k[i];
}
double norm = sqrt(accum);
std::cout<<"Residual : "<<norm;
}
int main() {
//Open the file:
std::ifstream fin("mcfe.txt");
// Declare variables:
int M, N, L;
// Ignore headers and comments:
while (fin.peek() == '%') fin.ignore(2048, '\n');
// Read defining parameters:
fin >> M >> N >> L;
// Create your matrix:
double* matrix;
double* matrixWithZeros;
matrix = new double[L];
matrixWithZeros = new double[M*N];
std::fill(matrix, matrix + L, 0.);
//for(int i=0;i<L;i++){
// std::cout<<matrix[i];
// }
std::cout<<std::endl;
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
matrixWithZeros[i*N+j]=0.;
//std::cout<<matrixWithZeros[i*N+j];
}
}
int* rowIndex = new int[L];
int* columnIndex = new int[L];
std::cout<<std::endl;
// Read the data
for (int l = 0; l < L; l++)
{
int m, n;
double data;
fin >> m >> n >> data;
matrix[l] = data;
//std::cout<<matrix[l];
//std::cout<<std::endl;
rowIndex[l] = m;
columnIndex[l] = n;
matrixWithZeros[((rowIndex[l]-1)*N)+(columnIndex[l]-1)] = matrix[l];
// cout<<matrixWithZeros[];
}
std::cout<<std::endl;
//for(int i=0;i<M;i++){
// for(int j=0;j<N;j++){
// matrixWithZeros[i*N+j]=matrix[i];
// }
// }
// for(int i=0;i<M;i++){
// for(int j=0;j<N;j++){
// std::cout<<matrixWithZeros[i*N+j];
// }
// }
fin.close();
std::cout<<std::endl;
double* vector = new double[N];
// For counting non Zero elements in Row
int * nonZeros = new int[M]{};
// Count non Zero elements in Rows
for(int i = 0; i < L; i++){
nonZeros[rowIndex[i] - 1]++;
}
// Perm array for keeping order after sorting
int* perm = new int[M];
// Initialization of perm array with values from 1..M
for(int i = 0; i < M; i++){
perm[i] = i + 1;
}
double* jdiag = new double[L];
int* col_ind = new int[L];
sortZeile(M,nonZeros,perm);
// Initialization of begin array with first value 1
int* begin = new int[nonZeros[0]];
// NonZerosCol is used to calculate the number of non Zeros in Column
begin[0] = 1;
double* jdsResult = new double[M];
double* normResult = new double[M];
fullenMatrix(M,L,nonZeros,perm,rowIndex,jdiag,matrix,col_ind,columnIndex,begin);
/* std::cout << "\nJdiag:\n";
for(int i = 0; i < L; i++){
std::cout<<jdiag[i] << " ";
}
std::cout<< "\nCol_ind:\n";
for(int i = 0; i < L; i++){
std::cout<<col_ind[i] << " ";
}
std::cout<< "\nPerm:\n";
for(int i = 0; i < M; i++){
std::cout<< perm[i] << " ";
}
std::cout<< "\nBegin:\n";
for(int i = 0; i < nonZeros[0]; i++){
std::cout<<begin[i] << " ";
}*/
//random vector
randomVector(N, vector);
//Matrix Vector multiplication with jds
jdsMult(M, nonZeros,begin,jdiag,vector,col_ind,perm,jdsResult);
// This is the output of the result vector
//std::cout<< "\njdsResult:\n";
// for(int i = 0; i < M; i++){
// std::cout << jdsResult[i] << " ";
// }
//Normal Matrix Vector multiplication
normMult(M, N, vector, matrixWithZeros, normResult);
// This is the output of the result vector
//std::cout<< "\nnormalMatrixResult:\n";
// for(int i = 0; i < M; i++){
// std::cout << normResult[i] << " ";
// }
residual(M, normResult, jdsResult);
return 0;
}
|