Example usage for org.apache.commons.math3.linear RealMatrix getColumnDimension

List of usage examples for org.apache.commons.math3.linear RealMatrix getColumnDimension

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealMatrix getColumnDimension.

Prototype

int getColumnDimension();

Source Link

Document

Returns the number of columns in the matrix.

Usage

From source file:Rotationforest.rotationalgo.java

public static void main(String[] args) {
    // TODO code application logic here

    double[][] a = new double[303][7];
    double[][] b = new double[303][7];
    double[][] c = new double[225][7];
    double[][] d = new double[225][7];
    double sum[] = new double[7];
    double mean[];
    double[][] inputs = new double[303][14];
    final RealMatrix cov;
    final RealMatrix cov1;

    read obj = new read();
    ArrayList<values> ar = new ArrayList<values>();
    ar = obj.run();/* ww w .  ja v  a 2  s.c  o  m*/
    // System.out.println("records= " + ar.size());
    Iterator<values> arIterator = ar.iterator();
    int i = 0;
    while (arIterator.hasNext()) {
        values values = arIterator.next();
        //inputs= new double[][]{{values.age,values.sex,values.cp,values.restbps,values.chol,values.fbs,values.restecg,values.thalach,values.exang,values.oldpeak,values.slope,values.ca,values.thal,values.clas }};

        inputs[i][0] = values.age;
        inputs[i][1] = values.sex;
        inputs[i][2] = values.cp;
        inputs[i][3] = values.restbps;
        inputs[i][4] = values.chol;
        inputs[i][5] = values.fbs;
        inputs[i][6] = values.restecg;
        inputs[i][7] = values.thalach;
        inputs[i][8] = values.exang;
        inputs[i][9] = values.oldpeak;
        inputs[i][10] = values.slope;
        inputs[i][11] = values.ca;
        inputs[i][12] = values.thal;
        inputs[i][13] = values.clas;
        i++;

        /*for (int m = 0; m <inputs.length; m++) {
        for (int n = 0; n <inputs[0].length; n++) {
        //System.out.print(inputs[m][n] + " ");
        }
        //System.out.print("\n\n\n");
        } */
    }
    /*  final   double [][]in =new double[303][14];
     for (int r=0; r<inputs.length; r++) {
    for (int c=0; c<inputs[r].length; c++) {
     in[r][c]=inputs[r][c];
    }
    }*/

    for (int m = 0; m < inputs.length; m++) {
        for (int n = 0; n <= 6; n++) {
            a[m][n] = inputs[m][n];
        }
    }

    // System.out.println(" main inputs = " + inputs);
    // Corresponding outputs, xor training data
    /*    
     for (int m = 0; m <inputs.length; m++) {
    for (int n = 0; n <=6; n++) {
        System.out.print(a[m][n] + " ");
    }
    System.out.print("\n\n\n");
    } */

    int k = 7;
    /*
    for (int m = 0; m <inputs.length; m++) {
     for (int n = 0; n <=6 && k<=13; n++,k++) {
        b[m][n]=inputs[m][k];
        System.out.print(inputs[m][k] + "");
        }
        System.out.println("\n");
     }
    */

    for (int m = 0; m < inputs.length; m++) {
        for (int n = 0; n < 7; n++) {
            b[m][n] = inputs[m][k];
            k++;
        }
        k = 7;
    }

    /*      
    for (int m = 0; m <b.length; m++) {
    for (int n = 0; n <=6; n++) {
    System.out.print(b[m][n] + " ");
    }
    System.out.print("\n\n"); 
    }
    */

    for (int m = 0; m < 225; m++) {
        for (int n = 0; n <= 6; n++) {
            c[m][n] = a[m][n];
        }
    }

    //printing c
    /*
    for (int m = 0; m <225; m++) {
    for (int n = 0; n <=6; n++) {
     System.out.print(c[m][n] + " ");
    }
    System.out.print("\n\n");
            
            
     }
    */

    for (int m = 0; m < 225; m++) {
        for (int n = 0; n <= 6; n++) {
            d[m][n] = b[m][n];

        }

    }

    Mean ob = new Mean();
    PCAMEAN ob1 = new PCAMEAN();
    Covariance b1 = new Covariance();

    double[] meanc = ob.calmean(c);
    double[] meand = ob.calmean(d);
    double[][] adc = ob1.getMeanAdjusted(c, meanc);
    double[][] add = ob1.getMeanAdjusted(d, meand);
    cov = b1.computeCovarianceMatrix(adc);
    cov1 = b1.computeCovarianceMatrix(add);

    // double[][]digi=ob1.covar(adc);
    int p = cov.getRowDimension();
    int r = cov.getColumnDimension();
    double[][] cc = new double[p][r];
    double[][] cd = new double[p][r];
    //print realmatrix

    for (int m = 0; m < p; m++) {
        for (int n = 0; n < r; n++) {
            cc[m][n] = cov.getEntry(m, n);
        }

    }
    for (int m = 0; m < p; m++) {
        for (int n = 0; n < r; n++) {
            cd[m][n] = cov1.getEntry(m, n);
        }

    }

    eigencal e = new eigencal();
    double[][] evectorc = e.eigenv(cc);
    double[][] evectord = e.eigenv(cd);

    int r1 = evectord.length + evectorc.length;
    int r2 = evectord[0].length + evectorc[0].length;

    rot = new double[r1][r2];

    for (int m = 0; m < r1; m++) {
        for (int n = 0; n < r2; n++) {
            rot[m][n] = 0;
        }
    }

    for (int m = 0; m < evectorc.length; m++) {
        for (int n = 0; n < evectorc[0].length; n++) {
            rot[m][n] = evectorc[m][n];
        }

        for (int m1 = 7, m3 = 0; m < evectord.length; m1++, m3++) {
            for (int n = 7, m4 = 0; n < evectord[0].length; n++, m4++) {
                rot[m][n] = evectord[m3][m4];
            }

        }

    }
    for (int m = 0; m < r1; m++) {
        for (int n = 0; n < r2; n++) {
            System.out.print(rot[m][n] + " ");
        }
        System.out.print("\n\n");

    }

    System.out.println("Splited the data set into two arrays a & b");
    System.out.println("Readed" + "\t" + c.length + "\t" + "records and 7 attributes in array a");
    System.out.println("Readed" + "\t" + d.length + "\t" + "records and 7 attributes in array b");
}

