Esempio n. 1
0
/*
  	#] Includes : 
  	#[ FactorIn :

	This routine tests for a factor in a dollar expression.

	Note that unlike with regular active or hidden expressions we cannot
	add memory as easily as dollars are rather volatile.
*/
int FactorIn(PHEAD WORD *term, WORD level)
{
	GETBIDENTITY
	WORD *t, *tstop, *m, *mm, *oldwork, *mstop, *n1, *n2, *n3, *n4, *n1stop, *n2stop;
	WORD *r1, *r2, *r3, *r4, j, k, kGCD, kGCD2, kLCM, jGCD, kkLCM, jLCM, size;
	UWORD *GCDbuffer, *GCDbuffer2, *LCMbuffer, *LCMb, *LCMc;
	int fromwhere = 0, i;
	DOLLARS d;
	t = term; GETSTOP(t,tstop); t++;
	while ( ( t < tstop ) && ( *t != FACTORIN || ( ( *t == FACTORIN )
	 && ( t[FUNHEAD] != -DOLLAREXPRESSION || t[1] != FUNHEAD+2 ) ) ) ) t += t[1];
	if ( t >= tstop ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Internal error. Could not find proper factorin_ function.");
		MUNLOCK(ErrorMessageLock);
		return(-1);
	}
	oldwork = AT.WorkPointer;
	d = Dollars + t[FUNHEAD+1];
#ifdef WITHPTHREADS
	{
		int nummodopt, dtype = -1;
		if ( AS.MultiThreaded ) {
			for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
				if ( t[FUNHEAD+1] == ModOptdollars[nummodopt].number ) break;
			}
			if ( nummodopt < NumModOptdollars ) {
				dtype = ModOptdollars[nummodopt].type;
				if ( dtype == MODLOCAL ) {
					d = ModOptdollars[nummodopt].dstruct+AT.identity;
				}
			}
		}
	}
#endif

	if ( d->type == DOLTERMS ) {
		fromwhere = 1;
	}
	else if ( ( d = DolToTerms(BHEAD t[FUNHEAD+1]) ) == 0 ) {
/*
		The variable cannot convert to an expression
		We replace the function by 1.
*/
		m = oldwork; n1 = term;
		while ( n1 < t ) *m++ = *n1++;
		n1 = t + t[1]; tstop = term + *term;
		while ( n1 < tstop ) *m++ = *n1++;
		*oldwork = m - oldwork;
		AT.WorkPointer = m;
		if ( Generator(BHEAD oldwork,level) ) return(-1);
		AT.WorkPointer = oldwork;
		return(0);
	}
	if ( d->where[0] == 0 ) {
		if ( fromwhere == 0 ) {
			if ( d->factors ) M_free(d->factors,"Dollar factors");
			M_free(d,"Dollar in FactorIn_");
		}
		return(0);
	}
/*
	Now we have an expression in d->where. Find the symbolic factor that
	divides the expression and the numerical factor that makes all
	coefficients integer.

	For the symbolic factor we make a copy of the first term, and then
	go through all terms, scratching in the copy the objects that do not
	occur in the terms.
*/
	m = oldwork;
	mm = d->where;
	k = *mm - ABS((mm[*mm-1]));
	for ( j = 0; j < k; j++ ) *m++ = *mm++;
	mstop = m;
	*oldwork = k;
/*
	The copy is in place. Now search through the terms. Start at the second term
*/
	mm = d->where + d->where[0];
	while ( *mm ) {
		m = oldwork+1;
		r2 = mm+*mm;
		r2 -= ABS(r2[-1]);
		r1 = mm+1;
		while ( m < mstop ) {
			while ( r1 < r2 ) {
				if ( *r1 != *m ) {
					r1 += r1[1]; continue;
				}
/*
				Now the various cases
			#[ SYMBOL :
*/
				if ( *m == SYMBOL ) {
					n1 = m+2; n1stop = m+m[1];
					n2stop = r1+r1[1];
					while ( n1 < n1stop ) {
						n2 = r1+2;
						while ( n2 < n2stop ) {
							if ( *n1 != *n2 ) { n2 += 2; continue; }
							if ( n1[1] > 0 ) {
								if ( n2[1] < 0 ) { n2 += 2; continue; }
								if ( n2[1] < n1[1] ) n1[1] = n2[1];
							}
							else {
								if ( n2[1] > 0 ) { n2 += 2; continue; }
								if ( n2[1] > n1[1] ) n1[1] = n2[1];
							}
							break;
						}
						if ( n2 >= n2stop ) {	/* scratch symbol */
							if ( m[1] == 4 ) goto scratch;
							m[1] -= 2;
							n3 = n1; n4 = n1+2;
							while ( n4 < mstop ) *n3++ = *n4++;
							*oldwork = n3 - oldwork;
							mstop -= 2; n1stop -= 2;
							continue;
						}
						n1 += 2;
					}
					break;
				}
/*
			#] SYMBOL : 
			#[ DOTPRODUCT :
*/
				else if ( *m == DOTPRODUCT ) {
					n1 = m+2; n1stop = m+m[1];
					n2stop = r1+r1[1];
					while ( n1 < n1stop ) {
						n2 = r1+2;
						while ( n2 < n2stop ) {
							if ( *n1 != *n2 || n1[1] != n2[1] ) { n2 += 3; continue; }
							if ( n1[2] > 0 ) {
								if ( n2[2] < 0 ) { n2 += 3; continue; }
								if ( n2[2] < n1[2] ) n1[2] = n2[2];
							}
							else {
								if ( n2[2] > 0 ) { n2 += 3; continue; }
								if ( n2[2] > n1[2] ) n1[2] = n2[2];
							}
							break;
						}
						if ( n2 >= n2stop ) {	/* scratch symbol */
							if ( m[1] == 5 ) goto scratch;
							m[1] -= 3;
							n3 = n1; n4 = n1+3;
							while ( n4 < mstop ) *n3++ = *n4++;
							*oldwork = n3 - oldwork;
							mstop -= 3; n1stop -= 3;
							continue;
						}
						n1 += 3;
					}
					break;
				}
/*
			#] DOTPRODUCT : 
			#[ VECTOR :
*/
				else if ( *m == VECTOR ) {
/*
					Here we have to be careful if there is more than
					one of the same
*/
					n1 = m+2; n1stop = m+m[1];
					n2 = r1+2;n2stop = r1+r1[1];
					while ( n1 < n1stop ) {
						while ( n2 < n2stop ) {
							if ( *n1 == *n2 && n1[1] == n2[1] ) {
								n2 += 2; goto nextn1;
							}
							n2 += 2;
						}
						if ( n2 >= n2stop ) {	/* scratch symbol */
							if ( m[1] == 4 ) goto scratch;
							m[1] -= 2;
							n3 = n1; n4 = n1+2;
							while ( n4 < mstop ) *n3++ = *n4++;
							*oldwork = n3 - oldwork;
							mstop -= 2; n1stop -= 2;
							continue;
						}
						n2 = r1+2;
nextn1:					n1 += 2;
					}
					break;
				}
/*
			#] VECTOR : 
			#[ REMAINDER :
*/
				else {
/*
					Again: watch for multiple occurrences of the same object
*/
					if ( m[1] != r1[1] ) { r1 += r1[1]; continue; }
					for ( j = 2; j < m[1]; j++ ) {
						if ( m[j] != r1[j] ) break;
					}
					if ( j < m[1] ) { r1 += r1[1]; continue; }
					r1 += r1[1]; /* to restart at the next potential match */
					goto nextm;  /* match */
				}
/*
			#] REMAINDER : 
*/
			}
            if ( r1 >= r2 ) { /* no factor! */
scratch:;
				r3 = m + m[1]; r4 = m;
				while ( r3 < mstop ) *r4++ = *r3++;
				*oldwork = r4 - oldwork;
				if ( *oldwork == 1 ) goto nofactor;
				mstop = r4;
				r1 = mm + 1;
				continue;
			}
			r1 = mm + 1;
nextm:		m += m[1];
		}
		mm = mm + *mm;
	}

