[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /*************************************************************** |
---|
| 5 | * File: p_Add_q__Template.cc |
---|
| 6 | * Purpose: template for p_Add_q |
---|
| 7 | * Author: obachman (Olaf Bachmann) |
---|
| 8 | * Created: 8/00 |
---|
[341696] | 9 | * Version: $Id$ |
---|
[35aab3] | 10 | *******************************************************************/ |
---|
| 11 | |
---|
| 12 | /*************************************************************** |
---|
| 13 | * |
---|
| 14 | * Returns: p + q, |
---|
| 15 | * Shorter, where Shorter == Length(p) + Length(q) - Length(p+q); |
---|
| 16 | * Destroys: p, q |
---|
| 17 | * |
---|
| 18 | ***************************************************************/ |
---|
| 19 | LINKAGE poly p_Add_q(poly p, poly q, int &Shorter, const ring r) |
---|
| 20 | { |
---|
| 21 | p_Test(p, r); |
---|
| 22 | p_Test(q, r); |
---|
| 23 | #if PDEBUG > 0 |
---|
| 24 | int l = pLength(p) + pLength(q); |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | // test for trivial cases |
---|
| 28 | Shorter = 0; |
---|
| 29 | if (q == NULL) return p; |
---|
| 30 | if (p == NULL) return q; |
---|
| 31 | |
---|
| 32 | number t, n1, n2; |
---|
| 33 | int shorter = 0; |
---|
| 34 | spolyrec rp; |
---|
| 35 | poly a = &rp; |
---|
| 36 | DECLARE_LENGTH(const unsigned long length = r->CmpL_Size); |
---|
| 37 | DECLARE_ORDSGN(const long* ordsgn = r->ordsgn); |
---|
| 38 | |
---|
| 39 | Top: // compare p and q w.r.t. monomial ordering |
---|
| 40 | p_MemCmp(p->exp, q->exp, length, ordsgn, goto Equal, goto Greater , goto Smaller); |
---|
| 41 | |
---|
| 42 | Equal: |
---|
| 43 | n1 = pGetCoeff(p); |
---|
| 44 | n2 = pGetCoeff(q); |
---|
[18057e] | 45 | #if 0 |
---|
| 46 | t = n_Add(n1,n2, r); |
---|
| 47 | n_Delete(&n1, r); |
---|
| 48 | #else |
---|
[e8a0aa] | 49 | t=n_InpAdd(n1,n2,r); |
---|
[18057e] | 50 | #endif |
---|
[35aab3] | 51 | n_Delete(&n2, r); |
---|
| 52 | q = p_LmFreeAndNext(q, r); |
---|
| 53 | |
---|
| 54 | if (n_IsZero(t, r)) |
---|
| 55 | { |
---|
| 56 | shorter += 2; |
---|
| 57 | n_Delete(&t, r); |
---|
| 58 | p = p_LmFreeAndNext(p, r); |
---|
| 59 | } |
---|
| 60 | else |
---|
| 61 | { |
---|
| 62 | shorter++; |
---|
| 63 | pSetCoeff0(p,t); |
---|
| 64 | a = pNext(a) = p; |
---|
| 65 | pIter(p); |
---|
| 66 | } |
---|
| 67 | if (p==NULL) { pNext(a) = q; goto Finish;} |
---|
| 68 | if (q==NULL) { pNext(a) = p; goto Finish;} |
---|
| 69 | goto Top; |
---|
| 70 | |
---|
| 71 | Greater: |
---|
| 72 | a = pNext(a) = p; |
---|
| 73 | pIter(p); |
---|
| 74 | if (p==NULL) { pNext(a) = q; goto Finish;} |
---|
| 75 | goto Top; |
---|
| 76 | |
---|
| 77 | Smaller: |
---|
| 78 | a = pNext(a) = q; |
---|
| 79 | pIter(q); |
---|
| 80 | if (q==NULL) { pNext(a) = p; goto Finish;} |
---|
| 81 | goto Top; |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | Finish: |
---|
| 85 | Shorter = shorter; |
---|
| 86 | |
---|
| 87 | p_Test(pNext(&rp), r); |
---|
| 88 | #if PDEBUG > 0 |
---|
| 89 | pAssume1(l - pLength(pNext(&rp)) == Shorter); |
---|
| 90 | #endif |
---|
| 91 | return pNext(&rp); |
---|
| 92 | } |
---|
| 93 | |
---|