From source file:scorePairing.ScorePairingWithStaticMethods.java

public static void translateBarycenterListOfPointToOrigin(RealMatrix matrix, RealVector barycenter) {

    int countOfPoint = matrix.getColumnDimension();

    for (int i = 0; i < countOfPoint; i++) {
        matrix.addToEntry(0, i, -1.0 * barycenter.getEntry(0));
        matrix.addToEntry(1, i, -1.0 * barycenter.getEntry(1));
        matrix.addToEntry(2, i, -1.0 * barycenter.getEntry(2));
    }//w  w  w  .j av  a2s  .  c  o  m
}

From source file:shapeCompare.ProcrustesAnalysis.java

private float computeResidual(RealMatrix matrixPointsModel, RealMatrix rotatedmatrixPointsCandidate) {

    float distance;

    double sumSquareDeltaDistances = 0.0;

    for (int i = 0; i < matrixPointsModel.getColumnDimension(); i++) {
        RealVector vectorFromModel = matrixPointsModel.getColumnVector(i);
        RealVector vectorFromCandidate = rotatedmatrixPointsCandidate.getColumnVector(i);

        distance = (float) vectorFromModel.getDistance(vectorFromCandidate);
        if (algoParameters != null) {
            float correctedDistance = computeCorrectedDistance(distance);
            sumSquareDeltaDistances += (correctedDistance * correctedDistance);
            continue;
        }//from   w  ww . j a  v  a2  s.c o  m
        sumSquareDeltaDistances += distance * distance;
    }

    float rmsd = (float) Math.sqrt(sumSquareDeltaDistances / matrixPointsModel.getColumnDimension());

    return rmsd;
}

From source file:stats.KendallsCorrelation.java

/**
 * Computes the Kendall's Tau rank correlation matrix for the columns of the
 * input matrix./*w  w w  . j a v  a2 s.  c o  m*/
 *
 * @param matrix matrix with columns representing variables to correlate
 * @return correlation matrix
 */
public RealMatrix computeCorrelationMatrix(final RealMatrix matrix) {
    int nVars = matrix.getColumnDimension();
    RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < i; j++) {
            double corr = correlation(matrix.getColumn(i), matrix.getColumn(j)).getFirst();
            outMatrix.setEntry(i, j, corr);
            outMatrix.setEntry(j, i, corr);
        }
        outMatrix.setEntry(i, i, 1d);
    }
    return outMatrix;
}

From source file:stats.SpearmansCorrelation.java

/**
 * Applies rank transform to each of the columns of <code>matrix</code> using
 * the current <code>rankingAlgorithm</code>.
 *
 * @param matrix/*  w  w  w. j  a  va 2s .  c o  m*/
 *          matrix to transform
 * @return a rank-transformed matrix
 */
