Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fuzzyrelations;
- import java.util.*;
- public class FuzzyRelations {
- static Scanner sc = new Scanner(System.in);
- static double[][] matrix1, matrix2;
- static void printMatrix(double[][] matrix) {
- for (double[] d : matrix) {
- System.out.println(Arrays.toString(d));
- }
- }
- static void union(double[][] a, double[][] b) {
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a[i].length; j++) {
- a[i][j] = a[i][j] > b[i][j] ? a[i][j] : b[i][j];
- }
- }
- System.out.println("Union is:");
- printMatrix(a);
- }
- static void intersection(double[][] a, double[][] b) {
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a[i].length; j++) {
- a[i][j] = a[i][j] < b[i][j] ? a[i][j] : b[i][j];
- }
- }
- System.out.println("Intersection is:");
- printMatrix(a);
- }
- static void maxMinComposition() {
- System.out.println("Enter your first relation matrix m,n ");
- int x1 = sc.nextInt();
- int y1 = sc.nextInt();
- double m1[][] = new double[x1][y1];
- System.out.println("Enter your relation matrix ");
- for (int i = 0; i < m1.length; i++) {
- for (int j = 0; j < m1[i].length; j++) {
- m1[i][j] = sc.nextDouble();
- }
- }
- System.out.println("Enter the second relation matrix m,n ");
- int x2 = sc.nextInt();
- int y2 = sc.nextInt();
- double m2[][] = new double[x2][y2];
- System.out.println("Enter your relation matrix ");
- for (int i = 0; i < m2.length; i++) {
- for (int j = 0; j < m2[i].length; j++) {
- m2[i][j] = sc.nextDouble();
- }
- }
- int max = m1.length > m2.length ? m1.length : m2.length;
- double ans[][] = new double[max][max];
- int p = 0;
- for (int i = 0; i < max; i++) {
- double outer[] = m1[i];
- double inner[] = new double[m2.length];
- //System.out.println(Arrays.toString(outer)+"\t");
- for (int y = 0; y < max; y++) {
- for (int j = 0; j < m2.length; j++) {
- inner[j] = m2[j][y];
- }
- double maximum = 0.0;
- double minimum = 0.0;
- for (int k = 0; k < outer.length; k++) {
- minimum = inner[k] < outer[k] ? inner[k] : outer[k];
- if (maximum < minimum) {
- maximum = minimum;
- }
- }
- ans[i][y] = maximum;
- }
- // System.out.print(Arrays.toString(inner));
- }
- printMatrix(ans);
- }
- static double[][] getMatrix() {
- System.out.println("Enter your relation matrix m,n ");
- int x1 = sc.nextInt();
- int y1 = sc.nextInt();
- double m1[][] = new double[x1][y1];
- System.out.println("Enter your relation matrix ");
- for (int i = 0; i < m1.length; i++) {
- for (int j = 0; j < m1[i].length; j++) {
- m1[i][j] = sc.nextDouble();
- }
- }
- return m1;
- }
- public static void main(String[] args) {
- // TODO code application logic here
- // Scanner sc = new Scanner(System.in);
- int choice = 0;
- do {
- System.out.println("Enter your choice:\n1:Union\n2:Intersection\n3:MaxMin Composition.\n0:exit ");
- choice = sc.nextInt();
- switch (choice) {
- case 3:
- maxMinComposition();
- break;
- case 2:
- matrix1 = getMatrix();
- matrix2 = getMatrix();
- intersection(matrix1, matrix2);
- break;
- case 1:
- matrix1 = getMatrix();
- matrix2 = getMatrix();
- union(matrix1, matrix2);
- break;
- }
- } while (choice != 0);
- }
- }
- /*
- run:
- Enter your choice:
- 1:Union
- 2:Intersection
- 3:MaxMin Composition.
- 0:exit
- 2
- Enter your relation matrix m,n
- 2 2
- Enter your relation matrix
- 0.2 0.3
- 0.1 0.5
- Enter your relation matrix m,n
- 2 2
- Enter your relation matrix
- 0.4 0.6
- 0.7 0.8
- Intersection is:
- [0.2, 0.3]
- [0.1, 0.5]
- Enter your choice:
- 1:Union
- 2:Intersection
- 3:MaxMin Composition.
- 0:exit
- 3
- Enter your first relation matrix m,n
- 3 2
- Enter your relation matrix
- 0.5 0.1 0.2 0.9 0.8 0.6
- Enter the second relation matrix m,n
- 2 3
- Enter your relation matrix
- 0.6 0.4 0.7 0.5 0.8 0.9
- [0.5, 0.4, 0.5]
- [0.5, 0.8, 0.9]
- [0.6, 0.6, 0.7]
- Enter your choice:
- 1:Union
- 2:Intersection
- 3:MaxMin Composition.
- 0:exit
- */
Add Comment
Please, Sign In to add comment