[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[954622] | 4 | /* $Id: polys0.cc,v 1.8 1998-01-05 16:39:27 Singular Exp $ */ |
---|
[32df82] | 5 | |
---|
[0e1846] | 6 | /* |
---|
| 7 | * ABSTRACT - all basic methods to convert polynomials to strings |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /* includes */ |
---|
| 11 | #include "mod2.h" |
---|
| 12 | #include "tok.h" |
---|
| 13 | #include "structs.h" |
---|
| 14 | #include "febase.h" |
---|
| 15 | #include "numbers.h" |
---|
| 16 | #include "ring.h" |
---|
| 17 | #include "ipid.h" |
---|
| 18 | #include "polys.h" |
---|
| 19 | |
---|
[6a6dccc] | 20 | BOOLEAN pVectorOut=TRUE; |
---|
[0e1846] | 21 | /*2 |
---|
| 22 | * writes a monomial (p), |
---|
| 23 | * uses form x*gen(.) if ko != coloumn number of p |
---|
| 24 | */ |
---|
| 25 | static void writemon(poly p, int ko) |
---|
| 26 | { |
---|
| 27 | BOOLEAN wroteCoef=FALSE,writeGen=FALSE; |
---|
| 28 | |
---|
[116a98] | 29 | nNormalize(pGetCoeff(p)); |
---|
| 30 | |
---|
| 31 | if (((pGetComp(p) == (short)ko) |
---|
| 32 | &&(pIsConstantComp(p))) |
---|
[da97958] | 33 | || ((!nIsOne(pGetCoeff(p))) |
---|
[116a98] | 34 | && (!nIsMOne(pGetCoeff(p))) |
---|
| 35 | ) |
---|
[0e1846] | 36 | #ifdef DRING |
---|
| 37 | || (pDRING && pdIsConstantComp(p)) |
---|
| 38 | #endif |
---|
| 39 | ) |
---|
| 40 | { |
---|
[da97958] | 41 | nWrite(p->coef); |
---|
| 42 | wroteCoef=((pShortOut==0)||(currRing->parameter!=NULL)); |
---|
| 43 | writeGen=TRUE; |
---|
| 44 | } |
---|
| 45 | else if (nIsMOne(pGetCoeff(p))) |
---|
| 46 | { |
---|
| 47 | StringAppendS("-"); |
---|
[0e1846] | 48 | } |
---|
[da97958] | 49 | |
---|
| 50 | int i; |
---|
[0e1846] | 51 | for (i=0; i<pVariables; i++) |
---|
| 52 | { |
---|
| 53 | #ifdef DRING |
---|
| 54 | if ((!pDRING)||(i!=2*pdN)) |
---|
| 55 | #endif |
---|
| 56 | { |
---|
[51c163] | 57 | Exponent_t ee = pGetExp(p,i+1); |
---|
[0e1846] | 58 | if (ee!=0) |
---|
| 59 | { |
---|
| 60 | if (wroteCoef) |
---|
| 61 | StringAppendS("*"); |
---|
| 62 | //else |
---|
| 63 | wroteCoef=(pShortOut==0); |
---|
| 64 | writeGen=TRUE; |
---|
| 65 | #ifdef DRING |
---|
| 66 | if((pDRING)&&(pdN<=i)&&(i<2*pdN)&&(pdDFlag(p)==0)) |
---|
| 67 | { |
---|
| 68 | StringAppendS(RingVar(i-pdN)); |
---|
| 69 | ee=-ee; |
---|
| 70 | } |
---|
| 71 | else |
---|
| 72 | StringAppendS(RingVar(i)); |
---|
| 73 | #else |
---|
| 74 | StringAppendS(RingVar(i)); |
---|
| 75 | #endif |
---|
| 76 | if (ee != 1) |
---|
| 77 | { |
---|
| 78 | if (pShortOut==0) StringAppendS("^"); |
---|
| 79 | StringAppend("%d", ee); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | } |
---|
[954622] | 84 | //StringAppend("{%d}",p->Order); |
---|
[51c163] | 85 | if (pGetComp(p) != (Exponent_t)ko) |
---|
[0e1846] | 86 | { |
---|
| 87 | if (writeGen) StringAppendS("*"); |
---|
[116a98] | 88 | StringAppend("gen(%d)", pGetComp(p)); |
---|
[0e1846] | 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | char* pString0(poly p) |
---|
| 93 | { |
---|
| 94 | if (p == NULL) |
---|
| 95 | { |
---|
| 96 | return StringAppendS("0"); |
---|
| 97 | } |
---|
[51c163] | 98 | if ((pGetComp(p) == 0) || (!pVectorOut)) |
---|
[0e1846] | 99 | { |
---|
| 100 | writemon(p,0); |
---|
| 101 | p = pNext(p); |
---|
| 102 | while (p!=NULL) |
---|
| 103 | { |
---|
| 104 | if ((p->coef==NULL)||nGreaterZero(p->coef)) |
---|
| 105 | StringAppendS("+"); |
---|
| 106 | writemon(p,0); |
---|
| 107 | p = pNext(p); |
---|
| 108 | } |
---|
| 109 | return StringAppendS(""); |
---|
| 110 | } |
---|
| 111 | |
---|
[51c163] | 112 | Exponent_t k = 1; |
---|
[0e1846] | 113 | StringAppendS("["); |
---|
| 114 | loop |
---|
| 115 | { |
---|
[51c163] | 116 | while (k < pGetComp(p)) |
---|
[0e1846] | 117 | { |
---|
| 118 | StringAppendS("0,"); |
---|
| 119 | k++; |
---|
| 120 | } |
---|
| 121 | writemon(p,k); |
---|
[da97958] | 122 | pIter(p); |
---|
[51c163] | 123 | while ((p!=NULL) && (k == pGetComp(p))) |
---|
[0e1846] | 124 | { |
---|
| 125 | if (nGreaterZero(p->coef)) StringAppendS("+"); |
---|
| 126 | writemon(p,k); |
---|
[da97958] | 127 | pIter(p); |
---|
[0e1846] | 128 | } |
---|
| 129 | if (p == NULL) break; |
---|
| 130 | StringAppendS(","); |
---|
| 131 | k++; |
---|
| 132 | } |
---|
| 133 | return StringAppendS("]"); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | char* pString(poly p) |
---|
| 137 | { |
---|
| 138 | StringSetS(""); |
---|
| 139 | return pString0(p); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /*2 |
---|
| 143 | * writes a polynomial p to stdout |
---|
| 144 | */ |
---|
| 145 | void pWrite0(poly p) |
---|
| 146 | { |
---|
[da97958] | 147 | PrintS(pString(p)); |
---|
[0e1846] | 148 | } |
---|
| 149 | |
---|
| 150 | /*2 |
---|
| 151 | * writes a polynomial p to stdout followed by \n |
---|
| 152 | */ |
---|
| 153 | void pWrite(poly p) |
---|
| 154 | { |
---|
| 155 | pWrite0(p); |
---|
| 156 | PrintLn(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /*2 |
---|
| 160 | *the standard debugging output: |
---|
| 161 | *print the first two monomials of the poly, |
---|
| 162 | *possibly followed by the string "+..." |
---|
| 163 | */ |
---|
| 164 | void wrp(poly p) |
---|
| 165 | { |
---|
| 166 | poly r; |
---|
| 167 | |
---|
| 168 | if (p==NULL) PrintS("NULL"); |
---|
| 169 | else if (pNext(p)==NULL) pWrite0(p); |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | r = pNext(pNext(p)); |
---|
| 173 | pNext(pNext(p)) = NULL; |
---|
| 174 | pWrite0(p); |
---|
| 175 | if (r!=NULL) |
---|
| 176 | { |
---|
| 177 | PrintS("+..."); |
---|
| 178 | pNext(pNext(p)) = r; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | } |
---|