non-academic guidance for computer science

Information

This article was written on 01 Úno 2012, and is filled under edit (nedokonceno), Java, oop, Tutoring.

Matrix Calculator


Ukázka použití

Operace mezi maticemi

		int n=10;
		int[][] matice = new int[n][n];

		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++)
				matice[j][i]=1;

		Matrix<NumberCore> C = Factory.createMatrix(matice);
		Matrix<NumberCore> A = Factory.createMatrix(matice);
		Matrix<NumberCore> B = Factory.createMatrix(matice);
		B.neutral();

			C=B.plus(A);

		for(int i=1; i<=n; i++)
		{
			for(int j=1; j<=n; j++)
			{
				System.out.print("  "+C.getElement(j, i));
			}
			System.out.println();
		}

	}

Výstup

 2   1   1   1   1   1   1   1   1   1 
 1  2  1  1  1  1  1  1  1  1
 1  1  2  1  1  1  1  1  1  1
 1  1  1  2  1  1  1  1  1  1
 1  1  1  1  2  1  1  1  1  1
 1  1  1  1  1  2  1  1  1  1
 1  1  1  1  1  1  2  1  1  1
 1  1  1  1  1  1  1  2  1  1
 1  1  1  1  1  1  1  1  2  1
 1  1  1  1  1  1  1  1  1  2

Dokumentace & Licence

Počítání matic v lineárním čase

Nutná rozhraní

Entry

public interface Entry<E> extends DeepClonable<E>
{
	///////////////////////////////////////////////////////////////

	E times(E entry);
	E plus(E entry);
	E minus(E entry);
	void zero();
	void neutral();

	///////////////////////////////////////////////////////////////

}

DeepClone

public interface DeepClonable<E> extends Cloneable,java.io.Serializable
{
	///////////////////////////////////////////////////////////////

	public E duplicate();

	///////////////////////////////////////////////////////////////
}

Kód

Kernel

Factory

public class Factory
{
	///////////////////////////////////////////////////////////////

	public static Matrix<NumberCore> createMatrix(int[][] array)
	{
		NumberCore[][] result = new NumberCore[array.length][array[0].length];

		for(int y=0; y< array.length; y++)
			for(int x=0; x< array[y].length; x++)
				result[y][x]=new NumberCore(array[y][x]);

		return new Matrix<NumberCore>(result);
	}

	///////////////////////////////////////////////////////////////

}

HashTable

/////////////////////////////////////////////////////////////////////////////

class Pair<K,V>
{
    K key;
    V value;

    Pair(K k, V v){
        key = k;
        value = v;
    }

    public int hashCode()
    {
        return key.hashCode() + 3 * value.hashCode();
    }

	public boolean equals(Object other)
    {
        if (! (other instanceof Pair<?,?>)) return false;
        Pair<?,?> o = (Pair<?,?>) other;
        return key.equals(o.key) && value.equals(o.value);
    }
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/** Trida DSAHashTable reprezentuje rozptylovaci tabulku se zřetězením (první varianta v učebnici). */
public class HashTable<K,V> implements Iterable<Pair<K,V>>
{

    private transient volatile int count = -1 ;
    private transient volatile int capacity = -1 ;
    private transient volatile int troubleshoot = -1 ;
    private transient volatile java.util.Set < Pair < K,V > > [ ] table ;

    /**Vytvori prazdnou instanci DSAHashTable, delka vnitrniho pole je nastavena na size. */

    @SuppressWarnings("unchecked")
	HashTable ( int capacity )
    {
		this.count = 0;
		this.capacity = capacity ;
		this.troubleshoot = capacity;

		this.table    = (java.util.Set<Pair<K, V> >[]) new java.util.Set<?>[capacity];
    	this.initCells ( this.table );
    }

/////////////////////////////////////////////////////////////////////////////

    /** Ulozi dvojici (key, value) do rozptylovaci tabulky.
     * Pokud uz v tabulce je jina dvojice se stejnym klicem,
     * je smazana.
     * Klic ani hodnota nesmi byt null.
     * Pokud by pocet dvojic v tabulce po vykonani put mel vzrust nad
     *  dvojnasobek delky vnitrniho pole, vnitrni pole zdvojnasobi.
     */
	void put ( K key , V value )
    {

		if ( value == null || key == null )
    	    return ;    	    	

		Pair<K,V> pair = new Pair<K,V> ( key , value ) ;

			remove(key);

    		int index = this.getIndexOf ( pair.key ) ;
    		table [ index ].add ( pair ) ;

    		count++;

        	if ( ! this.isBigEnough ( ) )
        		resize ( capacity << 1 ) ;
    }

