Skip to content

Commit 17ad9f1

Browse files
committed
small fixes
1 parent 0c13de0 commit 17ad9f1

3 files changed

Lines changed: 72 additions & 41 deletions

File tree

.vscode/launch.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,35 @@
5858
"preLaunchTask": "make gdb",
5959
"miDebuggerPath": "/usr/bin/gdb"
6060
},
61+
{
62+
"name": "k2tclosure: make all and debug",
63+
"type": "cppdbg",
64+
"request": "launch",
65+
"program": "${fileDirname}/k2tclosure.x",
66+
//"args": [ "-o", "tmp/poli3.2", "-I", "tmp/poli3.ck2.p", "tmp/poli3.ck2"
67+
// "args": [ "-o", "web/cnr80k.txt.2", "web/cnr80k.txt.k2"
68+
"args": [ "-v", "web/cnr80k.k2"
69+
],
70+
"stopAtEntry": true,
71+
"cwd": "${fileDirname}",
72+
"environment": [],
73+
"externalConsole": false,
74+
"MIMode": "gdb",
75+
"setupCommands": [
76+
{
77+
"description": "Enable pretty-printing for gdb",
78+
"text": "-enable-pretty-printing",
79+
"ignoreFailures": true
80+
},
81+
{
82+
"description": "Set Disassembly Flavor to Intel",
83+
"text": "-gdb-set disassembly-flavor intel",
84+
"ignoreFailures": true
85+
}
86+
],
87+
"preLaunchTask": "make gdb",
88+
"miDebuggerPath": "/usr/bin/gdb"
89+
},
6190
{
6291
"name": "k2cpdf: make all and debug",
6392
"type": "cppdbg",

k2ops.c

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,52 @@ static int k2tree_equals_rec(size_t size, const k2mat_t *a, size_t *posa,
9393
if (roota & (1 << k)) {
9494
assert(rootb & (1 << k));
9595
int eqr = k2tree_equals_rec(size/2,a,posa,b,posb);
96-
if(eqr>0) return eqr+1; // a and b are different at level eqr+1
97-
if(eqr < eq) eq = eqr; // keep track of deepest level reached
96+
if(eqr>=0) return eqr+1; // a and b are different at level eqr+1
97+
if(eqr < eq) eq = eqr; // keep track of deepest level reached
9898
}
9999
}
100100
return eq -1; // a: equals to :b, increase depth by one
101101
}
102102
}
103103

104+
// main entry point for matrix equality with limitations, see below:
105+
// check if size x size k2 compressed matrices :a and :b are equal
106+
// if a or b have main_diag_1 or backp!=NULL we cannot say: return INT32_MAX
107+
// if a==b return -d, where d>0 is the number of levels traversed
108+
// if a!=b return the level>=0 containing the first difference
109+
// (first in the sense of the first level encountered in dfs order)
110+
// Note that if a==b we return the number of visited levels negated,
111+
// while if a!=b we return the level of the first difference counting from 0 (root)
112+
// these two values differ by one: if the tree has 2 level (0 and 1) a difference
113+
// can be at level 1 at most, but the number of traversed levels is 2
114+
// :a and :b must be of size at least 2*MMsize but their content can be
115+
// arbitrary: all 0's, all 1's, or generic
116+
// note: here all 0's matrices are considered of depth 1 even if they are empty
117+
// only the tree strucure is considered, not the main diagonal flag or backpointers
118+
// TODO: use k2copy_normalize to compare any pair of matrices
119+
int mequals(size_t size, const k2mat_t *a, const k2mat_t *b)
120+
{
121+
assert(size>=2*MMsize);
122+
assert(a!=NULL && b!=NULL);
123+
if(a->main_diag_1 || b->main_diag_1)
124+
return INT32_MAX; // cannot say
125+
if( (a->backp!=NULL) || (a->backp!=NULL) )
126+
return INT32_MAX; // cannot say
127+
if(k2is_empty(a) && k2is_empty(b))
128+
return -1; // if a==0 && b==0: a==b and one level traversed
129+
else if(k2is_empty(b))
130+
return 0; // if b==0 && a!=0: a!=b and difference at level 0
131+
else if(k2is_empty(a))
132+
return 0; // if a==0 && b!=0: a!=b as above
133+
// a and b are both non-zero, with no backp or main_diag
134+
size_t posa=0,posb=0;
135+
int eq = k2tree_equals_rec(size,a,&posa,b,&posb);
136+
// do extra checks if the matrices are equal
137+
assert(eq>=0 || (posa==k2pos(a) && posb==k2pos(b) && (posa==posb) ) );
138+
return eq;
139+
}
140+
141+
104142
// return number of levels in the k2_tree storing matrix :a
105143
// only the tree structure is considered, not the main_diagonal flag or backpointers
106144
// note: root with no children: 1 level
@@ -177,42 +215,6 @@ void k2copy_normalise(const k2mat_t *a, k2mat_t *b)
177215
}
178216

