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 | * Version: $Id: p_Add_q__T.cc,v 1.1.1.1 2003-10-06 12:15:58 Singular Exp $ |
---|
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); |
---|
45 | t = n_Add(n1,n2, r); |
---|
46 | n_Delete(&n1, r); |
---|
47 | n_Delete(&n2, r); |
---|
48 | q = p_LmFreeAndNext(q, r); |
---|
49 | |
---|
50 | if (n_IsZero(t, r)) |
---|
51 | { |
---|
52 | shorter += 2; |
---|
53 | n_Delete(&t, r); |
---|
54 | p = p_LmFreeAndNext(p, r); |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | shorter++; |
---|
59 | pSetCoeff0(p,t); |
---|
60 | a = pNext(a) = p; |
---|
61 | pIter(p); |
---|
62 | } |
---|
63 | if (p==NULL) { pNext(a) = q; goto Finish;} |
---|
64 | if (q==NULL) { pNext(a) = p; goto Finish;} |
---|
65 | goto Top; |
---|
66 | |
---|
67 | Greater: |
---|
68 | a = pNext(a) = p; |
---|
69 | pIter(p); |
---|
70 | if (p==NULL) { pNext(a) = q; goto Finish;} |
---|
71 | goto Top; |
---|
72 | |
---|
73 | Smaller: |
---|
74 | a = pNext(a) = q; |
---|
75 | pIter(q); |
---|
76 | if (q==NULL) { pNext(a) = p; goto Finish;} |
---|
77 | goto Top; |
---|
78 | |
---|
79 | |
---|
80 | Finish: |
---|
81 | Shorter = shorter; |
---|
82 | |
---|
83 | p_Test(pNext(&rp), r); |
---|
84 | #if PDEBUG > 0 |
---|
85 | pAssume1(l - pLength(pNext(&rp)) == Shorter); |
---|
86 | #endif |
---|
87 | return pNext(&rp); |
---|
88 | } |
---|
89 | |
---|