|
FuzzyMatrix.java
|
// FILE. . . . . d:/hak/hlt/src/hlt/math/fuzzy/FuzzyMatrix.java // EDIT BY . . . Hassan Ait-Kaci // ON MACHINE. . Hp-Zbook // STARTED ON. . Wed Mar 21 11:24:34 2018 // Last modified on Sat Aug 25 08:29:18 2018 by hak package hlt.math.fuzzy; // for the set of fuzzy degrees used by this fuzzy matrix (see end of file): import hlt.language.util.DoubleArrayList;
| This is a generic class defining linear algebra operations on matrices of fuzzy degrees in [0.0,1.0] in terms of two redefinable binary fuzzy operations on [0.0,1.0]: one called sup (standing for supremum) as an additive law; and one called inf (standing for infimum) as a multiplicative law. If not overridden, the default implementations for sup and inf are respectively Math.max and Math.min. |
public class FuzzyMatrix extends FuzzyAlgebra { /* ******************************************************************** */ /* FIELDS */ /* ******************************************************************** */ protected final int rownum; // number of rows protected final int colnum; // number of columns protected final double[][] data; // rownum-by-colnum array
| Returns the number of rows |
public final int rownum () { return rownum; }
| Returns the number of columns |
public final int colnum () { return colnum; }
| Returns the data array |
public final double[][] data () { return data; }
| Just a convenience to remind that making a copy is in effect (can then be used instead of true). |
static boolean COPY = true; /* ******************************************************************** */ /* CONSTRUCTORS */ /* ******************************************************************** */
| Create a new fuzzy matrix with a new rownum-by-colnum data array of 0.0's. |
public FuzzyMatrix (int rownum, int colnum) { this.rownum = rownum; this.colnum = colnum; data = new double[rownum][colnum]; }
| Create a new FuzzyMatrix sharing the given array data of doubles in [0.0,0.1] (trusting them to be so). |
public FuzzyMatrix (double[][] data) { this(data,!COPY); }
| If the flag copy is false, create a fuzzy matrix sharing the given data array; otherwise, create a fuzzy matrix with a new copy of the data array, in which case it also verifies that all the double degrees in data are indeed in the continuous interval [0.0,0.1]. |
public FuzzyMatrix (double[][] data, boolean copy) { rownum = data.length; colnum = data[0].length; if (copy) { this.data = new double[rownum][colnum]; for (int i = 0; i < rownum; i++) for (int j = 0; j < colnum; j++) this.data[i][j] = FuzzyTools.checkFuzzyValue(data[i][j]); } else this.data = data; } /* ******************************************************************** */ /* METHODS */ /* ******************************************************************** */
| Returns a new FuzzyMatrix with a data array that is a copy of this one's. |
public FuzzyMatrix copy () { return copy(data); }
| Returns a new FuzzyMatrix having a different data array than that of the given one, but containg equal array degrees as the given one. |
public FuzzyMatrix copy (FuzzyMatrix M) { return copy(M.data); }
| Returns a new FuzzyMatrix whose data array is a copy of the given data array. |
public FuzzyMatrix copy (double[][] data) { return new FuzzyMatrix(data,COPY); } /* ******************************************************************** */
| Sets the i,j entry in data to degree (NB: i and j indices start at 1 and end at rownum and colnum). Throws a NonFuzzyValueException exception if degree is not within [0.0,1.0]. |
public FuzzyMatrix set (int i, int j, double degree) { if (i < 1 || i > rownum || j < 1 || j > colnum) throw new RuntimeException("Row/Column index out of bounds: ("+i+","+j+")"); // NB: adjusting indices to start at 0 for internal data array: data[i-1][j-1] = FuzzyTools.checkFuzzyValue(degree); return this; }
| Returns true if this fuzzy matrix is pointwise-equal to the given one. |
public boolean equals (FuzzyMatrix B) { FuzzyMatrix A = this; if (A == B) return true; if (A.data == B.data) return true; if (B.rownum != A.rownum || B.colnum != A.colnum) throw new RuntimeException("Incompatible matrix dimensions: <" +A.rownum+","+A.colnum+"> =/= <"+B.rownum+","+B.colnum+">"); for (int i = 0; i < rownum; i++) for (int j = 0; j < colnum; j++) if (A.data[i][j] != B.data[i][j]) return false; return true; }
| This is an in-place pointwise update that modifies each entry of this fuzzy matrix to the value of the corresponding entry in the given matrix. |
public FuzzyMatrix update (FuzzyMatrix M) { if (rownum != M.rownum || colnum != M.colnum) throw new RuntimeException("Incompatible matrix dimensions: <" +rownum+","+colnum+"> =/= <"+M.rownum+","+M.colnum+">"); for (int i = 0; i < rownum; i++) for (int j = 0; j < colnum; j++) data[i][j] = M.data[i][j]; return this; } /* ******************************************************************** */
| Returns a random double in [0.0,1.0]. |
public static double random () { double r = Math.random(); // this returns a double in [0.,1.0) - does not include 1.0 // when r is kinda close to 1.0 we toss a coin heavily biased towards not returning 1.0 if (Math.ceil(10*r) == 10.0) if (Math.random() > 0.75) return 1.0; // when r is kinda close to 0.0 we toss a coin heavily biased towards not returning 0.0 if (Math.floor(10*r) == 0.0) if (Math.random() > 0.75) return 0.0; // otherwise return r as is return r; }
| Create and return a random rownum-by-colnum matrix with degrees in [0.0,1.0] |
public static FuzzyMatrix random (int rownum, int colnum) { FuzzyMatrix M = new FuzzyMatrix(rownum,colnum); for (int i = 0; i < rownum; i++) for (int j = 0; j < colnum; j++) M.data[i][j] = FuzzyMatrix.random(); return M; } /* ******************************************************************** */
| Returns a new fuzzy matrix corresponding the transpose of this one. |
public FuzzyMatrix transpose () { FuzzyMatrix M = new FuzzyMatrix(colnum,rownum); for (int i = 0; i < rownum; i++) for (int j = 0; j < colnum; j++) M.data[j][i] = data[i][j]; return M; }
| Returns a new FuzzyMatrix equal to this plus M (does not modify this). |
public FuzzyMatrix plus (FuzzyMatrix M) { if (rownum != M.rownum || colnum != M.colnum) throw new RuntimeException("Incompatible matrix dimensions: <" +rownum+","+colnum+"> =/= <"+M.rownum+","+M.colnum+">"); FuzzyMatrix N = new FuzzyMatrix(rownum,colnum); for (int i = 0; i < rownum; i++) for (int j = 0; j < colnum; j++) N.data[i][j] = sup(data[i][j],M.data[i][j]); return N; }
| Modifies in place the entries of this to those of this plus M. |
public FuzzyMatrix i_plus (FuzzyMatrix M) { if (rownum != M.rownum || colnum != M.colnum) throw new RuntimeException("Incompatible matrix dimensions: <" +rownum+","+colnum+"> =/= <"+M.rownum+","+M.colnum+">"); for (int i = 0; i < rownum; i++) for (int j = 0; j < colnum; j++) data[i][j] = sup(data[i][j],M.data[i][j]); return this; }
| Returns a new FuzzyMatrix equal to this times M (does not modify this). |
public FuzzyMatrix times (FuzzyMatrix M) { if (colnum != M.rownum) throw new RuntimeException("Incompatible col/rom matrix dimensions: "+colnum+" =/= "+M.rownum); FuzzyMatrix N = new FuzzyMatrix(rownum,M.colnum); for (int i = 0; i < rownum; i++) for (int j = 0; j < M.colnum; j++) { double accumulator = 0.0; for (int k = 0; k < colnum; k++) accumulator = sup(accumulator,inf(data[i][k],M.data[k][j])); N.data[i][j] = accumulator; } return N; }
| Modifies in place the entries of this to those of this times M. |
public FuzzyMatrix i_times (FuzzyMatrix M) { if (colnum != M.rownum) throw new RuntimeException("Incompatible col/rom matrix dimensions: "+colnum+" =/= "+M.rownum); for (int i = 0; i < rownum; i++) for (int j = 0; j < M.colnum; j++) { double accumulator = 0.0; for (int k = 0; k < colnum; k++) accumulator = sup(accumulator,inf(data[i][k],M.data[k][j])); data[i][j] = accumulator; } return this; } /* ************************************************************************ */ final protected DoubleArrayList degrees = new DoubleArrayList(); // the degrees of this fuzzy matrix
| Returns the set of degrees of this fuzzy matrix. |
public final DoubleArrayList degrees () { if (degrees.isEmpty()) computeDegrees(); return degrees; }
| Recomputes and returns the set of degrees of this fuzzy matrix. |
public final void computeDegrees () { degrees.setSize(0); // erase all entries if any for (int i = 0; i < rownum; i++) for (int j = 0; j < colnum; j++) addDegree(data[i][j]); }
| Inserts a double degree into this matrix' set of degree keeping this set sorted in increasing order, and returns the set. |
protected final DoubleArrayList addDegree (double degree) { int i = 0; while (i < degrees.size() && degree > degrees.get(i)) i++; if (degree != degrees.get(i)) degrees.add(i,degree); // will shift degrees at indices to the right and insert degree at i return degrees; } /* ************************************************************************ */
| Default double precision for printf. |
public static String precision = "%9.4f ";
| Prints this fuzzy matrix to standard output |
public void show () { for (int i = 0; i < rownum; i++) { for (int j = 0; j < colnum; j++) System.out.printf(precision, data[i][j]); System.out.println(); } } }
This file was generated on Sat Aug 25 09:29:34 CEST 2018 from file FuzzyMatrix.java
by the hlt.language.tools.Hilite Java tool written by Hassan Aït-Kaci