21 static double p_iteration_threshold = 1e-3;
25 double *evals,
int initialize)
30 double *tmp_vec =
N_GNEW(n,
double);
31 double *last_vec =
N_GNEW(n,
double);
39 int Max_iterations = 30 * n;
41 double tol = 1 - p_iteration_threshold;
47 for (i = 0; i < neigs; i++) {
48 curr_vector = eigs[i];
52 for (j = 0; j < n; j++)
53 curr_vector[j] = rand() % 100;
55 for (j = 0; j < i; j++) {
56 alpha = -
dot(eigs[j], 0, n - 1, curr_vector);
57 scadd(curr_vector, 0, n - 1, alpha, eigs[j]);
59 len =
norm(curr_vector, 0, n - 1);
64 vecscale(curr_vector, 0, n - 1, 1.0 / len, curr_vector);
68 cpvec(last_vec, 0, n - 1, curr_vector);
72 cpvec(curr_vector, 0, n - 1, tmp_vec);
75 for (j = 0; j < i; j++) {
76 alpha = -
dot(eigs[j], 0, n - 1, curr_vector);
77 scadd(curr_vector, 0, n - 1, alpha, eigs[j]);
79 len =
norm(curr_vector, 0, n - 1);
80 if (len < 1e-10 || iteration > Max_iterations) {
85 vecscale(curr_vector, 0, n - 1, 1.0 / len, curr_vector);
86 angle =
dot(curr_vector, 0, n - 1, last_vec);
87 }
while (fabs(angle) < tol);
88 evals[i] = angle * len;
93 for (; i < neigs; i++) {
97 curr_vector = eigs[i];
99 for (j = 0; j < n; j++)
100 curr_vector[j] = rand() % 100;
102 for (j = 0; j < i; j++) {
103 alpha = -
dot(eigs[j], 0, n - 1, curr_vector);
104 scadd(curr_vector, 0, n - 1, alpha, eigs[j]);
106 len =
norm(curr_vector, 0, n - 1);
107 vecscale(curr_vector, 0, n - 1, 1.0 / len, curr_vector);
114 for (i = 0; i < neigs - 1; i++) {
116 largest_eval = evals[largest_index];
117 for (j = i + 1; j < neigs; j++) {
118 if (largest_eval < evals[j]) {
120 largest_eval = evals[largest_index];
123 if (largest_index != i) {
124 cpvec(tmp_vec, 0, n - 1, eigs[i]);
125 cpvec(eigs[i], 0, n - 1, eigs[largest_index]);
126 cpvec(eigs[largest_index], 0, n - 1, tmp_vec);
128 evals[largest_index] = evals[i];
129 evals[i] = largest_eval;
136 return (iteration <= Max_iterations);
154 storage = (
float *) realloc(C[0], dim1 * dim3 *
sizeof(A[0]));
155 *CC = C = (
float **) realloc(C, dim1 *
sizeof(A));
157 storage = (
float *) malloc(dim1 * dim3 *
sizeof(A[0]));
158 *CC = C = (
float **) malloc(dim1 *
sizeof(A));
161 for (i = 0; i < dim1; i++) {
166 for (i = 0; i < dim1; i++) {
167 for (j = 0; j < dim3; j++) {
169 for (k = 0; k < dim2; k++) {
170 sum += A[i][k] * B[k][j];
172 C[i][j] = (float) (sum);
190 storage = (
double *) realloc(C[0], dim1 * dim3 *
sizeof(
double));
191 *CC = C = (
double **) realloc(C, dim1 *
sizeof(
double *));
193 storage = (
double *) malloc(dim1 * dim3 *
sizeof(
double));
194 *CC = C = (
double **) malloc(dim1 *
sizeof(
double *));
197 for (i = 0; i < dim1; i++) {
202 for (i = 0; i < dim1; i++) {
203 for (j = 0; j < dim3; j++) {
205 for (k = 0; k < dim2; k++) {
206 sum += A[i][k] * B[k][j];
215 int dim2,
float ***CC)
229 storage = (
float *) realloc(C[0], dim1 * dim2 *
sizeof(A[0]));
230 *CC = C = (
float **) realloc(C, dim1 *
sizeof(A));
232 storage = (
float *) malloc(dim1 * dim2 *
sizeof(A[0]));
233 *CC = C = (
float **) malloc(dim1 *
sizeof(A));
236 for (i = 0; i < dim1; i++) {
241 for (i = 0; i < dim1; i++) {
245 for (j = 0; j < dim2; j++) {
247 for (k = 0; k < nedges; k++) {
248 sum += ewgts[k] * B[j][edges[k]];
250 C[i][j] = (float) (sum);
258 void cpvec(
double *copy,
int beg,
int end,
double *vec)
264 for (i = end - beg + 1; i; i--) {
270 double dot(
double *vec1,
int beg,
int end,
double *vec2)
278 for (i = end - beg + 1; i; i--) {
279 sum += (*vec1++) * (*vec2++);
286 void scadd(
double *vec1,
int beg,
int end,
double fac,
double *vec2)
292 for (i = end - beg + 1; i; i--) {
293 (*vec1++) += fac * (*vec2++);
304 for (i = end - beg + 1; i; i--) {
305 (*vec1++) = alpha * (*vec2++);
310 double norm(
double *vec,
int beg,
int end)
312 return (sqrt(
dot(vec, beg, end, vec)));
328 for (i = n; i; i--) {
333 for (i = n; i; i--) {
346 for (i = 0; i < n; i++)
347 vec[i] = rand() %
RANGE;
360 for (i = 0; i < n; i++) {
362 for (j = 0; j < matrix[i].
nedges; j++)
363 res += matrix[i].ewgts[j] * vector[matrix[i].edges[j]];
377 for (i = 0; i < n; i++) {
379 for (j = 0; j < n; j++)
380 res += matrix[i][j] * vector[j];
392 for (i = 0; i < n; i++) {
393 result[i] = vector1[i] - vector2[i];
402 for (i = 0; i < n; i++) {
403 result[i] = vector1[i] + vector2[i];
410 vectors_mult_addition(
int n,
double *vector1,
double alpha,
414 for (i = 0; i < n; i++) {
415 vector1[i] = vector1[i] + alpha * vector2[i];
425 for (i = 0; i < n; i++) {
426 result[i] = vector[i] *
alpha;
434 for (i = 0; i < n; i++)
443 for (i = 0; i < n; i++) {
444 result += vector1[i] * vector2[i];
453 double max_val = -1e50;
455 for (i = 0; i < n; i++)
456 if (fabs(vector[i]) > max_val)
457 max_val = fabs(vector[i]);
464 void orthogvec(
int n,
double *vec1,
475 vectors_mult_addition(n, vec1, alpha, vec2);
481 void mat_mult_vec(
vtx_data * L,
int n,
double *vec,
double *result)
489 for (i = 0; i < n; i++) {
493 for (j = 0; j < L[i].
nedges; j++) {
494 sum -= ewgts[j] * vec[edges[j]];
505 double *vector,
double *result)
511 for (i = 0; i < dim1; i++) {
513 for (j = 0; j < dim2; j++)
514 res += matrix[j][i] * vector[j];
523 double *vector,
double *result)
529 for (i = 0; i < dim1; i++) {
531 for (j = 0; j < dim2; j++)
532 res += matrix[i][j] * vector[j];
552 for (i = n; i; i--) {
557 for (i = n; i; i--) {
564 void right_mult_with_vectorf
565 (
vtx_data * matrix,
int n,
float *vector,
float *result) {
569 for (i = 0; i < n; i++) {
571 for (j = 0; j < matrix[i].
nedges; j++)
572 res += matrix[i].ewgts[j] * vector[matrix[i].edges[j]];
578 void right_mult_with_vector_fd
579 (
float **matrix,
int n,
float *vector,
double *result) {
583 for (i = 0; i < n; i++) {
585 for (j = 0; j < n; j++)
586 res += matrix[i][j] * vector[j];
594 (
float *packed_matrix,
int n,
float *vector,
float *result) {
600 for (i = 0; i < n; i++) {
603 for (index = 0, i = 0; i < n; i++) {
605 vector_i = vector[i];
607 res += packed_matrix[index++] * vector_i;
609 for (j = i + 1; j < n; j++, index++) {
610 res += packed_matrix[index] * vector[j];
611 result[j] += packed_matrix[index] * vector_i;
622 for (i = 0; i < n; i++) {
623 result[i] = vector1[i] - vector2[i];
632 for (i = 0; i < n; i++) {
633 result[i] = vector1[i] + vector2[i];
642 for (i = 0; i < n; i++) {
643 vector1[i] = vector1[i] + alpha * vector2[i];
651 for (i = 0; i < n; i++) {
652 result[i] = (float) vector[i] * alpha;
660 for (i = 0; i < n; i++)
669 for (i = 0; i < n; i++) {
670 result += vector1[i] * vector2[i];
680 for (i = 0; i < n; i++)
688 for (i = 0; i < n; i++)
696 float max_val = -1e30f;
697 for (i = 0; i < n; i++)
698 if (fabs(vector[i]) > max_val)
699 max_val = (float) (fabs(vector[i]));
708 for (i = 0; i < n; i++) {
718 for (i = 0; i < n; i++) {
719 if ((v = vec[i]) != 0.0)
729 for (i = 0; i < n; i++) {
742 for (i = 0; i < n; i++) {
743 if ((v = source[i]) >= 0.0) {
746 target[i] = (float) d;
757 for (i = 0; i < n; i++) {
758 if ((v = vec[i]) > 0.0) {
768 void init_vec_orth1f(
int n,
float *vec)
773 for (i = 0; i < n; i++)
774 vec[i] = (
float) (rand() %
RANGE);
783 void mat_mult_vecf(
vtx_data * L,
int n,
float *vec,
float *result)
791 for (i = 0; i < n; i++) {
795 for (j = 0; j < L[i].
nedges; j++) {
796 sum -= ewgts[j] * vec[edges[j]];
void vectors_additionf(int n, float *vector1, float *vector2, float *result)
void invert_sqrt_vec(int n, float *vec)
void sqrt_vec(int n, float *vec)
void orthog1(int n, double *vec)
void vecscale(double *vec1, int beg, int end, double alpha, double *vec2)
void invert_vec(int n, float *vec)
void set_vector_val(int n, double val, double *result)
int power_iteration(double **square_mat, int n, int neigs, double **eigs, double *evals, int initialize)
void copy_vectorf(int n, float *source, float *dest)
void cpvec(double *copy, int beg, int end, double *vec)
void set_vector_valf(int n, float val, float *result)
void vectors_addition(int n, double *vector1, double *vector2, double *result)
void copy_vector(int n, double *source, double *dest)
void mult_dense_mat(double **A, float **B, int dim1, int dim2, int dim3, float ***CC)
void right_mult_with_vector_transpose(double **matrix, int dim1, int dim2, double *vector, double *result)
void square_vec(int n, float *vec)
double max_absf(int n, float *vector)
void vectors_mult_additionf(int n, float *vector1, float alpha, float *vector2)
void right_mult_with_vector_f(float **matrix, int n, double *vector, double *result)
void vectors_scalar_multf(int n, float *vector, float alpha, float *result)
double vectors_inner_productf(int n, float *vector1, float *vector2)
void mult_sparse_dense_mat_transpose(vtx_data *A, double **B, int dim1, int dim2, float ***CC)
void sqrt_vecf(int n, float *source, float *target)
void mult_dense_mat_d(double **A, float **B, int dim1, int dim2, int dim3, double ***CC)
double vectors_inner_product(int n, double *vector1, double *vector2)
void init_vec_orth1(int n, double *vec)
double max_abs(int n, double *vector)
void orthog1f(int n, float *vec)
void vectors_substractionf(int n, float *vector1, float *vector2, float *result)
double norm(double *vec, int beg, int end)
void vectors_subtraction(int n, double *vector1, double *vector2, double *result)
void right_mult_with_vector_ff(float *packed_matrix, int n, float *vector, float *result)
void scadd(double *vec1, int beg, int end, double fac, double *vec2)
void vectors_scalar_mult(int n, double *vector, double alpha, double *result)
void right_mult_with_vector_d(double **matrix, int dim1, int dim2, double *vector, double *result)
void right_mult_with_vector(vtx_data *matrix, int n, double *vector, double *result)