Matrix inversion through EROs

Hello, I would appreciate some help with the following, the questions refer to the code below;


(i) Change the read matrix() routine so that it reads the dimensions m,n rather than being
passed them by the calling program. The specimen program overload.cpp (`Reference parameters.
. . ') will show the implications of this change.

(ii) Insert a routine
void print_matrix ( int m, int n, double a[10][10] )
which prints the matrix in a decent way. That is, the eld width should be 10, the precision 4, and the
notation ios::fixed

(iii) Delete the statement from the main program in which m and n are read. Now they are read
by the read matrix() routine.

(iv) Delete the existing output statements from main() and replace them with calls to your
print matrix() routine

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
#include <iostream>
#include <iomanip>

using namespace std;

void augment ( int m, double a[10][10], double b[10][20] )
{
  int i, j;
  for ( i=0; i<m; ++i )
  {
    for (j=0; j<m; ++j )
    {
      b[i][j] = a[i][j];
      b[i][j+m] = i == j;
    }
  }
}

void peeloff ( int m, double a[10][20], double c[10][10] )
{
  int i, j;
  for ( i=0; i<m; ++i )
  {
    for (j=0; j<m; ++j )
    {
      c[i][j] = a[i][j+m];
    }
  }
}


void swap ( int r, int s, int m, int n, double a[10][20] )
{
  int j;
  double temp;

  if ( r == s )
    return;

  for ( j=0; j<n; ++j )
  {
    temp = a[r][j];
    a[r][j] = a[s][j];
    a[s][j] = temp;
  }
}

void scale ( int r, double s, int m, int n, double a[10][20] )
{
  int j;
  for ( j=0; j<n; ++j )
  {
    a[r][j] *= s;
  }
}

void subtract ( int r, double s, int t, int m, int n, double a[10][20] )
{
  int j;
  for (j=0; j<n; ++j)
  {
    a[r][j] -= s * a[t][j];
  }
}

void multiply (int ell, int m, int n,  double a[10][10], double b[10][10],
        double c[10][10])
{
  int i,j,k;
  double tot;

  for (i=0; i<ell; ++i)
  {
    for (k=0; k < n; ++k )
    {
      tot = 0;
      for ( j=0; j<m; ++j )
      { tot += a[i][j] * b[j][k]; }
      c[i][k] = tot;
    }
  }
}

void read_matrix ( int m, int n,  double a[10][10] )
{
  int i,j;
  for ( i=0; i<m; ++i )
  {
    for ( j=0; j<n; ++j )
    {
      cin >> a[i][j];
    }
  }
}

void gje ( int m, int n, double a[10][20] )
{
  int i, j, k, pivot;
  double s;

  k = -1;

  for ( j=0; j<n; ++j )
  {
    pivot = -1;
    for ( i=k+1; pivot < 0 && i<m; ++i )
    {
      if ( a[i][j] != 0 )
	pivot = i;
    }

    if ( pivot >= 0 )
    {
      ++k;
      swap ( pivot, k, m, n,  a );
      s = 1 / a[k][j];
      scale ( k, s, m, n, a );
      for ( i=0; i<m; ++i )
      {
        if ( i != k )
	{
	  s = a[i][j];
	  subtract ( i, s, k, m, n, a );
        }
      }
    }
  } 

 cout << "gje finished k " << k << endl;
}

main()
{
  double a[10][10], b[10][20], c[10][10], d[10][10];

  int m, n, i,j;

  cin >> m >> n;

  read_matrix ( m,n,a );

  augment ( m, a, b );
  gje ( m, 2*m, b );
  peeloff ( m, b, c );
  multiply (m,m,m,a,c,d);

  cout << "Matrix\n";

  for ( i=0; i<m; ++i )
  {
    for ( j=0; j<m; ++j )
    {
      cout << setprecision ( 10 );
      cout <<  "  " << a[i][j];
    }
    cout << endl;
  }

  cout << "\nInverse\n";

  for ( i=0; i<m; ++i )
  {
    for ( j=0; j<m; ++j )
    {
      cout << setw ( 10 );
      cout << setprecision ( 10 );
      cout << "  " <<  c[i][j];
    }
    cout << endl;
  }

  cout << "\nProduct\n";
  for ( i=0; i<m; ++i )
  {
    for ( j=0; j<m; ++j )
    {
      cout << setw ( 10 );
      cout << setprecision ( 10 );
      cout << "  " <<  d[i][j];
    }
    cout << endl;
  }

  return 0;
}
Topic archived. No new replies allowed.