Search in sources :

Example 1 with Variable

use of org.apache.ratis.examples.arithmetic.expression.Variable in project incubator-ratis by apache.

the class TestArithmetic method runTestPythagorean.

public static void runTestPythagorean(RaftClient client, int start, int count) throws IOException {
    Preconditions.assertTrue(count > 0, () -> "count = " + count + " <= 0");
    Preconditions.assertTrue(start >= 2, () -> "start = " + start + " < 2");
    final Variable a = new Variable("a");
    final Variable b = new Variable("b");
    final Variable c = new Variable("c");
    final Expression pythagorean = SQRT.apply(ADD.apply(SQUARE.apply(a), SQUARE.apply(b)));
    final int end = start + 2 * count;
    for (int n = (start & 1) == 0 ? start + 1 : start; n < end; n += 2) {
        int n2 = n * n;
        int half_n2 = n2 / 2;
        assign(client, a, n);
        assign(client, b, half_n2);
        assign(client, c, pythagorean, (double) half_n2 + 1);
        assignNull(client, a);
        assignNull(client, b);
        assignNull(client, c);
    }
}
Also used : Variable(org.apache.ratis.examples.arithmetic.expression.Variable) Expression(org.apache.ratis.examples.arithmetic.expression.Expression)

Example 2 with Variable

use of org.apache.ratis.examples.arithmetic.expression.Variable in project incubator-ratis by apache.

the class TestArithmetic method runGaussLegendre.

void runGaussLegendre(RaftClient client) throws IOException {
    defineVariable(client, "a0", 1);
    defineVariable(client, "b0", DIV.apply(1, SQRT.apply(2)));
    defineVariable(client, "t0", DIV.apply(1, 4));
    defineVariable(client, "p0", 1);
    double previous = 0;
    boolean converged = false;
    for (int i = 1; i < 8; i++) {
        final int i_1 = i - 1;
        final Variable a0 = new Variable("a" + i_1);
        final Variable b0 = new Variable("b" + i_1);
        final Variable t0 = new Variable("t" + i_1);
        final Variable p0 = new Variable("p" + i_1);
        final Variable a1 = defineVariable(client, "a" + i, DIV.apply(ADD.apply(a0, b0), 2));
        final Variable b1 = defineVariable(client, "b" + i, SQRT.apply(MULT.apply(a0, b0)));
        final Variable t1 = defineVariable(client, "t" + i, SUBTRACT.apply(t0, MULT.apply(p0, SQUARE.apply(SUBTRACT.apply(a0, a1)))));
        final Variable p1 = defineVariable(client, "p" + i, MULT.apply(2, p0));
        final Variable pi_i = new Variable("pi_" + i);
        final Expression e = assign(client, pi_i, DIV.apply(SQUARE.apply(a1), t0));
        final double pi = e.evaluate(null);
        if (converged) {
            Assert.assertTrue(pi == previous);
        } else if (pi == previous) {
            converged = true;
        }
        LOG.info("{} = {}, converged? {}", pi_i, pi, converged);
        previous = pi;
    }
    Assert.assertTrue(converged);
}
Also used : Variable(org.apache.ratis.examples.arithmetic.expression.Variable) Expression(org.apache.ratis.examples.arithmetic.expression.Expression)

Example 3 with Variable

use of org.apache.ratis.examples.arithmetic.expression.Variable in project incubator-ratis by apache.

the class TestArithmetic method defineVariable.

static Variable defineVariable(RaftClient client, String name, double value) throws IOException {
    final Variable x = new Variable(name);
    assign(client, x, value);
    return x;
}
Also used : Variable(org.apache.ratis.examples.arithmetic.expression.Variable)

Example 4 with Variable

use of org.apache.ratis.examples.arithmetic.expression.Variable in project incubator-ratis by apache.

the class Get method operation.

@Override
protected void operation(RaftClient client) throws IOException {
    RaftClientReply getValue = client.sendReadOnly(Expression.Utils.toMessage(new Variable(name)));
    Expression response = Expression.Utils.bytes2Expression(getValue.getMessage().getContent().toByteArray(), 0);
    System.out.println(String.format("%s=%s", name, (DoubleValue) response).toString());
}
Also used : Variable(org.apache.ratis.examples.arithmetic.expression.Variable) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Expression(org.apache.ratis.examples.arithmetic.expression.Expression)

Example 5 with Variable

use of org.apache.ratis.examples.arithmetic.expression.Variable in project incubator-ratis by apache.

the class TestArithmetic method defineVariable.

static Variable defineVariable(RaftClient client, String name, Expression e) throws IOException {
    final Variable x = new Variable(name);
    assign(client, x, e, null);
    return x;
}
Also used : Variable(org.apache.ratis.examples.arithmetic.expression.Variable)

Aggregations

Variable (org.apache.ratis.examples.arithmetic.expression.Variable)6 Expression (org.apache.ratis.examples.arithmetic.expression.Expression)3 DoubleValue (org.apache.ratis.examples.arithmetic.expression.DoubleValue)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 Test (org.junit.Test)1