Advertisement
Guest User

Matrix.java

a guest
Oct 31st, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package de.skysoldier.graphics3D;
  2.  
  3. import java.io.PrintStream;
  4.  
  5. public class Matrix {
  6.    
  7.     private int rows;
  8.     private int cols;
  9.     private double matrixData[][];
  10.     private Values3D matrixType;
  11.    
  12.     public Matrix(){
  13.         this(new double[1][1]);
  14.     }
  15.    
  16.     public Matrix(double matrixData[][]){
  17.         this(matrixData.length, matrixData[0].length, Values3D.MATRIX_NORMAL);
  18.         this.matrixData = matrixData;
  19.     }
  20.    
  21.     public Matrix(int rows, int cols, Values3D matrixType){
  22.         this(rows, cols);
  23.         this.matrixData = new double[rows][cols];
  24.         this.matrixType = matrixType;
  25.     }
  26.    
  27.     public Matrix(int rows, int cols){
  28.         this.rows = rows;
  29.         this.cols = cols;
  30.         this.matrixData = new double[rows][cols];
  31.         this.matrixType = Values3D.MATRIX_NORMAL;
  32.         this.fill(0);
  33.     }
  34.    
  35.     public Matrix multiply(Matrix multiplier){
  36.         if(getCols() != multiplier.getRows()){
  37.             throw new ArithmeticException("matrices not multipliable! (multiplicand cols (" + getCols() + ") != multiplier rows (" + multiplier.getRows() + ") )");
  38.         }
  39.         else{
  40.             Matrix product = new Matrix(getRows(), multiplier.getCols());
  41.             for(int i = 0; i < getRows(); i++){
  42.                 for(int j = 0; j < multiplier.getCols(); j++){
  43.                     double n = 0;
  44.                     for(int k = 0; k < getCols(); k++){ // or multiplier.getRows(); , no difference
  45.                         n += getCellData(i, k) * multiplier.getCellData(k, j);
  46.                     }
  47.                     product.setCellData(i, j, n);
  48.                 }
  49.             }
  50.             return product;
  51.         }
  52.     }
  53.    
  54.     public void print(PrintStream printer){
  55.         if(printer != null){
  56.             printer.println(toString());
  57.         }
  58.         else{
  59.             System.out.println(toString());
  60.         }
  61.         System.out.println("");
  62.     }
  63.    
  64.     public String toString(){
  65.         String dataString = "";
  66.         for(int i = 0; i < getRows(); i++){
  67.             for(int j = 0; j < getCols(); j++){
  68.                 dataString += matrixData[i][j] + " ";
  69.             }
  70.             dataString += "\n";
  71.         }
  72.         return dataString;
  73.     }
  74.    
  75.     public void fill(int n){
  76.         for(int i = 0; i < matrixData.length; i++){
  77.             for(int j = 0; j < matrixData[0].length; j++){
  78.                 matrixData[i][j] = n;
  79.             }
  80.         }
  81.     }
  82.    
  83.     public Point3D toPoint3D(){
  84.         if(!(getRows() == 4 && getCols() == 1)){
  85.             throw new UnsupportedOperationException("Point3D Matrix should have size 4x1");
  86.         }
  87.         else{
  88.             return new Point3D(getCellData(0, 0), getCellData(1, 0), getCellData(2, 0));
  89.         }
  90.     }
  91.    
  92.     public Matrix clone(){
  93.         return new Matrix(getMatrixData());
  94.     }
  95.    
  96.     public int getRows(){
  97.         return rows;
  98.     }
  99.    
  100.     public int getCols(){
  101.         return cols;
  102.     }
  103.    
  104.     public double[][] getMatrixData(){
  105.         return matrixData;
  106.     }
  107.    
  108.     public double getCellData(int row, int col){
  109.         return matrixData[row][col];
  110.     }
  111.    
  112.     public Values3D getMatrixType(){
  113.         return matrixType;
  114.     }
  115.    
  116.     public void setCellData(int row, int col, double n){
  117.         matrixData[row][col] = n;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement