org.opentestsystem.airose.regression.orderedprobit.LinearEquationSolver.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.airose.regression.orderedprobit.LinearEquationSolver.java

Source

/*******************************************************************************
 * Copyright (c) 2013 American Institutes for Research
 * 
 * This file is part of AIROSE.
 * 
 * AIROSE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * AIROSE 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AIROSE.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package org.opentestsystem.airose.regression.orderedprobit;

import org.apache.commons.math3.linear.*;

public class LinearEquationSolver {

    /*
     * If there are n observation points and k dimensions then XpX is a n X k
     * matrix. XpY is a n X 1 matrix. The returned matrix is will be a k X 1
     * matrix.
     */
    public static double[][] SolveLinear(double[][] XpX, double[][] XpY) {

        RealMatrix coefficients = new Array2DRowRealMatrix(XpX, false);
        LUDecomposition luDecomposition = new LUDecomposition(coefficients);

        DecompositionSolver solver = luDecomposition.getSolver();

        double[] yRowVector = new double[XpY.length];
        for (int counter1 = 0; counter1 < XpY.length; ++counter1) {
            yRowVector[counter1] = XpY[counter1][0];
        }

        RealVector constants = new ArrayRealVector(yRowVector, false);
        RealVector solution = solver.solve(constants);

        double[][] result = new double[solution.getDimension()][1];
        for (int counter1 = 0; counter1 < solution.getDimension(); ++counter1) {
            result[counter1][0] = solution.getEntry(counter1);
        }
        return result;
    }

    public static void main(String argv[]) {
        double[][] xData = new double[][] { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } };
        double[][] yData = new double[][] { { 1 }, { -2 }, { 1 } };

        double[][] result = SolveLinear(xData, yData);
        for (int counter1 = 0; counter1 < result.length; ++counter1) {
            System.err.println(result[counter1][0]);
        }
    }
}