Skip to content
Snippets Groups Projects
Commit db7fd411 authored by Lucas Christesen Ahler's avatar Lucas Christesen Ahler
Browse files

gsl-matrix exercise

parent 9b1b8a88
No related branches found
No related tags found
No related merge requests found
CFLAGS = -Wall -O1 -std=gnu11
CFLAGS += $(shell /usr/bin/gsl-config --cflags)
LDLIBS += $(shell /usr/bin/gsl-config --libs)
default: main
./$< > out.txt
.PHONEY: clean
clean:
$(RM) main out.txt
\ No newline at end of file
#include<stdio.h>
#include<gsl/gsl_vector.h>
#include<gsl/gsl_matrix.h>
#include<gsl/gsl_blas.h>
#include<gsl/gsl_linalg.h>
#define RND (double)rand()/RAND_MAX
void print_vector(char s[], gsl_vector* v) {
printf("%s\n",s);
for(int i=0;i<v->size;i++) {
printf("%15g",gsl_vector_get(v,i));
printf("\n");
}
}
int main(){
int n =3;
gsl_matrix* A = gsl_matrix_alloc(n,n);
gsl_matrix* Acopy = gsl_matrix_alloc(n,n);
gsl_vector* x = gsl_vector_alloc(n);
gsl_vector* b = gsl_vector_alloc(n);
gsl_vector* y = gsl_vector_calloc(n);
gsl_matrix_set(A,0,0,6.13);
gsl_matrix_set(A,0,1,8.08);
gsl_matrix_set(A,0,2,-4.36);
gsl_matrix_set(A,1,0,-2.90);
gsl_matrix_set(A,1,1,-6.31);
gsl_matrix_set(A,1,2,1.00);
gsl_matrix_set(A,2,0,5.86);
gsl_matrix_set(A,2,1,-3.89);
gsl_matrix_set(A,2,2,0.19);
gsl_matrix_memcpy(Acopy,A);
gsl_vector_set(b,0,6.23);
gsl_vector_set(b,1,5.37);
gsl_vector_set(b,2,2.29);
gsl_linalg_HH_solve(Acopy,b,x);
gsl_blas_dgemv(CblasNoTrans,1,A,x,0,y);
print_vector("Right hand side is:",b);
print_vector("Check A*x should be equal b:",y);
gsl_matrix_free(A);
gsl_matrix_free(Acopy);
gsl_vector_free(x);
gsl_vector_free(b);
gsl_vector_free(y);
return 0;
}
\ No newline at end of file
Right hand side is:
6.23
5.37
2.29
Check A*x should be equal b:
6.23
5.37
2.29
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment