]> git.xolatile.top Git - soikk-matrix-calculator.git/commitdiff
Bug fixes, UI fixes main
authorSoikk <76824648+Soikk@users.noreply.github.com>
Sun, 24 Oct 2021 22:52:13 +0000 (00:52 +0200)
committerSoikk <76824648+Soikk@users.noreply.github.com>
Sun, 24 Oct 2021 22:52:13 +0000 (00:52 +0200)
You can now raise a matrix to a negative power and store the resulting matrixes from an operation with a scalar in another matrix

matrix-calculator.c
matrix-operations.c

index a9cebd8acf34064b6c8175a4d01001c268005c17..06b90f69b03aff0020a6f9646d624ef2eb65c756 100644 (file)
@@ -290,69 +290,99 @@ void matrixCalculator(int input){
 
                case FILL:
                        printf("What matrix would you like to fill with a number?\n");
 
                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", &d);
+                       }while(d < 0 || d > 2);
+                       printf("Where would you like to store the resulting matrix?\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",
                        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");
+                               (d==0)?"A":(d==1)?"B":"C");
                        scanf("%f", &n);
                        scanf("%f", &n);
-                       matrices[s] = fillN(matrices[s], n);
+                       matrices[s] = fillN(matrices[d], n);
                        break;
                case ADD_N:
                        printf("What matrix would you like to add a number to?\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", &d);
+                       }while(d < 0 || d > 2);
+                       printf("Where would you like to store the resulting matrix?\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",
                        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");
+                               (d==0)?"A":(d==1)?"B":"C");
                        scanf("%f", &n);
                        scanf("%f", &n);
-                       matrices[s] = addN(matrices[s], n);
+                       matrices[s] = addN(matrices[d], n);
                        break;
                case SUBSTRACT_N:
                        printf("What matrix would you like to substract a number from?\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", &d);
+                       }while(d < 0 || d > 2);
+                       printf("Where would you like to store the resulting matrix?\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",
                        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");
+                               (d==0)?"A":(d==1)?"B":"C");
                        scanf("%f", &n);
                        scanf("%f", &n);
-                       matrices[s] = substractN(matrices[s], n);
+                       matrices[s] = substractN(matrices[d], n);
                        break;
                case MULTIPLY_N:
                        printf("What matrix would you like to multiply by a number?\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", &d);
+                       }while(d < 0 || d > 2);
+                       printf("Where would you like to store the resulting matrix?\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",
                        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");
+                               (d==0)?"A":(d==1)?"B":"C");
                        scanf("%f", &n);
                        scanf("%f", &n);
-                       matrices[s] = multiplyByN(matrices[s], n);
+                       matrices[s] = multiplyByN(matrices[d], n);
                        break;
                case DIVIDE_N:
                        break;
                case DIVIDE_N:
-                       printf("What matrix would you like to fill divide by a number?\n");
+                       printf("What matrix would you like to fill with a number?\n");
+                       do{
+                               printf("Matrix A (0), B (1) or C (2)? ");
+                               scanf("%d", &d);
+                       }while(d < 0 || d > 2);
+                       printf("Where would you like to store the resulting matrix?\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",
                        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");
+                               (d==0)?"A":(d==1)?"B":"C");
                        scanf("%f", &n);
                        scanf("%f", &n);
-                       matrices[s] = divideByN(matrices[s], n);
+                       matrices[s] = divideByN(matrices[d], n);
                        break;
                case RAISE_TO_N:
                        printf("What matrix would you like to raise to the power of a number?\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", &d);
+                       }while(d < 0 || d > 2);
+                       printf("Where would you like to store the resulting matrix?\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",
                        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");
+                               (d==0)?"A":(d==1)?"B":"C");
                        scanf("%f", &n);
                        scanf("%f", &n);
-                       matrices[s] = raiseMatrixToN(matrices[s], n);
+                       matrices[s] = raiseMatrixToN(matrices[d], n);
                        break;
 
                case ADD_M:
                        break;
 
                case ADD_M:
index ae253f7df419c15abf80c3101e26c8cda445799f..18c29c16ed66fb71b302ba3a4291df24c018feb4 100644 (file)
@@ -21,6 +21,7 @@ matrix_t matrixError(error_t error){
        }
        return createMatrix(0, 0);
 }
        }
        return createMatrix(0, 0);
 }
+
 // Creates a rows x columns matrix;
 matrix_t createMatrix(size_t rows, size_t columns){
        matrix_t newmatrix;
 // Creates a rows x columns matrix;
 matrix_t createMatrix(size_t rows, size_t columns){
        matrix_t newmatrix;
@@ -155,8 +156,11 @@ matrix_t divideMatrices(matrix_t a, matrix_t b){
 matrix_t raiseMatrixToN(matrix_t m, int n){
        if(!isSquare(m))
                return matrixError(NOT_SQUARE);
 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);
        matrix_t raisedmatrix = identityMatrix(m.rows);
+       if(n < 0){
+               m = inverse(m);
+               n = -n;
+       }
        for(int i = 0; i < n; ++i)
                raisedmatrix = multiplyMatrices(raisedmatrix, m);
        return raisedmatrix;
        for(int i = 0; i < n; ++i)
                raisedmatrix = multiplyMatrices(raisedmatrix, m);
        return raisedmatrix;
@@ -164,6 +168,7 @@ 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){
 
 // 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){
+       // TODO: not remove any
        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){
        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){
@@ -239,5 +244,5 @@ matrix_t inverse(matrix_t m){
        int det = determinant(m);
        if(det == 0)
                return matrixError(ZERO_DET);
        int det = determinant(m);
        if(det == 0)
                return matrixError(ZERO_DET);
-       return multiplyByN(adjugate(m), (float)1/det);
+       return divideByN(adjugate(m), det);
 }
\ No newline at end of file
 }
\ No newline at end of file