nofactor:;
/*
	For the coefficient we have to determine the LCM of the denominators
	and the GCD of the numerators.
*/
	GCDbuffer = NumberMalloc("FactorIn"); GCDbuffer2 = NumberMalloc("FactorIn");
	LCMbuffer = NumberMalloc("FactorIn"); LCMb = NumberMalloc("FactorIn"); LCMc = NumberMalloc("FactorIn");
	r1 = d->where;
/*
	First take the first term to load up the LCM and the GCD
*/
	r2 = r1 + *r1;
	j = r2[-1];
	r3 = r2 - ABS(j);
	k = REDLENG(j);
	if ( k < 0 ) k = -k;
	while ( ( k > 1 ) && ( r3[k-1] == 0 ) ) k--;
	for ( kGCD = 0; kGCD < k; kGCD++ ) GCDbuffer[kGCD] = r3[kGCD];
	k = REDLENG(j);
	if ( k < 0 ) k = -k;
	r3 += k;
	while ( ( k > 1 ) && ( r3[k-1] == 0 ) ) k--;
	for ( kLCM = 0; kLCM < k; kLCM++ ) LCMbuffer[kLCM] = r3[kLCM];
	r1 = r2;
/*
	Now go through the rest of the terms in this dollar buffer.
*/
	while ( *r1 ) {
		r2 = r1 + *r1;
		j = r2[-1];
		r3 = r2 - ABS(j);
		k = REDLENG(j);
		if ( k < 0 ) k = -k;
		while ( ( k > 1 ) && ( r3[k-1] == 0 ) ) k--;
		if ( ( ( GCDbuffer[0] == 1 ) && ( kGCD == 1 ) ) ) {
/*
			GCD is already 1
*/
		}
		else if ( ( ( k != 1 ) || ( r3[0] != 1 ) ) ) {
			if ( GcdLong(BHEAD GCDbuffer,kGCD,(UWORD *)r3,k,GCDbuffer2,&kGCD2) ) {
				goto onerror;
			}
			kGCD = kGCD2;
			for ( i = 0; i < kGCD; i++ ) GCDbuffer[i] = GCDbuffer2[i];
		}
		else {
			kGCD = 1; GCDbuffer[0] = 1;
		}
		k = REDLENG(j);
		if ( k < 0 ) k = -k;
		r3 += k;
		while ( ( k > 1 ) && ( r3[k-1] == 0 ) ) k--;
		if ( ( ( LCMbuffer[0] == 1 ) && ( kLCM == 1 ) ) ) {
			for ( kLCM = 0; kLCM < k; kLCM++ )
				LCMbuffer[kLCM] = r3[kLCM];
		}
		else if ( ( k != 1 ) || ( r3[0] != 1 ) ) {
			if ( GcdLong(BHEAD LCMbuffer,kLCM,(UWORD *)r3,k,LCMb,&kkLCM) ) {
				goto onerror;
			}
			DivLong((UWORD *)r3,k,LCMb,kkLCM,LCMb,&kkLCM,LCMc,&jLCM);
			MulLong(LCMbuffer,kLCM,LCMb,kkLCM,LCMc,&jLCM);
			for ( kLCM = 0; kLCM < jLCM; kLCM++ )
				LCMbuffer[kLCM] = LCMc[kLCM];
		}
		else {} /* LCM doesn't change */
		r1 = r2;
	}
/*
	Now put the factor together: GCD/LCM
*/
	r3 = (WORD *)(GCDbuffer);
	if ( kGCD == kLCM ) {
		for ( jGCD = 0; jGCD < kGCD; jGCD++ )
			r3[jGCD+kGCD] = LCMbuffer[jGCD];
		k = kGCD;
	}
	else if ( kGCD > kLCM ) {
		for ( jGCD = 0; jGCD < kLCM; jGCD++ )
			r3[jGCD+kGCD] = LCMbuffer[jGCD];
		for ( jGCD = kLCM; jGCD < kGCD; jGCD++ )
			r3[jGCD+kGCD] = 0;
		k = kGCD;
	}
	else {
		for ( jGCD = kGCD; jGCD < kLCM; jGCD++ )
			r3[jGCD] = 0;
		for ( jGCD = 0; jGCD < kLCM; jGCD++ )
			r3[jGCD+kLCM] = LCMbuffer[jGCD];
		k = kLCM;
	}
	j = 2*k+1;
	mm = m = oldwork + oldwork[0];
