Thursday, November 25, 2010

Simple Matrix Operations for the C Language

matrix is a simple C library for basic matrix operations. Supported operations are:
  • Creating matrices
  • Summation
  • Subtraction
  • Multiplication
  • Multiplication with a scaler
  • Inverse
  • Determinant
  • Echelon Form
  • Submatrix extraction
  • Saving and loading matrices

This is an open source project under the GPL. That means you can use and change it for any purpose but you have to make the source codes public.
The header file and an example are given below. Also you can download the source code by clicking here.

An example is given below:

/*================================================================
  matrix, a simple library for matrix operations.
  Copyright (C) 2010-2011 by Mehmet Hakan Satman.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2
  as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 
 USA

  The author may be reached at mhsatman@yahoo.com.
 *============================================================*/

#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"

int main()
{
    int n=10;
    int m=10;
    /*
    Creating a matrix with rows n, columns m
    */
    Matrix *m1=Matrix_create(n,m);
    Matrix *m2;
    /*
    Randomizing the matrix.
    */
    int i,j;
    double val=1;
    for (i=0;i<n;i++){
        for(j=0;j<m;j++){
            val=((double)random()/RAND_MAX)*1;
            if(((double)random()/RAND_MAX)<0.5) val*=-1;
            Matrix_set(m1,i,j,val);
        }
    }

    /*
    Dumping the content of the matrix
    */
    Matrix_dump(m1);

    /*
    Getting inverse of the matrix.
    */
    m1=Matrix_inverse(m1);

    /*
    Getting the second column of the matrix
    */
    m2=Matrix_getcol(m1,1);

    /*
    Saving matrices
    */
    Matrix_save(m1,"matrix1.dat");
    Matrix_save(m2,"matrix2.dat");

    /*
    Reloading matrices
    */
    m1=Matrix_load("matrix1.dat");
    m2=Matrix_load("matrix2.dat");

    /*
    Calculating determinant
    */
    printf("Determinant of m1 is %f\n", Matrix_determinant(m1));

    //this will return nan, becase m2 is not a square matrix
    printf("Determinant of m2 is %f\n", Matrix_determinant(m2));

    /*
    Free the memory
    */
    Matrix_delete(m1);
    Matrix_delete(m2);

    printf("OK\n");

    return(0);
}

The main page of this library is http://www.mhsatman.com/

3 comments:

  1. Hey, Do you have the matrix.zip??, the link you shared below is broken.

    If you do have the code, I appreciate if you could send it to dslemusp@gmail.com

    Thanks

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. The link is changed to

    http://mhsatman.com/old/cmatrix.php

    ReplyDelete

Thanks