-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExtractMatrix.cpp
216 lines (177 loc) · 6.99 KB
/
ExtractMatrix.cpp
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
/*******************************************************************/
/*** FILE : ExtractMatrix.c ***/
/*** AUTHOR: David P Jacobs ***/
/*** PROGRAMMER: Sekhar Muddana ***/
/*** DATE WRITTEN: May 1990. ***/
/*** MODIFIED : David Lee (8/92) ***/
/*** - Added code to extract information ***/
/*** from a sparse matrix. ***/
/*** 9/93 - Trent Whiteley ***/
/*** switched variables of type Term_list ***/
/*** to type Term * ***/
/*** PUBLIC ROUTINES: ***/
/*** SparseExtractMatrix() ***/
/*** PRIVATE ROUTINES: ***/
/*** SparseFillDependent() ***/
/*** SparseProcessDependentBasis() ***/
/*** MODULE DESCRIPTION: ***/
/*******************************************************************/
#include <list>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include "ExtractMatrix.h"
#include "Basis_table.h"
#include "Build_defs.h"
#include "CreateMatrix.h"
#include "Memory_routines.h"
#include "Mult_table.h"
#include "Scalar_arithmetic.h"
#include "SparseReduceMatrix.h"
#include "Type_table.h"
static void SparseFillDependent(const SparseMatrix &SM, vector<int> &Dependent);
#if 0
static void PrintDependent(void);
#endif
static void ProcessIndependentBasis(const vector<int> &Dependent, const vector<Unique_basis_pair> &ColtoBP, vector<Basis> &BasisNames);
static void SparseProcessDependentBasis(const SparseMatrix &SM, const vector<Unique_basis_pair> &ColtoBP, vector<Basis> &BasisNames);
static void ProcessOtherIndependentBasis(const vector<Unique_basis_pair> &ColtoBP, int J);
static Type Cur_type;
static Type T1;
static Type T2;
static int Cur_type_degree;
static int Cur_type_len;
static int Num_cols;
static int MatrixRank;
/* Added (8/92) by DCL. This is virtually identical to ExtractFromMatrix()
except for the calls to SparseFillDependent() and SparseProcessDependent-
Basis(). Operation are performed on a locally visible pointer to the
matrix and then that pointer is copied to the one passed in upon exit */
int SparseExtractFromMatrix(const SparseMatrix &SM, int Cols, int Rank, Name N, const vector<Unique_basis_pair> &ColtoBP)
{
Num_cols = Cols;
MatrixRank = Rank;
Cur_type_len = GetTargetLen();
Cur_type = (Type) Mymalloc(Cur_type_len * sizeof(Degree));
assert_not_null(Cur_type);
T1 = (Type) Mymalloc((Cur_type_len + 2) * sizeof(Degree)); /* +2 to avoid buffer overflow */
assert_not_null(T1);
NameToType(N,T1);
NameToType(N,Cur_type);
Cur_type_degree = GetDegree(Cur_type);
T2 = (Type) Mymalloc(Cur_type_len * sizeof(Degree));
assert_not_null(T2);
if (Num_cols > 0 ) {
vector<int> Dependent(Num_cols, 0);
vector<Basis> BasisNames(Num_cols, 0);
SparseFillDependent(SM, Dependent);
ProcessIndependentBasis(Dependent, ColtoBP, BasisNames);
SparseProcessDependentBasis(SM, ColtoBP, BasisNames);
}
ProcessOtherIndependentBasis(ColtoBP, 0);
free(Cur_type);
free(T1);
free(T2);
return(OK);
}
void SparseFillDependent(const SparseMatrix &SM, vector<int> &Dependent)
{
if (SM.empty() || (Num_cols == 0))
return;
/* This routine is much simpler than its sister FillDependent()
since the first node in the linked list of each row will contain
the first nonzero element */
/* place ones in the correct columns of the Dependent structure */
for(int i=0; i < MatrixRank; i++)
{
Dependent[SM[i].begin()->getColumn()] = S_one();
}
}
#if 0
void PrintDependent(void)
{
int j;
if (Num_cols == 0)
return;
printf("The Dependent Array is : \n");
for (j=0;j<Num_cols;j++)
printf("%d",Dependent[j]);
printf("\n");
}
#endif
void ProcessIndependentBasis(const vector<int> &Dependent, const vector<Unique_basis_pair> &ColtoBP, vector<Basis> &BasisNames)
{
if (Num_cols == 0)
return;
vector<pair<Basis, Scalar> > tl(1);
for (int j=0;j<Num_cols;j++) {
if (!Dependent[j]) {
Basis b1 = ColtoBP[j].left_basis;
Basis b2 = ColtoBP[j].right_basis;
Basis n = EnterBasis(b1,b2,TypeToName(Cur_type));
tl[0] = make_pair(n, 1);
EnterProduct(b1, b2, tl);
BasisNames[j] = n;
}
}
}
/* Again this is virtually identical to the sister routine of
ProcessDependentBasis except we get the information from the
sparse matrix structure. Also we do not use the dependent structure
since the first element is each linked list representing a row is
a nonzero element*/
void SparseProcessDependentBasis(const SparseMatrix &SM, const vector<Unique_basis_pair> &ColtoBP, vector<Basis> &BasisNames)
{
if (Num_cols == 0)
return;
vector<pair<Basis, Scalar> > tl;
for(int rowId = 0; rowId < MatrixRank; rowId++) {
const SparseRow &row = SM[rowId];
tl.clear();
SparseRow::const_iterator ii = row.begin();
for(ii++; ii!=row.end(); ii++) {
int k = ii->getColumn();
Scalar S_temp = Get_Matrix_Element(SM, rowId, k);
tl.push_back(make_pair(BasisNames[k], S_minus(S_temp)));
}
int j=row.begin()->getColumn();
Basis b1 = ColtoBP[j].left_basis;
Basis b2 = ColtoBP[j].right_basis;
EnterProduct(b1, b2, tl);
}
}
void ProcessOtherIndependentBasis(const vector<Unique_basis_pair> &ColtoBP, int J)
{
vector<pair<Basis, Scalar> > tl(1);
if (Cur_type_len == J) {
int deg = GetDegree(T1);
if ((deg > 0) && (deg < Cur_type_degree)) {
for (int i=0;i<Cur_type_len;i++)
T2[i] = Cur_type[i] - T1[i];
const Basis m1 = BeginBasis(TypeToName(T1));
const Basis m2 = EndBasis(TypeToName(T1));
const Basis n1 = BeginBasis(TypeToName(T2));
const Basis n2 = EndBasis(TypeToName(T2));
if ((0 < m1) && (m1 <= m2) && (0 < n1) && (n1 <= n2)) {
for (int i=m1;i<=m2;i++) {
for (int j=n1;j<=n2;j++) {
if (GetCol(ColtoBP, i, j) == -1) {
Basis n = EnterBasis(i,j,TypeToName(Cur_type));
tl[0] = make_pair(n, 1);
EnterProduct(i, j, tl);
}
}
}
}
}
}
else {
for (int i=0;i<=Cur_type[J];i++) {
Degree save = T1[i];
T1[J] = i;
ProcessOtherIndependentBasis(ColtoBP, J+1);
T1[i] = save;
}
}
}