/*
	Now compose the new term
*/
	n1 = term;
	while ( n1 < t ) *m++ = *n1++;
	n1 += n1[1];
	n2 = oldwork+1;
	while ( n2 < mm ) *m++ = *n2++;
	while ( n1 < tstop ) *m++ = *n1++;
/*
	And the coefficient
*/
	size = term[*term-1];
	size = REDLENG(size);
	if ( MulRat(BHEAD (UWORD *)tstop,size,(UWORD *)r3,k,
								(UWORD *)m,&size) ) goto onerror;
	size = INCLENG(size);
	k = size < 0 ? -size: size;
	m[k-1] = size;
	m += k;
	*mm = (WORD)(m - mm);
	AT.WorkPointer = m;
	if ( Generator(BHEAD mm,level) ) goto onerror;
	AT.WorkPointer = oldwork;
	if ( fromwhere == 0 ) {
		if ( d->factors ) M_free(d->factors,"Dollar factors");
		M_free(d,"Dollar in FactorIn");
	}
	NumberFree(GCDbuffer,"FactorIn"); NumberFree(GCDbuffer2,"FactorIn");
	NumberFree(LCMbuffer,"FactorIn"); NumberFree(LCMb,"FactorIn"); NumberFree(LCMc,"FactorIn");
	return(0);
onerror:
	AT.WorkPointer = oldwork;
	MLOCK(ErrorMessageLock);
	MesCall("FactorIn");
	MUNLOCK(ErrorMessageLock);
	NumberFree(GCDbuffer,"FactorIn"); NumberFree(GCDbuffer2,"FactorIn");
	NumberFree(LCMbuffer,"FactorIn"); NumberFree(LCMb,"FactorIn"); NumberFree(LCMc,"FactorIn");
	return(-1);
}
Esempio n. 2
0
File: if.c Progetto: Sumith1896/form
WORD DoIfStatement(PHEAD WORD *ifcode, WORD *term)
{
	GETBIDENTITY
	WORD *ifstop, *ifp;
	UWORD *coef1 = 0, *coef2, *coef3, *cc;
	WORD ncoef1, ncoef2, ncoef3, i = 0, first, *r, acoef, ismul1, ismul2, j;
	UWORD *Spac1, *Spac2;
	ifstop = ifcode + ifcode[1];
	ifp = ifcode + 3;
	if ( ifp >= ifstop ) return(1);
	if ( ( ifp + ifp[1] ) >= ifstop ) {
		switch ( *ifp ) {
			case LONGNUMBER:
				if ( ifp[2] ) return(1);
				else return(0);
			case MATCH:
			case TYPEIF:
				if ( HowMany(BHEAD ifp,term) ) return(1);
				else return(0);
			case TYPEFINDLOOP:
				if ( Lus(term,ifp[3],ifp[4],ifp[5],ifp[6],ifp[2]) ) return(1);
				else return(0);
			case TYPECOUNT:
				if ( CountDo(term,ifp) ) return(1);
				else return(0);
			case COEFFI:
			case MULTIPLEOF:
				return(1);
			case IFDOLLAR:
				{
					DOLLARS d = Dollars + ifp[2];
#ifdef WITHPTHREADS
					int nummodopt, dtype = -1;
					if ( AS.MultiThreaded ) {
						for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
							if ( ifp[2] == ModOptdollars[nummodopt].number ) break;
						}
						if ( nummodopt < NumModOptdollars ) {
							dtype = ModOptdollars[nummodopt].type;
							if ( dtype == MODLOCAL ) {
								d = ModOptdollars[nummodopt].dstruct+AT.identity;
							}
						}
					}
					dtype = d->type;
#else
					int dtype = d->type;  /* We use dtype to make the operation atomic */
#endif
					if ( dtype == DOLZERO ) return(0);
					if ( dtype == DOLUNDEFINED ) {
						if ( AC.UnsureDollarMode == 0 ) {
							MesPrint("$%s is undefined",AC.dollarnames->namebuffer+d->name);
							Terminate(-1);
						}
					}
				}
				return(1);
			case IFEXPRESSION:
				r = ifp+2; j = ifp[1] - 2;
				while ( --j >= 0 ) {
					if ( *r == AR.CurExpr ) return(1);
					r++;
				}
				return(0);
			case IFISFACTORIZED:
				r = ifp+2; j = ifp[1] - 2;
				if ( j == 0 ) {
					if ( ( Expressions[AR.CurExpr].vflags & ISFACTORIZED ) != 0 )
						return(1);
					else
						return(0);
				}
				while ( --j >= 0 ) {
					if ( ( Expressions[*r].vflags & ISFACTORIZED ) == 0 ) return(0);
					r++;
				}
				return(1);
			case IFOCCURS:
				{
					WORD *OccStop = ifp + ifp[1];
					ifp += 2;
					while ( ifp < OccStop ) {
						if ( FindVar(ifp,term) == 1 ) return(1);
						if ( *ifp == DOTPRODUCT ) ifp += 3;
						else ifp += 2;
					}
				}
				return(0);
			default:
/*
				Now we have a subexpression. Test first for one with a single item.
*/
				if ( ifp[3] == ( ifp[1] + 3 ) ) return(DoIfStatement(BHEAD ifp,term));
				ifstop = ifp + ifp[1];
				ifp += 3;
				break;
		}
	}
