~wattazoum/albert/albert-mod

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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*******************************************************************/
/***  FILE :        ReduceMatrix.c                               ***/
/***  AUTHOR:       David P Jacobs                               ***/
/***  PROGRAMMER:   Sekhar Muddana                               ***/
/***  DATE WRITTEN: May 1990.                                    ***/
/***  MODIFIED:     David Lee (Aug 1992).                        ***/
/***                     - Added code to gather statistics.      ***/
/***  PUBLIC ROUTINES:                                           ***/
/***      int ReduceMatrix()                                     ***/
/***  PRIVATE ROUTINES:                                          ***/
/***  MODULE DESCRIPTION:                                        ***/
/*******************************************************************/

#include <stdio.h>
#include "Build_defs.h"
#include "CreateMatrix.h"

#define assert_not_null(p) if (p == NULL) return(0)

/*********************
These are for density measurement on last matrix. 
*********************/
extern short int gather_density_flag;
extern long unsigned int num_elements;
extern long unsigned int max_num_elements;

static Matrix TheMatrix;
static int Num_rows;
static int Num_cols;


int ReduceTheMatrix(Mptr,Rows,Cols,Rank)
Matrix Mptr;
int Rows;
int Cols;
int *Rank;
{
    int i,j;
    int nextstairrow = 0;					/* next row with stair step 1 */
    Scalar S_zero();

    TheMatrix = Mptr; 
    Num_rows = Rows;
    Num_cols = Cols;

    if ((Rows == 0) || (Cols == 0))
        return(OK);

    assert_not_null(Mptr);

    for (i=0;i<Num_cols;i++) {
        for (j=nextstairrow;j<Num_rows;j++) {
             if (TheMatrix[j*Num_cols + i] != S_zero())
                 break;
        }
        if (j<Num_rows) {
            Interchange(nextstairrow,j);
            Knockout(nextstairrow,i);
			   nextstairrow++;
        }
    }

    *Rank = nextstairrow;

    return(OK);
}


Interchange(Row1,Row2)
int Row1,Row2;
{
    int j;
    int row1_start,row2_start;
    Scalar temp;
   
    row1_start = Row1 * Num_cols; 
    row2_start = Row2 * Num_cols; 

    for (j=0;j<Num_cols;j++) {
        temp = TheMatrix[row1_start + j];
        TheMatrix[row1_start + j] = TheMatrix[row2_start + j];
        TheMatrix[row2_start + j] = temp;
    }
}


Knockout(Row,Col)
int Row;
int Col;
{
    Scalar S_one();
    Scalar S_inv();
    Scalar S_minus();

    int j;
    Scalar x;
    Scalar one = S_one();

    if ((x = TheMatrix[Row*Num_cols + Col]) != one)
        MultRow(Row,S_inv(x));
    for (j=0;j<Row;j++)
        AddRow(S_minus(TheMatrix[j*Num_cols + Col]),Row,j);
    for (j=Row+1;j<Num_rows;j++)
        AddRow(S_minus(TheMatrix[j*Num_cols + Col]),Row,j);
}


MultRow(Row,Factor)
int Row;
Scalar Factor;
{
    Scalar S_mul();

    int j;
    int row_start = Row * Num_cols;

    for (j=0;j<Num_cols;j++)
    {
        TheMatrix[row_start + j] = S_mul(TheMatrix[row_start + j],Factor);
    } 
} 


AddRow(Factor,Row1,Row2)
Scalar Factor;
int Row1,Row2;
{
    Scalar S_zero();
    Scalar S_add();
    Scalar S_mul();

    Scalar before;
    Scalar after;

    int j;
    int row1_start,row2_start;
   
    if (Factor == S_zero())
        return;

    row1_start = Row1 * Num_cols; 
    row2_start = Row2 * Num_cols; 

    for (j=0;j<Num_cols;j++)
	 {
        before = TheMatrix[row2_start+j];
        TheMatrix[row2_start + j] = S_add(TheMatrix[row2_start + j],
                                       S_mul(Factor,TheMatrix[row1_start + j])); 
        if (gather_density_flag)
        {
           after = TheMatrix[row2_start+j];
           if ((before==S_zero()) && (after != S_zero()))
           {
              num_elements++;
              if (num_elements > max_num_elements)
              {
                 max_num_elements=num_elements;
              }
           }
           if ((before!=S_zero()) && (after == S_zero()))
              num_elements--;
       }
	 }
		
		
}
    

/*
int GetRank()
{
    Scalar S_zero();

    int i,j;
    int rank = 0;

    for (i=0;i<Num_rows;i++) {
        for (j=0;j<Num_cols;j++) {
            if (TheMatrix[i*Num_cols + j] != S_zero()) {
                rank++;
                break;
            }
        }
    }
    return(rank);
}

*/

PrintTheRMatrix()
{
    int i,j,k=0;

    printf("The Reduced Matrix is : \n");
    for (i=0;i<Num_rows;i++) {
        for (j=0;j<Num_cols;j++) {
            printf(" %3d",TheMatrix[i*Num_cols + j]);
            k = (k + 1)%25;
            if (k == 0)
                printf("\n");
        }
        k = 0;
        printf("\n");
    }
    printf("\n");
}

/*
main()
{
    void S_init();

    S_init();

    CreateRandomMatrix();
    ReduceTheMatrix();
    PrintTheRMatrix();
    printf("Num_rows = %d Rank = %d\n",Num_rows,GetRank());
}

CreateRandomMatrix()
{
    char *Mymalloc();
    int i,j,k;
    int Num_cells;

    Num_cols = Num_rows = rand() % 10 + 10;
    Num_cells = Num_rows * Num_cols;
    TheMatrix = (Scalar *) (Mymalloc(Num_cells * sizeof(Scalar))); 
    assert_not_null(TheMatrix);
    for (i=0;i<Num_rows;i++)
        for (j=0;j<Num_rows;j++)
            TheMatrix[i*Num_rows + j] = 0;
    for (i=0;i<(Num_rows-4);i++)
            TheMatrix[i*Num_rows +i] = 1;
    PrintTheRMatrix();
    printf("Before reduction: Rank =  %d\n",GetRank());
    for (i=0;i<25;i++) {
        Interchange(rand()%Num_rows,rand()%Num_rows);
        MultRow(rand()%Num_rows,rand()%25);
        AddRow(rand()%25,rand()%Num_rows,rand()%Num_rows);
    }
    return(1);
}

*/