-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsparse.c
More file actions
58 lines (54 loc) · 1.64 KB
/
sparse.c
File metadata and controls
58 lines (54 loc) · 1.64 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "spmat.h"
#include "eigen.h"
#include "sparse.h"
#include <assert.h>
#include "MatrixAndVectorOps.h"
/*
* Power iterations with sparse matrix implementation
*/
/*
* Calculates Dominant normalized eigenvector using power iterations
* Input: Matrix, initial vector to start calculation with and matrix size
* Output: Dominant normalized eigenvector, meaning eigenvector with biggest absolute eigenvalue
*/
double* PowerIterationsWithSparse(double** matrix, double * init_vector, int size)
{
int iter = 0;
/*int i;*/
double* new_vector;
/*
spmat* A = spmat_allocate_list(size);
for (i = 0; i < size; ++i) {
A->add_row(A,matrix[i],i);
}
*/
new_vector = (double*)malloc(size*sizeof(double));
/*printVector(size,init_vector);*/
while(hasEpsilonDifference(init_vector,new_vector,size) == 0)
{
/*PowerIteration(A,init_vector,new_vector,size);*/
/*printf("%d\n",iter);*/
iterate(matrix,init_vector,new_vector,size);
iter += 2;
}
/* printf("Iter: %d\n",iter);
printf("Normalized eigenvector is: \n");
printVector(size,init_vector);*/
/*A->free(A);*/
free(new_vector);
return init_vector;
}
/*
* 2 Power iterations implementation with sparse matrix
* Input: Sparse matrix, 2 vectors and matrix size
* Output: new_vector will contain b_k (from formula in hw1), vector will contain b_k+1
*/
void PowerIteration(spmat* A, double* vector, double* new_vector, int size)
{
A->mult(A,vector,new_vector);
normalizeVector(size,new_vector);
A->mult(A,new_vector,vector);
normalizeVector(size,vector);
}