1 | #include "mod2.h" |
---|
2 | #include <omalloc.h> |
---|
3 | #include "p_polys.h" |
---|
4 | #include "prCopy.h" |
---|
5 | #include "ideals.h" |
---|
6 | #include "ring.h" |
---|
7 | #include "febase.h" |
---|
8 | #include "fast_maps.h" |
---|
9 | |
---|
10 | /******************************************************************************* |
---|
11 | ** |
---|
12 | *F maEggt . . . . . . . . . . . . . . . . . . . . . . . . returns extended ggt |
---|
13 | */ |
---|
14 | // return NULL if deg(ggt(m1, m2)) < 2 |
---|
15 | // else return m = ggT(m1, m2) and q1, q2 such that m1 = q1*m m2 = q2*m |
---|
16 | static poly maEggT(const poly m1, const poly m2, poly &q1, poly &q2,const ring r) |
---|
17 | { |
---|
18 | |
---|
19 | int i; |
---|
20 | int dg = 0; |
---|
21 | poly ggt = NULL; |
---|
22 | q1 = p_Init(r); |
---|
23 | q2 = p_Init(r); |
---|
24 | ggt=p_Init(r); |
---|
25 | |
---|
26 | for (i=1;i<=r->N;i++) { |
---|
27 | Exponent_t e1 = p_GetExp(m1, i, r); |
---|
28 | Exponent_t e2 = p_GetExp(m2, i, r); |
---|
29 | if (e1 > 0 && e2 > 0){ |
---|
30 | Exponent_t em = (e1 > e2 ? e2 : e1); |
---|
31 | dg += em; |
---|
32 | p_SetExp(ggt, i, em, r); |
---|
33 | p_SetExp(q1, i, e1 - em, r); |
---|
34 | p_SetExp(q2, i, e2 - em, r); |
---|
35 | } |
---|
36 | else { |
---|
37 | p_SetExp(q1, i, e1, r); |
---|
38 | p_SetExp(q2, i, e2, r); |
---|
39 | } |
---|
40 | } |
---|
41 | if (dg>1) |
---|
42 | { |
---|
43 | p_Setm(ggt, r); |
---|
44 | p_Setm(q1, r); |
---|
45 | p_Setm(q2, r); |
---|
46 | |
---|
47 | |
---|
48 | } |
---|
49 | else { |
---|
50 | p_LmFree(ggt, r); |
---|
51 | p_LmFree(q1, r); |
---|
52 | p_LmFree(q2, r); |
---|
53 | ggt = NULL; |
---|
54 | } |
---|
55 | return ggt; |
---|
56 | } |
---|
57 | |
---|
58 | /******************************************************************** |
---|
59 | ** * |
---|
60 | * maFindBestggT * |
---|
61 | * finds ggT with the highest cost * |
---|
62 | *******************************************************************/ |
---|
63 | |
---|
64 | static mapoly maFindBestggT(mapoly mp, mapoly & choice, mapoly & fp, mapoly & fq,const ring r) |
---|
65 | { |
---|
66 | int ggt_deg = 0; |
---|
67 | poly p = mp->src; |
---|
68 | mapoly iter = choice; |
---|
69 | poly ggT = NULL; |
---|
70 | fp = NULL; |
---|
71 | fq = NULL; |
---|
72 | poly fp_p=NULL; |
---|
73 | poly fq_p=NULL; |
---|
74 | choice=NULL; |
---|
75 | while ((iter != NULL) && (pDeg(iter->src, r) > ggt_deg)) |
---|
76 | { |
---|
77 | // maMonomial_Out(iter, r, NULL); |
---|
78 | poly q1, q2, q; |
---|
79 | |
---|
80 | q = maEggT(p, iter->src, q1, q2,r); |
---|
81 | if (q != NULL) |
---|
82 | { |
---|
83 | assume((q1!=NULL)&&(q2!=NULL)); |
---|
84 | if (pDeg(q,r) > ggt_deg) |
---|
85 | { |
---|
86 | choice=iter; |
---|
87 | if (ggT != NULL) |
---|
88 | { |
---|
89 | p_LmFree(ggT, r); |
---|
90 | p_LmFree(fp_p, r); |
---|
91 | p_LmFree(fq_p, r); |
---|
92 | |
---|
93 | } |
---|
94 | ggt_deg = pDeg(q, r); |
---|
95 | ggT = q; |
---|
96 | fp_p = q1; |
---|
97 | fq_p = q2; |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | p_LmFree(q, r); |
---|
102 | p_LmFree(q1, r); |
---|
103 | p_LmFree(q2, r); |
---|
104 | } |
---|
105 | } |
---|
106 | iter=iter->next; |
---|
107 | } |
---|
108 | if(ggT!=NULL){ |
---|
109 | |
---|
110 | int dq =pTotaldegree(fq_p,r); |
---|
111 | if (dq!=0){ |
---|
112 | fq=maPoly_InsertMonomial(mp, fq_p, r, NULL); |
---|
113 | fp=maPoly_InsertMonomial(mp, fp_p, r, NULL); |
---|
114 | return maPoly_InsertMonomial(mp, ggT, r, NULL); |
---|
115 | } |
---|
116 | else { |
---|
117 | fq=NULL; |
---|
118 | p_LmFree(fq_p, r); |
---|
119 | p_LmFree(ggT, r); |
---|
120 | fp=maPoly_InsertMonomial(mp, fp_p, r, NULL); |
---|
121 | choice->ref++; |
---|
122 | return choice; |
---|
123 | } |
---|
124 | } |
---|
125 | else { |
---|
126 | return NULL; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | /******************************************************************** |
---|
133 | ** * |
---|
134 | * maPoly_Optimize * |
---|
135 | * adds and integrates subexpressions * |
---|
136 | *******************************************************************/ |
---|
137 | |
---|
138 | void maPoly_Optimize(mapoly mpoly, ring src_r){ |
---|
139 | assume(mpoly!=NULL && mpoly->src!=NULL); |
---|
140 | mapoly iter = mpoly; |
---|
141 | mapoly choice; |
---|
142 | mapoly ggT=NULL; |
---|
143 | mapoly fp=NULL; |
---|
144 | mapoly fq=NULL; |
---|
145 | while (iter->next!=NULL){ |
---|
146 | choice=iter->next; |
---|
147 | if ((iter->f1==NULL)){ |
---|
148 | ggT=maFindBestggT(iter, choice, fp, fq,src_r); |
---|
149 | if (choice!=NULL){ |
---|
150 | iter->f1=fp; |
---|
151 | iter->f2=ggT; |
---|
152 | if (fq!=NULL){ |
---|
153 | ggT->ref++; |
---|
154 | choice->f1=fq; |
---|
155 | choice->f2=ggT; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | } |
---|
160 | iter=iter->next; |
---|
161 | } |
---|
162 | |
---|
163 | } |
---|