commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles <gil...@harfang.homelinux.org>
Subject [Math] Re: Error by running QuadraticProblem
Date Thu, 13 Oct 2016 15:26:24 GMT
Hello.

On Thu, 13 Oct 2016 12:28:30 +0200, Liudmila Belenki wrote:
> Dear  Sirs or Madams,
>
> I use  the library org.apache.commons.math3  for the approximation of
> data set by Levenberg-Marquard method.
>
> As example, I try to run a test program QuadraticProblem.
> There is an error by calling of a class  PointVectorValuePair
>
>
> Exception in thread "main" java.lang.Error: Unresolved compilation 
> problems:
> 	The type
> org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem
> cannot be resolved. It is indirectly referenced from required .class
> files
> 	The method optimize(LeastSquaresProblem) from the type
> LevenbergMarquardtOptimizer refers to the missing type
> LeastSquaresProblem
>
> 	at
> 
> org.apache.commons.math3.fitting.leastsquares.QuadraticProblem.main(QuadraticProblem.java:79)
>
>
> Please explain me, how I can the error eliminate.

It's a compilation problem.
But I can't see right-away what causes it.
Which environment do you use to compile this class (ant, maven, 
other?)?

First thing I'd suggest is to not define your own classes
within the library's namespace (see below).

> Thanks.
>
> Regards,
> Liudmila Belenki
>
>
>
>
> package org.apache.commons.math3.fitting.leastsquares;
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You should not define the classes in _our_ package, as it could
obscure the source of the problem.

Use something like
   package my;
or
   package liudmila;


Regards,
Gilles

> import java.util.*;
> import org.apache.commons.math3.linear.ArrayRealVector;
> import org.apache.commons.math3.linear.RealMatrix;
> import 
> org.apache.commons.math3.analysis.DifferentiableMultivariateFunction;
> import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
> import org.apache.commons.math3.exception.ConvergenceException;
> import org.apache.commons.math3.exception.util.LocalizedFormats;
> import org.apache.commons.math3.optim.ConvergenceChecker;
> import org.apache.commons.math3.optim.PointValuePair;
> import org.apache.commons.math3.util.Incrementor;
> import org.apache.commons.math3.util.Precision;
> import org.apache.commons.math3.util.FastMath;
> import org.apache.commons.math3.stat.descriptive.UnivariateStatistic;
> import org.apache.commons.math3.stat.descriptive.rank.Median;
> import org.apache.commons.math3.stat.descriptive.rank.Percentile;
> import org.apache.commons.math3.stat.correlation.*;
> import org.apache.commons.math3.fitting.*;
> import org.apache.commons.math3.optimization.fitting.*;
> import org.apache.commons.math3.stat.clustering.*;
> import org.apache.commons.math3.stat.*;
> import 
> org.apache.commons.math3.fitting.leastsquares.RecognizerLibrary;
> import org.apache.commons.math3.stat.descriptive.UnivariateStatistic;
> import
> 
> org.apache.commons.math3.stat.descriptive.MultivariateSummaryStatistics;
> import org.apache.commons.math3.exception.MathIllegalStateException;
> import org.apache.commons.math3.exception.util.LocalizedFormats;
> import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
> import org.apache.commons.math3.analysis.MultivariateVectorFunction;
> //import
> 
> org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem.Evaluation;
> import org.apache.commons.math3.linear.Array2DRowRealMatrix;
> import org.apache.commons.math3.linear.ArrayRealVector;
> import org.apache.commons.math3.linear.DiagonalMatrix;
> import org.apache.commons.math3.linear.EigenDecomposition;
> import org.apache.commons.math3.linear.RealMatrix;
> import org.apache.commons.math3.linear.RealVector;
> import org.apache.commons.math3.optim.AbstractOptimizationProblem;
> import org.apache.commons.math3.optim.ConvergenceChecker;
> import org.apache.commons.math3.optim.PointVectorValuePair;
> import org.apache.commons.math3.util.FastMath;
> import org.apache.commons.math3.util.Incrementor;
> import org.apache.commons.math3.util.Pair;
>
>
> //private static class QuadraticProblem
> public class QuadraticProblem {
>
> 	public static void main(String[] args) {
>
> 		QuadraticProblem problem = new QuadraticProblem();
>
> 		 problem.addPoint(1, 34.234064369);
> 		 problem.addPoint(2, 68.2681162306);
> 		 problem.addPoint(3, 118.6158990846);
> 		 problem.addPoint(4, 184.1381972386);
> 		 problem.addPoint(5, 266.5998779163);
> 		 problem.addPoint(6, 364.1477352516);
> 		 problem.addPoint(7, 478.0192260919);
> 		 problem.addPoint(8, 608.1409492707);
> 		 problem.addPoint(9, 754.5988686671);
> 		 problem.addPoint(10, 916.1288180859);
>
> 		 LevenbergMarquardtOptimizer optimizer = new
> LevenbergMarquardtOptimizer(100, 1e-10, 1e-10, 1e-10, 0);
>
> 		 final double[] weights = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
>
> 		 final double[] initialSolution = {1, 1, 1};
>
>
> 		 PointVectorValuePair optimum =
> 				 optimizer.optimize(100,
>                  problem,
>                  problem.calculateTarget(),
>                  weights,
>                  initialSolution);
>
> 		 final double[] optimalValues = optimum.getPoint();
>
> 		 System.out.println("A: " + optimalValues[0]);
> 		 System.out.println("B: " + optimalValues[1]);
> 		 System.out.println("C: " + optimalValues[2]);
>
> 	}
>
> //private static final long serialVersionUID = 7072187082052755854L;
> Serializable
> private List<Double> x;
> private List<Double> y;
>
> public QuadraticProblem() {
>     x = new ArrayList<Double>();
>     y = new ArrayList<Double>();
> }
>
> public void addPoint(double x, double y) {
>     this.x.add(x);
>     this.y.add(y);
> }
>
> public double[] calculateTarget() {
>     double[] target = new double[y.size()];
>     for (int i = 0; i < y.size(); i++) {
>         target[i] = y.get(i).doubleValue();
>     }
>     return target;
> }
>
> private double[][] jacobian(double[] variables) {
>     double[][] jacobian = new double[x.size()][3];
>     for (int i = 0; i < jacobian.length; ++i) {
>         jacobian[i][0] = x.get(i) * x.get(i);
>         jacobian[i][1] = x.get(i);
>         jacobian[i][2] = 1.0;
>     }
>     return jacobian;
> }
>
> public double[] value(double[] variables) {
>     double[] values = new double[x.size()];
>     for (int i = 0; i < values.length; ++i) {
>         values[i] = (variables[0] * x.get(i) + variables[1]) *
> x.get(i) + variables[2];
>     }
>     return values;
> }
>
> public MultivariateMatrixFunction jacobian() {
>     return new MultivariateMatrixFunction() {
>         private static final long serialVersionUID = 
> -8673650298627399464L;
>         public double[][] value(double[] point) {
>             return jacobian(point);
>         }
>     };
> }
> }


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


Mime
View raw message