Hi Mansour,

Le 15/07/2014 15:02, Mansour Al Akeel a écrit :
> I am new to commons-math. I need to solve simple equations in the form
> "x + 3 = 10"
> The closest thing I was able to find is
> http://commons.apache.org/proper/commons-math/userguide/linear.html#a3.4_Solving_linear_systems

If you have only one free variable (x in this case), then you should
rather user one of the UnivariateSolver implementations from the
org.apache.commons.math3.analysis.solver package. In the general case, I
would recommend the BracketingNthOrderBrentSolver, but if you only have
linear systems, there are not much differences betwen solvers.

> 
> 
> However, this needs a square matrix, and can not build the
> coefficients and constants from the string.
> Is there a matrix builder that I can use to create the 2 dimensional
> arrays to solve this equation ?

This problem is not 2-dimensional, it is really one dimension only (one
equation for solving one unknown). It would be 2-dimensional if you had
two equations for two unknowns. In this case (2-dimensional), you should
have somathing like:

  a1 x + b1 y + c1 = d1
  a2 x + b2 y + c2 = d2

where a1, b1, c1, d1, a2, b2, c2 and d2 are constants and x and y are
the two unknowns you want to find.

You should start by grouping the additive constants c1 and d1 as well as
c2 and d2 to have your system in a canonical form. You can do this by
subgtracting c1 from both sides of equation 1 and c2 from both sides of
equation 2 so they disappear from the left hand side, you then get:

  a1 x + b1 y = d1 - c1
  a2 x + b2 y = d2 - c2

This is rewritten in matrix form:

  [ a1   b1 ]   [ x ]        [d1 - c1]
  [         ] * [   ]    =   [       ]
  [ a2   b2 ]   [ y ]        [d2 - c2]

So the system you would provide to Apache Commons Math would be solved
as follows:

  double a1 = ...;
  double b1 = ...;
  double c1 = ...;
  double d1 = ...;
  double a2 = ...;
  double b2 = ...;
  double c2 = ...;
  double d2 = ...;

  RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
                                               { a1, b1 },
                                               { a2, b2}
                                              });
  RealVector v = MatrixUtils.createRealVector(new double[] {
                                                d1 - c1,
                                                d2 - c2
                                              });

  // find vector u solution of m * u = v
  double singularityThreshold = 1.0e-10; // or some other small number
  DecompositionSolver solver =
    new LUDecomposition(m, singularityThreshold).getSolver();
  RealVector u = solver.solve(v);


There are no parser in Apache Commons Math to build such systems
directly from string representations. We already discussed about this
and decided not to implement parsers, as they are already big projects
by themselves and depend on the syntax you want to parse. Parsers are
therefore considered to be out of Apache Commons Math scope. They belong
to higher level projects that could implement them for their own
specific syntax, and rely on Apache Commons Math to sovle the problem
once it has been translated from strings to structured numbered.

Hope this helps
Luc

> 
> Thank you.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
> For additional commands, e-mail: user-h...@commons.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
For additional commands, e-mail: user-h...@commons.apache.org

Reply via email to