179217

180-
// main entry point for matrix equality with limitations, see below:
181-
// check if size x size k2 compressed matrices :a and :b are equal
182-
// if a or b have main_diag_1 or backp!=NULL we cannot say: return INT32_MAX
183-
// if a==b return -d, where d>0 is the number of levels traversed
184-
// if a!=b return the level>=0 containing the first difference
185-
// (first in the sense of the first level encountered in dfs order)
186-
// Note that if a==b we return the number of visited levels negated,
187-
// while if a!=b we return the level of the first difference counting from 0 (root)
188-
// these two values differ by one: if the tree has 2 level (0 and 1) a difference
189-
// can be at level 1 at most, but the number of traversed levels is 2
190-
// :a and :b must be of size at least 2*MMsize but their content can be
191-
// arbitrary: all 0's, all 1's, or generic
192-
// note: here all 0's matrices are considered of depth 1 even if they are empty
193-
// only the tree strucure is considered, not the main diagonal flag or backpointers
194-
// TODO: use k2copy_normalize to compare any pair of matrices
195-
int mequals(size_t size, const k2mat_t *a, const k2mat_t *b)
196-
{
197-
assert(size>=2*MMsize);
198-
assert(a!=NULL && b!=NULL);
199-
if(a->main_diag_1 || b->main_diag_1)
200-
return INT32_MAX; // cannot say
201-
if( (a->backp!=NULL) || (a->backp!=NULL) )
202-
return INT32_MAX; // cannot say
203-
if(k2is_empty(a) && k2is_empty(b))
204-
return -1; // if a==0 && b==0: a==b and one level traversed
205-
else if(k2is_empty(b))
206-
return 0; // if b==0 && a!=0: a!=b and difference at level 0
207-
else if(k2is_empty(a))
208-
return 0; // if a==0 && b!=0: a!=b as above
209-
// a and b are both non-zero, with no backp or main_diag
210-
size_t posa=0,posb=0;
211-
int eq = k2tree_equals_rec(size,a,&posa,b,&posb);
212-
// do extra checks if the matrices are equal
213-
assert(eq<0 || (posa==k2pos(a) && posb==k2pos(b) && (posa==posb) ) );
214-
return eq;
215-
}
216218

217219
// add indentity matrix to a
218220
void madd_identity(k2mat_t *a)

k2tclosure.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ int main (int argc, char **argv) {
119119

120120
// init matrix variables (valid for b128 and k2tree)
121121
k2mat_t a=K2MAT_INITIALIZER;
122-
k2mat_t b=a,aIa=a;
122+
k2mat_t aIa=a;
123123

124124
// load first matrix possibly initializing k2 library
125125
mload_from_file(&a, iname1);
@@ -156,16 +156,16 @@ int main (int argc, char **argv) {
156156
assert((p&TSIZEMASK)==a.pos); // low bits contain size of whole matrix
157157
#endif
158158
if(verbose) mshow_stats(&a,"Current matrix",stdout);
159+
k2mat_t b=K2MAT_INITIALIZER;
159160
mmake_pointer(&a,&b);
160161
madd_identity(&b); // b = a + I
161162
if(verbose) printf("Multiplying (A+I)A\n");
162163
mmult(&b,&a,&aIa); // aIa = (a+I)a
163164
if(verbose) printf("Checking if fixed point\n");
164165
int eq = mequals(a.fullsize, &a,&aIa);
165-
printf("mequals: %d\n", eq);
166166
if(eq <0) break;
167167
k2mat_t temp = a; a = aIa; aIa = temp; // swap a and aIa for the next iteration
168-
matrix_free(&aIa);
168+
matrix_free(&aIa);
169169
}
170170
if(verbose) printf("Fixed point reached, stopping\n");
171171
if(verbose) mshow_stats(&aIa,"Transitive closure matrix",stdout);

0 commit comments

Comments
 (0)