Matrices in the C language. This project was made with the aim of creating a library where there will be simple operations for working with matrices.
- matrix
- creating a matrix
- removing the matrix
- comparison
- matrix addition
- matrix subtraction
- Multiplying a matrix by a number
- two-matrix multiplication
- Matrix transposition
- matrix of algebraic additions
- determinant
- inverse matrix
- library
matrix as a data type
The matrix is pressed as a structure - matrix_t
typedef struct matrix_struct {
double **matrix;
int rows;
int columns;
} matrix_t;
Example of creating a matrix as a data type:
int main() {
matrix_t my_matrix;
}
Many functions accept a result matrix. The result matrix should be empty.
To create a matrix, you need to call the function MatrixCreate()
and pass three arguments there - the required number of rows, columns, the data type itself (its address). The function will return 0 if everything is completed successfully.
matrix_t my_matrix;
int fail = MatrixCreate(4, 4, &my_matrix);
To delete a matrix, call the function MatrixRemove()
.
The function takes on one parameter - matrix_t
If memory is not allocated, the data type is protected from deletion if it contains NULL; In case its contents were changed incorrectly, outside of these functions, make an error while the program is running.
int main() {
matrix_t my_matrix;
int fail = MatrixCreate(4, 4, &my_matrix);
MatrixRemove(&my_matrix);
}
Comparison of two matrices.
To compare two matrices, you need to call the function MatrixEq()
and pass two matrices to it as two arguments.
#define SUCCESS 1
#define FAILURE 0
int succes = (MatrixEq(&my_matrix, &other_matrix) == 1) ? SUCCESS : FAILURE;
The function will return 1 if the matrices are equal, otherwise 0;
To add two matrices, use the function MatrixSum()
.
Pass three arguments to it, the last of which will be the result of addition. The function will return one if there is an error.
matrix_t result;
int fail = MatrixSum(&matrix_one, &matrix_two, &result);
To subtract two matrices, use the function MatrixSub()
.
Pass three arguments to it, the last of which will be the result of subtraction. The function will return one if there is an error.
matrix_t result;
int fail = MatrixSub(&matrix_one, &matrix_two, &result);
To multiply a matrix by a number, use the function MatrixMultNum()
. The function takes three parameters, the first is a matrix, the second is a number, and the third is the returned result.
The function will return a number other than 0 - if there is an error (or it will be impossible to multiply two matrices).
double other_num = 5.3;
matrix_t result;
int fail = MatrixMultNum(&my_matrix, other_num, &result);
To multiply two matrices, use the function MatrixMultMatrix()
. The function takes three parameters, all matrices the last one is the result matrix. The function returns 0 if everything was successful.
matrix_t result;
int fail = MatrixMultNum(&my_matrix, &other_matrix, &result);
To get the transposed matrix in the function MatrixTranspose()
pass two arguments, the first is your matrix. The second is the result matrix.
matrix_t result;
int fail = MatrixTranspose(&my_matrix, &result);
To get the matrix of algebraic complements, use the function ``MatrixCalcComplements()`'. The first argument of the function is your matrix. The second argument of the function is the result matrix.
matrix_t result;
int fail = MatrixCalcComplements(&my_matrix, &result);
I used recursion to find
In order to find the determinant of the matrix (determinant), use the function MatrixDet()
. The first argument of the function is the matrix, the second argument of the function is the result of the type double
double result;
int fail = MatrixDet(&my_matrix, &result);
To find the inverse matrix, use MatrixInverse()
. The first argument of the function is the matrix, the second argument of the function is the matrix for the result.
Returns a number other than 0 in case of a wide variety of errors.
matrix_t result;
int fail = MatrixInverse(&my_matrix, &result)
I use a makefile to build projects. To build the library, use the makefile matrix.a target. He will put it in the lib directory.
jokerooo7@pc macOS % make matrix.a