/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.geoforge.alg.regression;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import org.geoforge.alg.function.GfrFunctionPolynomial3;
import org.geoforge.alg.jama.JamMatrix;
/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*/
public class GfrRegreessionPolDim3
{
/*
* !!!! Done in a hurry, x and y are swapped for specific need,
* should be
*/
public static GfrFunctionPolynomial3 s_getRegressionPolynomialOrder3(
ArrayList<Point2D.Double> altP2dData) throws Exception
{
return GfrRegreessionPolDim3.s_getRegressionPolynomialOrder3(
altP2dData,
true);
}
/*
* if isRegressionNotReverse:
* regression is Y = a.X^3 + b.x^2 + x.X +d
* else
* regression is X = a.Y^3 + b.Y^2 + x.Y +d
*/
public static GfrFunctionPolynomial3 s_getRegressionPolynomialOrder3(
ArrayList<Point2D.Double> altP2dData,
boolean isRegressionNotReverse)
{
int intSizeDataSet = altP2dData.size();
if (intSizeDataSet < 4)
throw new UnsupportedOperationException("intSizeDataSet < 4");
double dblS1 = 0.0d;
double dblS2 = 0.0d;
double dblS3 = 0.0d;
double dblS4 = 0.0d;
double dblS5 = 0.0d;
double dblS6 = 0.0d;
double dblT0 = 0.0d;
double dblT1 = 0.0d;
double dblT2 = 0.0d;
double dblT3 = 0.0d;
for (int i = 0; i < intSizeDataSet; i++)
{
if (isRegressionNotReverse)
{
dblS1 += Math.pow(altP2dData.get(i).getX(), 1.0d);
dblS2 += Math.pow(altP2dData.get(i).getX(), 2.0d);
dblS3 += Math.pow(altP2dData.get(i).getX(), 3.0d);
dblS4 += Math.pow(altP2dData.get(i).getX(), 4.0d);
dblS5 += Math.pow(altP2dData.get(i).getX(), 5.0d);
dblS6 += Math.pow(altP2dData.get(i).getX(), 6.0d);
dblT0 += Math.pow(altP2dData.get(i).getX(), 0.0d) * altP2dData.get(i).getY();
dblT1 += Math.pow(altP2dData.get(i).getX(), 1.0d) * altP2dData.get(i).getY();
dblT2 += Math.pow(altP2dData.get(i).getX(), 2.0d) * altP2dData.get(i).getY();
dblT3 += Math.pow(altP2dData.get(i).getX(), 3.0d) * altP2dData.get(i).getY();
}
else
{
dblS1 += Math.pow(altP2dData.get(i).getY(), 1.0d);
dblS2 += Math.pow(altP2dData.get(i).getY(), 2.0d);
dblS3 += Math.pow(altP2dData.get(i).getY(), 3.0d);
dblS4 += Math.pow(altP2dData.get(i).getY(), 4.0d);
dblS5 += Math.pow(altP2dData.get(i).getY(), 5.0d);
dblS6 += Math.pow(altP2dData.get(i).getY(), 6.0d);
dblT0 += Math.pow(altP2dData.get(i).getY(), 0.0d) * altP2dData.get(i).getX();
dblT1 += Math.pow(altP2dData.get(i).getY(), 1.0d) * altP2dData.get(i).getX();
dblT2 += Math.pow(altP2dData.get(i).getY(), 2.0d) * altP2dData.get(i).getX();
dblT3 += Math.pow(altP2dData.get(i).getY(), 3.0d) * altP2dData.get(i).getX();
}
}
double[][] dblsValuesMatOne =
{
{
dblS6, dblS5, dblS4, dblS3
},
{
dblS5, dblS4, dblS3, dblS2
},
{
dblS4, dblS3, dblS2, dblS1
},
{
dblS3, dblS2, dblS1, intSizeDataSet
},
};
double[][] dblsValuesMatTwo =
{
{
dblT3
},
{
dblT2
},
{
dblT1
},
{
dblT0
},
};
JamMatrix matOne = new JamMatrix(dblsValuesMatOne);
JamMatrix matTwo = new JamMatrix(dblsValuesMatTwo);
System.out.println(matOne.toString());
JamMatrix matOneInverted = matOne.inverse();
JamMatrix matSolution = matOneInverted.times(matTwo);
GfrFunctionPolynomial3 fnc = new GfrFunctionPolynomial3(
matSolution.get(0, 0),
matSolution.get(1, 0),
matSolution.get(2, 0),
matSolution.get(3, 0));
return fnc;
}
}