	/////////////////////////////////////////////////////////////////////////////
    /** Vrati hodnotu asociovanou s danym klicem nebo null, pokud dany klic v tabulce neni. */

	synchronized V get ( K key )
    {
		Pair<K,V> pair = this.getPair(key);
		if(pair==null) return null;
		return pair.value;
	}
/////////////////////////////////////////////////////////////////////////////

	private synchronized Pair<K,V> getPair ( K key )
    {
    	for ( Pair < K,V > e : this.table [ this.getIndexOf ( key ) ] )
    		if ( e . key . equals ( key ) )
    			return e;
    	return null ;
	}

/////////////////////////////////////////////////////////////////////////////

	private synchronized void initCells ( java.util.Set<Pair<K, V>>[] table )
	{

    		for ( int i = 0 ; i<table.length ; i ++ )
        			table [ i ] = new GenericSet<Pair<K,V>>();	

	}

/////////////////////////////////////////////////////////////////////////////

    /** Smaze dvojici s danym klicem. Pokud v tabulce dany klic neni, nedela nic. */
	synchronized void remove ( K key )
    {
		int index = this.getIndexOf(key);
		Pair<K,V> pair= this.getPair(key);
		if(pair!=null)
		{
				this.table[index].remove(pair);
				this.count--;
				return;
		}

    }

/////////////////////////////////////////////////////////////////////////////
    /** Vrati vnitrni pole. */
	java.util.Set<Pair<K,V>>[] getArray()
    {
		//OK
		return this.table ;
    }

/////////////////////////////////////////////////////////////////////////////
    /** Pro dany klic vrati index v poli. */
    int getIndexOf ( K key )
    {
    	//OK
		return key.hashCode ( ) % troubleshoot ;
    }

/////////////////////////////////////////////////////////////////////////////
    /** Pokud je pocet prvku mensi nebo roven dvojnasobku delky vnitrniho pole, vrati true, jinak vrati false. */
    private boolean isBigEnough()
    {
    	//OK
    	return this.count <= this.capacity << 1 ;
    }

/////////////////////////////////////////////////////////////////////////////
    /** Zmeni delku vnitrniho pole a zkopiruje do nej vsechny dvojice. */
    @SuppressWarnings("unchecked")
	synchronized void resize ( int size )
    {
    	//OK
    	java.util.Set < Pair < K,V > > [ ] temp = (java.util.Set<Pair<K, V> >[]) new java.util.Set<?>[size];
		this.initCells ( temp ) ;
		this.troubleshoot = size;
		for ( Pair<K, V> p : this )
		{
			int index = this.getIndexOf ( p.key ) ;
			temp [ index ].add ( p ) ;
		}
		this.capacity = size;
		this.table = temp;
	}

	/////////////////////////////////////////////////////////////////////////////
    /** Vrati iterator pres vsechny dvojice v tabulce. */
    public java.util.Iterator < Pair < K,V > >  iterator ( )
    {
    	java.util.Set< Pair < K,V >> c = new GenericSet< Pair < K,V >>();
    		for ( int i = 0 ; i < this.capacity ; i ++ )
        			for( Pair < K,V > p : table [ i ] )
        				c.add ( p ) ;	

    	return c.iterator();

    }

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

    private class GenericSet<G> extends java.util.HashSet<G>
    {
		private static final long serialVersionUID = -1093311608282332682L;
    }

    public int getSize()
    {
    	return this.count;
    }
}

 

NumberCore

public class NumberCore implements Entry<NumberCore>
{

	///////////////////////////////////////////////////////////////

	private java.math.BigInteger value;

	///////////////////////////////////////////////////////////////

	public NumberCore(int value)
	{ this.value = new java.math.BigInteger(String.valueOf(value)); }

	///////////////////////////////////////////////////////////////

	private NumberCore(java.math.BigInteger value)
	{ this.value = value; }

	///////////////////////////////////////////////////////////////