/*
	Here is the composite condition.
*/
	coef3 = NumberMalloc("DoIfStatement");
	Spac1 = NumberMalloc("DoIfStatement");
	Spac2 = (UWORD *)(TermMalloc("DoIfStatement"));
	ncoef1 = 0; first = 1; ismul1 = 0;
	do {
		if ( !first ) {
			ifp += 2;
			if ( ifp[-2] == ORCOND && ncoef1 ) {
				coef1 = Spac1;
				ncoef1 = 1; coef1[0] = coef1[1] = 1;
				goto SkipCond;
			}
			if ( ifp[-2] == ANDCOND && !ncoef1 ) goto SkipCond;
		}
		coef2 = Spac2;
		ncoef2 = 1;
		ismul2 = 0;
		switch ( *ifp ) {
			case LONGNUMBER:
				ncoef2 = ifp[2];
				j = 2*(ABS(ncoef2));
				cc = (UWORD *)(ifp + 3);
				for ( i = 0; i < j; i++ ) coef2[i] = cc[i];
				break;
			case MATCH:
			case TYPEIF:
				coef2[0] = HowMany(BHEAD ifp,term);
				coef2[1] = 1;
				if ( coef2[0] == 0 ) ncoef2 = 0;
				break;
			case TYPECOUNT:
				acoef = CountDo(term,ifp);
				coef2[0] = ABS(acoef);
				coef2[1] = 1;
				if ( acoef == 0 ) ncoef2 = 0;
				else if ( acoef < 0 ) ncoef2 = -1;
				break;
			case TYPEFINDLOOP:
				acoef = Lus(term,ifp[3],ifp[4],ifp[5],ifp[6],ifp[2]);
				coef2[0] = ABS(acoef);
				coef2[1] = 1;
				if ( acoef == 0 ) ncoef2 = 0;
				else if ( acoef < 0 ) ncoef2 = -1;
				break;
			case COEFFI:
				r = term + *term;
				ncoef2 = r[-1];
				i = ABS(ncoef2);
				cc = (UWORD *)(r - i);
				if ( ncoef2 < 0 ) ncoef2 = (ncoef2+1)>>1;
				else ncoef2 = (ncoef2-1)>>1;
				i--; for ( j = 0; j < i; j++ ) coef2[j] = cc[j];
				break;
			case SUBEXPR:
				ncoef2 = coef2[0] = DoIfStatement(BHEAD ifp,term);
				coef2[1] = 1;
				break;
			case MULTIPLEOF:
				ncoef2 = 1;
				coef2[0] = ifp[2];
				coef2[1] = 1;
				ismul2 = 1;
				break;
			case IFDOLLAREXTRA:
				break;
			case IFDOLLAR:
				{
/*
					We need to abstract a long rational in coef2
					with length ncoef2. What if that cannot be done?
*/
					DOLLARS d = Dollars + ifp[2];
#ifdef WITHPTHREADS
					int nummodopt, dtype = -1;
					if ( AS.MultiThreaded ) {
						for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
							if ( ifp[2] == ModOptdollars[nummodopt].number ) break;
						}
						if ( nummodopt < NumModOptdollars ) {
							dtype = ModOptdollars[nummodopt].type;
							if ( dtype == MODLOCAL ) {
								d = ModOptdollars[nummodopt].dstruct+AT.identity;
							}
							else {
								LOCK(d->pthreadslockread);
							}
						}
					}
#endif
/*
					We have to pick up the IFDOLLAREXTRA pieces for [1], [$y] etc.
*/
					if ( ifp+3 < ifstop && ifp[3] == IFDOLLAREXTRA ) {
						if ( d->nfactors == 0 ) {
							MLOCK(ErrorMessageLock);
							MesPrint("Attempt to use a factor of an unfactored $-variable");
							MUNLOCK(ErrorMessageLock);
							Terminate(-1);
						} {
						WORD num = GetIfDollarNum(ifp+3,ifstop);
						WORD *w;
						while ( ifp+3 < ifstop && ifp[3] == IFDOLLAREXTRA ) ifp += 3;
						if ( num > d->nfactors ) {
							MLOCK(ErrorMessageLock);
							MesPrint("Dollar factor number %s out of range",num);
							MUNLOCK(ErrorMessageLock);
							Terminate(-1);
						}
						if ( num == 0 ) {
							ncoef2 = 1; coef2[0] = d->nfactors; coef2[1] = 1;
							break;
						}
						w = d->factors[num-1].where;
						if ( w == 0 ) {
							if ( d->factors[num-1].value < 0 ) {
								ncoef2 = -1; coef2[0] = -d->factors[num-1].value; coef2[1] = 1;
							}
							else {
								ncoef2 = 1; coef2[0] = d->factors[num-1].value; coef2[1] = 1;
							}
							break;
						}
						if ( w[*w] == 0 ) {
							r = w + *w - 1;
							i = ABS(*r);
							if ( i == ( *w-1 ) ) {
								ncoef2 = (i-1)/2;
								if ( *r < 0 ) ncoef2 = -ncoef2;
								i--; cc = coef2; r = w + 1;
								while ( --i >= 0 ) *cc++ = (UWORD)(*r++);
								break;
							}
						}
						goto generic;
						}
					}
					else {
					  switch ( d->type ) {
						case DOLUNDEFINED:
							if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
								if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
								MLOCK(ErrorMessageLock);
								MesPrint("$%s is undefined",AC.dollarnames->namebuffer+d->name);
								MUNLOCK(ErrorMessageLock);
								Terminate(-1);
							}
							ncoef2 = 0; coef2[0] = 0; coef2[1] = 1;
							break;
						case DOLZERO:
							ncoef2 = coef2[0] = 0; coef2[1] = 1;
							break;
						case DOLSUBTERM:
							if ( d->where[0] != INDEX || d->where[1] != 3
							|| d->where[2] < 0 || d->where[2] >= AM.OffsetIndex ) {
								if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
									if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
									MLOCK(ErrorMessageLock);
									MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
									MUNLOCK(ErrorMessageLock);
									Terminate(-1);
								}
								ncoef2 = 0; coef2[0] = 0; coef2[1] = 1;
								break;
							}
							d->index = d->where[2];
						case DOLINDEX:
							if ( d->index == 0 ) {
								ncoef2 = coef2[0] = 0; coef2[1] = 1;
							}
							else if ( d->index > 0 && d->index < AM.OffsetIndex ) {
								ncoef2 = 1; coef2[0] = d->index; coef2[1] = 1;
							}
							else if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
								if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
								MLOCK(ErrorMessageLock);
								MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
								MUNLOCK(ErrorMessageLock);
								Terminate(-1);
							}
							ncoef2 = coef2[0] = 0; coef2[1] = 1;
							break;
						case DOLWILDARGS:
							if ( d->where[0] <= -FUNCTION ||
							( d->where[0] < 0 && d->where[2] != 0 )
							|| ( d->where[0] > 0 && d->where[d->where[0]] != 0 )
							) {
								if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
									if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
									MLOCK(ErrorMessageLock);
									MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
									MUNLOCK(ErrorMessageLock);
									Terminate(-1);
								}
								ncoef2 = coef2[0] = 0; coef2[1] = 1;
								break;
							}
						case DOLARGUMENT:
							if ( d->where[0] == -SNUMBER ) {
								if ( d->where[1] == 0 ) {
									ncoef2 = coef2[0] = 0;
								}
								else if ( d->where[1] < 0 ) {
									ncoef2 = -1;
									coef2[0] = -d->where[1];
								}
								else {
									ncoef2 = 1;
									coef2[0] = d->where[1];
								}
								coef2[1] = 1;
							}
							else if ( d->where[0] == -INDEX
							&& d->where[1] >= 0 && d->where[1] < AM.OffsetIndex ) {
								if ( d->where[1] == 0 ) {
									ncoef2 = coef2[0] = 0; coef2[1] = 1;
								}
								else {
									ncoef2 = 1; coef2[0] = d->where[1];
									coef2[1] = 1;
								}
							}
							else if ( d->where[0] > 0
							&& d->where[ARGHEAD] == (d->where[0]-ARGHEAD)
							&& ABS(d->where[d->where[0]-1]) ==
										(d->where[0] - ARGHEAD-1) ) {
								i = d->where[d->where[0]-1];
								ncoef2 = (ABS(i)-1)/2;
								if ( i < 0 ) { ncoef2 = -ncoef2; i = -i; }
								i--; cc = coef2; r = d->where + ARGHEAD+1;
								while ( --i >= 0 ) *cc++ = (UWORD)(*r++);
							}
							else {
								if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
									if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
									MLOCK(ErrorMessageLock);
									MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
									MUNLOCK(ErrorMessageLock);
									Terminate(-1);
								}
								ncoef2 = 0; coef2[0] = 0; coef2[1] = 1;
							}
							break;
   						case DOLNUMBER:
						case DOLTERMS:
							if ( d->where[d->where[0]] == 0 ) {
								r = d->where + d->where[0]-1;
								i = ABS(*r);
								if ( i == ( d->where[0]-1 ) ) {
									ncoef2 = (i-1)/2;
									if ( *r < 0 ) ncoef2 = -ncoef2;
									i--; cc = coef2; r = d->where + 1;
									while ( --i >= 0 ) *cc++ = (UWORD)(*r++);
									break;
								}
							}
