All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mikera.matrixx.impl.PermutationMatrix Maven / Gradle / Ivy

Go to download

Fast double-precision vector and matrix maths library for Java, supporting N-dimensional numeric arrays.

There is a newer version: 0.67.0
Show newest version
package mikera.matrixx.impl;

import java.util.Arrays;

import mikera.arrayz.ISparse;
import mikera.indexz.Index;
import mikera.indexz.Indexz;
import mikera.matrixx.AMatrix;
import mikera.matrixx.Matrix;
import mikera.vectorz.AVector;
import mikera.vectorz.Vector;
import mikera.vectorz.impl.AxisVector;
import mikera.vectorz.util.ErrorMessages;
import mikera.vectorz.util.IntArrays;
import mikera.vectorz.util.VectorzException;

/**
 * Class representing a square permutation matrix
 * i.e. has single 1.0 in every row and column
 * 
 * Mutable only for swapping rows and columns
 * 
 * @author Mike
 *
 */
public final class PermutationMatrix extends ABooleanMatrix implements IFastRows, IFastColumns, ISparse  {
	private static final long serialVersionUID = 8098287603508120428L;

	private final Index perm;
	
	private PermutationMatrix(Index perm) {
		super(perm.length(),perm.length());
		if (!perm.isPermutation()) throw new IllegalArgumentException("Not a valid permutation: "+perm);
		this.perm=perm;
	}
	
	public static PermutationMatrix createIdentity(int length) {
		return new PermutationMatrix(Indexz.createSequence(length));
	}
	
	public static PermutationMatrix createSwap(int i, int j, int length) {
		PermutationMatrix p=createIdentity(length);
		p.swapRows(i, j);
		return p;
	}
	
	public static PermutationMatrix create(Index rowPermutations) {
		return new PermutationMatrix(rowPermutations.clone());
	}
	
	public static PermutationMatrix wrap(Index rowPermutations) {
		return new PermutationMatrix(rowPermutations);
	}
	
	public static PermutationMatrix create(int... rowPermutations) {
		Index index=Index.of(rowPermutations);
		return wrap(index);
	}
	

	public static PermutationMatrix wrap(int[] rowPermutations) {
		return wrap(Index.wrap(rowPermutations));
	}
	
	public static PermutationMatrix createRandomPermutation(int length) {
		Index index=Indexz.createRandomPermutation(length);
		return new PermutationMatrix(index);
	}
	
	@Override
	public void addToArray(double[] data, int offset) {
		for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy