views:

448

answers:

6

I have a program that looks like the following:

double[4][4] startMatrix;
double[4][4] inverseMatrix;
initialize(startMatrix) //this puts the information I want in startMatrix

I now want to calculate the inverse of startMatrix and put it into inverseMatrix. I have a library function for this purpose whose prototype is the following:

void MatrixInversion(double** A, int order, double** B)

that takes the inverse of A and puts it in B. The problem is that I need to know how to convert the double[4][4] into a double** to give to the function. I've tried just doing it the "obvious way":

MatrixInversion((double**)startMatrix, 4, (double**)inverseMatrix))

but that doesn't seem to work. Is that actually the right way to do it?

+1  A: 

Two dimensional array is not a pointer to pointer or something similar. The correct type for you startMatrix is double (*)[4]. For your function, the signature should be like:

MatrixInversion( double (*A)[4], int order, double (*B)[4] );
leiz
The function is apparently working with square matrices of any size (order). Restricting it to 4x4 matrices only is hardly acceptable. Also, there's no point to pass the 'order' now.
AndreyT
@AndreyT, I was just showing him/her how to do it. If I wanted to it be general, I could show him how to have a class that represents a matrix.
leiz
In a *square* matrix, row count is the same as column count. You hardcoded your column count as 4. Now there's no point to pass the row count anymore - it also must be 4 and only 4.
AndreyT
The OP called the function "a library function". This normally means that there's no freedom to change the function's interface.
AndreyT
@AndreyT, ok, I did NOT see that it was a library function.
leiz
+4  A: 

No, there's no right way to do specifically that. A double[4][4] array is not convertible to a double ** pointer. These are two alternative, incompatible ways to implement a 2D array. Something needs to be changed: either the function's interface, or the structure of the array passed as an argument.

The simplest way to do the latter, i.e. to make your existing double[4][4] array compatible with the function, is to create temporary "index" arrays of type double *[4] pointing to the beginnings of each row in each matrix

double *startRows[4] = { startMatrix[0], startMatrix[1], startMatrix[2] , startMatrix[3] };
double *inverseRows[4] = { ... };

and pass these "index" arrays instead

MatrixInversion(startRows, 4, inverseRows);

Once the function finished working, you can forget about the 'start...' arrays, since the result will be placed into your original 'matrix...' arrays correctly.

AndreyT
A: 
David
James Black
This is not a correct declaration. Only the first size can be omitted in the array parameter declaration. Also, fixing the size of the matrix kills the intended felxibility of the function (i.e its ability to process matrix of any size).
AndreyT
AndreyT
+1  A: 

Since you are using C++, the proper way to do something like this would be with a custom class and some templates. The following example is rather rough, but it gets the basic point across.

#include <iostream>

using namespace std;

template <int matrix_size>
class SquareMatrix
{
    public:
        int size(void) { return matrix_size; }
        double array[matrix_size][matrix_size];
        void copyInverse(const SquareMatrix<matrix_size> & src);
        void print(void);
};

template <int matrix_size>
void SquareMatrix<matrix_size>::copyInverse(const SquareMatrix<matrix_size> & src)
{
    int inv_x;
    int inv_y;

    for (int x = 0; x < matrix_size; x++)
    {
        inv_x = matrix_size - 1 - x;
        for (int y = 0; y < matrix_size; y++)
        {
            inv_y = matrix_size - 1 - y;
            array[x][y] = src.array[inv_x][inv_y];
        }
    }
}

template <int matrix_size>
void SquareMatrix<matrix_size>::print(void)
{
    for (int y = 0; y < 4; y++)
    {
        for (int x = 0; x < 4; x++)
        {
            cout << array[x][y] << " ";
        }   
        cout << endl;
    }
}

template <int matrix_size>
void Initialize(SquareMatrix<matrix_size> & matrix);

int main(int argc, char * argList[])
{
    SquareMatrix<4> startMatrix;
    SquareMatrix<4> inverseMatrix;

    Initialize(startMatrix);

    inverseMatrix.copyInverse(startMatrix);

    cout << "Start:" << endl;
    startMatrix.print();

    cout << "Inverse:" << endl;
    inverseMatrix.print();

    return 0;
}

