DMelt:Math/6 Equations

From HandWiki
Limitted access. First login to DataMelt if you are a full DataMelt member. Then login to HandWiki as a user.

Latex equations

Equation is a statement of an equality containing one or more variables. You can create PNG images with equations using the Software:LaTeX syntax using the class jhplot.HLatexEq jhplot.HLatexEq. These images can be included on the web page or presentations. Here is a small code that shows how to make a PNG image

from jhplot import *
eq="\int^{100}_{i=0} F(x) dx" # use LaTeX syntax for this equation
image="/tmp/equation.png"     # output file with PNG image of this equation
q1=HLatexEq(eq, 32)           # create PNG image from LaTeX using the font size 32 
q1.export(image)              # making the image
print "Created :",image
IView(image)                  #  View the created PNG image

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 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.

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 org.apache.commons.math3.linear.DecompositionSolver org.apache.commons.math3.linear.DecompositionSolver 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 in the Linear Algebra section.

Solving non-linear systems

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

Solving differential equations