1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /*************************************************************** |
---|
5 | * File: p_Minus_m_Mult_q__Template.cc |
---|
6 | * Purpose: template for p_Minus_m_Mult_q |
---|
7 | * Author: obachman (Olaf Bachmann) |
---|
8 | * Created: 8/00 |
---|
9 | * Version: $Id: p_Minus_m_Mult_q__Template.cc,v 1.1 2000-08-24 14:42:43 obachman Exp $ |
---|
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 | poly p_Minus_m_Mult_q(poly p, poly m, poly q, int& Shorter, const poly spNoether, const ring r) |
---|
21 | { |
---|
22 | Shorter = 0; |
---|
23 | // we are done if q == NULL || m == NULL |
---|
24 | if (q == NULL || m == NULL) return p; |
---|
25 | |
---|
26 | spolyrec rp; |
---|
27 | poly a = &rp, // collects the result |
---|
28 | qm = NULL, // stores q*m |
---|
29 | c; // used for temporary storage |
---|
30 | |
---|
31 | |
---|
32 | number tm = pGetCoeff(m), // coefficient of m |
---|
33 | tneg = p_nNeg(p_nCopy(tm, r), r), // - (coefficient of m) |
---|
34 | tb, // used for tm*coeff(a1) |
---|
35 | tc; // used as intermediate number |
---|
36 | |
---|
37 | |
---|
38 | int shorter = 0; |
---|
39 | DECLARE_LENGTH(const unsigned long length = r->ExpLSize); |
---|
40 | DECLARE_ORDSGN(const long* ordsgn = r->ordsgn); |
---|
41 | |
---|
42 | const unsigned long* m_e = m->exp.l; |
---|
43 | omBin bin = r->PolyBin; |
---|
44 | |
---|
45 | if (p == NULL) goto Finish; // return tneg*q if (p == NULL) |
---|
46 | |
---|
47 | omTypeAllocBin(poly, qm, bin); |
---|
48 | assume(pGetComp(q) == 0 || pGetComp(m) == 0); |
---|
49 | p_MemAdd(qm->exp.l, q->exp.l, m_e, length); |
---|
50 | |
---|
51 | // MAIN LOOP: |
---|
52 | Top: |
---|
53 | // compare qm = m*q and p w.r.t. monomial ordering |
---|
54 | p_MemCmp(qm->exp.l, p->exp.l, length, ordsgn, goto Equal, goto Greater, goto Smaller ); |
---|
55 | |
---|
56 | Equal: // qm equals p |
---|
57 | |
---|
58 | tb = p_nMult(pGetCoeff(q), tm, r); |
---|
59 | tc = pGetCoeff(p); |
---|
60 | if (!p_nEqual(tc, tb, r)) |
---|
61 | { |
---|
62 | shorter++; |
---|
63 | tc = p_nSub(tc, tb, r); |
---|
64 | p_nDelete(&(pGetCoeff(p)), r); |
---|
65 | pSetCoeff0(p,tc); // adjust coeff of p |
---|
66 | a = pNext(a) = p; // append p to result and advance p |
---|
67 | pIter(p); |
---|
68 | } |
---|
69 | else |
---|
70 | { // coeffs are equal, so their difference is 0: |
---|
71 | shorter += 2; |
---|
72 | p_nDelete(&tc, r); |
---|
73 | FreeAndAdvance(p); |
---|
74 | } |
---|
75 | p_nDelete(&tb, r); |
---|
76 | pIter(q); |
---|
77 | if (q == NULL || p == NULL) goto Finish; // are we done ? |
---|
78 | // no, so update qm |
---|
79 | p_MemAdd(qm->exp.l, q->exp.l, m_e, length); |
---|
80 | goto Top; |
---|
81 | |
---|
82 | |
---|
83 | Greater: |
---|
84 | pSetCoeff0(qm, p_nMult(pGetCoeff(q), tneg, r)); |
---|
85 | a = pNext(a) = qm; // append qm to result and advance q |
---|
86 | pIter(q); |
---|
87 | if (q == NULL) // are we done? |
---|
88 | { |
---|
89 | qm = NULL; |
---|
90 | goto Finish; |
---|
91 | } |
---|
92 | // construct new qm |
---|
93 | omTypeAllocBin(poly, qm, bin); |
---|
94 | p_MemAdd(qm->exp.l, q->exp.l, m_e, length); |
---|
95 | goto Top; |
---|
96 | |
---|
97 | Smaller: |
---|
98 | a = pNext(a) = p;// append p to result and advance p |
---|
99 | pIter(p); |
---|
100 | if (p == NULL) goto Finish;; |
---|
101 | goto Top; |
---|
102 | |
---|
103 | |
---|
104 | Finish: // q or p is NULL: Clean-up time |
---|
105 | |
---|
106 | if (q == NULL) // append rest of p to result |
---|
107 | pNext(a) = p; |
---|
108 | else // append (- m*q) to result |
---|
109 | { |
---|
110 | pSetCoeff0(m, tneg); |
---|
111 | pNext(a) = p_Mult_m(q, m, spNoether, r); |
---|
112 | pSetCoeff0(m, tm); |
---|
113 | } |
---|
114 | |
---|
115 | p_nDelete(&tneg, r); |
---|
116 | if (qm != NULL) omFreeBinAddr(qm); |
---|
117 | Shorter = shorter; |
---|
118 | return pNext(&rp); |
---|
119 | } |
---|