3

I wrote C++ codes that calculates the determinant of matrix using the recursive method.

Now, I would like to rewrite that recursive method using an iterative method. But I cannot figure out how to accomplish that.

The question is: How to rewrite the specific recursive method (int Determinant(int *&, const int)) into an iterative method?

Here are my C++ codes:

// Determinant C++
#include <iostream>
#include <ctime>
using namespace std;

void RandInitArray(int *, const int, int = 0, int = 20);
void Display(int *, const int);

int CalculateDeterminant(int *&, const int);
int Determinant(int *&, const int);

int main()
{
    start = clock();
    srand((unsigned int)time(NULL));

    int N = 12;     // Size of matrix
    int * S = new int[N * N];
    int a(-10), b(10), det;

    RandInitArray(S, N, a, b);
    cout.precision(4);
    Display(S, N);

    det = CalculateDeterminant(S, N);

    cout << "\nDeterminant = " << det << "\n\n";

    cin.get();
    return 0;
}

void RandInitArray(int * arr, const int N, int a, int b)
{
    for (int i = 0; i < N * N; i++)
        arr[i] = rand() % (b - a + 1) + a;
}

void Display(int *arr, const int N)
{
    for (int i = 0; i < N * N; i++)
        cout << arr[i] << ((i + 1) % N ? "\t" : "\n");
}

int CalculateDeterminant(int *& S, const int N)
{
    int rez;

    if (N < 1)
        cout << "Size of matrix must be positive\n";
    else if (N == 1)
        rez = *S;
    else if (N == 2)
        rez = S[0] * S[3] - S[1] * S[2];
    else if (N == 3)
        rez = S[0] * S[4] * S[8] + S[1] * S[5] * S[6] + S[2] * S[3] * S[7] -
        S[2] * S[4] * S[6] - S[1] * S[3] * S[8] - S[0] * S[5] * S[7];
    else
        rez = Determinant(S, N);

    return rez;
}

int Determinant(int *& S, const int N)
{
    int sign(1), det(0), res, M(N - 1);
    int  * _S;

    for (int k = 0; k < N; k++)
    {
        _S = new int[M * M];

        int ind = 0;
        for (int i = N; i < N * N; i++)
        {
            if (i % N != k)
                _S[ind++] = S[i];
        }

        if (M == 3)
        {
            res = S[k] == 0 ? 0 : _S[0] * _S[4] * _S[8] + _S[1] * _S[5] * _S[6] + _S[2] * _S[3] * _S[7] -
            _S[2] * _S[4] * _S[6] - _S[1] * _S[3] * _S[8] - _S[0] * _S[5] * _S[7];

            delete[] _S;
        }
        else
            res = S[k] == 0 ? 0 : Determinant(_S, N - 1);

        det += S[k] * sign * res;
        sign *= -1;
    }

    delete[] S;

    return det;
}

I know this is not the smartest way to calculate determinant of a large matrix. I can simplify matrix using matrix transformations and drastically speed up calculations.

Despite that any help or hint would be greatly appreciated.

L_J
  • 2,351
  • 10
  • 23
  • 28
  • 2
    Do you think all of this code is relevant to the question? Create a minimal example, please. – DeiDei Jun 09 '18 at 21:20
  • @DeiDei that's right. Only **int Determinant(int *& S, const int N)** - method is important. – L_J Jun 09 '18 at 21:25
  • "How to rewrite" - don't rewrite, just implement it as you need. – mentallurg Jun 09 '18 at 21:38
  • The `(ptr, cnt)` interface is not recommended. You are suggested to use a pair of iterators or use a specific class (like C++20 `span`.) – L. F. Jun 09 '18 at 23:11

1 Answers1

1

You should sum up a total of N! product terms, each of which is a unique product N elements none of which reside on same row nor colomn: Sorted in row order, colomn indices are permutations of range [1··N), with odd permutations negated. You can use std::next_permutation to calculate permutation and invert sign per iteration.

Red.Wave
  • 2,790
  • 11
  • 17