[35aab3] | 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 |
---|
[341696] | 9 | * Version: $Id$ |
---|
[35aab3] | 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 | |
---|
[009d80] | 40 | number tm = pGetCoeff(m), // coefficient of m |
---|
[35aab3] | 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); |
---|
[b12a68] | 69 | #ifdef HAVE_RINGS |
---|
[af378f7] | 70 | if (!nIsZero(tb)) { |
---|
[585bbcb] | 71 | #endif |
---|
[35aab3] | 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 | } |
---|
[b12a68] | 88 | #ifdef HAVE_RINGS |
---|
[585bbcb] | 89 | } |
---|
[a76e11] | 90 | else |
---|
| 91 | { // coeff itself is zero |
---|
| 92 | shorter += 1; |
---|
| 93 | } |
---|
[585bbcb] | 94 | #endif |
---|
[35aab3] | 95 | n_Delete(&tb, r); |
---|
| 96 | pIter(q); |
---|
| 97 | if (q == NULL || p == NULL) goto Finish; // are we done ? |
---|
| 98 | // no, so update qm |
---|
| 99 | goto SumTop; |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | Greater: |
---|
[b12a68] | 103 | #ifdef HAVE_RINGS |
---|
[585bbcb] | 104 | tb = n_Mult(pGetCoeff(q), tneg, r); |
---|
[af378f7] | 105 | if (!nIsZero(tb)) |
---|
[886483] | 106 | { |
---|
[585bbcb] | 107 | #endif |
---|
[009d80] | 108 | pSetCoeff0(qm, n_Mult(pGetCoeff(q), tneg, r)); |
---|
| 109 | a = pNext(a) = qm; // append qm to result and advance q |
---|
[b12a68] | 110 | #ifdef HAVE_RINGS |
---|
[886483] | 111 | } |
---|
| 112 | else |
---|
[009d80] | 113 | { |
---|
| 114 | shorter++; |
---|
| 115 | } |
---|
[585bbcb] | 116 | n_Delete(&tb, r); |
---|
[009d80] | 117 | #endif |
---|
[35aab3] | 118 | pIter(q); |
---|
| 119 | if (q == NULL) // are we done? |
---|
| 120 | { |
---|
| 121 | qm = NULL; |
---|
| 122 | goto Finish; |
---|
| 123 | } |
---|
| 124 | // construct new qm |
---|
| 125 | goto AllocTop; |
---|
| 126 | |
---|
| 127 | Smaller: |
---|
| 128 | a = pNext(a) = p;// append p to result and advance p |
---|
| 129 | pIter(p); |
---|
| 130 | if (p == NULL) goto Finish; |
---|
| 131 | goto CmpTop; |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | Finish: // q or p is NULL: Clean-up time |
---|
| 135 | if (q == NULL) // append rest of p to result |
---|
| 136 | { |
---|
| 137 | pNext(a) = p; |
---|
| 138 | if (p == NULL) last = a; |
---|
| 139 | } |
---|
| 140 | else // append (- m*q) to result |
---|
| 141 | { |
---|
| 142 | pSetCoeff0(m, tneg); |
---|
| 143 | last = a; |
---|
| 144 | if (spNoether != NULL) |
---|
| 145 | { |
---|
| 146 | int ll = 0; |
---|
| 147 | pNext(a) = r->p_Procs->pp_Mult_mm_Noether(q, m, spNoether, ll, r, last); |
---|
| 148 | shorter += ll; |
---|
| 149 | } |
---|
| 150 | else |
---|
[a76e11] | 151 | { |
---|
[35aab3] | 152 | pNext(a) = r->p_Procs->pp_Mult_mm(q, m, r, last); |
---|
[a76e11] | 153 | #ifdef HAVE_RINGS |
---|
| 154 | if (! rField_is_Domain(r)) |
---|
| 155 | { |
---|
| 156 | shorter += pLength(q) - pLength(pNext(a)); |
---|
| 157 | } |
---|
| 158 | #endif |
---|
| 159 | } |
---|
[35aab3] | 160 | pSetCoeff0(m, tm); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | n_Delete(&tneg, r); |
---|
| 164 | if (qm != NULL) p_FreeBinAddr(qm, r); |
---|
| 165 | Shorter = shorter; |
---|
| 166 | p_Test(pNext(&rp), r); |
---|
| 167 | return pNext(&rp); |
---|
| 168 | } |
---|