JScience is a comprehensive Java library for the scientific community encompassing various areas of science including math, physics, sociology, biology, astronomy, economics.

/*
// require(name:'JScience', version:'3.2', url:'http://jscience.org/jscience-vm14.jar')
import org.jscience.mathematics.numbers.Complex
import static org.jscience.mathematics.numbers.Complex.I
import static org.jscience.mathematics.numbers.Complex.valueOf as c
import org.jscience.mathematics.functions.Polynomial
import static org.jscience.mathematics.functions.Polynomial.valueOf as p
import org.jscience.mathematics.functions.Variable
*/

// require(name:'JScience', version:'4.3.1', url:'http://jscience.org/jscience-4.3.1-bin.zip')
import org.jscience.mathematics.number.Complex
import static org.jscience.mathematics.number.Complex.I
import static org.jscience.mathematics.number.Complex.valueOf as c
import org.jscience.mathematics.function.Polynomial
import static org.jscience.mathematics.function.Polynomial.valueOf as p
import org.jscience.mathematics.function.Variable

// Defines two local variables (x, y).
def varX = new Variable.Local<Complex>("x")
def varY = new Variable.Local<Complex>("y")

use (JScienceCategory) {
    def ONE = Complex.ONE
    def TWO = c(2, 0)
    // f(x) = ix² + 2x + 1
    def x = p(ONE, varX)
    def fx = I * x ** 2 + TWO * x + ONE
    println fx
    println fx ** 2
    println fx.differentiate(varX)
    println fx.integrate(varY)
    println fx.compose(fx)

    // Calculates expression.
    varX.set(c(2, 3))
    println fx.evaluate()
}

class JScienceCategory {
    static power(Polynomial p, int n) {
        p.pow(n)
    }
    static multiply(Complex c, Polynomial p) {
        p.times(c)
    }
    static multiply(Polynomial p, Complex c) {
        p.times(c)
    }
}

Running this script yields:

[0.0 + 1.0i]x² + [2.0 + 0.0i]x + [1.0 + 0.0i]
[-1.0 + 0.0i]x4 + [0.0 + 4.0i]x³ + [4.0 + 2.0i]x² + [4.0 + 0.0i]x + [1.0 + 0.0i]
[0.0 + 2.0i]x + [2.0 + 0.0i]
[0.0 + 1.0i]x²y + [2.0 + 0.0i]xy + [1.0 + 0.0i]y
[0.0 - 1.0i]x4 + [-4.0 + 0.0i]x³ + [-2.0 + 6.0i]x² + [4.0 + 4.0i]x + [3.0 + 1.0i]
-7.0 + 1.0i