	public synchronized NumberCore minus(NumberCore entry)
	{ return new NumberCore(this.value.min(entry.value)); }

	///////////////////////////////////////////////////////////////

	public synchronized NumberCore plus(NumberCore entry)
	{ return new NumberCore(this.value.add(entry.value)); }

	///////////////////////////////////////////////////////////////

	public synchronized NumberCore times(NumberCore entry)
	{ return new NumberCore(this.value.multiply(entry.value)); }

	///////////////////////////////////////////////////////////////

	public String toString()
	{ return this.value.toString(); }

	///////////////////////////////////////////////////////////////

	public NumberCore duplicate()
	{ return new NumberCore(this.value); }

	///////////////////////////////////////////////////////////////

	public void neutral()
	{ this.value=java.math.BigInteger.ONE; }

	///////////////////////////////////////////////////////////////

	public void zero()
	{ this.value=java.math.BigInteger.ZERO; }

	///////////////////////////////////////////////////////////////

}

Matrix

public class Matrix<E extends Entry<E>> implements Entry<Matrix<E>>
{
	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

	private int size;
	private HashTable<Integer,HashTable<Integer,E>> data;

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

	public Matrix ( E[][] array )
    { 

    		if( array==null ) return;

			size = array.length;
			this.data = new HashTable<Integer,HashTable<Integer,E>>(size);

			for(int i=1; i<=size ; i++)
			{
				this.data.put(i, new HashTable<Integer,E>(size));
				for(int j=1; j<=size; j++)
				{
					this.data.get(i).put(j, array[j-1][i-1]);
				}
			}

    		///////////////////////////////////////////////////////

    }

	private Matrix ( HashTable<Integer, HashTable<Integer, E>> array )
    { 

    		if( array==null ) return;

			size = array.getSize();
			this.data=array;

    		///////////////////////////////////////////////////////

    }

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

	public synchronized E getElement( int x , int y )
	{

		return this.data.get(y).get(x);

	}

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

	public int getSize()
	{
		return this.size;
	}

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////	

	public synchronized Matrix<E> minus(Matrix<E> other)
	{
	HashTable<Integer,HashTable<Integer,E>> array = new HashTable<Integer,HashTable<Integer,E>>(size);

		for(int i=1; i<=size ; i++)
		{
			array.put(i, new HashTable<Integer,E>(size));
			for(int j=1; j<=size; j++)
			{
				array.get(i).put(j, this.getElement(i,j).minus(other.getElement(i, j)));
			}
		}

		return new Matrix<E>(array);
	}

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

	public synchronized Matrix<E> plus(Matrix<E> other)
	{
		HashTable<Integer,HashTable<Integer,E>> array = new HashTable<Integer,HashTable<Integer,E>>(size);

		for(int i=1; i<=size ; i++)
		{
			array.put(i, new HashTable<Integer,E>(size));
			for(int j=1; j<=size; j++)
			{
				array.get(i).put(j, this.getElement(i,j).plus(other.getElement(i, j)));
			}
		}

		return new Matrix<E>(array);

	}

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

	public synchronized Matrix<E> times(Matrix<E> other)
	{
	HashTable<Integer,HashTable<Integer,E>> array = new HashTable<Integer,HashTable<Integer,E>>(size);

		for(int i = 1; i <= size; i++)
		{
			array.put(i, new HashTable<Integer,E>(size));
		    for(int j = 1; j <= size; j++) {

		    	E el = this.getElement(j, i).duplicate();
		    	el.zero();

		        for(int s = 1; s<=other.size; s++)
		        {
		        	el=el.plus((this.getElement(s, i).times(other.getElement(j, s))));
		        }
		        array.get(i).put(j,el);

		        }
		    }

		return new Matrix<E>(array);
	}

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

	public Matrix<E> duplicate()
	{
		return new Matrix<E>(this.data);
	}

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

	public void neutral()
	{

		for(int i=1; i<=size ; i++)
			for(int j=1; j<=size; j++)
			{
				if(i==j)
					this.getElement(i, j).neutral();
				else
					this.getElement(i, j).zero();
			}

	}

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

	public void zero()
	{
		for(int i=1; i<=size ; i++)
			for(int j=1; j<=size; j++)
					this.getElement(i, j).zero();
	}

	///////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////

}

///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

Napsat komentář