1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /*************************************************************** |
---|
5 | * File: p_Minus_mm_Mult_qq__Template.cc |
---|
6 | * Purpose: template for p_Minus_m_Mult_q |
---|
7 | * Author: obachman (Olaf Bachmann) |
---|
8 | * Created: 8/00 |
---|
9 | * Version: $Id: p_Minus_mm_Mult_qq__T.cc,v 1.7 2008-01-30 18:49:43 wienand Exp $ |
---|
10 | *******************************************************************/ |
---|
11 | |
---|
12 | /*************************************************************** |
---|
13 | * |
---|
14 | * Returns: p - m*q |
---|
15 | * Shorter, where Shorter == Length(p) + Length(q) - Length(p - m*q); |
---|
16 | * Destroys: p |
---|
17 | * Const: m, q |
---|
18 | * |
---|
19 | ***************************************************************/ |
---|
20 | LINKAGE poly p_Minus_mm_Mult_qq(poly p, poly m, poly q, int& Shorter, const poly spNoether, const ring r, poly &last) |
---|
21 | { |
---|
22 | p_Test(p, r); |
---|
23 | p_Test(q, r); |
---|
24 | p_LmTest(m, r); |
---|
25 | |
---|
26 | #if PDEBUG > 0 |
---|
27 | int l_debug = pLength(p) + pLength(q); |
---|
28 | #endif |
---|
29 | |
---|
30 | Shorter = 0; |
---|
31 | // we are done if q == NULL || m == NULL |
---|
32 | if (q == NULL || m == NULL) return p; |
---|
33 | |
---|
34 | spolyrec rp; |
---|
35 | poly a = &rp, // collects the result |
---|
36 | qm = NULL, // stores q*m |
---|
37 | c; // used for temporary storage |
---|
38 | |
---|
39 | |
---|
40 | number tm = pGetCoeff(m), // coefficient of m |
---|
41 | tneg = n_Neg(n_Copy(tm, r), r), // - (coefficient of m) |
---|
42 | tb, // used for tm*coeff(a1) |
---|
43 | tc; // used as intermediate number |
---|
44 | |
---|
45 | |
---|
46 | int shorter = 0; |
---|
47 | DECLARE_LENGTH(const unsigned long length = r->ExpL_Size); |
---|
48 | DECLARE_ORDSGN(const long* ordsgn = r->ordsgn); |
---|
49 | |
---|
50 | const unsigned long* m_e = m->exp; |
---|
51 | omBin bin = r->PolyBin; |
---|
52 | |
---|
53 | if (p == NULL) goto Finish; // return tneg*q if (p == NULL) |
---|
54 | |
---|
55 | pAssume(p_GetComp(q, r) == 0 || p_GetComp(m, r) == 0); |
---|
56 | |
---|
57 | AllocTop: |
---|
58 | p_AllocBin(qm, bin, r); |
---|
59 | SumTop: |
---|
60 | p_MemSum(qm->exp, q->exp, m_e, length); |
---|
61 | p_MemAddAdjust(qm, r); |
---|
62 | |
---|
63 | CmpTop: |
---|
64 | // compare qm = m*q and p w.r.t. monomial ordering |
---|
65 | p_MemCmp(qm->exp, p->exp, length, ordsgn, goto Equal, goto Greater, goto Smaller ); |
---|
66 | |
---|
67 | Equal: // qm equals p |
---|
68 | tb = n_Mult(pGetCoeff(q), tm, r); |
---|
69 | #ifdef HAVE_ZERODIVISORS |
---|
70 | if (!nIsZero(tb)) { |
---|
71 | #endif |
---|
72 | tc = pGetCoeff(p); |
---|
73 | if (!n_Equal(tc, tb, r)) |
---|
74 | { |
---|
75 | shorter++; |
---|
76 | tc = n_Sub(tc, tb, r); |
---|
77 | n_Delete(&(pGetCoeff(p)), r); |
---|
78 | pSetCoeff0(p,tc); // adjust coeff of p |
---|
79 | a = pNext(a) = p; // append p to result and advance p |
---|
80 | pIter(p); |
---|
81 | } |
---|
82 | else |
---|
83 | { // coeffs are equal, so their difference is 0: |
---|
84 | shorter += 2; |
---|
85 | n_Delete(&tc, r); |
---|
86 | p = p_LmFreeAndNext(p, r); |
---|
87 | } |
---|
88 | #ifdef HAVE_ZERODIVISORS |
---|
89 | } |
---|
90 | #endif |
---|
91 | n_Delete(&tb, r); |
---|
92 | pIter(q); |
---|
93 | if (q == NULL || p == NULL) goto Finish; // are we done ? |
---|
94 | // no, so update qm |
---|
95 | goto SumTop; |
---|
96 | |
---|
97 | |
---|
98 | Greater: |
---|
99 | #ifdef HAVE_ZERODIVISORS |
---|
100 | tb = n_Mult(pGetCoeff(q), tneg, r); |
---|
101 | if (!nIsZero(tb)) |
---|
102 | { |
---|
103 | #endif |
---|
104 | pSetCoeff0(qm, n_Mult(pGetCoeff(q), tneg, r)); |
---|
105 | a = pNext(a) = qm; // append qm to result and advance q |
---|
106 | #ifdef HAVE_ZERODIVISORS |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | shorter++; |
---|
111 | } |
---|
112 | n_Delete(&tb, r); |
---|
113 | #endif |
---|
114 | pIter(q); |
---|
115 | if (q == NULL) // are we done? |
---|
116 | { |
---|
117 | qm = NULL; |
---|
118 | goto Finish; |
---|
119 | } |
---|
120 | // construct new qm |
---|
121 | goto AllocTop; |
---|
122 | |
---|
123 | Smaller: |
---|
124 | a = pNext(a) = p;// append p to result and advance p |
---|
125 | pIter(p); |
---|
126 | if (p == NULL) goto Finish; |
---|
127 | goto CmpTop; |
---|
128 | |
---|
129 | |
---|
130 | Finish: // q or p is NULL: Clean-up time |
---|
131 | if (q == NULL) // append rest of p to result |
---|
132 | { |
---|
133 | pNext(a) = p; |
---|
134 | if (p == NULL) last = a; |
---|
135 | } |
---|
136 | else // append (- m*q) to result |
---|
137 | { |
---|
138 | pSetCoeff0(m, tneg); |
---|
139 | last = a; |
---|
140 | if (spNoether != NULL) |
---|
141 | { |
---|
142 | int ll = 0; |
---|
143 | pNext(a) = r->p_Procs->pp_Mult_mm_Noether(q, m, spNoether, ll, r, last); |
---|
144 | shorter += ll; |
---|
145 | } |
---|
146 | else |
---|
147 | pNext(a) = r->p_Procs->pp_Mult_mm(q, m, r, last); |
---|
148 | pSetCoeff0(m, tm); |
---|
149 | } |
---|
150 | |
---|
151 | n_Delete(&tneg, r); |
---|
152 | if (qm != NULL) p_FreeBinAddr(qm, r); |
---|
153 | Shorter = shorter; |
---|
154 | p_Test(pNext(&rp), r); |
---|
155 | return pNext(&rp); |
---|
156 | } |
---|