Random Flow Theory and FOREX - page 40

 
Prival >> :

Please do the transpose procedure, I can't do it in MQL. Here is an example in matcad. One condition is that dimension m and n are not known beforehand and the function should preferably be inversal, i.e. it doesn't matter which array to transpose, of course it should be called multiple times and work correctly.

Try it:



void transposeMatrix( int rows, int cols, double a[], double & t[] )
{
    for( int i = 0; i < rows; i++ )
    {
        for( int j = 0; j < cols; j++ )
        {
            t[ j * rows + i ] = a[ i * cols + j ];
        }
    }
}

void example()
{
    int m = 5;
    int n = 3;

    int aRows = m + 1;
    int aCols = 1;
    double A[];

    ArrayResize( A, aRows * aCols );

    for( int i = 0; i <= m; i++ ) A[ i ] = i;

    int bRows = m + 1;
    int bCols = n + 1;
    double B[];

    ArrayResize( B, bRows * bCols );

    for( i = 0; i <= m; i++ )
    {
        for( int j = 0; j <= n; j++ ) B[ i * bCols + j ] = i + j;
    }

    // Получаем результат транспонирования A в AT

    double AT[];
    ArrayResize( AT, aRows * aCols );

    transposeMatrix( aRows, aCols, A, AT );

    // Получаем результат транспонирования B в BT

    double BT[];
    ArrayResize( BT, bRows * bCols );

    transposeMatrix( bRows, bCols, B, BT );
}
 

No, the matrix A and B must be transposed, but you have generated two more matrices AT and BT, i.e. you have not transposed, but have made matrices which are transposed in relation to the original matrix

 

I don't see any difference. Add more:


ArrayCopy( A, AT );
ArrayCopy( B, BT );

if necessary, and get the transpose result in the original matrices.


Although I would just use AT and BT where I need transposed matrices, as they tend to go in pairs.

 
bstone писал(а) >>

I don't see any difference. Add more:

if necessary, and get the transpose result in the original matrices.


Although I would just use AT and BT where I need the transpose matrices, as they tend to go in pairs.

Thanks for the idea of making copies of AT and BT and using them where I need them. I'll check with a pencil if it can be done, but I'm afraid not, as Kalman filter is iterative, and one and the same matrix is transposed several times, not to multiply them for each tick.

But your transposition didn't work.

You did it wrong - matrix B and BT are two-dimensional, i.e. their rank = 2, and you have 1.

Insert Print("-------",ArrayDimension(BT)); the answer is 1

i.e. you have a one dimensional array and you have not swapped any rows with columns.

 
Prival >> :

i.e. you have a one-dimensional array and you have not swapped any rows with columns.

Don't jump to conclusions - arrays are one-dimensional outside, but two-dimensional inside :)

 
bstone писал(а) >>

Don't jump to conclusions - arrays are one-dimensional on the outside but two-dimensional on the inside :)

Understand then flies all matrix algebra, here's a picture again from matcad. Continuing with my example.

and there's not just multiplication, but all kinds of operations + their own specific ones that are unique to matrix algebra.

If dimensionality is violated during transposition, all is bad, very bad.

It is not a simple problem, though at first sight, what there rows and columns to swap places, but here so at a glance it does not turn out.

Although I know for sure this problem is solvable, but in MQL it is right hand on left ear, and God forbid I can reach it.

 
Prival >> :

Understand then flies all matrix algebra, here is a picture again from matcad. Continuing with my example.


Prival, believe me, I am very well versed in matrix algebra. I've offered you the solution of your problem in terms of MQL4. There is simply no other solution, since in MQL4, only the first dimension can be managed dynamically.


In order to get a complete library of matrix operations you need to rework your current version to work with a different abstraction for representing matrices. Specifically, you need to switch from two-dimensional arrays to linear arrays.


Think about how matcad stores matrices? Is it not in linear arrays? Or do you think it stores them on two-dimensional hard disks? For centuries, multidimensional arrays have been stored in memory linearly, using two ways of distributing the data: line-by-line or column-by-column.


If you have a 3x2 matrix, in memory (a linear array) when stacked line by line, it will look like this: { a(1,1) a(1,2) a(2,1) a(2,2) a(3,1) a(3,2) }


Therefore, all matrix operations must use this data organization. The transition from two-dimensional to linear indexing in the existing code will not be a problem. But you will have full control over dynamic matrix dimensions.

 

By the way, the operation of transposing rectangular matrices in the way you originally wanted it (i.e. immediately in the original array) is not as trivial as it seems at first sight. In fact, it is a complex problem and there are many tricky algorithms to solve it. The programming gods have been trying to solve it effectively (minimum number of permutations, cache-locality, minimum additional memory, etc.) since 1950.


So don't kick yourself too much for not being able to solve it.

 
Prival >> :

But your transposition didn't work.

You did the wrong thing - matrix B and BT are two dimensional, i.e. their rank = 2, and you have 1.

Insert Print("-------",ArrayDimension(BT)); the answer is 1

i.e. you have a one dimensional array and you have not swapped any rows with columns.

Are "rank of a matrix" and "rank of an array" identical? As far as I remember, the rank of a matrix is the highest order of its minors... But what is the "rank of an array"... frankly, I don't know...

 
bstone писал(а) >>

Prival, believe me, I have a very good understanding of matrix algebra. I offered you a solution to your problem in terms of MQL4. There is no other solution, since in MQL4, only the first dimension can be controlled dynamically.

This is where the problem is solved in one more way, if you can transfer arrays into and out of a procedure, but MQL4 doesn't allow to do it either. I've created an indicator based on the above model, got rid of all transpositions, matrix calls, matrix exponentiation (SQRT(-1), but I couldn't avoid it), etc. I implemented Kalman in MQL4 (2*2 matrix) but I've spent more than 3 months on it. And now if I decide to add 1 more currency, all over again. And that's a huge time waster. I've long been choosing a programming language based on the speed of accomplishing a given task (and I know about memory, efficiency, stack, once upon a time I used to program in assembler). The main thing is time, it is the only value we have.

I will wait for MQL5.