no.uib.cipr.matrix.DenseMatrix Java Examples

The following examples show how to use no.uib.cipr.matrix.DenseMatrix. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MtjMatrixTest.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testMultiply() {
  double[][] tmpMatrix = { { 1, 2, 3 }, { 4, 5, 6 } };
  double[] tmpVector = { 1, 2, 3 };
  double[] resultExpected = { 14, 32 };
  DenseMatrix denseMatrix = new DenseMatrix(tmpMatrix);
  DenseVector denseVector = new DenseVector(tmpVector);
  AbstractMatrix abstractMatrix = new MtjMatrix(denseMatrix);
  AbstractVector abstractVector = new MtjVector(denseVector);

  AbstractVector resultActual = abstractMatrix.multiply(abstractVector);
  assertEquals(resultExpected.length, resultActual.getDimension());
  for (int i = 0; i < resultActual.getDimension(); i++) {
    assertEquals(resultExpected[i], resultActual.getEntry(i), Helper.TOLERANCE);
  }

}
 
Example #2
Source File: MtjMatrixTest.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetSubMatrixDense() {
  double[][] tmpMatrix = { { 1, 2, 3 }, { 4, 5, 6 } };
  double[][] expteced = { { 5, 6 } };
  DenseMatrix denseMatrix = new DenseMatrix(tmpMatrix);
  AbstractMatrix abstractMatrix = new MtjMatrix(denseMatrix);

  AbstractMatrix resultActual = abstractMatrix.getSubMatrix(1, 1, 1, 2);
  assertEquals(1, resultActual.getRowDimension());
  assertEquals(2, resultActual.getColumnDimension());
  for (int i = 0; i < resultActual.getRowDimension(); i++) {
    for (int j = 0; j < resultActual.getColumnDimension(); j++) {
      assertEquals(expteced[i][j], resultActual.getEntry(i, j), Helper.TOLERANCE);
    }
  }
}
 
Example #3
Source File: LinkedSparseMatrixTest.java    From matrix-toolkits-java with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void ignoredTimedTransMult() {
    Stopwatch watch = Stopwatch.createUnstarted();
    DenseMatrix dense = new DenseMatrix(1000, 1000);
    int[][] nz = Utilities.getRowPattern(dense.numRows(),
            dense.numColumns(), 100);
    Utilities.rowPopulate(dense, nz);
    log.info("created matrices");
    Matrix sparse = new LinkedSparseMatrix(dense.numRows(),
            dense.numColumns());
    sparse.set(dense);

    for (Matrix m : Lists.newArrayList(dense, sparse)) {
        log.info("starting " + m.getClass());
        Matrix t = new DenseMatrix(m);
        Matrix o = new DenseMatrix(dense.numRows(), dense.numColumns());
        log.info("warming up " + m.getClass() + " " + o.getClass());
        for (int i = 0; i < 10; i++)
            m.transAmult(t, o);
        log.info("starting " + m.getClass() + " " + o.getClass());
        watch.start();
        for (int i = 0; i < 100; i++)
            m.transAmult(t, o);
        watch.stop();
        log.info(m.getClass() + " " + o.getClass() + " " + watch);
    }
}
 
Example #4
Source File: MatrixVectorIoTest.java    From matrix-toolkits-java with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testWriteRead() throws Exception {
    DenseMatrix mat = new DenseMatrix(
            new double[][]{{1.1, 1.2}, {1.3, 1.4}});
    File matrixFile = new File("TestMatrixFile");
    BufferedWriter out = new BufferedWriter(new FileWriter(matrixFile));
    MatrixVectorWriter writer = new MatrixVectorWriter(out);
    MatrixInfo mi = new MatrixInfo(false, MatrixInfo.MatrixField.Real,
            MatrixInfo.MatrixSymmetry.General);
    writer.printMatrixInfo(mi);
    writer.printMatrixSize(new MatrixSize(mat.numRows(), mat.numColumns(),
            mat.numColumns() * mat.numRows()), mi);
    writer.printArray(mat.getData());
    writer.close();
    Matrix newMat = new DenseMatrix(new MatrixVectorReader(new FileReader(
            matrixFile)));

    MatrixTestAbstract.assertMatrixEquals(mat, newMat);
}
 
Example #5
Source File: MtjMatrix.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
public MtjMatrix(int rowDimension, int columnDimension, boolean sparse) {
  if (sparse) {
    this.matrix = new FlexCompRowMatrix(rowDimension, columnDimension);
  } else {
    this.matrix = new DenseMatrix(rowDimension, columnDimension);
  }
}
 
Example #6
Source File: GMRES.java    From matrix-toolkits-java with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Sets the restart parameter
 * 
 * @param restart
 *            GMRES iteration is restarted after this number of iterations
 */
