Complex values and operations
Code: "matrix_jscience.groovy". Programming language: Groovy DMelt Version 1. Last modified: 05/09/2015. License: Free
https://datamelt.org/code/cache/matrix_jscience_3512.groovy
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.



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("x")
def varY = new Variable.Local("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)
    }
}