generic:;
							if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
								if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
								MLOCK(ErrorMessageLock);
								MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
								MUNLOCK(ErrorMessageLock);
								Terminate(-1);
							}
							ncoef2 = 0; coef2[0] = 0; coef2[1] = 1;
							break;
					  }
					}
#ifdef WITHPTHREADS
					if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
				}
				break;
			case IFEXPRESSION:
				r = ifp+2; j = ifp[1] - 2; ncoef2 = 0;
				while ( --j >= 0 ) {
					if ( *r == AR.CurExpr ) { ncoef2 = 1; break; }
					r++;
				}
				coef2[0] = ncoef2;
				coef2[1] = 1;
				break;
			case IFISFACTORIZED:
				r = ifp+2; j = ifp[1] - 2;
				if ( j == 0 ) {
					ncoef2 = 0;
					if ( ( Expressions[AR.CurExpr].vflags & ISFACTORIZED ) != 0 ) {
						ncoef2 = 1;
					}
				}
				else {
					ncoef2 = 1;
					while ( --j >= 0 ) {
						if ( ( Expressions[*r].vflags & ISFACTORIZED ) == 0 ) {
							ncoef2 = 0;
							break;
						}
						r++;
					}
				}
				coef2[0] = ncoef2;
				coef2[1] = 1;
				break;
			case IFOCCURS:
				{
					WORD *OccStop = ifp + ifp[1], *ifpp = ifp+2;
					ncoef2 = 0;
					while ( ifpp < OccStop ) {
						if ( FindVar(ifpp,term) == 1 ) {
							ncoef2 = 1; break;
						}
						if ( *ifpp == DOTPRODUCT ) ifp += 3;
						else ifpp += 2;
					}
					coef2[0] = ncoef2;
					coef2[1] = 1;
				}
				break;
			default:
				break;
		}
		if ( !first ) {
			if ( ifp[-2] != ORCOND && ifp[-2] != ANDCOND ) {
				if ( ( ifp[-2] == EQUAL || ifp[-2] == NOTEQUAL ) &&
				( ismul2 || ismul1 ) ) {
					if ( ismul1 && ismul2 ) {
						if ( coef1[0] == coef2[0] ) i = 1;
						else i = 0;
					}
					else {
						if ( ismul1 ) {
							if ( ncoef2 )
								Divvy(BHEAD coef2,&ncoef2,coef1,ncoef1);
							cc = coef2; ncoef3 = ncoef2;
						}
						else {
							if ( ncoef1 )
								Divvy(BHEAD coef1,&ncoef1,coef2,ncoef2);
							cc = coef1; ncoef3 = ncoef1;
						}
						if ( ncoef3 < 0 ) ncoef3 = -ncoef3;
						if ( ncoef3 == 0 ) {
							if ( ifp[-2] == EQUAL ) i = 1;
							else i = 0;
						}
						else if ( cc[ncoef3] != 1 ) {
							if ( ifp[-2] == EQUAL ) i = 0;
							else i = 1;
						}
						else {
							for ( j = 1; j < ncoef3; j++ ) {
								if ( cc[ncoef3+j] != 0 ) break;
							}
							if ( j < ncoef3 ) {
								if ( ifp[-2] == EQUAL ) i = 0;
								else i = 1;
							}
							else if ( ifp[-2] == EQUAL ) i = 1;
							else i = 0;
						}
					}
					goto donemul;
				}
				else if ( AddRat(BHEAD coef1,ncoef1,coef2,-ncoef2,coef3,&ncoef3) ) {
					NumberFree(coef3,"DoIfStatement"); NumberFree(Spac1,"DoIfStatement"); TermFree(Spac2,"DoIfStatement");
					MesCall("DoIfStatement"); return(-1);
				}
				switch ( ifp[-2] ) {
					case GREATER:
						if ( ncoef3 > 0 ) i = 1;
						else i = 0;
						break;
					case GREATEREQUAL:
						if ( ncoef3 >= 0 ) i = 1;
						else i = 0;
						break;
					case LESS:
						if ( ncoef3 < 0 ) i = 1;
						else i = 0;
						break;
					case LESSEQUAL:
						if ( ncoef3 <= 0 ) i = 1;
						else i = 0;
						break;
					case EQUAL:
						if ( ncoef3 == 0 ) i = 1;
						else i = 0;
						break;
					case NOTEQUAL:
						if ( ncoef3 != 0 ) i = 1;
						else i = 0;
						break;
				}
donemul:		if ( i ) { ncoef2 = 1; coef2 = Spac2; coef2[0] = coef2[1] = 1; }
				else ncoef2 = 0;
				ismul1 = ismul2 = 0;
			}
		}
		else {
			first = 0;
		}
		coef1 = Spac1;
		i = 2*ABS(ncoef2);
		for ( j = 0; j < i; j++ ) coef1[j] = coef2[j];
		ncoef1 = ncoef2;
