Jump to content

QR decomposition: Difference between revisions

→‎{{header|C}}: bugfix; still ugly
(Added BBC BASIC)
(→‎{{header|C}}: bugfix; still ugly)
Line 444:
}
 
mat matrix_copy(voidint *n;double a[][n], int m, int n)
{
mat x = matrix_new(m, n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
x->v[i][j] = ((double(*)[n])a)[i][j];
return x;
}
Line 523:
for(int i = 0; i < m->m; i++) {
for (int j = 0; j < m->n; j++) {
printf(" %58.3f", m->v[i][j]);
}
printf("\n");
Line 534:
mat q[m->m];
mat z = m, z1;
for (int k = 0; k < m->mn - 1; k++) {
double e[m->m], x[m->m], a;
z1 = matrix_minor(z, k);
Line 557:
*Q = q[0];
*R = matrix_mul(q[0], m);
for (int i = 1; i < m->mn - 1; i++) {
z1 = matrix_mul(q[i], *Q);
if (i > 1) matrix_delete(*Q);
Line 571:
 
double in[][3] = {
{ 12, -51, 4 },
{ 6, 167, -68 },
{ -4, 24, -41 },
{ -1, 1, 0},
{ 2, 0, 3},
};
 
Line 579 ⟶ 581:
{
mat R, Q;
mat x = matrix_copy(in, 35, 3);
householder(x, &R, &Q);
 
printfputs("Q\n"); matrix_show(Q);
printfputs("R\n"); matrix_show(R);
 
// to show their product is the input matrix
mat m = matrix_mul(Q, R);
puts("Q * R"); matrix_show(m);
 
matrix_delete(x);
matrix_delete(R);
matrix_delete(Q);
matrix_delete(m);
return 0;
}</lang>Output:<lang>Q
{{out}}
0.857 -0.394 0.331
<pre>
0.429 0.903 -0.034
Q
-0.286 0.171 0.943
0.846 -0.391 0.335 0.064 -0.120
0.423 0.904 -0.033 0.018 -0.046
-0.282 0.170 0.941 -0.029 0.065
-0.071 0.014 0.007 0.997 0.007
0.141 -0.017 -0.023 0.003 0.990
 
R
14.000177 21 20.000667 -1413.000402
-0.000 175.000043 -70.000080
- 0.000 - 0.000 -35.000</lang>076
-0.857000 -0.394000 0.331264
-0.429000 -0.903000 -0 2.034959
 
Q * R
12.000 -51.000 4.000
6.000 167.000 -68.000
-4.000 24.000 -41.000
-01.286000 0 1.171000 -0.943000
2.000 -0.000 3.000
</pre>
 
=={{header|Common Lisp}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.