@@ -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
218220void madd_identity (k2mat_t * a )
0 commit comments