[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[da97958] | 4 | /* $Id: polys0.cc,v 1.4 1997-04-12 16:04:43 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 | |
---|
| 20 | int pVectorOut=TRUE; |
---|
| 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 | |
---|
| 29 | if ((pIsConstantComp(p)) |
---|
[da97958] | 30 | || ((!nIsOne(pGetCoeff(p))) |
---|
| 31 | && (!nIsMOne(pGetCoeff(p))) |
---|
| 32 | ) |
---|
[0e1846] | 33 | #ifdef DRING |
---|
| 34 | || (pDRING && pdIsConstantComp(p)) |
---|
| 35 | #endif |
---|
| 36 | ) |
---|
| 37 | { |
---|
[da97958] | 38 | nWrite(p->coef); |
---|
| 39 | wroteCoef=((pShortOut==0)||(currRing->parameter!=NULL)); |
---|
| 40 | writeGen=TRUE; |
---|
| 41 | } |
---|
| 42 | else if (nIsMOne(pGetCoeff(p))) |
---|
| 43 | { |
---|
| 44 | StringAppendS("-"); |
---|
[0e1846] | 45 | } |
---|
[da97958] | 46 | |
---|
| 47 | int i; |
---|
[0e1846] | 48 | for (i=0; i<pVariables; i++) |
---|
| 49 | { |
---|
| 50 | #ifdef DRING |
---|
| 51 | if ((!pDRING)||(i!=2*pdN)) |
---|
| 52 | #endif |
---|
| 53 | { |
---|
| 54 | short ee = p->exp[i+1]; |
---|
| 55 | if (ee!=0) |
---|
| 56 | { |
---|
| 57 | if (wroteCoef) |
---|
| 58 | StringAppendS("*"); |
---|
| 59 | //else |
---|
| 60 | wroteCoef=(pShortOut==0); |
---|
| 61 | writeGen=TRUE; |
---|
| 62 | #ifdef DRING |
---|
| 63 | if((pDRING)&&(pdN<=i)&&(i<2*pdN)&&(pdDFlag(p)==0)) |
---|
| 64 | { |
---|
| 65 | StringAppendS(RingVar(i-pdN)); |
---|
| 66 | ee=-ee; |
---|
| 67 | } |
---|
| 68 | else |
---|
| 69 | StringAppendS(RingVar(i)); |
---|
| 70 | #else |
---|
| 71 | StringAppendS(RingVar(i)); |
---|
| 72 | #endif |
---|
| 73 | if (ee != 1) |
---|
| 74 | { |
---|
| 75 | if (pShortOut==0) StringAppendS("^"); |
---|
| 76 | StringAppend("%d", ee); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | if (p->exp[0] != (short)ko) |
---|
| 82 | { |
---|
| 83 | if (writeGen) StringAppendS("*"); |
---|
| 84 | StringAppend("gen(%d)", p->exp[0]); |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | char* pString0(poly p) |
---|
| 89 | { |
---|
| 90 | if (p == NULL) |
---|
| 91 | { |
---|
| 92 | return StringAppendS("0"); |
---|
| 93 | } |
---|
| 94 | if ((p->exp[0] == 0) || (!pVectorOut)) |
---|
| 95 | { |
---|
| 96 | writemon(p,0); |
---|
| 97 | p = pNext(p); |
---|
| 98 | while (p!=NULL) |
---|
| 99 | { |
---|
| 100 | if ((p->coef==NULL)||nGreaterZero(p->coef)) |
---|
| 101 | StringAppendS("+"); |
---|
| 102 | writemon(p,0); |
---|
| 103 | p = pNext(p); |
---|
| 104 | } |
---|
| 105 | return StringAppendS(""); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | short k = 1; |
---|
| 109 | StringAppendS("["); |
---|
| 110 | loop |
---|
| 111 | { |
---|
| 112 | while (k < p->exp[0]) |
---|
| 113 | { |
---|
| 114 | StringAppendS("0,"); |
---|
| 115 | k++; |
---|
| 116 | } |
---|
| 117 | writemon(p,k); |
---|
[da97958] | 118 | pIter(p); |
---|
[0e1846] | 119 | while ((p!=NULL) && (k == p->exp[0])) |
---|
| 120 | { |
---|
| 121 | if (nGreaterZero(p->coef)) StringAppendS("+"); |
---|
| 122 | writemon(p,k); |
---|
[da97958] | 123 | pIter(p); |
---|
[0e1846] | 124 | } |
---|
| 125 | if (p == NULL) break; |
---|
| 126 | StringAppendS(","); |
---|
| 127 | k++; |
---|
| 128 | } |
---|
| 129 | return StringAppendS("]"); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | char* pString(poly p) |
---|
| 133 | { |
---|
| 134 | StringSetS(""); |
---|
| 135 | return pString0(p); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /*2 |
---|
| 139 | * writes a polynomial p to stdout |
---|
| 140 | */ |
---|
| 141 | void pWrite0(poly p) |
---|
| 142 | { |
---|
[da97958] | 143 | PrintS(pString(p)); |
---|
[0e1846] | 144 | } |
---|
| 145 | |
---|
| 146 | /*2 |
---|
| 147 | * writes a polynomial p to stdout followed by \n |
---|
| 148 | */ |
---|
| 149 | void pWrite(poly p) |
---|
| 150 | { |
---|
| 151 | pWrite0(p); |
---|
| 152 | PrintLn(); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /*2 |
---|
| 156 | *the standard debugging output: |
---|
| 157 | *print the first two monomials of the poly, |
---|
| 158 | *possibly followed by the string "+..." |
---|
| 159 | */ |
---|
| 160 | void wrp(poly p) |
---|
| 161 | { |
---|
| 162 | poly r; |
---|
| 163 | |
---|
| 164 | if (p==NULL) PrintS("NULL"); |
---|
| 165 | else if (pNext(p)==NULL) pWrite0(p); |
---|
| 166 | else |
---|
| 167 | { |
---|
| 168 | r = pNext(pNext(p)); |
---|
| 169 | pNext(pNext(p)) = NULL; |
---|
| 170 | pWrite0(p); |
---|
| 171 | if (r!=NULL) |
---|
| 172 | { |
---|
| 173 | PrintS("+..."); |
---|
| 174 | pNext(pNext(p)) = r; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|