### Table of Contents

# Solving equations

Numerous Java packages can be used to solve linear, non-linear and differential equations.

# Solving linear, quadratic and cubic equations

To solve linear, quadratic and cubic equations, use the jhplot.math.Numeric package. In general, only real solutions are considered.

from jhplot.math.Numeric import * a=solveLinear(1,2) # solves ax+b=0. a=1, b=2 print a b= solveQuadratic(1, 2, -1) # roots of the quadratic equation print b b= solveCubic(1, 2, 4,2) print b c=solveQuartic(1,-2, 3, 4, -2) print b

# Polynomial solving (symbolic)

For solving polynomial equations, one can use the symbolic calculation engine. Read more in Sect.Analytic calculations

from jhplot.math import * from jhplot import * j=Symbolic("jscl") # using jscl engine j.expand("solve(c+b*x+a*x^2,x)") # answer: root[0](c, b, a)

# Algebraic equation systems

One solve algebraic equation systems of any degree, with several indeterminates, by computing the Groebner bases of polynomial ideals. For example, let this system for the indeterminates x, y:

x^2 + y^2 = 4 x*y = 1

We use:

>>> j.expand("groebner({x^2 + y^2 - 4, x*y - 1}, {x, y})")

The returned output is:

{1-4*x^2+x^4, 4*x-x^3-y}

which allows to find x, then y from x. This operation doesn't calculate the roots, it just writes the equation. For example, it wouldn't give “a = 4/5” but “5*a-4” (”= 0” implied). Groebner basis computation is explained in more details below:

# Solving linear systems

Consider a linear systems of equations of the form AX=B. For example, consider

2x + 3y - 2z = 1 -x + 7y + 6x = -2 4x - 3y - 5z = 1

We will solve this using DecompositionSolver of the Apache Common Math package:

from org.apache.commons.math3.linear import * # get the coefficient matrix A using LU decomposition coeff= Array2DRowRealMatrix([[2,3,-2],[-1,7,6],[4,-3,-5]]) solver =LUDecompositionImpl(coeff).getSolver() # use solve(RealVector) to solve the system constants = ArrayRealVector([1, -2, 1 ]) solution = solver.solve(constants); print "Solution: x=",solution.getEntry(0), "y=",solution.getEntry(1),"z=",solution.getEntry(2)

The execution of this code prints:

Solution: x= -0.369863013699 y= 0.178082191781 z= -0.602739726027

Read more for different types of decomposition here. Please read more the Linear algebra section.

# Solving non-linear systems

You can solve non-linear equations using the jMathLab symbolic kernel as explained in Section Equations