Previous :
"Working with vectors"
Next
"Example : Matrix-Vector Operations"
Tutorial
Working with matrices
Download the source code and compile it - in the case of an installation in the own home directory - e.g. by :
gcc test1.c -O2 -march=athlon-xp -I $HOME/local/include -I $HOME/include/UMFPACK -L $HOME/local/lib $HOME/local/lib/libmeml.a -lm -llapack /usr/lib/libf2c.a -lumfpack -lamd -lcblas -lf77blas -latlas -o test1
/*
This is a small example how to do
matrix manipulation and matrix operations with meml
more functions are documented under
http://www.meml.smial.de/doc/group__meml__matrix__manipulation.html
http://www.meml.smial.de/doc/group__meml__matrix__operatoren.html
*/
# include "meml.h"
# define SIZE 10000
int main (void)
{
int i;
M_TYPE A_type;
MEML_INT A_row, A_col ;
ME_MATRIX * DenseMatrix;
ME_MATRIX * ListSparse;
ME_MATRIX * Compressed;
ME_MATRIX * A;
MEML_FLOAT data[] ={1,3,7 , -1,0,1 , 0, 0 ,5 , 0,1,0};
/* creating a matrix from an array of doubles */
DenseMatrix=meml_matrix_new_form_carray(ND,4,3,data);
/* creating an empty sparse matrix */
ListSparse=meml_matrix_new(LS,SIZE,SIZE);
/* setting up a tridiagonal matrix*/
meml_matrix_element_set(ListSparse,0,0,42);
meml_matrix_element_set(ListSparse,0,1,42);
meml_matrix_element_set(ListSparse,0,1,42);
meml_matrix_element_set(ListSparse,SIZE-1,SIZE-1,42);
for (i=0;i<SIZE-1;i++)
{
meml_matrix_element_set(ListSparse,i,i,42);
meml_matrix_element_set(ListSparse,i,i+1,-1);
meml_matrix_element_set(ListSparse,i+1,i,1);
}
/* printing a matrix on the screen */
printf("ListSparse = \n");
meml_matrix_print(ListSparse);
printf("DenseMatrix = \n");
meml_matrix_print(DenseMatrix);
/* scaling a matrix */
meml_matrix_scaling_f(0.5,DenseMatrix);
printf("DenseMatrix (scaled) = \n");
meml_matrix_print(DenseMatrix);
/* rounding towards plus infinity */
meml_matrix_ceil (DenseMatrix);
printf("DenseMatrix (after ceil) = \n");
meml_matrix_print(DenseMatrix);
/* converting a matrix to a new type ... */
Compressed = meml_matrix_convert(CS,ListSparse);
/* ... and scaling it */
meml_matrix_scaling_f(1.0/42.0,Compressed);
/* adding two matrices */
A=meml_matrix_add(Compressed,ListSparse);
/* in most cases it is faster not to mix
the different matrix formats but it works */
/* now let's have a look what we get */
meml_matrix_property_get (A, &A_col, &A_row, &A_type);
/* the resulting matrix format of meml_matrix_add is not defined
but it will be sparse matrix type and in this case
it should be a list based
sparse matrix type
*/
if (A_type == LS)
printf("List Sparse\n");
else if (A_type == CS)
printf("Compressed Sparse\n");
else if (A_type == ND)
printf("Dense Matrix\n");
/* finally let's change some more
single values in a matrix */
meml_matrix_element_add(DenseMatrix,0,0,28);
meml_matrix_element_mul(DenseMatrix,2,2,13);
printf("DenseMatrix (at last) = \n");
meml_matrix_print(DenseMatrix);
meml_matrix_free(A);
meml_matrix_free(DenseMatrix);
meml_matrix_free(ListSparse);
meml_matrix_free(Compressed);
exit(EXIT_SUCCESS);
}
Previous :
"Working with vectors"
Next
"Example : Matrix-Vector Operations"