Beispiel #1
0
//scalar products of vectors
static void vector_scalar_product_TwoVectorsWithCoordinatesFourAndFive_IntegerFourtyOne(void ** state){
    vector_t * vec1 = vector_test(4,5);
    vector_t * vec2 = vector_test(4,5);
    assert_int_equal(vector_scalar_product(vec1,vec2),41);
    vector_free(vec1);
    vector_findLength(vec2);

}
Beispiel #2
0
/*solution saved in b */
void conjugate_gradient(sparse_matrix A, double *b, int size) {
	double *x, *r, *p, *Ap, *aux, rnew, rold, alfa;
	int i;

    x = (double*) malloc(size*sizeof(double));
    r = (double*) malloc(size*sizeof(double));
    p = (double*) malloc(size*sizeof(double));
    Ap = (double*) malloc(size*sizeof(double));
    aux = (double*) malloc(size*sizeof(double));

	for (i = 0; i < size; i ++) {
		x[i] = 0;
		r[i] = b[i];
		p[i] = b[i];
	}

	rold = inner_product(r, r, size);
    /* result of operations from all void functions used here are stored in the last argument */
	while (1) {
		matrix_vector_product(A, p, Ap);
		alfa = rold / inner_product(p, Ap, size); /*step length*/
		vector_scalar_product(p, alfa, size, aux); 
		vector_sum(x, aux, size, x);
		vector_scalar_product(Ap, alfa, size, aux);
		vector_subtraction(r, aux, size, r);
		rnew = inner_product(r, r, size);
		if (sqrt(rnew) < E)
                        break;
                vector_scalar_product(p, rnew / rold, size, p);
                vector_sum(p, r, size, p);
		rold = rnew;
	}

	for (i = 0; i < size; i ++) {
		b[i] = x[i];
	}

	free(x);
	free(r);
	free(p);
	free(Ap);
	free(aux);
}