123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author LH
- */
- public class Matrix
- {
-
- private double[][] matrixdata;
-
- public Matrix(int height, int width)
- {
- matrixdata = new double[height][width];
- }
-
- public double getEntry(int height,int width)
- {
-
- return matrixdata[height-1][width-1];
- }
-
-
- public void setEntry(int height,int width,double value)
- {
- matrixdata[height-1][width-1]=value;
- }
-
- public int getHeight()
- {
- return matrixdata.length;
- }
-
- public int getWidth()
- {
- if(getHeight()>0)
- {
- return matrixdata[0].length;
- }
- return 0;//wenn keine zeilen dann auch keine spalten....
- }
-
- public Matrix add(Matrix m2)
- {
-
- if(m2.getHeight()==getHeight()&&m2.getWidth()==getWidth())
- {
- int height = getHeight();
- int width = getWidth();
- int cells = height*width;
- Matrix o = new Matrix(height,width);
- for (int i = 0; i < cells; i++)
- {
-
-
- int row = i/width+1;
- int col = i%width+1;
- o.setEntry(row,col, m2.getEntry(row, col)+getEntry(row, col));
- }
- return o;
- }
- return null;//keine add mögl
- }
-
- public Matrix multiply(Matrix m2)
- {
-
- if(m2.getHeight()==getWidth())
- {
-
- int height = getHeight();
- int width = m2.getWidth();
- Matrix o = new Matrix(height,m2.getWidth());
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j <width; j++)
- {
- double cc = 0;
- for (int k = 0; k < getWidth(); k++)
- {
- System.out.print((i+1)+"|"+(j+1)+"|"+(k+1));
- double mt = m2.getEntry(k+1,j+1);
- cc=cc+(getEntry(i+1, k+1)*mt);
- System.out.println("|"+mt+"|"+cc);
- }
- o.setEntry(i+1, j+1, cc);
- }
- }
- return o;
- }
- return null;//keine add mögl
- }
-
-
- public String toString()
- {
- String result = "";
- String zeile = "";
- String klammerauf= "│",klammerzu="│";
- for (int i = 0; i < matrixdata.length; i++)
- {
- klammerauf= "│";
- klammerzu="│";
- if(i==matrixdata.length-1)
- {
- klammerauf="└";
- klammerzu="┘";
- }
- if(i==0)
- {
- klammerauf="┌";
- klammerzu="┐";
- }
- double[] linedata = matrixdata[i];
- zeile=klammerauf;
- for (double zelle : linedata)
- {
- zeile+=zelle+"\t";
- }
-
- zeile=zeile.trim();
- zeile+=klammerzu;
- result+=zeile+"\n";
- }
-
- return result;
- }
-
-
-
- public static void main(String[] args) throws Exception
- {
- Matrix m = new Matrix(4,4);
-
-
- m.setEntry(1, 1, 0);
- m.setEntry(1, 2, 1);
- m.setEntry(1, 3, 0);
- m.setEntry(1, 4, 0);
-
- m.setEntry(2, 1, 0);
- m.setEntry(2, 2, 0);
- m.setEntry(2, 3, 1);
- m.setEntry(2, 4, 0);
-
- m.setEntry(3, 1, 0);
- m.setEntry(3, 2, 0);
- m.setEntry(3, 3, 1);
- m.setEntry(3, 4, 0);
-
- m.setEntry(4, 1, -1);
- m.setEntry(4, 2, 1);
- m.setEntry(4, 3, -1);
- m.setEntry(4, 4, 3);
- InteractiveIO.write(m.multiply(m)+"");
- }
-
- }
|