Code breaks when randomly specifying matrix values

I'm trying to code an LU decomposition solver. My code actually seems to work fairly well when I specify a vector of vectors manually. However when I comment out the manual assignments for V,b, and x, and uncomment the code that randomly generates values for it, other parts of my program begin to fail. The odd thin is the problem doesn't appear to be related at all to filling the matrix values. After uncommenting the code to randomly generate values, my vector of vectors V and by b vector receive random values and everything seems to work normally except when I calculate x and y. Those values just remain at 0 and don't seem to take any values. What the heck am I doing wrong here?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <random>
#include <ctime>
#include <chrono>
#include <algorithm>

using namespace std;
void rref(std::vector<std::vector<double>> & vec);

void matrixPrint(const std::vector<std::vector<double>> & V);
void luDecomp(vector<vector<double>> & A,vector<vector<double>> & L,vector<vector<double>> & U);

;

int main() {


    typedef std::chrono::high_resolution_clock myclock;
    myclock::time_point beginning = myclock::now();

    int n;

    cout<<"size of matrix?"; cin>>n;
    std::vector<int> x (n);
    std::vector<int> b (n);
    std::vector<int> xcomp (n);

    std::vector<int> temp (n);




    myclock::duration d = myclock::now() - beginning;
    unsigned seed2 = d.count();
    cout<<seed2<<endl;
    minstd_rand0 generator (seed2);
    uniform_int_distribution<int> distribution(-9999999,9999999);

    vector<vector<double>> V(n, vector<double>(n));
    vector<vector<double>> L(n, vector<double>(n));
    vector<vector<double>> U(n, vector<double>(n));


    V={{3,6,-3},{-1,0,11},{1,1,-2}};
    x={5,68,45};
    b={288,490,-17};
//commenting the above and uncommenting the below fills my vector of vectors, //but y and x vecors refuse to change, they remain at 0 and the vectors will not take any new values assigned

 /*   for(int r = 0; r < n; r++)
    {

        for(int c = 0; c < n; c++)
        {
            double val=double(distribution(generator))/10000;
            V[r][c]=val;
        }
    }
    for(int c = 0; c < n; c++)
    {
        double val=double(distribution(generator))/10000;
        b[c]=val;
    }

    cout<<"Original Random Matrix"<<endl;
    matrixPrint(V);

    cout<<"random b";
    for (int i=0; i<V.size();i++)
    {
        cout <<b[i]<<",";
    }
    cout<<endl;
*/
    //init U and L

    for(int i=0; i<U.size();i++)
    {
        U[i][i]=1;
        L[i][0]=V[i][0];
    }





    matrixPrint(V);


    luDecomp(V,L,U);

    cout<<"umatrix"<<endl;
    matrixPrint(U);
    cout<<"Lmatrix"<<endl;
    matrixPrint(L);



    //backsolve

    temp[0]=b[0]/L[0][0];
    for(int r=1;r<L.size();r++)
    {
        double sum=0;
        for (int c=0;c<r;c++)
        {
            sum=sum+L[r][c]*temp[c];
        }
        temp[r]=(b[r]-sum)/L[r][r];
        cout <<",";

    }

    cout<<"yvec"<<endl;
    for (int i=0; i<U.size();i++)
    {
        cout <<temp[i]<<",";
    }
    cout<<endl;

    //forward solve
    xcomp[U.size()-1]=temp[U.size()-1];

    for(int r=U.size()-1;r>0-1;r--)
    {
        double sum =0;
        for(int c=U.size()-1;c>r;c--)
        {
            sum=sum+xcomp[c]*U[r][c];

        }
        xcomp[r]=temp[r]-sum;
    }


    cout<<"xvec"<<endl;
    for (int i=0; i<U.size();i++)
    {
        cout <<xcomp[i]<<",";
    }
    cout<<endl;




    return 0;

}




void luDecomp(vector<vector<double>> & A, vector<vector<double>> & L, vector<vector<double>> & U) {
    for (int c = 0; c < U.size(); c++) {

        U[0][c] = A[0][c] / A[0][0];


    }

    //calc column c values
    for (int c=1;c<U.size();c++)
    {


        //calc L[c][r] then [c][r+1]... so on
        for (int r=1; r<U.size();r++) {
            double sum = 0;
            //i is the iterative value from 0 to end of row/column
            for (int i = 0; i < U.size(); i++) {
                sum = sum + L[r][i] * U[i][c];
            }

            L[r][c] = A[r][c] - sum;



        }
        //Calc rows
        for (int col=c+1;col<U.size();col++)
        {
            double sum =0;
            for (int i = 0; i < U.size(); i++) {
                sum = sum + L[c][i]*U[i][col];
            }

            U[c][col] =(A[c][col]-sum)/L[c][c];
        }
    }

}


//prints the matrix
void matrixPrint(const vector<vector<double>> & matrix)
{
    for(int r=0; r<matrix.size(); r++)
    {

        for(int c=0; c<matrix[0].size(); c++)
        {

            //print element A[r][c]
            cout<<"["<<r<<"]["<<c<<"]="<<matrix[r][c]<<"  ";
        }
        cout<<endl;
    }
    cout<<endl;
}
Topic archived. No new replies allowed.