Upper Tringular Matrix

Mar 18, 2015 at 10:18am
Hallo,

I will get uppertringular part of a matrix.For example ı have a matrix :
1 2 3
4 5 6
7 8 9 ı will register as 1 2 3 4 5 6 9 in an array.This is my Code what is wrong ?

double *upperTriangular(const double* Mtrx,int n){

double* U = new double[n];

double upper = 0;

cout << "Upper-triangular matrix " << endl;

for(int i = 0; i < n; i++){
U[i] = 0.0;
}


for (int i = 0; i < n; i++) {

for (int j = i; j < n ; j++) {

// cout<<"Mtrx :"<<Mtrx[i*n+j]<<endl;
upper=Mtrx[i*n+j];
// cout<<"upper :"<<upper<<endl;
U[i]=upper;

// cout<<U[i];
}



//cout<<U[i];
}

for (int i = 0; i < n; i++) {

cout<<U[i]<<" ";
}
cout<<"\n";


return U;

}

Mar 18, 2015 at 10:34am
please use the code tag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
double *upperTriangular(const double* Mtrx,int n){

    double* U = new double[n];

    double upper = 0;

    cout << "Upper-triangular matrix " << endl;

    for(int i = 0; i < n; i++){
        U[i] = 0.0;
    }

    for (int i = 0; i < n; i++) {

        for (int j = i; j < n ; j++) 
        {
            // cout<<"Mtrx :"<<Mtrx[i*n+j]<<endl;
            upper=Mtrx[i*n+j];
            // cout<<"upper :"<<upper<<endl;
            U[i]=upper;
            // cout<<U[i];
        }
        //cout<<U[i];
    }

    for (int i = 0; i < n; i++) 
    {
        cout<<U[i]<<" ";
    }
    cout<<"\n";

    return U;
}


furthermore: do you want to :
a) copy the data or
b) just point to the location?

i'll start with b because it's easy as shit
1
2
3
4
double *upperTriangular(const double* Mtrx)
{
    return Mtrx;
}


now a, wich is a little bit more complex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstring>

double *upperTriangular(const double* Mtrx, int n)
{
    double* matrix = new double[n];

 // copy first n elements from Mtrx to matrix
    memcpy(matrix, Mtrx, n * sizeof(double));

    return matrix;
}
int main(void)
{
    double matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

    double* upper = upperTriangular(&(matrix[0][0]), 3);

    for(int i = 0; i < 3; ++i)
        std::cout << upper[i] << std::endl;

    return 0;
}
Mar 18, 2015 at 11:52am
I get array uppertringular then ı multiply this array in another function with a matrix.
Mar 18, 2015 at 11:54am
I don't understand

Does my solution do what you wanted?

it returns the first n elements in matrix
Mar 18, 2015 at 12:48pm
ok, thank you ı understood your solution now.
Mar 18, 2015 at 1:05pm
good to hear :)
Mar 18, 2015 at 3:27pm
However, that is not "upper triangular". See http://en.wikipedia.org/wiki/Triangular_matrix

Besides, memcpy simply "mysteriously" copies raw bytes while an array to array copy like this with loop(s) show clearly what the function does. Its simply an indexing dilemma.


@candidate:
The problem in your code is that you have a N*N matrix and you try to copy the triangular elements from it into N-element array.

For example, N=3 --> N*N==9 and the triangular matrix has 6 elements. 6>3 means trouble.
Mar 18, 2015 at 4:40pm
Oh, I'm sorry, I didn't know what upper triangular is

Besides, memcpy simply "mysteriously" copies raw bytes while an array to array copy like this with loop(s) show clearly what the function does. Its simply an indexing dilemma.

You think so?
well, memcpy has a well defined behaviour, so it does not just mysteriously do things ^^

But as I stated earlier, I didn't know what upper triangular is an since it was a 3x3 matrix i though he wanted to have the first 3 elements, which is obviously not needed here.
Topic archived. No new replies allowed.