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 |
---|
9 | *******************************************************************/ |
---|
10 | |
---|
11 | /*************************************************************** |
---|
12 | * |
---|
13 | * Returns: p + q, |
---|
14 | * Shorter, where Shorter == Length(p) + Length(q) - Length(p+q); |
---|
15 | * Destroys: p, q |
---|
16 | * |
---|
17 | ***************************************************************/ |
---|
18 | LINKAGE poly p_Add_q__T(poly p, poly q, int &Shorter, const ring r) |
---|
19 | { |
---|
20 | p_Test(p, r); |
---|
21 | p_Test(q, r); |
---|
22 | #if PDEBUG > 0 |
---|
23 | int l = pLength(p) + pLength(q); |
---|
24 | #endif |
---|
25 | assume(p!=NULL && q!=NULL); |
---|
26 | |
---|
27 | Shorter = 0; |
---|
28 | |
---|
29 | number t, n1, n2; |
---|
30 | int shorter = 0; |
---|
31 | spolyrec rp; |
---|
32 | poly a = &rp; |
---|
33 | DECLARE_LENGTH(const unsigned long length = r->CmpL_Size); |
---|
34 | DECLARE_ORDSGN(const long* ordsgn = r->ordsgn); |
---|
35 | |
---|
36 | Top: // compare p and q w.r.t. monomial ordering |
---|
37 | p_MemCmp__T(p->exp, q->exp, length, ordsgn, goto Equal, goto Greater , goto Smaller); |
---|
38 | |
---|
39 | Equal: |
---|
40 | n1 = pGetCoeff(p); |
---|
41 | n2 = pGetCoeff(q); |
---|
42 | n_InpAdd__T(n1,n2,r->cf); |
---|
43 | t = n1; |
---|
44 | n_Delete__T(&n2, r->cf); |
---|
45 | q = p_LmFreeAndNext(q, r); |
---|
46 | |
---|
47 | if (n_IsZero__T(t, r->cf)) |
---|
48 | { |
---|
49 | shorter += 2; |
---|
50 | n_Delete__T(&t, r->cf); |
---|
51 | p = p_LmFreeAndNext(p, r); |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | shorter++; |
---|
56 | pSetCoeff0(p,t); |
---|
57 | a = pNext(a) = p; |
---|
58 | pIter(p); |
---|
59 | } |
---|
60 | if (p==NULL) { pNext(a) = q; goto Finish;} |
---|
61 | if (q==NULL) { pNext(a) = p; goto Finish;} |
---|
62 | goto Top; |
---|
63 | |
---|
64 | Greater: |
---|
65 | a = pNext(a) = p; |
---|
66 | pIter(p); |
---|
67 | if (p==NULL) { pNext(a) = q; goto Finish;} |
---|
68 | goto Top; |
---|
69 | |
---|
70 | Smaller: |
---|
71 | a = pNext(a) = q; |
---|
72 | pIter(q); |
---|
73 | if (q==NULL) { pNext(a) = p; goto Finish;} |
---|
74 | goto Top; |
---|
75 | |
---|
76 | |
---|
77 | Finish: |
---|
78 | Shorter = shorter; |
---|
79 | |
---|
80 | p_Test(pNext(&rp), r); |
---|
81 | #if PDEBUG > 0 |
---|
82 | pAssume1(l - pLength(pNext(&rp)) == Shorter); |
---|
83 | #endif |
---|
84 | return pNext(&rp); |
---|
85 | } |
---|
86 | |
---|