import java.io.PrintStream;
/**
- * DOCUMENT ME!
- *
- * @author $author$
- * @version $Revision$
+ * A class to model rectangular matrices of double values and operations on them
*/
public class Matrix
{
- /**
- * SMJSPUBLIC
+ /*
+ * the cell values in row-major order
*/
public double[][] value;
- /** DOCUMENT ME!! */
+ /*
+ * the number of rows
+ */
public int rows;
- /** DOCUMENT ME!! */
+ /*
+ * the number of columns
+ */
public int cols;
/** DOCUMENT ME!! */
* Creates a new Matrix object. For example
*
* <pre>
- * new Matrix(new double[][] {{2, 3}, {4, 5}, 2, 2)
+ * new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
* constructs
- * (2 3)
- * (4 5)
+ * (2 3 4)
+ * (5 6 7)
* </pre>
*
* Note that ragged arrays (with not all rows, or columns, of the same
*
* @param values
* the matrix values in row-major order
- * @param rows
- * @param cols
*/
- public Matrix(double[][] values, int rows, int cols)
+ public Matrix(double[][] values)
{
- this.rows = rows;
- this.cols = cols;
+ this.rows = values.length;
+ this.cols = this.rows == 0 ? 0 : values[0].length;
this.value = values;
}
}
}
- return new Matrix(out, cols, rows);
+ return new Matrix(out);
}
/**
}
}
- return new Matrix(tmp, in.rows, this.cols);
+ return new Matrix(tmp);
}
/**
for (int i = 0; i < rows; i++)
{
System.arraycopy(value[i], 0, newmat[i], 0, value[i].length);
- // for (int j = 0; j < cols; j++)
- // {
- // newmat[i][j] = value[i][j];
- // }
}
- return new Matrix(newmat, rows, cols);
+ return new Matrix(newmat);
}
/**
package jalview.math;
import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.util.Arrays;
+import java.util.Random;
import org.testng.annotations.Test;
int cols = 1000;
double[][] d1 = new double[rows][cols];
double[][] d2 = new double[cols][rows];
- Matrix m1 = new Matrix(d1, rows, cols);
- Matrix m2 = new Matrix(d2, cols, rows);
+ Matrix m1 = new Matrix(d1);
+ Matrix m2 = new Matrix(d2);
long start = System.currentTimeMillis();
m1.preMultiply(m2);
long elapsed = System.currentTimeMillis() - start;
@Test(groups = "Functional")
public void testPreMultiply()
{
- Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 } }, 1, 3); // 1x3
- Matrix m2 = new Matrix(new double[][] { { 5 }, { 6 }, { 7 } }, 3, 1); // 3x1
+ Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 } }); // 1x3
+ Matrix m2 = new Matrix(new double[][] { { 5 }, { 6 }, { 7 } }); // 3x1
/*
* 1x3 times 3x1 is 1x1
expectedExceptions = { IllegalArgumentException.class })
public void testPreMultiply_tooManyColumns()
{
- Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }, 2,
- 3); // 2x3
+ Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3
/*
* 2x3 times 2x3 invalid operation -
expectedExceptions = { IllegalArgumentException.class })
public void testPreMultiply_tooFewColumns()
{
- Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }, 2,
- 3); // 2x3
+ Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3
/*
* 3x2 times 3x2 invalid operation -
* (3020 30200)
* (5040 50400)
*/
- Matrix m1 = new Matrix(new double[][] { { 2, 3 }, { 4, 5 } }, 2, 2);
- Matrix m2 = new Matrix(new double[][] { { 10, 100 }, { 1000, 10000 } },
- 2, 2);
+ Matrix m1 = new Matrix(new double[][] { { 2, 3 }, { 4, 5 } });
+ Matrix m2 = new Matrix(new double[][] { { 10, 100 }, { 1000, 10000 } });
Matrix m3 = m1.postMultiply(m2);
assertEquals(Arrays.toString(m3.value[0]), "[3020.0, 30200.0]");
assertEquals(Arrays.toString(m3.value[1]), "[5040.0, 50400.0]");
* (2).(10 100 1000) = (20 200 2000)
* (3) (30 300 3000)
*/
- m1 = new Matrix(new double[][] { { 2 }, { 3 } }, 2, 1);
- m2 = new Matrix(new double[][] { { 10, 100, 1000 } }, 1, 3);
+ m1 = new Matrix(new double[][] { { 2 }, { 3 } });
+ m2 = new Matrix(new double[][] { { 10, 100, 1000 } });
m3 = m1.postMultiply(m2);
assertEquals(m3.rows, 2);
assertEquals(m3.cols, 3);
* [0, 0] = 2*5 + 3*6 + 4*7 = 56
* [0, 1] = 2*4 + 3*3 + 4*2 = 25
*/
- m1 = new Matrix(new double[][] { { 2, 3, 4 } }, 1, 3);
- m2 = new Matrix(new double[][] { { 5, 4 }, { 6, 3 }, { 7, 2 } }, 3, 2);
+ m1 = new Matrix(new double[][] { { 2, 3, 4 } });
+ m2 = new Matrix(new double[][] { { 5, 4 }, { 6, 3 }, { 7, 2 } });
m3 = m1.postMultiply(m2);
assertEquals(m3.rows, 1);
assertEquals(m3.cols, 2);
assertEquals(m3.value[0][1], 25d);
}
+ @Test(groups = "Functional")
+ public void testCopy()
+ {
+ Random r = new Random();
+ int rows = 5;
+ int cols = 11;
+ double[][] in = new double[rows][cols];
+
+ for (int i = 0; i < rows; i++)
+ {
+ for (int j = 0; j < cols; j++)
+ {
+ in[i][j] = r.nextDouble();
+ }
+ }
+ Matrix m1 = new Matrix(in);
+ Matrix m2 = m1.copy();
+ assertTrue(matrixEquals(m1, m2));
+ }
+
/**
* main method extracted from Matrix
*
}
}
- Matrix origmat = new Matrix(in, n, n);
+ Matrix origmat = new Matrix(in);
// System.out.println(" --- Original matrix ---- ");
// / origmat.print(System.out);