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 | |
---|
26 | // test for trivial cases |
---|
27 | Shorter = 0; |
---|
28 | if (q == NULL) return p; |
---|
29 | if (p == NULL) return q; |
---|
30 | |
---|
31 | number t, n1, n2; |
---|
32 | int shorter = 0; |
---|
33 | spolyrec rp; |
---|
34 | poly a = &rp; |
---|
35 | DECLARE_LENGTH(const unsigned long length = r->CmpL_Size); |
---|
36 | DECLARE_ORDSGN(const long* ordsgn = r->ordsgn); |
---|
37 | |
---|
38 | Top: // compare p and q w.r.t. monomial ordering |
---|
39 | p_MemCmp__T(p->exp, q->exp, length, ordsgn, goto Equal, goto Greater , goto Smaller); |
---|
40 | |
---|
41 | Equal: |
---|
42 | n1 = pGetCoeff(p); |
---|
43 | n2 = pGetCoeff(q); |
---|
44 | #if 0 |
---|
45 | t = n_Add__T(n1,n2, r); |
---|
46 | n_Delete__T(&n1, r); |
---|
47 | #else |
---|
48 | n_InpAdd__T(n1,n2,r); |
---|
49 | t = n1; |
---|
50 | #endif |
---|
51 | n_Delete__T(&n2, r); |
---|
52 | q = p_LmFreeAndNext(q, r); |
---|
53 | |
---|
54 | if (n_IsZero__T(t, r)) |
---|
55 | { |
---|
56 | shorter += 2; |
---|
57 | n_Delete__T(&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 | |
---|