template <int matrix_size>
void Initialize(SquareMatrix<matrix_size> & matrix)
{
    for (int x = 0; x < matrix_size; x++)
    {
        for (int y = 0; y < matrix_size; y++)
        {
            matrix.array[x][y] = (x+1)*10+(y+1);
        }
    }
}
e.James
i like your idea, the correct c++ way. a good example regardless of works or not.
EffoStaff Effo
Also good to take in a template type T, which is what the elements will be. And probably make size unsigned, since negative sizes don't make sense.
GMan
Yet implementing a relatively heavy operation (it's inverse, BTW, not transpose) as a template function parametrized by matrix size might result in code bloat, since the code will be re-instantiated for each particular size. The proper technique of dealing with this issue is to implement the bulk of the functionality by a function parametrized with a run-time size (as in the original question) and then build a "thin" template on top of that function. But that brings us back to the original problem.
AndreyT
A: 

by nice coding if c++:

struct matrix {
    double m[4][4];
};

matrix startMatrix;
matrix inverseMatrix;

so the interface would be

void MatrixInversion(matrix &A, int order, matrix &B);

and use it

MatrixInversion(startMatrix, 4, inverseMatrix);

The benefit

  1. the interface is very simple and clear.
  2. once need to modify "m" of matrix internally, you don't need to update the interface.

Or this way

struct matrix {
    void Inversion(matrix &inv, int order) {...}
protected:
    double m[4][4];
};

matrix startMatrix;
matrix inverseMatrix;
...

An ugly way in c

void MatrixInversion(void *A, int order, void *B);
MatrixInversion((void*)startMatrix, 4, (void*)inverseMatrix);

EDIT: reference code for MatrixInversion which will not crash:

void MatrixInversion(void *A, int order, void *B)
{
    double _a[4][4];
    double _b[4][4];

    memcpy(_a, A, sizeof _a);
    memcpy(_b, B, sizeof _b);
    // processing data here

    // copy back after done
    memcpy(B, _b, sizeof _b);
}
EffoStaff Effo
The same design error as in some other responses... You hardcoded the fixed matrix size in your matrix type. What are you going to do if you'd need a 5x5 matrix? 6x6? And what is the point of passing 4 to the function, when 4 is already hardcoded into the matrix type?
AndreyT
Your "ugly way in C" will simply crash the program. The OP already tried it, if you noticed, and it didn't work (for obvious reasons).
AndreyT
refactorying rule: improve design when necessary. see "The benefig" section. "4" is a specific example, that is, an integer, and its value is 4.
EffoStaff Effo
actually, wont crash because you got known already a 2d double array is not just double**, so, you'll could process it safely. see my reference code.
EffoStaff Effo
@AndreyT, you disappointed me, you just know nothing. if have trouble, please yourself write some code to do a test, not just say A is bad, B is not good.
EffoStaff Effo
Sorry, I have hard time understnding what you are trying to say. Again, the OP explictly stated in his question that what you suggest in your "ugly way in C" does not work. A just repeated it to you, since for some reason you decided to repost it. Most everybody here knows and understands that it won't work and why it won't work.
AndreyT
You still haven't explained how you are going to work with, say, a 5x5 matrix and why you are passing the 'order' when the order is fixed at compile time.
AndreyT
1. repost: found double** is not necessary at all. 2. various dimensional size: that not my point. while if i SHOULD give a proposal, please consider configurable object because we are talking about c++ here (sorry but do you know what is a configurable object?). though if you think the "C library" rule cannot be broken, adding parameters which indicate the size should be enough.
EffoStaff Effo
Sorry, but what I see so far is a growing pile up of irrelevant code. Please, re-read the original question.
AndreyT
@AndreyT, well, your are very interesting. OP said "The problem is that I need to know how to convert the double[4][4] into a double** to give to the function", and the question is equal to: "why cannot use double** on the MatrixInversion interface corresponding to a double startMatrix[4][4] array?". almost every body gave the correct explain including you. so what are you arguing with every body for? showing that your are a idealism?
EffoStaff Effo
A: 

For given reason that two-dimensional array (one contiguous block of memory) and an array of pointers (not contiguous) are very different things, you can't pass a two-dimensional array to a function working with pointer-to-pointer.

One thing you could do: templates. Make the size of the second dimension a template parameter.

#include <iostream>

template <unsigned N>
void print(double a[][N], unsigned order)
{
    for (unsigned y = 0; y < order; ++y) {
        for (unsigned x = 0; x < N; ++x) {
            std::cout << a[y][x] << ' ';
        }
        std::cout << '\n';
    }
}

int main()
{
    double arr[3][3] = {{1, 2.3, 4}, {2.5, 5, -1.0}, {0, 1.1, 0}};
    print(arr, 3);
}

Another, a bit clumsier way might be to make the function accept a pointer to a single-dimensional array, and both width and height given as arguments, and calculate the indexes into a two-dimensional representation yourself.

#include <iostream>

void print(double *a, unsigned height, unsigned width)
{
    for (unsigned y = 0; y < height; ++y) {
        for (unsigned x = 0; x < width; ++x) {
            std::cout << a[y * width + x] << ' ';
        }
        std::cout << '\n';
    }
}

int main()
{
    double arr[3][3] = {{1, 2.3, 4}, {2.5, 5, -1.0}, {0, 1.1, 0}};
    print(&arr[0][0], 3, 3);
}

Naturally, a matrix is something that deserves a class of its own (but the above might still be relevant, if you need to write helper functions).

UncleBens