All pastes #630846 Raw Edit

SparseMatrix.java

public java v1 · immutable
#630846 ·published 2007-07-23 15:10 UTC
rendered paste body
import javax.swing.*;public class Main{	public static void main(String[] args){		//double[][] n = new double[100][100];		//int m = 30;		int n = 50;		SparseMatrix mat = new SparseMatrix(n);		GlesMatrisVy vy = new GlesMatrisVy(mat);		Knappar knapp = new Knappar(mat,vy);		JFrame f = new JFrame();		f.add(vy, "North");		f.add(knapp, "South");		f.setVisible(true);		f.pack();		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	}}------------------------------import java.util.*;public class SparseMatrix{	private ArrayList<Triple> elem = new ArrayList<Triple>();	private int nz, dim;	private class Triple{		int rad, col;		double el;		public Triple(int r, int c, double e){			rad = r; col = c; el = e;		}	}	public SparseMatrix(){}		public SparseMatrix(int n){		dim = n; 	}	public SparseMatrix(double[][] matrix, int nz){		dim = matrix.length;		this.nz = nz;		for(int i=0; i<dim; i++){			for(int j=0; j<dim; j++){				if(matrix[i][j] != 0){					elem.add(new Triple(i,j,matrix[i][j]));				}			}		}	}	public int getDim(){		return dim;	}	public int nnz(){		return nz;	}	public double elemAt(int r, int c){		Triple arrayElement;		double retElementValue = 0;		int arraySize = elem.size();		//		Look at the class definition of Trippel//		The row , column and value can be retrieved by using//		the instance variables .rad .col .el				for(int i=0;i<arraySize;i++){				arrayElement = elem.get(i);				if (arrayElement.rad == r && arrayElement.col == c){					retElementValue = arrayElement.el;				}			}				return retElementValue;	}	public boolean symmetric(){		if(nz%2 != 0)			return false;		int ok = 0;		for(int i=0; i<nz; i++){			Triple t = elem.get(i);			Triple nt = new Triple(t.col, t.rad, t.el);			if(elem.contains(nt)) // returnerar true ifall elem innehåller trippeln nt				ok++;		}		return ok==nz;	}	public void addElem(int r, int c, double e){		if(e != 0){			elem.add(new Triple(r,c,e));			nz++;		}		if(dim<r) dim = r;		if(dim<c) dim = c;		}	public SparseMatrix mul(SparseMatrix mat){		SparseMatrix ret = new SparseMatrix(mat.getDim());		for(int i=0; i<dim; i++){			for(int j=0; j<dim; j++){				double e = 0;				for(int k=0; k<dim; k++){					e = e + elemAt(i,k) * mat.elemAt(k,j);				ret.addElem(i,j,e);				}			}		}		return ret;	}}------------------------------import java.awt.event.*;import javax.swing.*;import java.awt.*;public class GlesMatrisVy extends JPanel{	private SparseMatrix mod;	private final int SIZE = 4;	public GlesMatrisVy( SparseMatrix m ){		mod = m;		addMouseListener( new Lyss() );		setPreferredSize( new Dimension( SIZE * mod.getDim(), SIZE * mod.getDim() ) );	}	public void PaintComponent(Graphics g){		super.paintComponent(g);		g.setColor(Color.RED);		for( int i=0; i<mod.getDim(); i++ ){			for( int j=0; j<mod.getDim(); j++ ){				if( mod.elemAt(i,j) != 0 ){					g.fillOval( j*SIZE, i*SIZE, SIZE, SIZE );				}			}		}	}	public void bytModell( SparseMatrix m ){		mod = m;		}	class Lyss extends MouseAdapter{		public void mouseClicked( MouseEvent e ){			int y = e.getY()/SIZE;			int x = e.getX()/SIZE;			if( mod.elemAt(x,y) == 0 ){				double elem = Double.parseDouble(JOptionPane.showInputDialog( "Elem at row " + y + " and col " + x + "?" ));				mod.addElem(y,x,elem);				repaint();			}		}	}}--------------------------import java.awt.event.*;import javax.swing.*;import java.awt.*;	public class Knappar extends JPanel implements ActionListener{	private SparseMatrix mod;	GlesMatrisVy vy;	JButton knapp = new JButton("Multiplicate");	public Knappar(SparseMatrix m, GlesMatrisVy v){		mod = m;		vy = v;		knapp.addActionListener(this);		add(knapp);	}				public void actionPerformed(ActionEvent e){			mod = mod.mul(mod);			vy.bytModell(mod);			vy.repaint();		}}