private RealMatrix rankTransform(final RealMatrix matrix) {
    RealMatrix transformed = null;

    if (rankingAlgorithm instanceof NaturalRanking
            && ((NaturalRanking) rankingAlgorithm).getNanStrategy() == NaNStrategy.REMOVED) {
        final Set<Integer> nanPositions = new HashSet<Integer>();
        for (int i = 0; i < matrix.getColumnDimension(); i++) {
            nanPositions.addAll(getNaNPositions(matrix.getColumn(i)));
        }

        // if we have found NaN values, we have to update the matrix size
        if (!nanPositions.isEmpty()) {
            transformed = new BlockRealMatrix(matrix.getRowDimension() - nanPositions.size(),
                    matrix.getColumnDimension());
            for (int i = 0; i < transformed.getColumnDimension(); i++) {
                transformed.setColumn(i, removeValues(matrix.getColumn(i), nanPositions));
            }
        }
    }

    if (transformed == null) {
        transformed = matrix.copy();
    }

    for (int i = 0; i < transformed.getColumnDimension(); i++) {
        transformed.setColumn(i, rankingAlgorithm.rank(transformed.getColumn(i)));
    }

    return transformed;
}

From source file:syncleus.gremlann.topology.adjacency.RealMatrixAdjacency.java

public static void fill(final RealMatrix m, final double value) {
    if (m instanceof Array2DRowRealMatrix) {
        Array2DRowRealMatrix a = (Array2DRowRealMatrix) m;
        for (double[] n : a.getDataRef())
            Arrays.fill(n, value);
    } else {// ww w .  ja va  2 s.c  o  m
        for (int i = 0; i < m.getRowDimension(); i++) {
            for (int j = 0; j < m.getColumnDimension(); j++) {
                m.setEntry(i, j, value);
            }
        }

    }
}

From source file:us.levk.math.linear.EucledianDistanceClusterer.java

public Cluster eucledian(final RealMatrix original) throws IOException {
    try (HugeRealMatrix distances = new HugeRealMatrix(original.getRowDimension(),
            original.getRowDimension())) {
        final Map<Integer, Cluster> genehash = new HashMap<Integer, Cluster>() {
            private static final long serialVersionUID = 1L;

            {/*from w  w w  .j  a  va  2  s  .c o  m*/
                for (int index = original.getRowDimension(); --index >= 0; put(index, new Cluster(index)))
                    ;
            }
        };
        TreeMap<Double, int[]> sorted = new TreeMap<>();

        log.debug("Populating distance matrix");
        for (int i = 0; i < original.getRowDimension(); i++) {
            for (int j = i + 1; j < original.getRowDimension(); j++) {
                // Euclidean distance calculation.
                double total = 0;
                for (int k = 0; k < original.getColumnDimension(); k++) {
                    double left = original.getEntry(i, k);
                    double right = original.getEntry(j, k);
                    if (!isNaN(left) && !isNaN(right) && !isInfinite(left) && !isInfinite(right))
                        total += Math.pow(left - right, 2);
                }
                double distance = Math.pow(total, 0.5);

                distances.setEntry(i, j, distance);
                distances.setEntry(j, i, distance);
                int[] genePair = { i, j };
                // Enter the distance calculated and the genes measured into a
                // treemap. Will be automatically sorted.
                sorted.put(distance, genePair);
            }
        }
        log.debug("Initialized distances matrix " + distances);

        while (true) {
            // Get the first key of the TreeMap. Will be the shortest distance de
            // facto.
            final double minkey = (Double) sorted.firstKey();
            int[] minValues = (int[]) sorted.firstEntry().getValue();

            final int value1 = minValues[0], value2 = minValues[1];
            // find

            Cluster cluster = new Cluster(genehash.get(value1), genehash.get(value2)) {
                {
                    log.debug("Generating cluster from " + value1 + " and " + value2 + " in " + genehash);
                    contains().addAll(genehash.get(value1).contains());
                    contains().addAll(genehash.get(value2).contains());
                    d(minkey);
                    log.debug("Generated cluster " + this);
                }
            };

            genehash.put(cluster.id(), cluster);
            genehash.remove(value1);
            genehash.remove(value2);

            if (genehash.size() <= 1)
                break;

            // Iterate over all the current clusters to remeasure distance with the
            // previously clustered group.
            for (Cluster c : genehash.values()) {
                // Skip measuring the new cluster with itself.
                if (c == cluster)
                    continue;

                double distance = 0;
                int n = 0;
                // Get genes from each cluster. Distance is measured from each element
                // to every element.
                for (int current : c.contains())
                    for (int created : cluster.contains()) {
                        distance += distances.getEntry(current, created);
                        n++;
                    }

                distance = distance / n;

                int[] valuePair = { c.id(), cluster.id() };
                sorted.put(distance, valuePair);
            }

            // Get the shortest distance.
            // Check to make sure shortest distance does not include a gene pair
            // that
            // has already had its elements clustered.
            boolean minimized = false;
            while (!minimized) {
                double mk = sorted.firstKey();
                minValues = sorted.firstEntry().getValue();
                // If the gene pair is not present in the current gene set, remove
                // this
                // distance.
                if (!genehash.containsKey(minValues[0]) || !genehash.containsKey(minValues[1]))
                    sorted.remove(mk);
                else
                    minimized = true;
            }
        }

        return genehash.entrySet().iterator().next().getValue();
    }
}