public void setRestart(int restart) {
    this.restart = restart;
    if (restart <= 0)
        throw new IllegalArgumentException(
                "restart must be a positive integer");

    s = new DenseVector(restart + 1);
    H = new DenseMatrix(restart + 1, restart);
    rotation = new GivensRotation[restart + 1];

    v = new Vector[restart + 1];
    for (int i = 0; i < v.length; ++i)
        v[i] = r.copy().zero();
}
 
Example #7
Source File: AMG.java    From matrix-toolkits-java with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Solves directly at the coarsest level
 */
private void directSolve() {
    int k = m - 1;
    u[k].set(f[k]);
    DenseMatrix U = new DenseMatrix(u[k], false);

    if (transpose)
        lu.transSolve(U);
    else
        lu.solve(U);
}
 
Example #8
Source File: LinkedSparseMatrixTest.java    From matrix-toolkits-java with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void ignoredTimedMult() {
    Stopwatch watch = Stopwatch.createUnstarted();
    DenseMatrix dense = new DenseMatrix(1000, 1000);
    int[][] nz = Utilities.getRowPattern(dense.numRows(),
            dense.numColumns(), 100);
    Utilities.rowPopulate(dense, nz);
    log.info("created matrices");
    Matrix sparse = new LinkedSparseMatrix(dense.numRows(),
            dense.numColumns());
    sparse.set(dense);

    for (Matrix m : Lists.newArrayList(dense, sparse)) {
        log.info("starting " + m.getClass());
        Matrix t = new DenseMatrix(m);
        t.transpose();
        Matrix o = new DenseMatrix(dense.numRows(), dense.numColumns());
        log.info("warming up " + m.getClass() + " " + o.getClass());
        for (int i = 0; i < 10; i++)
            m.mult(t, o);
        log.info("starting " + m.getClass() + " " + o.getClass());
        watch.start();
        for (int i = 0; i < 100; i++)
            m.mult(t, o);
        watch.stop();
        log.info(m.getClass() + " " + o.getClass() + " " + watch);
    }
}
 
Example #9
Source File: MatrixTools.java    From systemsgenetics with GNU General Public License v3.0 5 votes vote down vote up
public static DenseDoubleMatrix2D toDenseDoubleMatrix(DenseMatrix matrix) {
    DenseDoubleMatrix2D matrix2D = new DenseDoubleMatrix2D(matrix.numRows(), matrix.numColumns());
    
    for(int c = 0; c<matrix.numColumns(); c++){
        for(int r = 0; r<matrix.numRows(); r++){
            matrix2D.setQuick(r,c, matrix.get(r, c));
        }
    }
    
    return matrix2D;
}
 
Example #10
Source File: MtjMatrix.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public MtjMatrix(double[][] matrix) {
  this.matrix = new DenseMatrix(matrix);
}
 
Example #11
Source File: SVDimpute.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
/**
	 * Parse the DenseMatrix of INPUT real values to a String 2D array, ready for printing
	 * to a file. It also fits the values to the original bounds if needed.
	 * @param mat The DenseMatrix with the input values in double format
	 * @param X The output String matrix, ready to be printed
	 * @param IS The InstanceSet with the original values, used to obtain the OUTPUT values
	 */
	protected void data2string(DenseMatrix mat, String [][] X,InstanceSet IS){
		Attribute a;
		Instance inst;
		double value;
		int in,out;
		
		for(int i=0;i<X.length;i++){
			in = 0;
			out = 0;
			inst = IS.getInstance(i);
			for(int j=0;j<X[i].length;j++){
				a = Attributes.getAttribute(j);
				//older version - SVDi only used inputs attributes
//				if(a.getDirectionAttribute() == Attribute.INPUT){
//					value = mat.get(i, in);
//					in++;
//				}
//				else{
//					value = inst.getAllOutputValues()[out];
//					out++;
//				}
				
				value = mat.get(i, j);
				if(a.getType() != Attribute.NOMINAL){
					if(value < a.getMinAttribute())
						value = a.getMinAttribute();
					else if(value > a.getMaxAttribute())
						value = a.getMaxAttribute();
				}

				if(a.getType() == Attribute.REAL)
					X[i][j] = String.valueOf(value);
				else if(a.getType() == Attribute.INTEGER)
					X[i][j] = String.valueOf(Math.round(value));
				else{
					value =  Math.round(value);
					if(value >= a.getNumNominalValues())
						value = a.getNumNominalValues()-1;
					if(value < 0)
						value = 0;
					
					X[i][j] = a.getNominalValue((int)value);
				}
			}
		}
		
	}
 
