[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[341696] | 4 | /* $Id$ */ |
---|
[35aab3] | 5 | |
---|
| 6 | /* |
---|
| 7 | * ABSTRACT - all basic methods to convert polynomials to strings |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /* includes */ |
---|
[599326] | 11 | #include <kernel/mod2.h> |
---|
| 12 | #include <kernel/structs.h> |
---|
| 13 | #include <kernel/numbers.h> |
---|
| 14 | #include <kernel/ring.h> |
---|
| 15 | #include <kernel/p_polys.h> |
---|
| 16 | #include <kernel/febase.h> |
---|
[35aab3] | 17 | |
---|
| 18 | /*2 |
---|
| 19 | * writes a monomial (p), |
---|
| 20 | * uses form x*gen(.) if ko != coloumn number of p |
---|
| 21 | */ |
---|
| 22 | static void writemon(poly p, int ko, ring r) |
---|
| 23 | { |
---|
| 24 | BOOLEAN wroteCoef=FALSE,writeGen=FALSE; |
---|
| 25 | |
---|
| 26 | if (pGetCoeff(p)!=NULL) |
---|
| 27 | n_Normalize(pGetCoeff(p),r); |
---|
| 28 | |
---|
| 29 | if (((p_GetComp(p,r) == (short)ko) |
---|
| 30 | &&(p_LmIsConstantComp(p, r))) |
---|
| 31 | || ((!n_IsOne(pGetCoeff(p),r)) |
---|
| 32 | && (!n_IsMOne(pGetCoeff(p),r)) |
---|
| 33 | ) |
---|
| 34 | ) |
---|
| 35 | { |
---|
| 36 | n_Write(p->coef,r); |
---|
| 37 | wroteCoef=(rShortOut(r) == FALSE ||(r->parameter!=NULL)); |
---|
| 38 | writeGen=TRUE; |
---|
| 39 | } |
---|
| 40 | else if (n_IsMOne(pGetCoeff(p),r)) |
---|
| 41 | { |
---|
| 42 | if (n_GreaterZero(pGetCoeff(p),r)) |
---|
| 43 | { |
---|
| 44 | n_Write(p->coef,r); |
---|
| 45 | wroteCoef=(rShortOut(r) == FALSE ||(r->parameter!=NULL)); |
---|
| 46 | writeGen=TRUE; |
---|
| 47 | } |
---|
| 48 | else |
---|
| 49 | StringAppendS("-"); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | int i; |
---|
| 53 | for (i=0; i<r->N; i++) |
---|
| 54 | { |
---|
| 55 | { |
---|
[27667b] | 56 | long ee = p_GetExp(p,i+1,r); |
---|
| 57 | if (ee!=0L) |
---|
[35aab3] | 58 | { |
---|
| 59 | if (wroteCoef) |
---|
| 60 | StringAppendS("*"); |
---|
| 61 | //else |
---|
| 62 | wroteCoef=(rShortOut(r) == FALSE); |
---|
| 63 | writeGen=TRUE; |
---|
| 64 | StringAppendS(rRingVar(i, r)); |
---|
[27667b] | 65 | if (ee != 1L) |
---|
[35aab3] | 66 | { |
---|
| 67 | if (rShortOut(r)==0) StringAppendS("^"); |
---|
[27667b] | 68 | StringAppend("%ld", ee); |
---|
[35aab3] | 69 | } |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | //StringAppend("{%d}",p->Order); |
---|
[0b5e3d] | 74 | if (p_GetComp(p, r) != (long)ko) |
---|
[35aab3] | 75 | { |
---|
| 76 | if (writeGen) StringAppendS("*"); |
---|
| 77 | StringAppend("gen(%d)", p_GetComp(p, r)); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | char* p_String0(poly p, ring lmRing, ring tailRing) |
---|
| 82 | { |
---|
| 83 | if (p == NULL) |
---|
| 84 | { |
---|
| 85 | return StringAppendS("0"); |
---|
| 86 | } |
---|
| 87 | if ((p_GetComp(p, lmRing) == 0) || (!lmRing->VectorOut)) |
---|
| 88 | { |
---|
| 89 | writemon(p,0, lmRing); |
---|
| 90 | p = pNext(p); |
---|
| 91 | while (p!=NULL) |
---|
| 92 | { |
---|
| 93 | if ((p->coef==NULL)||n_GreaterZero(p->coef,tailRing)) |
---|
| 94 | StringAppendS("+"); |
---|
| 95 | writemon(p,0, tailRing); |
---|
| 96 | p = pNext(p); |
---|
| 97 | } |
---|
| 98 | return StringAppendS(""); |
---|
| 99 | } |
---|
| 100 | |
---|
[0b5e3d] | 101 | long k = 1; |
---|
[35aab3] | 102 | StringAppendS("["); |
---|
| 103 | loop |
---|
| 104 | { |
---|
| 105 | while (k < p_GetComp(p,lmRing)) |
---|
| 106 | { |
---|
| 107 | StringAppendS("0,"); |
---|
| 108 | k++; |
---|
| 109 | } |
---|
| 110 | writemon(p,k,lmRing); |
---|
| 111 | pIter(p); |
---|
| 112 | while ((p!=NULL) && (k == p_GetComp(p, tailRing))) |
---|
| 113 | { |
---|
| 114 | if (n_GreaterZero(p->coef,tailRing)) StringAppendS("+"); |
---|
| 115 | writemon(p,k,tailRing); |
---|
| 116 | pIter(p); |
---|
| 117 | } |
---|
| 118 | if (p == NULL) break; |
---|
| 119 | StringAppendS(","); |
---|
| 120 | k++; |
---|
| 121 | } |
---|
| 122 | return StringAppendS("]"); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | char* p_String(poly p, ring lmRing, ring tailRing) |
---|
| 126 | { |
---|
| 127 | StringSetS(""); |
---|
| 128 | return p_String0(p, lmRing, tailRing); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /*2 |
---|
| 132 | * writes a polynomial p to stdout |
---|
| 133 | */ |
---|
| 134 | void p_Write0(poly p, ring lmRing, ring tailRing) |
---|
| 135 | { |
---|
| 136 | PrintS(p_String(p, lmRing, tailRing)); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /*2 |
---|
| 140 | * writes a polynomial p to stdout followed by \n |
---|
| 141 | */ |
---|
| 142 | void p_Write(poly p, ring lmRing, ring tailRing) |
---|
| 143 | { |
---|
| 144 | p_Write0(p, lmRing, tailRing); |
---|
| 145 | PrintLn(); |
---|
| 146 | } |
---|
| 147 | |
---|
[715f30] | 148 | #if !defined(__OPTIMIZE__) || defined(KDEBUG) |
---|
[35aab3] | 149 | /*2 |
---|
| 150 | *the standard debugging output: |
---|
| 151 | *print the first two monomials of the poly (wrp) or only the lead term (wrp0), |
---|
| 152 | *possibly followed by the string "+..." |
---|
| 153 | */ |
---|
| 154 | void p_wrp0(poly p, ring ri) |
---|
| 155 | { |
---|
| 156 | poly r; |
---|
| 157 | |
---|
| 158 | if (p==NULL) PrintS("NULL"); |
---|
| 159 | else if (pNext(p)==NULL) p_Write0(p, ri); |
---|
| 160 | else |
---|
| 161 | { |
---|
| 162 | r = pNext(p); |
---|
| 163 | pNext(p) = NULL; |
---|
| 164 | p_Write0(p, ri); |
---|
| 165 | if (r!=NULL) |
---|
| 166 | { |
---|
| 167 | PrintS("+..."); |
---|
| 168 | pNext(p) = r; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | } |
---|
[715f30] | 172 | #endif |
---|
[35aab3] | 173 | void p_wrp(poly p, ring lmRing, ring tailRing) |
---|
| 174 | { |
---|
| 175 | poly r; |
---|
| 176 | |
---|
| 177 | if (p==NULL) PrintS("NULL"); |
---|
| 178 | else if (pNext(p)==NULL) p_Write0(p, lmRing); |
---|
| 179 | else |
---|
| 180 | { |
---|
| 181 | r = pNext(pNext(p)); |
---|
| 182 | pNext(pNext(p)) = NULL; |
---|
| 183 | p_Write0(p, lmRing, tailRing); |
---|
| 184 | if (r!=NULL) |
---|
| 185 | { |
---|
| 186 | PrintS("+..."); |
---|
| 187 | pNext(pNext(p)) = r; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | } |
---|