SkipCond:
		ifp += ifp[1];
	} while ( ifp < ifstop );
Esempio n. 3
0
int FactorInExpr(PHEAD WORD *term, WORD level)
{
	GETBIDENTITY
	WORD *t, *tstop, *m, *oldwork, *mstop, *n1, *n2, *n3, *n4, *n1stop, *n2stop;
	WORD *r1, *r2, *r3, *r4, j, k, kGCD, kGCD2, kLCM, jGCD, kkLCM, jLCM, size, sign;
	WORD *newterm, expr = 0;
	WORD olddeferflag = AR.DeferFlag, oldgetfile = AR.GetFile, oldhold = AR.KeptInHold;
	WORD newgetfile, newhold;
	int i;
	EXPRESSIONS e;
	FILEHANDLE *file = 0;
	POSITION position, oldposition, startposition;
	WORD *oldcpointer = AR.CompressPointer;
	UWORD *GCDbuffer, *GCDbuffer2, *LCMbuffer, *LCMb, *LCMc;
	GCDbuffer = NumberMalloc("FactorInExpr"); GCDbuffer2 = NumberMalloc("FactorInExpr");
	LCMbuffer = NumberMalloc("FactorInExpr"); LCMb = NumberMalloc("FactorInExpr"); LCMc = NumberMalloc("FactorInExpr");
	t = term; GETSTOP(t,tstop); t++;
	while ( t < tstop ) {
		if ( *t == FACTORIN && t[1] == FUNHEAD+2 && t[FUNHEAD] == -EXPRESSION ) {
			expr = t[FUNHEAD+1];
			break;
		}
		t += t[1];
	}
	if ( t >= tstop ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Internal error. Could not find proper factorin_ function.");
		MUNLOCK(ErrorMessageLock);
		NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
		NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
		return(-1);
	}
	oldwork = AT.WorkPointer;
	if ( AT.previousEfactor && ( expr == AT.previousEfactor[0] ) ) {
/*
		We have a hit in the cache. Construct the new term.
		At the moment AT.previousEfactor[1] is reserved for future flags
*/
		goto PutTheFactor;
	}
/*
	No hit. We have to do the work. We start with constructing the factor
	in the WorkSpace. Later we will move it to the cache.
	Finally we will jump to PutTheFactor.
*/
	e = Expressions + expr;
	switch ( e->status ) {
		case LOCALEXPRESSION:
		case SKIPLEXPRESSION:
		case DROPLEXPRESSION:
		case GLOBALEXPRESSION:
		case SKIPGEXPRESSION:
		case DROPGEXPRESSION:
/*
			Expression is to be found in the input Scratch file.
			Set the file handle and the position.
			The rest is done by GetTerm.
*/
			newhold = 0;
			newgetfile = 1;
			file = AR.infile;
			break;
		case HIDDENLEXPRESSION:
		case HIDDENGEXPRESSION:
		case HIDELEXPRESSION:
		case HIDEGEXPRESSION:
		case DROPHLEXPRESSION:
		case DROPHGEXPRESSION:
		case UNHIDELEXPRESSION:
		case UNHIDEGEXPRESSION:
/*
			Expression is to be found in the hide Scratch file.
			Set the file handle and the position.
			The rest is done by GetTerm.
*/
			newhold = 0;
			newgetfile = 2;
			file = AR.hidefile;
			break;
		case STOREDEXPRESSION:
/*
			This is an 'illegal' case
*/
			MLOCK(ErrorMessageLock);
			MesPrint("Error: factorin_ cannot determine factors in stored expressions.");
			MUNLOCK(ErrorMessageLock);
			NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
			NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
			return(-1);
		case DROPPEDEXPRESSION:
/*
			We replace the function by 1.
*/
			m = oldwork; n1 = term;
			while ( n1 < t ) *m++ = *n1++;
			n1 = t + t[1]; tstop = term + *term;
			while ( n1 < tstop ) *m++ = *n1++;
			*oldwork = m - oldwork;
			AT.WorkPointer = m;
			if ( Generator(BHEAD oldwork,level) ) {
				NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
				NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
				return(-1);
			}
			AT.WorkPointer = oldwork;
			NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
			NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
			return(0);
		default:
			MLOCK(ErrorMessageLock);
			MesPrint("Error: Illegal expression in factorinexpr.");
			MUNLOCK(ErrorMessageLock);
			NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
			NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
			return(-1);
	}
/*
	Before we start with the file we set the buffers for the coefficient
	For the coefficient we have to determine the LCM of the denominators
	and the GCD of the numerators.
*/
	position = AS.OldOnFile[expr];
	AR.DeferFlag = 0; AR.KeptInHold = newhold; AR.GetFile = newgetfile;
	SeekScratch(file,&oldposition);
	SetScratch(file,&position);
	if ( GetTerm(BHEAD oldwork) <= 0 ) {
		MLOCK(ErrorMessageLock);
		MesPrint("(5) Expression %d has problems in scratchfile",expr);
		MUNLOCK(ErrorMessageLock);
		NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
		NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
		return(-1);
	}
	SeekScratch(file,&startposition);
	SeekScratch(file,&position);
/*
	Load the first term in the workspace
*/
	if ( GetTerm(BHEAD oldwork) == 0 ) {
		SetScratch(file,&oldposition); /* We still need this untill Processor is clean */
		AR.DeferFlag = olddeferflag;
		oldwork[0] = 4; oldwork[1] = 1; oldwork[2] = 1; oldwork[3] = 3; 
		goto Complete;
	}
	SeekScratch(file,&position);
	AR.DeferFlag = olddeferflag; AR.KeptInHold = oldhold; AR.GetFile = oldgetfile;

	r2 = m = oldwork + *oldwork;
	j = m[-1];
	m -= ABS(j);
	*oldwork = (WORD)(m-oldwork);
	AT.WorkPointer = newterm = mstop = m;
/*
	Now take the coefficient of the first term to load up the LCM and the GCD
*/
	r3 = m;
	k = REDLENG(j);
	if ( k < 0 ) { k = -k; sign = -1; }
	else { sign = 1; }
	while ( ( k > 1 ) && ( r3[k-1] == 0 ) ) k--;
	for ( kGCD = 0; kGCD < k; kGCD++ ) GCDbuffer[kGCD] = r3[kGCD];
	k = REDLENG(j);
	if ( k < 0 ) k = -k;
	r3 += k;
	while ( ( k > 1 ) && ( r3[k-1] == 0 ) ) k--;
	for ( kLCM = 0; kLCM < k; kLCM++ ) LCMbuffer[kLCM] = r3[kLCM];
/*
	The copy and the coefficient are in place. Now search through the terms.
*/
	for (;;) {
		AR.DeferFlag = 0; AR.KeptInHold = newhold; AR.GetFile = newgetfile;
		SetScratch(file,&position);
		size = GetTerm(BHEAD newterm);
		SeekScratch(file,&position);
		AR.DeferFlag = olddeferflag; AR.KeptInHold = oldhold; AR.GetFile = oldgetfile;
		if ( size == 0 ) break;
		m = oldwork+1;
		r2 = newterm + *newterm;
		r2 -= ABS(r2[-1]);
		r1 = newterm+1;
		while ( m < mstop ) {
			while ( r1 < r2 ) {
				if ( *r1 != *m ) {
					r1 += r1[1]; continue;
				}
/*
				Now the various cases
			#[ SYMBOL :
*/
				if ( *m == SYMBOL ) {
					n1 = m+2; n1stop = m+m[1];
					n2stop = r1+r1[1];
					while ( n1 < n1stop ) {
						n2 = r1+2;
						while ( n2 < n2stop ) {
							if ( *n1 != *n2 ) { n2 += 2; continue; }
							if ( n1[1] > 0 ) {
								if ( n2[1] < 0 ) { n2 += 2; continue; }
								if ( n2[1] < n1[1] ) n1[1] = n2[1];
							}
							else {
								if ( n2[1] > 0 ) { n2 += 2; continue; }
								if ( n2[1] > n1[1] ) n1[1] = n2[1];
							}
							break;
						}
						if ( n2 >= n2stop ) {	/* scratch symbol */
							if ( m[1] == 4 ) goto scratch;
							m[1] -= 2;
							n3 = n1; n4 = n1+2;
							while ( n4 < mstop ) *n3++ = *n4++;
							*oldwork = n3 - oldwork;
							mstop -= 2; n1stop -= 2;
							continue;
						}
						n1 += 2;
					}
					break;
				}
/*
			#] SYMBOL : 
			#[ DOTPRODUCT :
*/
				else if ( *m == DOTPRODUCT ) {
					n1 = m+2; n1stop = m+m[1];
					n2stop = r1+r1[1];
					while ( n1 < n1stop ) {
						n2 = r1+2;
						while ( n2 < n2stop ) {
							if ( *n1 != *n2 || n1[1] != n2[1] ) { n2 += 3; continue; }
							if ( n1[2] > 0 ) {
								if ( n2[2] < 0 ) { n2 += 3; continue; }
								if ( n2[2] < n1[2] ) n1[2] = n2[2];
							}
							else {
								if ( n2[2] > 0 ) { n2 += 3; continue; }
								if ( n2[2] > n1[2] ) n1[2] = n2[2];
							}
							break;
						}
						if ( n2 >= n2stop ) {	/* scratch dotproduct */
							if ( m[1] == 5 ) goto scratch;
							m[1] -= 3;
							n3 = n1; n4 = n1+3;
							while ( n4 < mstop ) *n3++ = *n4++;
							*oldwork = n3 - oldwork;
							mstop -= 3; n1stop -= 3;
							continue;
						}
						n1 += 3;
					}
					break;
				}
/*
			#] DOTPRODUCT : 
			#[ VECTOR :
*/
				else if ( *m == VECTOR ) {
/*
					Here we have to be careful if there is more than
					one of the same
*/
					n1 = m+2; n1stop = m+m[1];
					n2 = r1+2;n2stop = r1+r1[1];
					while ( n1 < n1stop ) {
						while ( n2 < n2stop ) {
							if ( *n1 == *n2 && n1[1] == n2[1] ) {
								n2 += 2; goto nextn1;
							}
							n2 += 2;
						}
						if ( n2 >= n2stop ) {	/* scratch vector */
							if ( m[1] == 4 ) goto scratch;
							m[1] -= 2;
							n3 = n1; n4 = n1+2;
							while ( n4 < mstop ) *n3++ = *n4++;
							*oldwork = n3 - oldwork;
							mstop -= 2; n1stop -= 2;
							continue;
						}
						n2 = r1+2;
nextn1:					n1 += 2;
					}
					break;
				}
/*
			#] VECTOR : 
			#[ REMAINDER :
*/
				else {
/*
					Again: watch for multiple occurrences of the same object
*/
					if ( m[1] != r1[1] ) { r1 += r1[1]; continue; }
					for ( j = 2; j < m[1]; j++ ) {
						if ( m[j] != r1[j] ) break;
					}
					if ( j < m[1] ) { r1 += r1[1]; continue; }
					r1 += r1[1]; /* to restart at the next potential match */
					goto nextm;  /* match */
				}
/*
			#] REMAINDER : 
*/
			}
            if ( r1 >= r2 ) { /* no factor! */
scratch:;
				r3 = m + m[1]; r4 = m;
				while ( r3 < mstop ) *r4++ = *r3++;
				*oldwork = r4 - oldwork;
				if ( *oldwork == 1 ) goto nofactor;
				mstop = r4;
				r1 = newterm + 1;
				continue;
			}
			r1 = newterm + 1;
nextm:		m += m[1];
		}
nofactor:;
/*
		Now the coefficient
*/
		r2 = newterm + *newterm;
		j = r2[-1];
		r3 = r2 - ABS(j);
		k = REDLENG(j);
		if ( k < 0 ) k = -k;
		while ( ( k > 1 ) && ( r3[k-1] == 0 ) ) k--;
		if ( ( ( GCDbuffer[0] == 1 ) && ( kGCD == 1 ) ) ) {
/*
			GCD is already 1
*/
		}
		else if ( ( ( k != 1 ) || ( r3[0] != 1 ) ) ) {
			if ( GcdLong(BHEAD GCDbuffer,kGCD,(UWORD *)r3,k,GCDbuffer2,&kGCD2) ) {
				goto onerror;
			}
			kGCD = kGCD2;
			for ( i = 0; i < kGCD; i++ ) GCDbuffer[i] = GCDbuffer2[i];
		}
		else {
			kGCD = 1; GCDbuffer[0] = 1;
		}
		k = REDLENG(j);
		if ( k < 0 ) k = -k;
		r3 += k;
		while ( ( k > 1 ) && ( r3[k-1] == 0 ) ) k--;
		if ( ( ( LCMbuffer[0] == 1 ) && ( kLCM == 1 ) ) ) {
			for ( kLCM = 0; kLCM < k; kLCM++ )
				LCMbuffer[kLCM] = r3[kLCM];
		}
		else if ( ( k != 1 ) || ( r3[0] != 1 ) ) {
			if ( GcdLong(BHEAD LCMbuffer,kLCM,(UWORD *)r3,k,LCMb,&kkLCM) ) {
				goto onerror;
			}
			DivLong((UWORD *)r3,k,LCMb,kkLCM,LCMb,&kkLCM,LCMc,&jLCM);
			MulLong(LCMbuffer,kLCM,LCMb,kkLCM,LCMc,&jLCM);
			for ( kLCM = 0; kLCM < jLCM; kLCM++ )
				LCMbuffer[kLCM] = LCMc[kLCM];
		}
		else {} /* LCM doesn't change */
	}
	SetScratch(file,&oldposition); /* Needed until Processor is thread safe */
	AR.DeferFlag = olddeferflag;
/*
	Now put the term together in oldwork: GCD/LCM
	We have already the algebraic contents there.
*/
	r3 = (WORD *)(GCDbuffer);
	r4 = (WORD *)(LCMbuffer);
	r1 = oldwork + *oldwork;
	if ( kGCD == kLCM ) {
		for ( jGCD = 0; jGCD < kGCD; jGCD++ ) *r1++ = *r3++;
		for ( jGCD = 0; jGCD < kGCD; jGCD++ ) *r1++ = *r4++;
		k = 2*kGCD+1;
	}
	else if ( kGCD > kLCM ) {
		for ( jGCD = 0; jGCD < kGCD; jGCD++ ) *r1++ = *r3++;
		for ( jGCD = 0; jGCD < kLCM; jGCD++ ) *r1++ = *r4++;
		for ( jGCD = kLCM; jGCD < kGCD; jGCD++ ) *r1++ = 0;
		k = 2*kGCD+1;
	}
	else {
		for ( jGCD = 0; jGCD < kGCD; jGCD++ ) *r1++ = *r3++;
		for ( jGCD = kGCD; jGCD < kLCM; jGCD++ ) *r1++ = 0;
		for ( jGCD = 0; jGCD < kLCM; jGCD++ ) *r1++ = *r4++;
		k = 2*kLCM+1;
	}
	if ( sign < 0 ) *r1++ = -k;
	else *r1++ = k;
	*oldwork = (WORD)(r1-oldwork);
/*
	Now put the new term in the cache
*/
Complete:;
	if ( AT.previousEfactor ) M_free(AT.previousEfactor,"Efactor cache");
	AT.previousEfactor = (WORD *)Malloc1((*oldwork+2)*sizeof(WORD),"Efactor cache");
	AT.previousEfactor[0] = expr;
	r1 = oldwork; r2 = AT.previousEfactor + 2; k = *oldwork;
	NCOPY(r2,r1,k)
	AT.previousEfactor[1] = 0;
/*
	Now we construct the new term in the workspace.
*/
PutTheFactor:;
	if ( AT.WorkPointer + AT.previousEfactor[2] >= AT.WorkTop ) {
		MLOCK(ErrorMessageLock);
		MesWork();
		MesPrint("Called from factorin_");
		MUNLOCK(ErrorMessageLock);
		NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
		NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
		return(-1);
	}
	n1 = oldwork; n2 = term; while ( n2 < t ) *n1++ = *n2++;
	n2 = AT.previousEfactor+2; GETSTOP(n2,n2stop); n3 = n2 + *n2;
	n2++; while ( n2 < n2stop ) *n1++ = *n2++;
	n2 = t + t[1]; while ( n2 < tstop ) *n1++ = *n2++;
	size = term[*term-1];
	size = REDLENG(size);
	k = n3[-1]; k = REDLENG(k);
	if ( MulRat(BHEAD (UWORD *)tstop,size,(UWORD *)n2stop,k,
							(UWORD *)n1,&size) ) goto onerror;
	size = INCLENG(size);
	k = size < 0 ? -size: size;
       n1 += k; n1[-1] = size;
	*oldwork = n1 - oldwork;
	AT.WorkPointer = n1;
	if ( Generator(BHEAD oldwork,level) ) {
		NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
		NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
		return(-1);
	}
	AT.WorkPointer = oldwork;
	AR.CompressPointer = oldcpointer;
	NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
	NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
	return(0);
onerror:
	AT.WorkPointer = oldwork;
	AR.CompressPointer = oldcpointer;
	MLOCK(ErrorMessageLock);
	MesCall("FactorInExpr");
	MUNLOCK(ErrorMessageLock);
	NumberFree(GCDbuffer,"FactorInExpr"); NumberFree(GCDbuffer2,"FactorInExpr");
	NumberFree(LCMbuffer,"FactorInExpr"); NumberFree(LCMb,"FactorInExpr"); NumberFree(LCMc,"FactorInExpr");
	return(-1);
}