Search in sources :

Example 1 with FunctionDomainException

use of cbit.vcell.parser.FunctionDomainException in project vcell by virtualcell.

the class RowColumnResultSet method extractColumn.

/**
 * getVariableNames method comment.
 *  If column is empty, return null or an empty array?
 *  For now, null...
 */
public synchronized double[] extractColumn(int c) throws ExpressionException {
    double[] values = null;
    if (getRowCount() > 0) {
        ColumnDescription colDescription = getColumnDescriptions(c);
        if (colDescription instanceof FunctionColumnDescription) {
            Expression exp = ((FunctionColumnDescription) colDescription).getExpression();
            // 
            // must rebind expression due to transient nature of expression binding (see ASTIdNode.symbolTableEntry)
            // 
            exp.bindExpression(getResultSetSymbolTableWithoutFunction());
            values = new double[getRowCount()];
            for (int r = 0; r < getRowCount(); r++) {
                try {
                    values[r] = exp.evaluateVector(getRow(r));
                } catch (DivideByZeroException e) {
                    e.printStackTrace(System.out);
                    values[r] = Double.NaN;
                } catch (FunctionDomainException e) {
                    e.printStackTrace(System.out);
                    values[r] = Double.NaN;
                }
            }
        } else {
            values = new double[getRowCount()];
            for (int r = 0; r < getRowCount(); r++) {
                values[r] = getRow(r)[c];
            }
        }
    }
    return (values);
}
Also used : DivideByZeroException(cbit.vcell.parser.DivideByZeroException) Expression(cbit.vcell.parser.Expression) FunctionDomainException(cbit.vcell.parser.FunctionDomainException) ColumnDescription(cbit.vcell.util.ColumnDescription)

Example 2 with FunctionDomainException

use of cbit.vcell.parser.FunctionDomainException in project vcell by virtualcell.

the class GradientFunctionDefinition method evaluateVector.

public double evaluateVector(Expression[] arguments, double[] values) throws ExpressionException {
    // 
    if (arguments.length != 2) {
        throw new ExpressionException("vcGrad() function expetcs 2 arguments");
    }
    Expression argNode = arguments[0];
    String opName = arguments[1].infix().replace("'", "");
    if (((values.length % GRADIENT_NUM_SPATIAL_ELEMENTS) != 0)) {
        throw new ExpressionException("number of grad values is not an even multiple of " + GRADIENT_NUM_SPATIAL_ELEMENTS + "\n" + "current point plus 12 spatial neighbors)");
    }
    int numInternalArgs = values.length / GRADIENT_NUM_SPATIAL_ELEMENTS;
    double[] tempArgs = new double[numInternalArgs];
    double result = 0;
    boolean isMagnitude = opName.equalsIgnoreCase(GRAD_MAGNITUDE);
    double[] magnitudeComponents = (isMagnitude ? new double[3] : null);
    for (int i = 0; i < 3; i += 1) {
        int axisCode = (opName.equalsIgnoreCase(GRAD_X) || (isMagnitude && i == 0) ? org.vcell.util.Coordinate.X_AXIS : 0) + (opName.equalsIgnoreCase(GRAD_Y) || (isMagnitude && i == 1) ? org.vcell.util.Coordinate.Y_AXIS : 0) + (opName.equalsIgnoreCase(GRAD_Z) || (isMagnitude && i == 2) ? org.vcell.util.Coordinate.Z_AXIS : 0);
        int[] gradCase = getGradCase(values, axisCode, numInternalArgs);
        // 
        if (gradCase == null) {
            throw new FunctionDomainException("grad(A,B) unknown case for axis code " + axisCode);
        } else if (gradCase[0] == NO_NEIGHBORS) {
            // =[p]= (bounded on both sides)
            // g(x) = 0;
            result = 0;
        } else if (gradCase[0] == USE_ADJACENT) {
            // =[p0][p]= or =[p][p0]= (bound on 1 side with 1 adjacent available)
            // g(x) = (u(p0) - u(p))/(p0-p)
            System.arraycopy(values, gradCase[2] * numInternalArgs, tempArgs, 0, tempArgs.length);
            double eV = argNode.evaluateVector(tempArgs);
            double eP = tempArgs[1 + axisCode];
            System.arraycopy(values, gradCase[1] * numInternalArgs, tempArgs, 0, tempArgs.length);
            double pV = argNode.evaluateVector(tempArgs);
            double pP = tempArgs[1 + axisCode];
            result = (eV - pV) / (eP - pP);
        } else if (gradCase[0] == USE_ADJACENT_PLUS) {
            // ...[p1][p0][p]= or =[p][p0][p1]... (bound on 1 side only)
            // g(x) = ((-3*u(p)) + (4*u(p0)) + (-u(p1))) / (p1-p)
            System.arraycopy(values, gradCase[3] * numInternalArgs, tempArgs, 0, tempArgs.length);
            double eeV = argNode.evaluateVector(tempArgs);
            double eeP = tempArgs[1 + axisCode];
            System.arraycopy(values, gradCase[2] * numInternalArgs, tempArgs, 0, tempArgs.length);
            double eV = argNode.evaluateVector(tempArgs);
            // double eP = tempArgs[1+axisCode];
            System.arraycopy(values, gradCase[1] * numInternalArgs, tempArgs, 0, tempArgs.length);
            double pV = argNode.evaluateVector(tempArgs);
            double pP = tempArgs[1 + axisCode];
            result = ((-3 * pV) + (4 * eV) + (-eeV)) / (eeP - pP);
        } else if (gradCase[0] == BOTH_NEIGHBORS) {
            // [pm][p][pp] (not bound on either side)
            // g(x) = (u(pp) - u(pm))/(pp-pm)
            System.arraycopy(values, gradCase[3] * numInternalArgs, tempArgs, 0, tempArgs.length);
            double eV = argNode.evaluateVector(tempArgs);
            double eP = tempArgs[1 + axisCode];
            System.arraycopy(values, gradCase[1] * numInternalArgs, tempArgs, 0, tempArgs.length);
            double wV = argNode.evaluateVector(tempArgs);
            double wP = tempArgs[1 + axisCode];
            result = (eV - wV) / (eP - wP);
        }
        if (!isMagnitude) {
            break;
        } else {
            magnitudeComponents[i] = result;
        }
    }
    if (isMagnitude) {
        result = Math.sqrt((magnitudeComponents[0] * magnitudeComponents[0]) + (magnitudeComponents[1] * magnitudeComponents[1]) + (magnitudeComponents[2] * magnitudeComponents[2]));
    }
    return result;
}
Also used : Expression(cbit.vcell.parser.Expression) FunctionDomainException(cbit.vcell.parser.FunctionDomainException) ExpressionException(cbit.vcell.parser.ExpressionException)

Aggregations

Expression (cbit.vcell.parser.Expression)2 FunctionDomainException (cbit.vcell.parser.FunctionDomainException)2 DivideByZeroException (cbit.vcell.parser.DivideByZeroException)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 ColumnDescription (cbit.vcell.util.ColumnDescription)1