00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00141 #ifndef vs_INCLUDED
00142 #define vs_INCLUDED "vs.h"
00143
00144 const static char *vs_rcs_id = vs_INCLUDED "$Revision$";
00145
00146
00147
00148 #ifndef __STRING_H__
00149 extern "C" {
00150 #include "string.h"
00151 }
00152 #endif
00153
00154
00155
00156 #ifndef defs_INCLUDED
00157 #include "defs.h"
00158 #endif
00159 #ifndef mat_INCLUDED
00160 #include "mat.h"
00161 #endif
00162 #ifndef lu_mat_INCLUDED
00163 #include "lu_mat.h"
00164 #endif
00165
00166 typedef struct mem_pool MEM_POOL;
00167
00168 template<class T>
00169 class VECTOR_SPACE {
00170
00171 public:
00172
00173 INT D() const {return _lud_is_valid ? _lud->LU_Matrix().Cols() : _bv.Rows();}
00174 INT N() const {return _lud_is_valid ? _lud->LU_Matrix().Rows() : _bv.Cols();}
00175
00176
00177 VECTOR_SPACE(INT n, MEM_POOL *pool, BOOL full = FALSE) :
00178 _bv(n,n,pool), _lud(NULL), _lud_is_valid(FALSE), _pool(pool) {
00179 if (full)
00180 _bv.D_Identity();
00181 else
00182 _bv.D_Subtract_Rows(n);
00183 }
00184
00185
00186
00187 VECTOR_SPACE(const LU_MAT<T>& lu, MEM_POOL* pool);
00188
00189
00190 VECTOR_SPACE(const VECTOR_SPACE* vs, MEM_POOL* pool) :
00191 _bv(vs->_bv, pool), _lud(NULL),
00192 _lud_is_valid(vs->_lud_is_valid), _pool(pool) {}
00193
00194 VECTOR_SPACE& operator = (const VECTOR_SPACE& vs);
00195
00196 ~VECTOR_SPACE() {
00197 if (_lud)
00198 CXX_DELETE(_lud, _pool);
00199 }
00200
00201 const MAT<T>& Basis() const { Make_Bv(); return _bv; }
00202 const LU_MAT<T>& Lu() const { Make_Lu(); return *_lud; }
00203
00204 void Print(FILE *f, BOOL = FALSE) const;
00205 BOOL In(const T* w) const;
00206 BOOL Insert(T* w);
00207 VECTOR_SPACE& operator +=(const VECTOR_SPACE&);
00208 VECTOR_SPACE& operator -=(const VECTOR_SPACE&);
00209 VECTOR_SPACE& operator *=(const VECTOR_SPACE&);
00210 MAT<T> Proj_Matrix() const;
00211 void Beautify() const;
00212 void Sanity_Check() const;
00213 static void Reduce_Row(T* row, INT elts);
00214
00215 private:
00216
00217 VECTOR_SPACE(const VECTOR_SPACE&);
00218 VECTOR_SPACE();
00219
00220 MAT<T> _bv;
00221 LU_MAT<T>* _lud;
00222 BOOL _lud_is_valid;
00223 MEM_POOL* _pool;
00224
00225 BOOL Has_Only_Elemetary_Basis_Vectors() const;
00226 void Reduce_Magnitude() const;
00227 static void Print_Element(FILE*, T);
00228 void Make_Bv_Aux() const;
00229 void Make_Lu_Aux() const;
00230 void Make_Bv() const {
00231 if (_lud_is_valid)
00232 ((VECTOR_SPACE*)this)->Make_Bv_Aux();
00233 }
00234 void Make_Lu() const {
00235 if (!_lud_is_valid)
00236 ((VECTOR_SPACE*)this)->Make_Lu_Aux();
00237 }
00238
00239 };
00240
00241 template <class T>
00242 VECTOR_SPACE<T>* Read_VS(const char* cp, T dummy, MEM_POOL* pool);
00243 extern const char* _Skip_Whitespace(const char* cp);
00244
00245 #ifdef __GNUC__
00246
00247
00248
00249 template <class T>
00250 VECTOR_SPACE<T>& VECTOR_SPACE<T>::operator = (const VECTOR_SPACE& vs) {
00251 _bv = vs._bv;
00252 _lud_is_valid = vs._lud_is_valid;
00253 if (_lud)
00254 CXX_DELETE(_lud, _pool);
00255 if (_lud_is_valid)
00256 _lud = CXX_NEW(LU_MAT<T>(*vs._lud, _pool), _pool);
00257 else
00258 _lud = NULL;
00259 return *this;
00260 }
00261
00262 template <class T>
00263 void VECTOR_SPACE<T>::Make_Bv_Aux() const
00264 {
00265 Is_True(_lud_is_valid, ("Bad call to Make_Bv_Aux()"));
00266
00267 ((VECTOR_SPACE<T>*)this)->_bv = _lud->Unfactor().Trans();
00268 ((VECTOR_SPACE<T>*)this)->_lud_is_valid = FALSE;
00269 #ifdef Is_True_On
00270 Sanity_Check();
00271 #endif
00272 }
00273
00274 template <class T>
00275 void VECTOR_SPACE<T>::Make_Lu_Aux() const
00276 {
00277 Is_True(!_lud_is_valid, ("Bad call to Make_Bv_Aux()"));
00278
00279 #ifdef Is_True_On
00280 Sanity_Check();
00281 #endif
00282
00283 if (_lud == NULL)
00284 ((VECTOR_SPACE<T>*)this)->_lud = CXX_NEW(LU_MAT<T>(_bv.Trans(), _pool),
00285 _pool);
00286 else
00287 *((VECTOR_SPACE<T>*)this)->_lud = LU_MAT<T>(_bv.Trans(), &LNO_local_pool);
00288
00289 ((VECTOR_SPACE<T>*)this)->_lud_is_valid = TRUE;
00290 }
00291
00292 template <class T>
00293 void VECTOR_SPACE<T>::Print(FILE *f, BOOL leave_lu) const
00294 {
00295 if (leave_lu && _lud_is_valid) {
00296 _lud->Print(f);
00297 return;
00298 }
00299
00300 Make_Bv();
00301
00302 fprintf(f, "basis vectors: {");
00303 INT i;
00304 for (i = 0; i < _bv.Rows(); i++) {
00305 if (i > 0)
00306 fprintf(f, ",(");
00307 else fprintf (f, "(");
00308 INT j;
00309 for (j = 0; j < _bv.Cols(); j++) {
00310 if (j > 0)
00311 fprintf(f, ",");
00312 Print_Element(f, _bv(i,j));
00313 }
00314 fprintf(f, ")");
00315 }
00316 fprintf(f, "}\n");
00317 }
00318
00319 template <class T>
00320 INT VECTOR_SPACE<T>::In(const T* w) const
00321 {
00322 Make_Lu();
00323
00324 INT r = _lud->LU_Matrix().Rows();
00325 INT c = _lud->LU_Matrix().Cols();
00326
00327
00328 INT i;
00329 for (i = 0; i < r; i++)
00330 if(w[i] != 0)
00331 break;
00332 if (i == r)
00333 return TRUE;
00334
00335
00336 INT curpivrow = 0;
00337 for (i = 0; i < c; i++)
00338 curpivrow += _lud->Is_Pivot(i);
00339 if (curpivrow == r)
00340 return TRUE;
00341
00342 T *hold = CXX_NEW_ARRAY(T, r, &LNO_local_pool);
00343 for (i = 0; i < r; i++)
00344 hold[i] = w[i];
00345
00346 _lud->Cfactor(hold, curpivrow);
00347
00348 BOOL rval = hold[curpivrow] == 0;
00349 CXX_DELETE_ARRAY(hold, &LNO_local_pool);
00350 return rval;
00351 }
00352
00353 template <class T>
00354 BOOL VECTOR_SPACE<T>::Insert(T* w)
00355 {
00356 Make_Lu();
00357 Reduce_Row(w, _bv.Cols());
00358 BOOL inserted = _lud->Cfactor_And_Insert(w, TRUE);
00359 return inserted;
00360 }
00361
00362 template <class T>
00363 VECTOR_SPACE<T>& VECTOR_SPACE<T>::operator +=(const VECTOR_SPACE<T>& v)
00364 {
00365 v.Make_Bv();
00366 const MAT<T>& mat = &v.Basis();
00367 INT i;
00368 for (i = 0; i < mat.Rows(); i++) {
00369 const T* w = &mat(i,0);
00370 Insert(w);
00371 }
00372 return *this;
00373 }
00374
00375 template <class T>
00376 MAT<T> VECTOR_SPACE<T>::Proj_Matrix() const
00377 {
00378
00379
00380 Make_Bv();
00381
00382 MAT<T> mt(_bv);
00383 MAT<T> m = mt.Trans();
00384 MAT<T> mtm = mt * m;
00385 MAT<T> mtm_inv = mtm.Inv();
00386 MAT<T> rv = m * mtm_inv * mt;
00387
00388 return rv;
00389 }
00390
00391 template <class T>
00392 VECTOR_SPACE<T>& VECTOR_SPACE<T>::operator -=(const VECTOR_SPACE<T>& vs)
00393 {
00394 if (D() == 0 || vs.D() == 0)
00395 return *this;
00396
00397 Make_Bv();
00398 MAT<T> pm = vs.Proj_Matrix();
00399
00400 VECTOR_SPACE<T> ret(_bv.Cols(), &LNO_local_pool);
00401
00402 T* space = CXX_NEW_ARRAY(T, vs.N(), &LNO_local_pool);
00403
00404 vs.Make_Bv();
00405 INT i;
00406 for (i = 0; i < D(); i++) {
00407 const T* v = &_bv(i,0);
00408 BOOL non_zero = FALSE;
00409 for (INT j = 0; j < vs.N(); j++) {
00410 space[j] = v[j];
00411 for (INT k = 0; k < vs.N(); k++)
00412 space[j] -= pm(j,k) * v[k];
00413 if (space[j] != 0)
00414 non_zero = TRUE;
00415 }
00416 if (non_zero)
00417 ret.Insert(space);
00418 }
00419
00420 CXX_DELETE_ARRAY(space, &LNO_local_pool);
00421
00422 *this = ret;
00423
00424 return *this;
00425 }
00426
00427 template <class T>
00428 BOOL VECTOR_SPACE<T>::Has_Only_Elemetary_Basis_Vectors() const
00429 {
00430 Make_Bv();
00431 INT i;
00432 for (i = 0; i < _bv.Rows(); i++) {
00433 const T* w = &_bv(i,0);
00434 BOOL seen_one = FALSE;
00435 for (INT j = 0; j < _bv.Cols(); j++) {
00436 if (w[j] == 1) {
00437 if (seen_one == TRUE)
00438 return FALSE;
00439 seen_one = TRUE;
00440 }
00441 else if (w[j] != 0)
00442 return FALSE;
00443 }
00444 FmtAssert(seen_one, ("Zero basis vector"));
00445 }
00446
00447 return TRUE;
00448 }
00449
00450 template <class T>
00451 VECTOR_SPACE<T>& VECTOR_SPACE<T>::operator *=(const VECTOR_SPACE<T>& vs)
00452 {
00453 FmtAssert(N() == vs.N(), ("Illegal intersection %d, %d", N(), vs.N()));
00454
00455 INT n = N();
00456
00457
00458
00459 if (D() == 0)
00460 return *this;
00461 else if (vs.D() == 0)
00462 return *this = vs;
00463
00464 Make_Bv();
00465 vs.Make_Bv();
00466
00467
00468
00469
00470 if (Has_Only_Elemetary_Basis_Vectors() &&
00471 vs.Has_Only_Elemetary_Basis_Vectors()) {
00472 INT i;
00473 for (i = 0; i < _bv.Rows(); ) {
00474 const T* w = &_bv(i,0);
00475 INT j;
00476 for (j = 0; j < _bv.Cols(); j++) {
00477 if (w[j] == 1)
00478 break;
00479 }
00480 FmtAssert(j < _bv.Cols(), ("Bad elementary vector in *this"));
00481 INT ii;
00482 for (ii = 0; ii < vs._bv.Rows(); ii++) {
00483 if (vs._bv(ii,j) == 1)
00484 break;
00485 }
00486 if (ii == vs._bv.Rows()) {
00487 if (i != _bv.Rows()-1)
00488 _bv.D_Swap_Rows(i, _bv.Rows()-1);
00489 _bv.D_Subtract_Rows(1);
00490 }
00491 else
00492 i++;
00493 }
00494 return *this;
00495 }
00496
00497
00498
00499
00500
00501
00502 MAT<T> Q(n, D() + vs.D(), &LNO_local_pool);
00503 INT i;
00504 for (i = 0; i < D(); i++) {
00505 for (INT j = 0; j < n; j++)
00506 Q(j,i) = _bv(i,j);
00507 }
00508 for (i = 0; i < vs.D(); i++) {
00509 INT j;
00510 for (j = 0; j < n; j++)
00511 Q(j,D()+i) = vs._bv(i,j);
00512 }
00513
00514
00515
00516 LU_MAT<T> luQ(Q, &LNO_local_pool);
00517 VECTOR_SPACE<T> kerQ(luQ, &LNO_local_pool);
00518 kerQ.Make_Bv();
00519
00520
00521
00522
00523 VECTOR_SPACE<T> answer(N(), &LNO_local_pool, FALSE);
00524
00525 FRAC* tmp = CXX_NEW_ARRAY(FRAC, n, &LNO_local_pool);
00526 for (i = 0; i < kerQ.D(); i++) {
00527
00528 INT j;
00529 for(j = 0; j < n; j++)
00530 tmp[j] = FRAC(0);
00531 for (INT ii = 0; ii < D(); ii++) {
00532 for (INT j = 0; j < n; j++)
00533 tmp[j] += kerQ._bv(i,ii) * _bv(ii,j);
00534 }
00535 FmtAssert(answer.In(tmp) == FALSE, ("Bug in intersection"));
00536 answer.Insert(tmp);
00537 }
00538 CXX_DELETE_ARRAY(tmp, &LNO_local_pool);
00539
00540 answer.Beautify();
00541 return *this = answer;
00542 }
00543
00544
00545
00546
00547 template <class T>
00548 VECTOR_SPACE<T>::VECTOR_SPACE(const LU_MAT<T>& lu, MEM_POOL *pool) :
00549 _bv(lu.LU_Matrix().Cols(), lu.LU_Matrix().Cols(), pool), _lud(NULL),
00550 _lud_is_valid(FALSE), _pool(pool)
00551 {
00552 T* x = CXX_NEW_ARRAY(T, lu.LU_Matrix().Rows(), &LNO_local_pool);
00553 T* zeros = CXX_NEW_ARRAY(T, lu.LU_Matrix().Rows(), &LNO_local_pool);
00554 T* ans = CXX_NEW_ARRAY(T, lu.LU_Matrix().Cols(), &LNO_local_pool);
00555
00556 _bv.D_Subtract_Rows(_bv.Rows());
00557 INT i;
00558 for (i = 0; i < lu.LU_Matrix().Rows(); i++)
00559 zeros[i] = T(0);
00560 INT f;
00561 for(f = 0; f < lu.LU_Matrix().Cols(); f++) {
00562
00563 if (!lu.Is_Pivot(f)) {
00564 if (!lu.U_Solve(zeros, ans, f))
00565 FmtAssert(0, ("Bad usolve in kernel computation"));
00566 if (!Insert(ans)) {
00567 FmtAssert(0, ("Bad insert in kernel computation"));
00568 }
00569 }
00570 }
00571
00572 CXX_DELETE_ARRAY(x, &LNO_local_pool);
00573 CXX_DELETE_ARRAY(zeros, &LNO_local_pool);
00574 CXX_DELETE_ARRAY(ans, &LNO_local_pool);
00575 }
00576
00577 template <class T>
00578 void VECTOR_SPACE<T>::Beautify() const
00579 {
00580 INT i;
00581 INT j;
00582
00583 T* x = CXX_NEW_ARRAY(T, N(), &LNO_local_pool);
00584
00585 VECTOR_SPACE hold(N(), &LNO_local_pool);
00586
00587
00588
00589
00590
00591 BOOL fine = TRUE;
00592
00593 Make_Bv();
00594 for (i = 0; fine && i < D(); i++) {
00595 INT the_non_zero = -1;
00596 for (j = 0; j < N(); j++) {
00597 if (_bv(i,j) != 0) {
00598 if (the_non_zero == -1)
00599 the_non_zero = j;
00600 else
00601 fine = FALSE;
00602 }
00603 }
00604 if (fine)
00605
00606 (*(MAT<T>*)&_bv)(i,the_non_zero) = T(1);
00607 }
00608 if (fine)
00609 return;
00610
00611
00612
00613 for (j = 0; j < N(); j++) {
00614 for(i = 0; i < N(); i++)
00615 x[i] = T(i==j);
00616 if (In(x))
00617 hold.Insert(x);
00618 }
00619
00620 *(VECTOR_SPACE<T>*)this -= hold;
00621
00622 Make_Bv();
00623 hold.Make_Bv();
00624 for (i = 0; i < hold.D(); i++)
00625 ((VECTOR_SPACE<T>*)this)->_bv.D_Add_Row(&hold._bv(i,0));
00626
00627 CXX_DELETE_ARRAY(x, &LNO_local_pool);
00628
00629 Reduce_Magnitude();
00630 }
00631
00632 #if 0
00633 template <class T>
00634 VECTOR_SPACE<T>* Read_VS(const char* cp, T dummy, MEM_POOL* pool)
00635 {
00636 VECTOR_SPACE<T>* rval = NULL;
00637 INT dim = -1;
00638 INT i;
00639 cp = _Skip_Whitespace(cp);
00640 Is_True(*cp != '{', ("Bad Read input"));
00641 c++;
00642 while ((cp = _Skip_Whitespace(cp)), *cp == '(') {
00643 T entry[32];
00644 cp++;
00645 for (i = 0; i < 32; i++) {
00646 entry[i] = atoi(cp);
00647 while (*cp != ',' && *cp != ')')
00648 cp++;
00649 if (*cp == ')') {
00650 cp++;
00651 while (*cp != '}' && *cp != '(')
00652 cp++;
00653 break;
00654 }
00655 }
00656 if (rval == NULL) {
00657 dim = i;
00658 rval = CXX_NEW(VECTOR_SPACE(i, pool), pool);
00659 }
00660 else {
00661 FmtAssert(dim == i, ("Bug, dim != i"));
00662 }
00663 rval->Insert(entry);
00664 }
00665 FmtAssert(*cp == '}', ("expected '}'"));
00666 }
00667 #endif
00668
00669 template <class T>
00670 void VECTOR_SPACE<T>::Sanity_Check() const
00671 {
00672 Make_Bv();
00673 INT i;
00674 for (i = 0; i < _bv.Rows(); i++) {
00675 INT j;
00676 for (j = 0; j < _bv.Cols(); j++) {
00677 if (_bv(i,j) != 0)
00678 break;
00679 }
00680 FmtAssert(j < _bv.Cols(), ("Sanity check failed vector space!"));
00681 }
00682 }
00683
00684 template <class T>
00685 void VECTOR_SPACE<T>::Reduce_Magnitude() const
00686 {
00687 INT i;
00688
00689 Make_Bv();
00690
00691 for (i = 0; i < _bv.Rows(); i++) {
00692 Reduce_Row((T*)&_bv(i,0), _bv.Cols());
00693 }
00694 }
00695
00696 #endif
00697
00698 #endif