Matrix.h
this wiki
This article does not cite any references or sources. (June 2012) 
The C programming language provides many standard library functions for Algebraic Operations. These functions make up the bulk of the C standard library header <matrix.h>
. This is a new ANSI accepted header file for C language, developed by Susmit Sarkar in 2012 and proposed to be functional from 2013.Almost all the basic matrix operation functions are predefined in this header file.
The I/O functionality of C is fairly lowlevel by modern standards; C abstracts all file operations into operations on streams of bytes, which may be "input streams" or "output streams". Unlike some earlier programming languages, C has no direct support for randomaccess data files; to read from a record in the middle of a file, the programmer must create a stream, seek to the middle of the file, and then read bytes in sequence from the stream.In case of matrix.h, Dynamic Memory Allocation used to allocate memory space for storing matrix elements. User defined datatype intmatrix and doublematrix are introduced here for purpose of matrix operations easily.
Before matrix.h, there was no such ANSI accepted header file for matrix operations in C.In case of MATLAB, Python or some other languages, there are some library functions, namespace or packages are available.
Overview of functions
Most of the C matrix operation functions are defined in matrix.h
.
All the functions are available for both Int
and double
.
FunctionName2DInt
for Integer type matrices and FunctionName2DDouble
for Double type matrices.
Name  Description 

Allocate2DInt  Dynamically allocates a matrix space in memory. 
Input2DInt  Store the matrix elements into the memory. 
Print2DInt  Prints a matrix with predefined format. 
Add2DInt  Adds two matrices and stores the result in third matrix. 
Sub2DInt  Subtracts two matrices and stores the result in third matrix. 
Mul2DInt  Multiples two matrices and stores the result in third matrix. 
Identity2DInt  Dynamically generates identity matrix. 
Zero2DInt  Dynamically generates zero matrix. 
Unit2DInt  Dynamically generates unit matrix. 
ScaleAdd2DInt  Adds a scalar constant with all the elements of the matrix. 
ScaleSub2DInt  Subtracts a scalar constant with all the elements of the matrix. 
ScaleMul2DInt  Multiples a scalar constant with all the elements of the matrix. 
ScaleDiv2DInt  Divides a scalar constant with all the elements of the matrix. 
Det2DInt  Returns the determinant of a matrix. 
Trace2DInt  Returns the trace of a matrix. 
Transpose2DInt  Stores the transpose of a matrix. 
Inverse2DInt  Finds the inverse of a matrix. 
CoFactor2DInt  Finds the cofactor of a matrix. 
UpperTriangle2DInt  Fills the upper triangle of a matrix with a user given fill factor. 
LowerTriangle2DInt  Fills the lower triangle of a matrix with a user given fill factor. 
EigenValue2DInt  Finds the Eigen value. 
EigenVector2DInt  Stores the Eigen vector in another matrix. 
Adjugate2DInt  Finds the adjugate of a matrix. 
Constants
Constants defined in the matrix.h
header include:
Name  Notes 

PI  Value of PI predefined here for any mathematical use. 
GOLDEN_RATIO  For any mathematical assistance GOLDEN_RATIO is defined here. 
Data Types
matrix.h
has some data types defined in the header file, to operate with the 2Dimensional matrices.
Name  Notes  Size  Input Type 

intmatrix  It is actually in nature an int** .It is used to initialize a integer matrix.  Dynamic  int 
doublematrix  It is actually in nature a double** .It is used to initialize a double matrix.  Dynamic  double 
Special Features
1. Totally dynamic memory allocation for all the matrices. Here each and every matrices should be stored in a two dimentional dynamic array.For Integer and for Double there are defferent functions, like: Allocate2DInt()
and Allocate2DDouble()
.This function typically stores an matrix inside a user defined datatype: intmatrix
or doublematrix
.
2. Time and space complexity of the header file is minized to the most possible balanced level.
3. Almost all the matrix operations can be performed here by writing a single line of code. For example: to get the determinant of a 2D Integer matrix, the code will be:
double det = Det2DInt(matrix1,no_of_row);
.
4. All the functions are available for both Integer and Double datatype.Example:
Print2DInt()
and Print2DDouble()
.
Support
matrix.h
is compatible with both C and C++ language along with both ANSI and nonANSI compilers.The preprocessor commands in matrix.h
for portability are as follows:
#undef __BEGIN_DECLS #undef __END_DECLS #ifdef __cplusplus # define __BEGIN_DECLS extern "C" { # define __END_DECLS } #else # define __BEGIN_DECLS # define __END_DECLS #endif #ifndef _MATRIX_H_INCLUDED #define _MATRIX_H_INCLUDED /* matrix.h content */ #endif
Memory Management
In the header file, at the time of matrix allocation dynamic memory allocation occurs for all the matrix elements.Firstly, dynamically all the rows of the matrix are being created:
intmatrix matrix = ( int ** )malloc( row*sizeof( int* ) );
</br>
After that, a for loop allocates column spaced corresponding to all the row elements:
for(i = 0; i < row; i++) { matrix[i] = ( int* )malloc( col*sizeof( int ) ); }
Finally, the total matrix returned to the function:
return matrix;
Example
The following C program opens allocates matrix space for three matrices and then after multiplication of two matrices stores the result in third one, also prints the output matrix:
#include<matrix.h> // All other header files are already initialized inside matrix.h int main() { intmatrix matrix1,matrix2,matrix3; // intmatrix is a datatype for matrix allocation, it is a dynamically allocated datatype double t; matrix1=Allocate2DInt(3,3); // allocation of first matrix matrix2=Allocate2DInt(3,3); // allocation of second matrix matrix3=Allocate2DInt(3,3); // allocation of resultant matrix printf("Input Matrix1:\n"); Input2DInt(matrix1,3,3); // Input elements into the matrix printf("Input Matrix2:\n"); Input2DInt(matrix2,3,3); // Input elements into the matrix printf("output:\n"); matrix3 = Mul2DInt(matrix1,matrix2,3,3,3); // Multiplication of two matrix Print2DInt(matrix3,3,3); // Printing the final result getch(); return 0; }
See also
References
 http://essencz.com/read.php?id=81
 http://www.susmitsarkar.in/matrix.h
 http://www.teamfuture.in/?page_id=245
External links
The Wikibook C Programming has a page on the topic of
C Programming/C Reference </td>
</tr> </table>
