How do I create symbolic variables/functions involving vectors using JScience?

265 views Asked by At

How do I create symbolic variables/functions involving vectors using JScience? Creating float variables is easy:

Variable<Float64> varX = Variable.local<Float64>("x");

How can I do the same for a vector? Once I have a vector variable, how do I create a Polynomial involving that vector (say, by using Euclidean distance)?

1

There are 1 answers

0
trashgod On

Given a Vector<Float64> of coefficients, you can construct a Polynomial<Float64> as shown below. As shown here, the highest order coefficient is first for convenience in applying Horner's scheme.

public static Polynomial<Float64> create(Vector<Float64> v) {
    Variable<Float64> x = new Variable.Local<Float64>("x");
    Polynomial<Float64> px = Polynomial.valueOf(Float64.ZERO, x);
    for (int i = 0, e = v.getDimension() - 1; i < v.getDimension(); i++, e--) {
        px = px.plus(Polynomial.valueOf(v.get(i), Term.valueOf(x, e)));
    }
    return px;
}

Typical usage:

Polynomial<Float64> px = create(Float64Vector.valueOf(3, 2, 1));
System.out.println(px);
System.out.println(px.evaluate(Float64.ONE));

Console:

[3.0]x² + [2.0]x + [1.0]
6.0