Changeset a7d970 in git for kernel/tgb.cc
- Timestamp:
- Feb 22, 2007, 11:45:12 AM (17 years ago)
- Branches:
- (u'spielwiese', '17f1d200f27c5bd38f5dfc6e8a0879242279d1d8')
- Children:
- ebe987f7339e431a23e717106ffe22dacf14a1a2
- Parents:
- 490afd567a4f8c21b1a8b7bff89f26c3053d6a38
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/tgb.cc
r490afd5 ra7d970 5 5 * Computer Algebra System SINGULAR * 6 6 ****************************************/ 7 /* $Id: tgb.cc,v 1.14 4 2007-02-21 10:02:47bricken Exp $ */7 /* $Id: tgb.cc,v 1.145 2007-02-22 10:44:59 bricken Exp $ */ 8 8 /* 9 9 * ABSTRACT: slimgb and F4 implementation … … 2217 2217 2218 2218 #ifdef NORO_CACHE 2219 class NoroCacheNode{2220 public:2221 NoroCacheNode** branches;2222 int branches_len;2223 2224 2225 NoroCacheNode(){2226 branches=NULL;2227 branches_len=0;2228 2229 }2230 NoroCacheNode* setNode(int branch, NoroCacheNode* node){2231 if (branch>=branches_len){2232 if (branches==NULL){2233 branches_len=branch+1;2234 branches_len=si_max(branches_len,3);2235 branches=(NoroCacheNode**) omalloc(branches_len*sizeof(NoroCacheNode*));2236 int i;2237 for(i=0;i<branches_len;i++){2238 branches[i]=NULL;2239 }2240 }else{2241 int branches_len_old=branches_len;2242 branches_len=branch+1;2243 branches=(NoroCacheNode**) omrealloc(branches,branches_len*sizeof(NoroCacheNode*));2244 int i;2245 for(i=branches_len_old;i<branches_len;i++){2246 branches[i]=NULL;2247 }2248 }2249 }2250 assume(branches[branch]==NULL);2251 branches[branch]=node;2252 return node;2253 }2254 NoroCacheNode* getBranch(int branch){2255 if (branch<branches_len) return branches[branch];2256 return NULL;2257 }2258 virtual ~NoroCacheNode(){2259 int i;2260 for(i=0;i<branches_len;i++){2261 delete branches[i];2262 }2263 omfree(branches);2264 }2265 NoroCacheNode* getOrInsertBranch(int branch){2266 if ((branch<branches_len)&&(branches[branch]))2267 return branches[branch];2268 else{2269 return setNode(branch,new NoroCacheNode());2270 }2271 }2272 };2273 class DenseRow{2274 public:2275 number* array;2276 int begin;2277 int end;2278 DenseRow(){2279 array=NULL;2280 }2281 ~DenseRow(){2282 omfree(array);2283 }2284 };2285 class SparseRow{2286 public:2287 int* idx_array;2288 number* coef_array;2289 int len;2290 SparseRow(){2291 len=0;2292 idx_array=NULL;2293 coef_array=NULL;2294 }2295 SparseRow(int n){2296 len=n;2297 idx_array=(int*) omalloc(n*sizeof(int));2298 coef_array=(number*) omalloc(n*sizeof(number));2299 }2300 ~SparseRow(){2301 omfree(idx_array);2302 omfree(coef_array);2303 }2304 };2305 2306 class DataNoroCacheNode:public NoroCacheNode{2307 public:2308 2309 int value_len;2310 poly value_poly;2311 #ifdef NORO_SPARSE_ROWS_PRE2312 SparseRow* row;2313 #else2314 DenseRow* row;2315 #endif2316 int term_index;2317 DataNoroCacheNode(poly p, int len){2318 value_len=len;2319 value_poly=p;2320 row=NULL;2321 term_index=-1;2322 }2323 #ifdef NORO_SPARSE_ROWS_PRE2324 DataNoroCacheNode(SparseRow* row){2325 if (row!=NULL)2326 value_len=row->len;2327 else2328 value_len=0;2329 value_poly=NULL;2330 this->row=row;2331 term_index=-1;2332 }2333 #endif2334 ~DataNoroCacheNode(2335 ){2336 //p_Delete(&value_poly,currRing);2337 if (row) delete row;2338 }2339 };2340 class NoroCache{2341 public:2342 poly temp_term;2343 void evaluatePlaceHolder(number* row,std::vector<NoroPlaceHolder>& place_holders);2344 void collectIrreducibleMonomials( std::vector<DataNoroCacheNode*>& res);2345 void collectIrreducibleMonomials(int level, NoroCacheNode* node, std::vector<DataNoroCacheNode*>& res);2346 void evaluateRows();2347 void evaluateRows(int level, NoroCacheNode* node);2348 #ifdef NORO_RED_ARRAY_RESERVER2349 int reserved;2350 poly* recursionPolyBuffer;2351 #endif2352 static const int backLinkCode=-222;2353 DataNoroCacheNode* insert(poly term, poly nf, int len){2354 //assume(impl.find(p_Copy(term,currRing))==impl.end());2355 //assume(len==pLength(nf));2356 assume(npIsOne(p_GetCoeff(term,currRing)));2357 if (term==nf){2358 term=p_Copy(term,currRing);2359 2360 ressources.push_back(term);2361 nIrreducibleMonomials++;2362 return treeInsertBackLink(term);2363 2364 } else{2365 2366 if (nf){2367 //nf=p_Copy(nf,currRing);2368 assume(p_LmCmp(nf,term,currRing)==-1);2369 ressources.push_back(nf);2370 }2371 return treeInsert(term,nf,len);2372 2373 }2374 2375 //impl[term]=std::pair<PolySimple,int> (nf,len);2376 }2377 #ifdef NORO_SPARSE_ROWS_PRE2378 DataNoroCacheNode* insert(poly term, SparseRow* srow){2379 //assume(impl.find(p_Copy(term,currRing))==impl.end());2380 //assume(len==pLength(nf));2381 2382 return treeInsert(term,srow);2383 2384 2385 //impl[term]=std::pair<PolySimple,int> (nf,len);2386 }2387 #endif2388 DataNoroCacheNode* insertAndTransferOwnerShip(poly t, ring r){2389 2390 ressources.push_back(t);2391 DataNoroCacheNode* res=treeInsertBackLink(t);2392 res->term_index=nIrreducibleMonomials;2393 nIrreducibleMonomials++;2394 return res;2395 }2396 poly lookup(poly term, BOOLEAN& succ, int & len);2397 DataNoroCacheNode* getCacheReference(poly term);2398 NoroCache(){2399 buffer=NULL;2400 #ifdef NORO_RED_ARRAY_RESERVER2401 reserved=0;2402 recursionPolyBuffer=(poly*)omalloc(1000000*sizeof(poly));2403 #endif2404 nIrreducibleMonomials=0;2405 nReducibleMonomials=0;2406 temp_term=pOne();2407 }2408 #ifdef NORO_RED_ARRAY_RESERVER2409 poly* reserve(int n){2410 poly* res=recursionPolyBuffer+reserved;2411 reserved+=n;2412 return res;2413 }2414 void free(int n){2415 reserved-=n;2416 }2417 #endif2418 ~NoroCache(){2419 int s=ressources.size();2420 int i;2421 for(i=0;i<s;i++){2422 p_Delete(&ressources[i].impl,currRing);2423 }2424 p_Delete(&temp_term,currRing);2425 #ifdef NORO_RED_ARRAY_RESERVER2426 omfree(recursionPolyBuffer);2427 #endif2428 }2429 2430 int nIrreducibleMonomials;2431 int nReducibleMonomials;2432 protected:2433 DataNoroCacheNode* treeInsert(poly term,poly nf,int len){2434 int i;2435 nReducibleMonomials++;2436 int nvars=pVariables;2437 NoroCacheNode* parent=&root;2438 for(i=1;i<nvars;i++){2439 parent=parent->getOrInsertBranch(p_GetExp(term,i,currRing));2440 }2441 return (DataNoroCacheNode*) parent->setNode(p_GetExp(term,nvars,currRing),new DataNoroCacheNode(nf,len));2442 }2443 #ifdef NORO_SPARSE_ROWS_PRE2444 DataNoroCacheNode* treeInsert(poly term,SparseRow* srow){2445 int i;2446 nReducibleMonomials++;2447 int nvars=pVariables;2448 NoroCacheNode* parent=&root;2449 for(i=1;i<nvars;i++){2450 parent=parent->getOrInsertBranch(p_GetExp(term,i,currRing));2451 }2452 return (DataNoroCacheNode*) parent->setNode(p_GetExp(term,nvars,currRing),new DataNoroCacheNode(srow));2453 }2454 #endif2455 DataNoroCacheNode* treeInsertBackLink(poly term){2456 int i;2457 int nvars=pVariables;2458 NoroCacheNode* parent=&root;2459 for(i=1;i<nvars;i++){2460 parent=parent->getOrInsertBranch(p_GetExp(term,i,currRing));2461 }2462 return (DataNoroCacheNode*) parent->setNode(p_GetExp(term,nvars,currRing),new DataNoroCacheNode(term,backLinkCode));2463 }2464 2465 //@TODO descruct nodes;2466 typedef std::vector<PolySimple> poly_vec;2467 poly_vec ressources;2468 //typedef std::map<PolySimple,std::pair<PolySimple,int> > cache_map;2469 //cache_map impl;2470 NoroCacheNode root;2471 number* buffer;2472 };2473 2219 void NoroCache::evaluateRows(){ 2474 2220 //after that can evaluate placeholders … … 2904 2650 #ifdef NORO_SPARSE_ROWS_PRE 2905 2651 //len input and out: Idea: reverse addition 2652 2906 2653 SparseRow* noro_red_to_non_poly(poly p, int &len, NoroCache* cache,slimgb_alg* c){ 2907 assume(len==pLength(p)); 2908 poly orig_p=p; 2909 if (p==NULL) { 2910 len=0; 2911 return NULL; 2912 } 2913 2914 number zero=npInit(0); 2915 MonRedResNP* mon=(MonRedResNP*) omalloc(len*sizeof(MonRedResNP)); 2916 int i=0; 2917 2918 while(p){ 2919 2920 poly t=p; 2921 pIter(p); 2922 pNext(t)=NULL; 2923 2924 #ifndef NDEBUG 2925 number coef_debug=p_GetCoeff(t,currRing); 2926 #endif 2927 MonRedResNP red=noro_red_mon_to_non_poly(t,cache,c); 2928 mon[i]=red; 2929 i++; 2930 } 2931 2932 assume(i==len); 2933 len=i; 2934 //in the loop before nIrreducibleMonomials increases, so position here is important 2935 number* temp_array=(number*) omalloc(cache->nIrreducibleMonomials*sizeof(number)); 2936 int temp_size=cache->nIrreducibleMonomials; 2937 memset(temp_array,0,sizeof(number)*cache->nIrreducibleMonomials); 2938 for(i=0;i<len;i++){ 2939 MonRedResNP red=mon[i]; 2940 if ((red.ref)){ 2941 if (red.ref->row){ 2942 SparseRow* row=red.ref->row; 2943 number coef=red.coef; 2944 int j; 2945 if (!(npIsOne(coef))){ 2946 for(j=0;j<row->len;j++){ 2947 int idx=row->idx_array[j]; 2948 assume(!(npIsZero(coef))); 2949 assume(!(npIsZero(row->coef_array[j]))); 2950 temp_array[idx]=npAddM(temp_array[idx],npMultM(row->coef_array[j],coef)); 2951 assume(idx<temp_size); 2952 }}else{ 2953 for(j=0;j<row->len;j++){ 2954 int idx=row->idx_array[j]; 2955 temp_array[idx]=npAddM(temp_array[idx],row->coef_array[j]); 2956 assume(idx<temp_size); 2957 } 2958 } 2959 } 2960 else{ 2961 if (red.ref->value_len==NoroCache::backLinkCode){ 2962 temp_array[red.ref->term_index]=npAddM(temp_array[red.ref->term_index],red.coef); 2963 } else { 2964 //PrintS("third case\n"); 2965 } 2966 } 2967 } 2968 } 2969 int non_zeros=0; 2970 for(i=0;i<cache->nIrreducibleMonomials;i++){ 2971 if (!(temp_array[i]==zero)){ 2972 non_zeros++; 2973 } 2974 } 2975 SparseRow* res=new SparseRow(non_zeros); 2976 int pos=0; 2977 for(i=0;i<cache->nIrreducibleMonomials;i++){ 2978 if (!(zero==temp_array[i])){ 2979 2980 res->idx_array[pos]=i; 2981 res->coef_array[pos]=temp_array[i]; 2982 2983 pos++; 2984 } 2985 2986 } 2987 omfree(temp_array); 2988 2989 omfree(mon); 2990 return res; 2654 if (npPrimeM<255){ 2655 return noro_red_to_non_poly_t<unsigned char>(p,len,cache,c); 2656 } else { 2657 if (npPrimeM<65000){ 2658 return noro_red_to_non_poly_t<unsigned short>(p,len,cache,c); 2659 } else{ 2660 return noro_red_to_non_poly_t<unsigned int>(p,len,cache,c); 2661 } 2662 } 2991 2663 } 2992 2664 #endif … … 3015 2687 #endif 3016 2688 2689 2690 2691 2692 3017 2693 #endif 3018 3019 3020 3021 3022 2694 3023 2695 #ifndef NORO_CACHE
Note: See TracChangeset
for help on using the changeset viewer.