matrix_t create_matrix(int rows, int columns);The matrix_type field must be initialized with the ZERO_MATRIX value.
void remove_matrix(matrix_t *A);#define SUCCESS 1
#define FAILURE 0
int eq_matrix(matrix_t *A, matrix_t *B);The matrices A, B are equal |A = B| if they have the same dimensions and the corresponding elements are identical, thus for all i and j: A(i,j) = B(i,j)
The comparison must be up to and including 7 decimal places.
matrix_t sum_matrix(matrix_t *A, matrix_t *B);
matrix_t sub_matrix(matrix_t *A, matrix_t *B);The sum of two matrices A = m × n and B = m × n of the same size is a matrix C = m × n = A + B of the same size whose elements are defined by the equations C(i,j) = A(i,j) + B(i,j).
The difference of two matrices A = m × n and B = m × n of the same size is a matrix C = m × n = A - B of the same size whose elements are defined by the equations C(i,j) = A(i,j) - B(i,j).
1 2 3 1 0 0 2 2 3
С = A + B = 0 4 5 + 2 0 0 = 2 4 5
0 0 6 3 4 1 3 4 7
matrix_t mult_number(matrix_t *A, double number);
matrix_t mult_matrix(matrix_t *A, matrix_t *B);The product of the matrix A = m × n by the number λ is the matrix B = m × n = λ × A whose elements are defined by the equations B = λ × A(i,j).
1 2 3 2 4 6
B = 2 × A = 2 × 0 4 2 = 0 8 4
2 3 4 4 6 8
The product of A = m × k by B = k × n is a matrix C = m × n = A × B of size m × n whose elements are defined by the equation C(i,j) = A(i,1) × B(1,j) + A(i,2) × B(2,j) + ... + A(i,k) × B(k,j).
1 4 1 -1 1 9 11 17
C = A × B = 2 5 × 2 3 4 = 12 13 22
3 6 15 15 27
The components of matrix C are calculated as follows:
C(1,1) = A(1,1) × B(1,1) + A(1,2) × B(2,1) = 1 × 1 + 4 × 2 = 1 + 8 = 9
C(1,2) = A(1,1) × B(1,2) + A(1,2) × B(2,2) = 1 × (-1) + 4 × 3 = (-1) + 12 = 11
C(1,3) = A(1,1) × B(1,3) + A(1,2) × B(2,3) = 1 × 1 + 4 × 4 = 1 + 16 = 17
C(2,1) = A(2,1) × B(1,1) + A(2,2) × B(2,1) = 2 × 1 + 5 × 2 = 2 + 10 = 12
C(2,2) = A(2,1) × B(1,2) + A(2,2) × B(2,2) = 2 × (-1) + 5 × 3 = (-2) + 15 = 13
C(2,3) = A(2,1) × B(1,3) + A(2,2) × B(2,3) = 2 × 1 + 5 × 4 = 2 + 20 = 22
C(3,1) = A(3,1) × B(1,1) + A(3,2) × B(2,1) = 3 × 1 + 6 × 2 = 3 + 12 = 15
C(3,2) = A(3,1) × B(1,2) + A(3,2) × B(2,2) = 3 × (-1) + 6 × 3 = (-3) + 18 = 15
C(3,3) = A(3,1) × B(1,3) + A(3,2) × B(2,3) = 3 × 1 + 6 × 4 = 3 + 24 = 27
matrix_t transpose(matrix_t *A);The transpose of matrix A is in switching its rows with its columns with their numbers retained
1 4 1 2 3
A = A^T = 2 5 = 4 5 6
3 6
matrix_t calc_complements(matrix_t *A);Minor M(i,j) is a (n-1)-order determinant obtained by deleting out the i-th row and the j-th column from the matrix A.
For the following matrix:
1 2 3
A = 0 4 2
5 2 1
The minor of the first element of the first row is:
M(1,1) = 4 2
2 1
|M| = 4 - 4 = 0
The minors of matrix will look like this:
0 -10 -20
M = -4 -14 -8
-8 2 4
The algebraic complement of a matrix element is the value of the minor multiplied by -1^(i+j).
The matrix of algebraic complement will look like this:
0 10 -20
M. = 4 -14 8
-8 -2 4
double determinant(matrix_t *A);The determinant is a number that is associated to each square matrix and calculated from the elements using special formulas.
Tip: The determinant can only be calculated for a square matrix.
The determinant of a matrix equals the sum of the products of elements of the row (column) and the corresponding algebraic complements.
Finding the determinant of matrix A by the first row:
1 2 3
A = 4 5 6
7 8 9
|A| = 1 × 5 6 - 2 × 4 6 + 3 × 4 5 = 1 × (5 × 9 - 8 × 6) - 2 × (4 × 9 - 6 × 7) + 3 × (4 × 8 - 7 × 5)
8 9 7 9 7 8
|A| = 1 × (45 - 48) - 2 × (36 - 42) + 3 × (32 - 35) = -3 + 12 + (-9) = 0
|A| = 0
If it is impossible to calculate the determinant of the given matrix, the function must return the value NAN.
matrix_t inverse_matrix(matrix_t *A);A matrix A to the power of -1 is called the inverse of a square matrix A if the product of these matrices equals the identity matrix.
If the determinant of the matrix is zero, then it does not have an inverse.
The formula to calculate the inverse of matrix is
The following matrix is given:
2 5 7
A = 6 3 4
5 -2 -3
Finding the determinant:
|A| = -1
Determinant |A| != 0 -> matrix has an inverse.
Construction of minor matrix:
-1 -38 -27
М = -1 -41 -29
-1 -34 -24
The matrix of algebraic complements:
-1 38 -27
М. = 1 -41 29
-1 34 -24
The transpose of matrix of algebraic complements:
-1 1 -1
М^T. = 38 -41 34
-27 29 -24
typedef enum {
CORRECT_MATRIX = 0,
INCORRECT_MATRIX = 1,
IDENTITY_MATRIX = 2,
ZERO_MATRIX = 3
} matrix_type_t;
typedef struct matrix_struct {
double** matrix;
int rows;
int columns;
matrix_type_t matrix_type;
} matrix_t;The matrix_type field defines the matrix type
CORRECT_MATRIX - the correct matrix
INCORRECT_MATRIX - in case of errors in actions with matrices
IDENTITY_MATRIX - a identity matrix
ZERO_MATRIX - a null matrix
The filling of the matrix in the case of the INCORRECT_MATRIX type is not defined.
The basic actions with matrices are implemented: create_matrix(creation), remove_matrix(cleaning and destruction), eq_matrix(comparison), sum_matrix(addition), sub_matrix(subtraction), mult_matrix(multiplication), mult_number(multiplication by number), transpose(transpose), determinant(calculation of determinant), calc_complements(calculation of matrix of algebraic complements), inverse_matrix(finding inverse of the matrix).
- The library is developed in the C language of the C11 standard using the gcc compiler
- Outdated and disused language constructs and library functions are not used. Attention is drawn to the marks legacy and obsolete in the official documentation on the language and the libraries used. During the development, a reference point was taken to the standard: POSIX.1-2017
- The solution is designed as a static library
- The library is designed in accordance with the principles of structured programming
- The matrix is implemented in the form of the structure described above
- The verified accuracy of the fractional part is a maximum of 6 decimal places