Example #12
Source File: BPCA.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Parse the DenseMatrix of INPUT real values to a String 2D array, ready for printing
 * to a file. It also fits the values to the original bounds if needed.
 * @param mat The DenseMatrix with the input values in double format
 * @param X The output String matrix, ready to be printed
 * @param IS The InstanceSet with the original values, used to obtain the OUTPUT values
 */
protected void data2string(DenseMatrix mat, String [][] X,InstanceSet IS){
	Attribute a;
	Instance inst;
	double value;
	int in,out;
	
	for(int i=0;i<X.length;i++){
		in = 0;
		out = 0;
		inst = IS.getInstance(i);
		for(int j=0;j<X[i].length;j++){
			a = Attributes.getAttribute(j);
			if(a.getDirectionAttribute() == Attribute.INPUT){
				value = mat.get(i, in);
				in++;
			}
			else{
				value = inst.getAllOutputValues()[out];
				out++;
			}
			if(a.getType() != Attribute.NOMINAL){
				if(value < a.getMinAttribute())
					value = a.getMinAttribute();
				else if(value > a.getMaxAttribute())
					value = a.getMaxAttribute();
			}

			if(a.getType() == Attribute.REAL)
				X[i][j] = String.valueOf(value);
			else if(a.getType() == Attribute.INTEGER)
				X[i][j] = String.valueOf(Math.round(value));
			else{
				value =  Math.round(value);
				if(value >= a.getNumNominalValues())
					value = a.getNumNominalValues()-1;
				if(value < 0)
					value = 0;
				
				X[i][j] = a.getNominalValue((int)value);
			}
		}
	}
	
}
 
