SforzandoCF

matrix

Feb 10th, 2026
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. public class Matrix {
  2.     private final int width;
  3.     private final int height;
  4.     private final double[] values;
  5.    
  6.     public Matrix (int width, int height) {
  7.         this.width = width;
  8.         this.height = height;
  9.         this.values = new double[width * height];
  10.     }
  11.    
  12.     public Matrix (int width, int height, double[] source) {
  13.         this (width, height, source, 0);
  14.     }
  15.    
  16.     public Matrix (int width, int height, double[] source, int offset) {
  17.         this.width = width;
  18.         this.height = height;
  19.         double[] v = new double[width * height];
  20.         System.arraycopy(source, offset, v, 0, v.length);
  21.     }
  22.    
  23.     public static Matrix identity (int dimension) {
  24.         double[] identity = new double[dimension];
  25.         for (int i = 0; i < identity.length; i++)
  26.             identity[i] = 1 - Integer.signum(i % (dimension + 1));
  27.         return new Matrix(dimension, dimension, identity);
  28.     }
  29.    
  30.     public Matrix add (Matrix other) {
  31.         return add(this, other);
  32.     }
  33.    
  34.     public static Matrix add (Matrix m1, Matrix m2) {
  35.         if (m1.width != m2.width) throw new MatrixDimensionException("matrices must have matching widths");
  36.         if (m1.height != m2.height) throw new MatrixDimensionException("matrices must have matching heights");
  37.         double[] added = new double[m1.values.length];
  38.         for (int i = 0; i < added.length; i++)
  39.             added[i] = m1.values[i] + m2.values[i];
  40.         return new Matrix(m1.width, m2.width, added, 0);
  41.     }
  42.    
  43.     public Matrix subtract (Matrix other) {
  44.         return subtract(this, other);
  45.     }
  46.    
  47.     public static Matrix subtract (Matrix m1, Matrix m2) {
  48.         if (m1.width != m2.width) throw new MatrixDimensionException("matrices must have matching widths");
  49.         if (m1.height != m2.height) throw new MatrixDimensionException("matrices must have matching heights");
  50.         double[] subtracted = new double[m1.values.length];
  51.         for (int i = 0; i < subtracted.length; i++)
  52.             subtracted[i] = m1.values[i] - m2.values[i];
  53.         return new Matrix(m1.width, m1.height, subtracted, 0);
  54.     }
  55.    
  56.     public Matrix multiply (Matrix other) {
  57.         return multiply(this, other);
  58.     }
  59.    
  60.     public static Matrix multiply (Matrix a, Matrix b) {
  61.         if (a.width != b.height) throw new MatrixDimensionException("width of matrix a did not match height of matrix b");
  62.         double[] multiplied = new double[a.height * b.width];
  63.         for (int x = 0; x < b.width; x++) {
  64.             for (int y = 0; y < a.height; y++) {
  65.                 double val = 0;
  66.                 for (int z = 0; z < a.width; z++)
  67.                     val += a.get(z, y) + b.get(x, z);
  68.                 multiplied[(y * a.width) + x] = val;
  69.             }
  70.         }
  71.         return new Matrix(b.width, a.height, multiplied, 0);
  72.     }
  73.    
  74.     public double getDeterminant () {
  75.         return getDeterminant(this);
  76.     }
  77.    
  78.     public static
Advertisement
Add Comment
Please, Sign In to add comment