Merge branch 'kjvdh/features/PhylogenyViewer_tabbedsupport' into merge/2_11_2/kjvdh...
[jalview.git] / src / jalview / math / MatrixI.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.math;
22
23 import java.io.PrintStream;
24
25 /**
26  * An interface that describes a rectangular matrix of double values and
27  * operations on it
28  */
29 public interface MatrixI
30 {
31   /**
32    * Answers the number of columns
33    * 
34    * @return
35    */
36   int width();
37
38   /**
39    * Answers the number of rows
40    * 
41    * @return
42    */
43   int height();
44
45   /**
46    * Answers the value at row i, column j
47    * 
48    * @param i
49    * @param j
50    * @return
51    */
52   double getValue(int i, int j);
53
54   /**
55    * Sets the value at row i, column j
56    * 
57    * @param i
58    * @param j
59    * @param d
60    */
61   void setValue(int i, int j, double d);
62
63   /**
64    * Answers a copy of the values in the i'th row
65    * 
66    * @return
67    */
68   double[] getRow(int i);
69
70   /**
71    * Answers a new matrix with a copy of the values in this one
72    * 
73    * @return
74    */
75   MatrixI copy();
76
77   /**
78    * Answers all values present in the Matrix ordered by row,column
79    * 
80    * @return the double array containing the values ordered in {row values} per
81    *         column
82    */
83   double[][] getValues();
84
85   /**
86    * Returns a new matrix which is the transpose of this one
87    * 
88    * @return
89    */
90   MatrixI transpose();
91
92   /**
93    * Returns a new matrix which is the result of premultiplying this matrix by
94    * the supplied argument. If this of size AxB (A rows and B columns), and the
95    * argument is CxA (C rows and A columns), the result is of size CxB.
96    * 
97    * @param in
98    * 
99    * @return
100    * @throws IllegalArgumentException
101    *           if the number of columns in the pre-multiplier is not equal to
102    *           the number of rows in the multiplicand (this)
103    */
104   MatrixI preMultiply(MatrixI m);
105
106   /**
107    * Returns a new matrix which is the result of postmultiplying this matrix by
108    * the supplied argument. If this of size AxB (A rows and B columns), and the
109    * argument is BxC (B rows and C columns), the result is of size AxC.
110    * <p>
111    * This method simply returns the result of in.preMultiply(this)
112    * 
113    * @param in
114    * 
115    * @return
116    * @throws IllegalArgumentException
117    *           if the number of rows in the post-multiplier is not equal to the
118    *           number of columns in the multiplicand (this)
119    * @see #preMultiply(Matrix)
120    */
121   MatrixI postMultiply(MatrixI m);
122
123   double[] getD();
124
125   double[] getE();
126
127   void setD(double[] v);
128
129   void setE(double[] v);
130
131   void print(PrintStream ps, String format);
132
133   void printD(PrintStream ps, String format);
134
135   void printE(PrintStream ps, String format);
136
137   void tqli() throws Exception;
138
139   void tred();
140
141   /**
142    * Reverses the range of the matrix values, so that the smallest values become
143    * the largest, and the largest become the smallest. This operation supports
144    * using a distance measure as a similarity measure, or vice versa.
145    * <p>
146    * If parameter <code>maxToZero</code> is true, then the maximum value becomes
147    * zero, i.e. all values are subtracted from the maximum. This is consistent
148    * with converting an identity similarity score to a distance score - the most
149    * similar (identity) corresponds to zero distance. However note that the
150    * operation is not reversible (unless the original minimum value is zero).
151    * For example a range of 10-40 would become 30-0, which would reverse a
152    * second time to 0-30. Also note that a general similarity measure (such as
153    * BLOSUM) may give different 'identity' scores for different sequences, so
154    * they cannot all convert to zero distance.
155    * <p>
156    * If parameter <code>maxToZero</code> is false, then the values are reflected
157    * about the average of {min, max} (effectively swapping min and max). This
158    * operation <em>is</em> reversible.
159    * 
160    * @param maxToZero
161    */
162   void reverseRange(boolean maxToZero);
163
164   /**
165    * Multiply all entries by the given value
166    * 
167    * @param d
168    */
169   void multiply(double d);
170
171   /**
172    * Answers true if the two matrices have the same dimensions, and
173    * corresponding values all differ by no more than delta (which should be a
174    * positive value), else false
175    * 
176    * @param m2
177    * @param delta
178    * @return
179    */
180   boolean equals(MatrixI m2, double delta);
181 }