Example #13
Source File: TaggerEmbeddings.java    From EasySRL with Apache License 2.0 4 votes vote down vote up
public TaggerEmbeddings(final File modelFolder, final double beta, final int maxTagsPerWord,
		final CutoffsDictionaryInterface cutoffs) throws IOException {
	super(cutoffs, beta, loadCategories(new File(modelFolder, "categories")), maxTagsPerWord);
	try {
		final FilenameFilter embeddingsFileFilter = new PatternFilenameFilter("embeddings.*");

		// If we're using POS tags or lexical features, load l.
		this.posFeatures = loadSparseFeatures(new File(modelFolder + "/postags"));
		this.lexicalFeatures = loadSparseFeatures(new File(modelFolder + "/frequentwords"));

		// Load word embeddings.
		embeddingsFeatures = loadEmbeddings(true, modelFolder.listFiles(embeddingsFileFilter));

		// Load embeddings for capitalization and suffix features.
		discreteFeatures = new HashMap<>();
		discreteFeatures.putAll(loadEmbeddings(false, new File(modelFolder, "capitals")));
		discreteFeatures.putAll(loadEmbeddings(false, new File(modelFolder, "suffix")));
		totalFeatures = (embeddingsFeatures.get(unknownLower).length + discreteFeatures.get(unknownSuffix).length
				+ discreteFeatures.get(capsLower).length + posFeatures.size() + lexicalFeatures.size())
				* (2 * contextWindow + 1);

		// Load the list of categories used by the model.
		categoryToIndex = new HashMap<>();
		for (int i = 0; i < lexicalCategories.size(); i++) {
			categoryToIndex.put(lexicalCategories.get(i), i);
		}

		// Load the weight matrix used by the classifier.
		weightMatrix = new DenseMatrix(lexicalCategories.size(), totalFeatures);
		loadMatrix(weightMatrix, new File(modelFolder, "classifier"));

		weightMatrixRows = new ArrayList<>(lexicalCategories.size());
		for (int i = 0; i < lexicalCategories.size(); i++) {
			final Vector row = new DenseVector(totalFeatures);
			for (int j = 0; j < totalFeatures; j++) {
				row.set(j, weightMatrix.get(i, j));
			}
			weightMatrixRows.add(row);
		}

		bias = new DenseVector(lexicalCategories.size());

		loadVector(bias, new File(modelFolder, "bias"));

	} catch (final Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #14
Source File: TaggerEmbeddings.java    From easyccg with MIT License 4 votes vote down vote up
public TaggerEmbeddings(File modelFolder, int maxSentenceLength, double beta, int maxTagsPerWord) {
  try {
    FilenameFilter embeddingsFileFilter = new PatternFilenameFilter("embeddings.*");

    // If we're using POS tags or lexical features, load l.
    this.posFeatures = loadSparseFeatures(new File(modelFolder + "/postags"));     
    this.lexicalFeatures = loadSparseFeatures(new File(modelFolder + "/frequentwords"));     
    
    // Load word embeddings.
    embeddingsFeatures = loadEmbeddings(true, modelFolder.listFiles(embeddingsFileFilter));
    
    // Load embeddings for capitalization and suffix features.
    discreteFeatures = new HashMap<String, double[]>();
    discreteFeatures.putAll(loadEmbeddings(false, new File(modelFolder, "capitals")));
    discreteFeatures.putAll(loadEmbeddings(false, new File(modelFolder, "suffix")));
    totalFeatures = 
      (embeddingsFeatures.get(unknownLower).length + 
          discreteFeatures.get(unknownSuffix).length + 
          discreteFeatures.get(capsLower).length +
          posFeatures.size()  + lexicalFeatures.size())       * (2 * contextWindow + 1);
    
    // Load the list of categories used by the model.
    lexicalCategories = loadCategories(new File(modelFolder, "categories"));
    
    // Load the weight matrix used by the classifier.
    weightMatrix = new DenseMatrix(lexicalCategories.size(), totalFeatures);
    loadMatrix(weightMatrix, new File(modelFolder, "classifier"));

    weightMatrixRows = new ArrayList<Vector>(lexicalCategories.size());
    for (int i=0; i<lexicalCategories.size(); i++) {
      Vector row = new DenseVector(totalFeatures);
      for (int j=0; j<totalFeatures; j++) {
        row.set(j, weightMatrix.get(i, j));
      }
      weightMatrixRows.add(row);
    }

    bias = new DenseVector(lexicalCategories.size());
    this.beta = beta;
    this.maxTagsPerWord = maxTagsPerWord;

    int maxCategoryID = 0;
    for (Category c : lexicalCategories) {
      maxCategoryID = Math.max(maxCategoryID, c.getID());
    }
    
    this.tagDict = ImmutableMap.copyOf(loadTagDictionary(modelFolder));
    
    terminalFactory = new SyntaxTreeNodeFactory(maxSentenceLength, maxCategoryID);
    loadVector(bias, new File(modelFolder, "bias"));

  }
  catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #15
Source File: AMG.java    From matrix-toolkits-java with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void setMatrix(Matrix A) {
    List<CompRowMatrix> Al = new LinkedList<CompRowMatrix>();
    List<CompColMatrix> Il = new LinkedList<CompColMatrix>();

    Al.add(new CompRowMatrix(A));

    for (int k = 0; Al.get(k).numRows() > min; ++k) {

        CompRowMatrix Af = Al.get(k);

        double eps = 0.08 * Math.pow(0.5, k);

        // Create the aggregates
        Aggregator aggregator = new Aggregator(Af, eps);

        // If no aggregates were created, no interpolation operator will be
        // created, and the setup phase stops
        if (aggregator.getAggregates().isEmpty())
            break;

        // Create an interpolation operator using smoothing. This also
        // creates the Galerkin operator
        Interpolator sa = new Interpolator(aggregator, Af, omega);

        Al.add(sa.getGalerkinOperator());
        Il.add(sa.getInterpolationOperator());
    }

    // Copy to array storage
    m = Al.size();
    if (m == 0)
        throw new RuntimeException("Matrix too small for AMG");

    I = new CompColMatrix[m - 1];
    this.A = new CompRowMatrix[m - 1];

    Il.toArray(I);
    for (int i = 0; i < Al.size() - 1; ++i)
        this.A[i] = Al.get(i);

    // Create a LU decomposition of the smallest Galerkin matrix
    DenseMatrix Ac = new DenseMatrix(Al.get(Al.size() - 1));
    lu = new DenseLU(Ac.numRows(), Ac.numColumns());
    lu.factor(Ac);

    // Allocate vectors at each level
    u = new DenseVector[m];
    f = new DenseVector[m];
    r = new DenseVector[m];
    for (int k = 0; k < m; ++k) {
        int n = Al.get(k).numRows();
        u[k] = new DenseVector(n);
        f[k] = new DenseVector(n);
        r[k] = new DenseVector(n);
    }

    // Set up the SSOR relaxation schemes
    preM = new SSOR[m - 1];
    postM = new SSOR[m - 1];
    for (int k = 0; k < m - 1; ++k) {
        CompRowMatrix Ak = this.A[k];
        preM[k] = new SSOR(Ak, reverse, omegaPreF, omegaPreR);
        postM[k] = new SSOR(Ak, reverse, omegaPostF, omegaPostR);
        preM[k].setMatrix(Ak);
        postM[k].setMatrix(Ak);
    }
}
 
Example #16
Source File: EV.java    From KEEL with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Copy constructor (soft)
 * @param eigenVectors the original eigenVectors 
 * @param eigenValues the original eigenValues
 */
public EV(DenseMatrix eigenVectors,double[] eigenValues){
	V = eigenVectors;
	d = eigenValues;
}