// sets q equal to the polynomial s.t. a = bq + r
void eucDiv(float a[], float b[], float q[]) {
    if (equalsZero(a) == 1) {
        printf("You are calling eucDiv with a equal to the zero vector\n");
        return;
    }
    if (equalsZero(b) == 1) {
        printf("You are calling a division by 0\n");
        return;
    }

    // printf("a in eucdiv call: ");
    // printPoly(a);

    // printf("b in eucdiv call: ");
    // printPoly(b);
    int i;
    // printf("\narg1 of eucdiv: ");
    // printPoly(a);
    // printf("\narg2 of eucdiv: ");
    // printPoly(b);
    int degA = degree(a);
    int degB = degree(b);
    //printf("\ndeg arg1: %d, deg arg2: %d\n", degA, degB);

    int d = degA - degB;
    if (d < 0) {
        printf("This is a nonsensical call of eucDiv\n");
        return;
    }

    float tempQ;
    setZero(q);

    // creates tempA as a clone of a
    float tempA[maxDegree+1];
    setEquals(tempA, a);
    float tempB[maxDegree+1];
    setEquals(tempB, b);
    // float tempPoly[maxDegree+1];

    // algorithm for euclidean division
    for (i = d; i >= 0; --i) {
        tempQ = tempA[degA + i - d] / tempB[degB];
        q[i] = tempQ;
        if (tempQ != 0) {
            scale(tempB, tempQ);
            polyTimesXn(tempB, i);
            subtract(tempA, tempB);
            polyTimesXn(tempB, -i);
            scale(tempB, 1.0/tempQ);
        }
    }
}
// sets q equal to the polynomial s.t. a = bq + r
void eucDiv(float a[], float b[], float q[]) {
	if (equalsZero(a) == 1) {
		printf("You are calling eucDiv with a equal to the zero vector\n");
		return;
	}
	if (equalsZero(b) == 1) {
		printf("You are calling a division by 0\n");
		return;
	}

	clearZeroes(a);
	clearZeroes(b);

	int i;
	int degA = degree(a);
	int degB = degree(b);
	int d = degA - degB;
	if (d < 0) { 
		printf("This is a nonsensical call of eucDiv\n");
		return;
	}

	float tempQ;
	setZero(q);

	// creates tempA as a clone of a
	float tempA[maxDegree+1];
	setEquals(tempA, a);
	float tempB[maxDegree+1];
	setEquals(tempB, b);

	// algorithm for euclidean division of polynomials
	for (i = d; i >= 0; --i) {
		tempQ = tempA[degA + i - d] / tempB[degB];
		q[i] = tempQ;
		// runs iff |tempQ| < 0.001
		if (isAboutZero(tempQ) == 0) {
			scale(tempB, tempQ);
			polyTimesXn(tempB, i);
			subtract(tempA, tempB);
			polyTimesXn(tempB, -i);
			scale(tempB, 1.0/tempQ);
		}
	}
}
int dividesPoly(float a[], float b[]) {
    int i;
    // creates tempA as a clone of a
    float tempA[maxDegree+1];
    setEquals(tempA, a);
    float tempB[maxDegree+1];
    setEquals(tempB, b);
    float q[maxDegree+1];
    setZero(q);

    int degA = degree(a);
    int degB = degree(b);
    int d = degA - degB;

    float tempQ;

    // algorithm for euclidean division
    for (i = d; i >= 0; --i) {
        tempQ = tempA[degA + i - d] / tempB[degB];
        q[i] = tempQ;
        if (tempQ != 0) {
            scale(tempB, tempQ);
            polyTimesXn(tempB, i);
            subtract(tempA, tempB);
            polyTimesXn(tempB, -i);
            scale(tempB, 1.0/tempQ);
        }
    }
    multiply(b, q, tempA);
    subtract(tempA, a);
    clearZeroes(tempA);
    if(equalsZero(tempA) == 1) {
        return 1;
    }
    else {
        return 0;
    }
}
// multiplies an MxM matrix by an MxM matrix; used for checking results
void matMMxmatMM(float A[][M][maxDegree+1], float B[][M][maxDegree+1], float AB[][M][maxDegree+1]) {
	int m1, m2, m;
	float temp[maxDegree+1];
	float temp2[maxDegree+1];
	for(m1 = 0; m1 < M; ++m1) {
		for(m2 = 0; m2 < M; ++m2) {
			setZero(temp);
			for(m = 0; m < M; ++m) {
				multiply(A[m1][m], B[m][m2], temp2);
				add(temp, temp2);
			}
			setEquals(AB[m1][m2], temp);
		}
	}
}
// multiplies an NxN matrix by an NxN matrix; used for checking results
void matNNxmatNN(float A[][N][maxDegree+1], float B[][N][maxDegree+1], float AB[][N][maxDegree+1]) {
	int n1, n2, n;
	float temp[maxDegree+1];
	float temp2[maxDegree+1];
	for(n1 = 0; n1 < N; ++n1) {
		for(n2 = 0; n2 < N; ++n2) {
			setZero(temp);
			for(n = 0; n < N; ++n) {
				multiply(A[n1][n], B[n][n2], temp2);
				add(temp, temp2);
			}
			setEquals(AB[n1][n2], temp);
		}
	}
}
// multiplies an NxM matrix by an MxM matrix; used for checking results
void matNMxmatMM(float A[][M][maxDegree+1], float Q[][M][maxDegree+1], float AQ[][M][maxDegree+1]) {
	int n, m, m1;
	float temp[maxDegree+1];
	float temp2[maxDegree+1];
	for(n = 0; n < N; ++n) {
		for(m = 0; m < M; ++m) {
			setZero(temp);
			for(m1 = 0; m1 < M; ++m1) {
				multiply(A[n][m1], Q[m1][m], temp2);
				add(temp, temp2);
			}
			setEquals(AQ[n][m], temp);
		}
	}
}
// multiplies an NxN matrix by an NxM matrix; used for checking results
void matNNxmatNM(float P[][N][maxDegree+1], float A[][M][maxDegree+1], float PA[][M][maxDegree+1]) {
	int n, m, n1;
	float temp[maxDegree+1];
	float temp2[maxDegree+1];
	for(n = 0; n < N; ++n) {
		for(m = 0; m < M; ++m) {
			setZero(temp);
			for(n1 = 0; n1 < N; ++n1) {
				multiply(P[n][n1], A[n1][m], temp2);
				add(temp, temp2);
			}
			setEquals(PA[n][m], temp);
		}
	}
}
Beispiel #8
0
//-----------------------------------------------------------------------------
//
//   buildStateTable()    Determine the set of runtime DFA states and the
//                        transition tables for these states, by the algorithm
//                        of fig. 3.44 in Aho.
//
//                        Most of the comments are quotes of Aho's psuedo-code.
//
//-----------------------------------------------------------------------------
void RBBITableBuilder::buildStateTable() {
    if (U_FAILURE(*fStatus)) {
        return;
    }
    //
    // Add a dummy state 0 - the stop state.  Not from Aho.
    int      lastInputSymbol = fRB->fSetBuilder->getNumCharCategories() - 1;
    RBBIStateDescriptor *failState = new RBBIStateDescriptor(lastInputSymbol, fStatus);
    failState->fPositions = new UVector(*fStatus);
    if (U_FAILURE(*fStatus)) {
        return;
    }
    fDStates->addElement(failState, *fStatus);
    if (U_FAILURE(*fStatus)) {
        return;
    }

    // initially, the only unmarked state in Dstates is firstpos(root),
    //       where toot is the root of the syntax tree for (r)#;
    RBBIStateDescriptor *initialState = new RBBIStateDescriptor(lastInputSymbol, fStatus);
    if (U_FAILURE(*fStatus)) {
        return;
    }
    initialState->fPositions = new UVector(*fStatus);
    if (U_FAILURE(*fStatus)) {
        return;
    }
    setAdd(initialState->fPositions, fTree->fFirstPosSet);
    fDStates->addElement(initialState, *fStatus);
    if (U_FAILURE(*fStatus)) {
        return;
    }

    // while there is an unmarked state T in Dstates do begin
    for (;;) {
        RBBIStateDescriptor *T = NULL;
        int32_t              tx;
        for (tx=1; tx<fDStates->size(); tx++) {
            RBBIStateDescriptor *temp;
            temp = (RBBIStateDescriptor *)fDStates->elementAt(tx);
            if (temp->fMarked == FALSE) {
                T = temp;
                break;
            }
        }
        if (T == NULL) {
            break;
        }

        // mark T;
        T->fMarked = TRUE;

        // for each input symbol a do begin
        int32_t  a;
        for (a = 1; a<=lastInputSymbol; a++) {
            // let U be the set of positions that are in followpos(p)
            //    for some position p in T
            //    such that the symbol at position p is a;
            UVector    *U = NULL;
            RBBINode   *p;
            int32_t     px;
            for (px=0; px<T->fPositions->size(); px++) {
                p = (RBBINode *)T->fPositions->elementAt(px);
                if ((p->fType == RBBINode::leafChar) &&  (p->fVal == a)) {
                    if (U == NULL) {
                        U = new UVector(*fStatus);
                    }
                    setAdd(U, p->fFollowPos);
                }
            }

            // if U is not empty and not in DStates then
            int32_t  ux = 0;
            UBool    UinDstates = FALSE;
            if (U != NULL) {
                U_ASSERT(U->size() > 0);
                int  ix;
                for (ix=0; ix<fDStates->size(); ix++) {
                    RBBIStateDescriptor *temp2;
                    temp2 = (RBBIStateDescriptor *)fDStates->elementAt(ix);
                    if (setEquals(U, temp2->fPositions)) {
                        delete U;
                        U  = temp2->fPositions;
                        ux = ix;
                        UinDstates = TRUE;
                        break;
                    }
                }

                // Add U as an unmarked state to Dstates
                if (!UinDstates)
                {
                    RBBIStateDescriptor *newState = new RBBIStateDescriptor(lastInputSymbol, fStatus);
                    if (U_FAILURE(*fStatus)) {
                        return;
                    }
                    newState->fPositions = U;
                    fDStates->addElement(newState, *fStatus);
                    if (U_FAILURE(*fStatus)) {
                        return;
                    }
                    ux = fDStates->size()-1;
                }

                // Dtran[T, a] := U;
                T->fDtran->setElementAt(ux, a);
            }
        }
    }
}