--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+
+
+typedef enum errors{
+ NOT_EQUAL,
+ NOT_MULTIPLIABLE,
+ NOT_SQUARE,
+ ZERO_DET,
+ UNKNOWN
+} error_t;
+
+typedef struct matrix{
+ size_t rows;
+ size_t columns;
+ float **matrix;
+} matrix_t;
+
+
+// Handles all of the different possible errors
+matrix_t matrixError(error_t error);
+
+// Creates a rows x columns matrix;
+matrix_t createMatrix(size_t rows, size_t columns);
+
+// Fills the matrix with values provided by the user
+void initializeMatrix(matrix_t *m);
+
+// Returns true if the matrix is square, false otherwise
+int isSquare(matrix_t m);
+
+// Displays the given matrix
+void displayMatrix(matrix_t m);
+
+// Returns the identity matrix n x n
+matrix_t identityMatrix(size_t n);
+
+// Returns a matrix of the same size as "m" filled with the number "n"
+matrix_t fillN(matrix_t m, float n);
+
+// Adds a number to all the positions of the matrix and returns the result
+matrix_t addN(matrix_t m, float n);
+
+// Substracts a number from all the positions of the matrix and returns the result
+matrix_t substractN(matrix_t m, float n);
+
+// Adds two matrices together and returns the result
+matrix_t addMatrices(matrix_t a, matrix_t b);
+
+// Negates all of the positions of the matrix and returns the result
+matrix_t negateMatrix(matrix_t m);
+
+// Substracts matrix "b" from "a" and returns the result
+matrix_t substractMatrices(matrix_t a, matrix_t b);
+
+// Multiplies all of the positions of the matrix by "n" and returns the result
+matrix_t multiplyByN(matrix_t m, float n);
+
+// Divides all of the positions of the matrix by "n" and returns the result
+matrix_t divideByN(matrix_t m, float n);
+
+// Multiplies matrix "a" by "b" and returns the result
+matrix_t multiplyMatrices(matrix_t a, matrix_t b);
+
+// Divides matrix "a" by "b" and returns the result
+matrix_t divideMatrices(matrix_t a, matrix_t b);
+
+// Raises the matrix to the nth power and returns the result
+matrix_t raiseMatrixToN(matrix_t m, int n);
+
+// Creates a submatrix from "matrix" without the column "column" and row "row" of "matrix"
+matrix_t createSubmatrix(matrix_t m, size_t row, size_t column);
+
+// Returns the determinant of the given (square) matrix
+int determinant(matrix_t m);
+
+// Returns the cofactor matrix of the given matrix
+matrix_t cofactor(matrix_t m);
+
+// Returns the transpose matrix of the given matrix
+matrix_t transpose(matrix_t m);
+
+// Returns the adjugate matrix of the given matrix
+matrix_t adjugate(matrix_t m);
+
+// Returns the inverse matrix of the given matrix
+matrix_t inverse(matrix_t m);
\ No newline at end of file
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include "matrix-operations.h"
+
+
+matrix_t matrices[3];
+
+typedef enum operations{
+ DISPLAY_MATRIX = 1,
+ CREATE_MATRIX, CREATE_IDENTITY_MATRIX,
+
+ IS_SQUARE, NEGATE, CREATE_SUBMATRIX, CALCULATE_DETERMINANT, CALCULATE_COFACTOR,
+ CALCULATE_TRANSPOSE, CALCULATE_ADJUGATE, CALCULATE_INVERSE,
+
+ FILL, ADD_N, SUBSTRACT_N, MULTIPLY_N, DIVIDE_N, RAISE_TO_N,
+
+ ADD_M, SUBSTRACT_M, MULTIPLY_M, DIVIDE_M
+} operation;
+
+int handleInput(){
+ int input = 0, s = 0;
+ do{
+ printf("Select operation\n");
+ printf("\t0. Exit.\n");
+ printf("\t1. Display matrix\n");
+ printf("\t2. Create matrix\n");
+ printf("\t3. Operations with a single matrix\n");
+ printf("\t4. Operations with a matrix and a scalar\n");
+ printf("\t5. Operations between 2 matrices\n");
+ printf("\n");
+ scanf("%d", &input);
+ printf("\n");
+ switch(input){
+ case 0:
+ return 0;
+ break;
+ case 1:
+ printf("Display matrix\n");
+ printf("Which matrix would you like to display?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ displayMatrix(matrices[s]);
+ break;
+ case 2:
+ do{
+ printf("Create matrix\n");
+ printf("\t0. Go back\n");
+ printf("\t1. Create custom matrix\n");
+ printf("\t2. Create identity matrix\n");
+ scanf("%d", &input);
+ switch(input){
+ case 1:
+ return CREATE_MATRIX;
+ break;
+ case 2:
+ return CREATE_IDENTITY_MATRIX;
+ break;
+ }
+ }while(input);
+ break;
+ case 3:
+ do{
+ printf("Operations with a single matrix\n");
+ printf("\t0. Go back\n");
+ printf("\t1. Check if matrix is square\n");
+ printf("\t2. Negate a matrix\n");
+ printf("\t3. Create a submatrix out of a matrix\n");
+ printf("\t4. Calculate the determinant of a matrix\n");
+ printf("\t5. Calculate the cofactor matrix of a matrix\n");
+ printf("\t6. Calculate the transpose matrix of a matrix\n");
+ printf("\t7. Calculate the adjugate matrix of a matrix\n");
+ printf("\t8. Calculate the inverse of a matrix\n");
+ scanf("%d", &input);
+ switch(input){
+ case 1:
+ return IS_SQUARE;
+ break;
+ case 2:
+ return NEGATE;
+ break;
+ case 3:
+ return CREATE_SUBMATRIX;
+ break;
+ case 4:
+ return CALCULATE_DETERMINANT;
+ break;
+ case 5:
+ return CALCULATE_COFACTOR;
+ break;
+ case 6:
+ return CALCULATE_TRANSPOSE;
+ break;
+ case 7:
+ return CALCULATE_ADJUGATE;
+ break;
+ case 8:
+ return CALCULATE_INVERSE;
+ break;
+ }
+ }while(input);
+ break;
+ case 4:
+ do{
+ printf("Operations with a matrix and a scalar\n");
+ printf("\t0. Go back\n");
+ printf("\t1. Fill matrix with a number\n");
+ printf("\t2. Add a number to a matrix\n");
+ printf("\t3. Substract a number from a matrix\n");
+ printf("\t4. Multiply matrix by a number\n");
+ printf("\t5. Divide matrix by a number\n");
+ printf("\t6. Raise matrix to the power of a number\n");
+ scanf("%d", &input);
+ switch(input){
+ case 1:
+ return FILL;
+ break;
+ case 2:
+ return ADD_N;
+ break;
+ case 3:
+ return SUBSTRACT_N;
+ break;
+ case 4:
+ return MULTIPLY_N;
+ break;
+ case 5:
+ return DIVIDE_N;
+ break;
+ case 6:
+ return RAISE_TO_N;
+ break;
+ }
+ }while(input);
+ break;
+ case 5:
+ do{
+ printf("Operations between 2 matrices\n");
+ printf("\t0. Go back\n");
+ printf("\t1. Add two matrices together\n");
+ printf("\t2. Substract one matrix from another\n");
+ printf("\t3. Multiply two matrices\n");
+ printf("\t4. Divide two matrices\n");
+ scanf("%d", &input);
+ switch(input){
+ case 1:
+ return ADD_M;
+ break;
+ case 2:
+ return SUBSTRACT_M;
+ break;
+ case 3:
+ return MULTIPLY_M;
+ break;
+ case 4:
+ return DIVIDE_M;
+ break;
+ }
+ }while(input);
+ break;
+ }
+ printf("\n");
+ if(!input)
+ input = -1;
+ }while(input);
+}
+
+void matrixCalculator(int input){
+ int s = 0, r = 0, c = 0, d = 0, f = 0;
+ float n;
+ switch(input){
+ case CREATE_MATRIX:
+ printf("Creating a new matrix\n");
+ printf("Insert the rows of the matrix: ");
+ scanf("%d", &r);
+ printf("Insert the columns of the matrix: ");
+ scanf("%d", &c);
+ printf("Where do you want to store the newly created matrix?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ matrices[s] = createMatrix(r, c);
+ initializeMatrix(&matrices[s]);
+ break;
+ case CREATE_IDENTITY_MATRIX:
+ printf("Creating a new identity matrix\n");
+ printf("Insert the dimensions of the matrix: ");
+ scanf("%d", &d);
+ printf("Where do you want to store the newly created matrix?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ matrices[s] = identityMatrix(d);
+ break;
+
+ case IS_SQUARE:
+ printf("Which matrix do you want to check if it is square?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("Is matrix %s square? %s\n",
+ (s==0)?"A":(s==1)?"B":"C", (isSquare(matrices[s])?"Yes":"No"));
+ break;
+ case NEGATE:
+ printf("Which matrix do you wanna negate?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ negateMatrix(matrices[s]);
+ break;
+ case CREATE_SUBMATRIX:
+ printf("Out of which matrix would you like to create a submatrix?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ // TODO: not remove any
+ printf("Which row would you like to remove? From 0 to %d\n", matrices[s].rows-1);
+ scanf("%d", &r);
+ printf("Which columns would you like to remove? From 0 to %d\n", matrices[s].columns-1);
+ scanf("%d", &c);
+ matrices[s] = createSubmatrix(matrices[s], r, c);
+ break;
+ case CALCULATE_DETERMINANT:
+ printf("Of which matrix would you like to calculate the determinant of?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("Determinant of matrix %s: %d\n",
+ (s==0)?"A":(s==1)?"B":"C", determinant(matrices[s]));
+ break;
+ case CALCULATE_COFACTOR:
+ printf("Of which matrix would you like to calculate the cofactor matrix of?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("Where would you like to store the resulting cofactor matrix?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &d);
+ }while(d < 0 || d > 2);
+ matrices[d] = cofactor(matrices[s]);
+ break;
+ case CALCULATE_TRANSPOSE:
+ printf("Of which matrix would you like to calculate the transpose matrix of?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("Where would you like to store the resulting transpose matrix?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &d);
+ }while(d < 0 || d > 2);
+ matrices[d] = transpose(matrices[s]);
+ break;
+ case CALCULATE_ADJUGATE:
+ printf("Of which matrix would you like to calculate the adjugate matrix of?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("Where would you like to store the resulting adjugate matrix?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &d);
+ }while(d < 0 || d > 2);
+ matrices[d] = adjugate(matrices[s]);
+ break;
+ case CALCULATE_INVERSE:
+ printf("Of which matrix would you like to calculate the inverse matrix of?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("Where would you like to store the resulting inverse matrix?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &d);
+ }while(d < 0 || d > 2);
+ matrices[d] = inverse(matrices[s]);
+ break;
+
+ case FILL:
+ printf("What matrix would you like to fill with a number?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("With which number would you like to fill matrix %s?\n",
+ (s==0)?"A":(s==1)?"B":"C");
+ scanf("%f", &n);
+ matrices[s] = fillN(matrices[s], n);
+ break;
+ case ADD_N:
+ printf("What matrix would you like to add a number to?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("With which number would you like to add to matrix %s?\n",
+ (s==0)?"A":(s==1)?"B":"C");
+ scanf("%f", &n);
+ matrices[s] = addN(matrices[s], n);
+ break;
+ case SUBSTRACT_N:
+ printf("What matrix would you like to substract a number from?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("With which number would you like to substract from matrix %s?\n",
+ (s==0)?"A":(s==1)?"B":"C");
+ scanf("%f", &n);
+ matrices[s] = substractN(matrices[s], n);
+ break;
+ case MULTIPLY_N:
+ printf("What matrix would you like to multiply by a number?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("With which number would you like to multiply matrix %s by?\n",
+ (s==0)?"A":(s==1)?"B":"C");
+ scanf("%f", &n);
+ matrices[s] = multiplyByN(matrices[s], n);
+ break;
+ case DIVIDE_N:
+ printf("What matrix would you like to fill divide by a number?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("With which number would you like to divide matrix %s by?\n",
+ (s==0)?"A":(s==1)?"B":"C");
+ scanf("%f", &n);
+ matrices[s] = divideByN(matrices[s], n);
+ break;
+ case RAISE_TO_N:
+ printf("What matrix would you like to raise to the power of a number?\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ printf("With which number would you like to raise matrix %s to?\n",
+ (s==0)?"A":(s==1)?"B":"C");
+ scanf("%f", &n);
+ matrices[s] = raiseMatrixToN(matrices[s], n);
+ break;
+
+ case ADD_M:
+ printf("Which two matrices would you like to add up?\n");
+ printf("1st matrix\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &d);
+ }while(d < 0 || d > 2);
+ printf("2nd matrix\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &f);
+ }while(f < 0 || f > 2);
+ printf("In what matrix would you like to store the resulting matrix of adding up matrices %s and %s?\n",
+ (d==0)?"A":(d==1)?"B":"C", (f==0)?"A":(f==1)?"B":"C");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ matrices[s] = addMatrices(matrices[d], matrices[f]);
+ break;
+ case SUBSTRACT_M:
+ printf("Which two matrices would you like to substract?\n");
+ printf("1st matrix\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &d);
+ }while(d < 0 || d > 2);
+ printf("2nd matrix\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &f);
+ }while(f < 0 || f > 2);
+ printf("In what matrix would you like to store the resulting matrix of substracting matrices %s and %s?\n",
+ (d==0)?"A":(d==1)?"B":"C", (f==0)?"A":(f==1)?"B":"C");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ matrices[s] = substractMatrices(matrices[d], matrices[f]);
+ break;
+ case MULTIPLY_M:
+ printf("Which two matrices would you like to multiply?\n");
+ printf("1st matrix\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &d);
+ }while(d < 0 || d > 2);
+ printf("2nd matrix\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &f);
+ }while(f < 0 || f > 2);
+ printf("In what matrix would you like to store the resulting matrix of multiplying matrices %s and %s?\n",
+ (d==0)?"A":(d==1)?"B":"C", (f==0)?"A":(f==1)?"B":"C");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ matrices[s] = multiplyMatrices(matrices[d], matrices[f]);
+ break;
+ case DIVIDE_M:
+ printf("Which two matrices would you like to divide?\n");
+ printf("1st matrix\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &d);
+ }while(d < 0 || d > 2);
+ printf("2nd matrix\n");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &f);
+ }while(f < 0 || f > 2);
+ printf("In what matrix would you like to store the resulting matrix of dividing matrices %s and %s?\n",
+ (d==0)?"A":(d==1)?"B":"C", (f==0)?"A":(f==1)?"B":"C");
+ do{
+ printf("Matrix A (0), B (1) or C (2)? ");
+ scanf("%d", &s);
+ }while(s < 0 || s > 2);
+ matrices[s] = divideMatrices(matrices[d], matrices[f]);
+ break;
+ }
+ printf("Matrix %s:\n", (s==0)?"A":(s==1)?"B":"C");
+ displayMatrix(matrices[s]);
+ printf("\n");
+}
+
+int main(){
+
+ for(int i = 0; i < 3; i++)
+ matrices[i] = createMatrix(1, 1);
+
+ int input = 0;
+ do{
+ input = handleInput();
+ matrixCalculator(input);
+ }while(input);
+
+ return 0;
+}
\ No newline at end of file
--- /dev/null
+#include "matrix-operations.h"
+
+
+matrix_t matrixError(error_t error){
+ switch(error){
+ case NOT_EQUAL:
+ perror("Matrices are not of the same dimensions\n");
+ break;
+ case NOT_MULTIPLIABLE:
+ perror("Matrix A's number of columns is different to matrix B's number of rows\n");
+ break;
+ case NOT_SQUARE:
+ perror("Matrix is not square");
+ break;
+ case ZERO_DET:
+ perror("Determinant of the matrix should be 0\n");
+ break;
+ case UNKNOWN:
+ default:
+ perror("Unknown error\n");
+ }
+ return createMatrix(0, 0);
+}
+// Creates a rows x columns matrix;
+matrix_t createMatrix(size_t rows, size_t columns){
+ matrix_t newmatrix;
+ newmatrix.rows = rows;
+ newmatrix.columns = columns;
+ newmatrix.matrix = malloc(rows*sizeof(float*));
+ for(size_t i = 0; i < rows; ++i)
+ newmatrix.matrix[i] = calloc(columns,sizeof(float));
+ return newmatrix;
+}
+
+// Fills the matrix with values provided by the user
+void initializeMatrix(matrix_t *m){
+ printf("Initializing %dx%d matrix\n", m->rows, m->columns);
+ for(size_t r = 0; r < m->rows; ++r){
+ printf("Please insert %d numbers for each of the columns of the %d%s row\n",
+ m->columns, r+1, (r+1==1)?("st"):((r+1==2)?("nd"):((r+1==3)?("rd"):("th"))));
+ for(size_t c = 0; c < m->columns; ++c){
+ scanf("%f", &m->matrix[r][c]);
+ }
+ }
+}
+
+// Returns true if the matrix is square, false otherwise
+int isSquare(matrix_t m){
+ return m.rows == m.columns;
+}
+
+// Displays the given matrix
+void displayMatrix(matrix_t m){
+ for(size_t r = 0; r < m.rows; ++r){
+ for(size_t c = 0; c < m.columns; ++c)
+ printf("%3.3g ", m.matrix[r][c]);
+ printf("\n");
+ }
+}
+
+// Returns the identity matrix n x n
+matrix_t identityMatrix(size_t n){
+ matrix_t identityMatrix = createMatrix(n, n);
+ for(size_t r = 0; r < n; ++r)
+ for(size_t c = 0; c < n; ++c)
+ if(r == c)
+ identityMatrix.matrix[r][c] = 1;
+ return identityMatrix;
+}
+
+// Returns a matrix of the same size as "m" filled with the number "n"
+matrix_t fillN(matrix_t m, float n){
+ matrix_t filledmatrix = createMatrix(m.rows, m.columns);
+ for(size_t r = 0; r < m.rows; ++r)
+ for(size_t c = 0; c < m.columns; ++c)
+ filledmatrix.matrix[r][c] = n;
+ return filledmatrix;
+}
+
+// Adds a number to all the positions of the matrix and returns the result
+matrix_t addN(matrix_t m, float n){
+ matrix_t addedmatrix = createMatrix(m.rows, m.columns);
+ for(size_t r = 0; r < m.rows; ++r)
+ for(size_t c = 0; c < m.columns; ++c)
+ addedmatrix.matrix[r][c] = m.matrix[r][c]+n;
+ return addedmatrix;
+}
+
+// Substracts a number from all the positions of the matrix and returns the result
+matrix_t substractN(matrix_t m, float n){
+ matrix_t substractedmatrix = createMatrix(m.rows, m.columns);
+ for(size_t r = 0; r < m.rows; ++r)
+ for(size_t c = 0; c < m.columns; ++c)
+ substractedmatrix.matrix[r][c] = m.matrix[r][c]-n;
+ return substractedmatrix;
+}
+
+// Adds two matrices together and returns the result
+matrix_t addMatrices(matrix_t a, matrix_t b){
+ if(a.rows != b.rows || a.columns != b.columns)
+ return matrixError(NOT_EQUAL);
+ matrix_t addedmatrices = createMatrix(a.rows, b.columns);
+ for(size_t r = 0; r < a.rows; ++r)
+ for(size_t c = 0; c < a.columns; ++c)
+ addedmatrices.matrix[r][c] = a.matrix[r][c] + b.matrix[r][c];
+ return addedmatrices;
+}
+
+// Negates all of the positions of the matrix and returns the result
+matrix_t negateMatrix(matrix_t m){
+ matrix_t negatedmatrix = createMatrix(m.rows, m.columns);
+ for(size_t r = 0; r < m.rows; ++r)
+ for(size_t c = 0; c < m.columns; ++c)
+ negatedmatrix.matrix[r][c] = -m.matrix[r][c];
+ return negatedmatrix;
+}
+
+// Substracts matrix "b" from "a" and returns the result
+matrix_t substractMatrices(matrix_t a, matrix_t b){
+ return addMatrices(a, negateMatrix(b));
+}
+
+// Multiplies all of the positions of the matrix by "n" and returns the result
+matrix_t multiplyByN(matrix_t m, float n){
+ matrix_t multipliedmatrix = createMatrix(m.rows, m.columns);
+ for(size_t r = 0; r < m.rows; ++r)
+ for(size_t c = 0; c < m.columns; ++c)
+ multipliedmatrix.matrix[r][c] = m.matrix[r][c]*n;
+ return multipliedmatrix;
+}
+
+// Divides all of the positions of the matrix by "n" and returns the result
+matrix_t divideByN(matrix_t m, float n){
+ return multiplyByN(m, (float)1/n);
+}
+
+// Multiplies matrix "a" by "b" and returns the result
+matrix_t multiplyMatrices(matrix_t a, matrix_t b){
+ if(a.columns != b.rows)
+ return matrixError(NOT_MULTIPLIABLE);
+ matrix_t multipliedmatrices = createMatrix(a.rows, b.columns);
+ for(size_t r = 0; r < multipliedmatrices.rows; ++r)
+ for(size_t c = 0; c < multipliedmatrices.columns; ++c)
+ for(size_t n = 0; n < a.columns; ++n)
+ multipliedmatrices.matrix[r][c] += a.matrix[r][n]*b.matrix[n][c];
+ return multipliedmatrices;
+}
+
+// Divides matrix "a" by "b" and returns the result
+matrix_t divideMatrices(matrix_t a, matrix_t b){
+ return multiplyMatrices(a, inverse(b));
+}
+
+// Raises the matrix to the nth power and returns the result
+matrix_t raiseMatrixToN(matrix_t m, int n){
+ if(!isSquare(m))
+ return matrixError(NOT_SQUARE);
+ // TODO: only accepts positive values of n
+ matrix_t raisedmatrix = identityMatrix(m.rows);
+ for(int i = 0; i < n; ++i)
+ raisedmatrix = multiplyMatrices(raisedmatrix, m);
+ return raisedmatrix;
+}
+
+// Creates a submatrix from "matrix" without the column "column" and row "row" of "matrix"
+matrix_t createSubmatrix(matrix_t m, size_t row, size_t column){
+ int mod = m.rows, nmod = mod-1, sr = 0, sc = 0;
+ matrix_t submatrix = createMatrix(nmod, nmod);
+ for(size_t r = 0; r < mod; ++r){
+ sc = 0;
+ if(r != row){
+ for(size_t c = 0; c < mod; ++c){
+ if(c != column){
+ submatrix.matrix[sr][sc] = m.matrix[r][c];
+ ++sc;
+ }
+ }
+ ++sr;
+ }
+ }
+ return submatrix;
+}
+
+// Returns the determinant of the given (square) matrix
+int determinant(matrix_t m){
+ if(!isSquare(m)){
+ matrixError(NOT_SQUARE);
+ return (int)NULL;
+ }
+ int det = 0;
+ if(m.rows == 1)
+ return m.matrix[0][0];
+ else{
+ int count = 0, nmod = m.rows-1;
+ for(size_t r = 0; r < m.rows; ++r){
+ matrix_t submatrix = createSubmatrix(m, 0, r);
+ int subdeterminant = determinant(submatrix);
+ int step = m.matrix[0][r]*subdeterminant;
+ if((count++)%2 == 0)
+ det += step;
+ else
+ det -= step;
+ }
+ return det;
+ }
+}
+
+// Returns the cofactor matrix of the given matrix
+matrix_t cofactor(matrix_t m){
+ if(!isSquare(m))
+ return matrixError(NOT_SQUARE);
+ matrix_t cofactormatrix = createMatrix(m.rows, m.columns);
+ for(size_t r = 0; r < m.rows; ++r){
+ for(size_t c = 0; c < m.columns; ++c){
+ matrix_t submatrix = createSubmatrix(m, r, c);
+ int d = determinant(submatrix);
+ cofactormatrix.matrix[r][c] = (r+c)%2?-d:d;
+ }
+ }
+ return cofactormatrix;
+}
+
+// Returns the transpose matrix of the given matrix
+matrix_t transpose(matrix_t m){
+ matrix_t transposematrix = createMatrix(m.columns, m.rows);
+ for(size_t r = 0; r < m.columns; ++r)
+ for(size_t c = 0; c < m.rows; ++c)
+ transposematrix.matrix[r][c] = m.matrix[c][r];
+ return transposematrix;
+}
+
+// Returns the adjugate matrix of the given matrix
+matrix_t adjugate(matrix_t m){
+ return transpose(cofactor(m));
+}
+
+// Returns the inverse matrix of the given matrix
+matrix_t inverse(matrix_t m){
+ int det = determinant(m);
+ if(det == 0)
+ return matrixError(ZERO_DET);
+ return multiplyByN(adjugate(m), (float)1/det);
+}
\ No newline at end of file