Advertisement
Guest User

brand new story

a guest
Feb 24th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.38 KB | None | 0 0
  1. /*    implewment (almost) ALL the methods in this class.
  2.  *
  3.  *    
  4.  *    This project is based on Chapter 3, section 5, Matrices of Relations
  5.  *    
  6.  *    Good luck
  7.  */
  8.  
  9. import java.util.*;
  10. import java.lang.Math;
  11. /**
  12.  *
  13.  * @author  
  14.  * @version (a version number or a date)
  15.  */
  16. public class FunctionsChapter3StylePart2
  17. {
  18.     int[][] matrix;
  19.  
  20.     public FunctionsChapter3StylePart2(int[][] r)
  21.     {
  22.         matrix = r;
  23.     }
  24.  
  25.     public int getNumRows()
  26.     {
  27.         return matrix.length;
  28.     }
  29.  
  30.     public int getNumCols()
  31.     {
  32.         return matrix[0].length;
  33.     }
  34.  
  35.     /*
  36.      *    replaces the current relation instance variable with r
  37.      *    
  38.      *    YES - this method gets used in the my (stipulator) tester
  39.      */
  40.     public void setRelation(int[][] r)
  41.     {
  42.         matrix = r;
  43.     }
  44.  
  45.     /*
  46.      *    returns the current relation instance variable
  47.      */
  48.     public int[][] getRelation()
  49.     {  
  50.         return matrix;
  51.     }
  52.  
  53.     /*
  54.      *    retruns the number of Order Pairs in the relation
  55.      *      that is, the number of one's (1) in the Matrix
  56.      */
  57.     public int getSize()
  58.     {
  59.         int count = 0;
  60.         for(int i = 0; i < matrix.length; i++){
  61.             for(int j = 0; j < matrix[0].length; j++){
  62.                 if(matrix[i][j] == 1){
  63.                     count++;
  64.                 }
  65.             }
  66.         }
  67.         return count;
  68.     }
  69.  
  70.     /*
  71.      *    f is a function if
  72.      *       for each x in X, there is exactly one y in Y with (x,y) in f
  73.      *    returns true if the matrix forms a function
  74.      *    returns false otherwise
  75.      */
  76.     public boolean isFunction()
  77.     {        
  78.         for(int i = 0; i < matrix.length; i++){
  79.             int count = 0;
  80.             for(int j = 0; j < matrix[0].length; j++){
  81.                 count += matrix[i][j];
  82.             }
  83.             if(count != 1) return false;
  84.         }
  85.         return true;
  86.     }
  87.  
  88.     /*
  89.      *    A function f from X to Y is said to be one to one if
  90.      *    for each y in Y, there is at most one x in X with f(x) = y
  91.      *
  92.      *    returns true if the matrix is a function and the function is one to one
  93.      *    returns false otherwise
  94.      */
  95.     public boolean isOneToOne()   // column sum is one for every column
  96.     {
  97.         for(int j = 0; j < matrix[0].length; j++){  
  98.             int count = 0;
  99.             for(int i = 0; i < matrix.length; i++){
  100.                 count += matrix[i][j];
  101.             }
  102.             if(count != 1) return false;
  103.         }
  104.         return true;
  105.     }
  106.  
  107.     /*
  108.      *    A function from X to Y is said to be onto if
  109.      *    the range of f == Y
  110.      *
  111.      *    returns true if the matrix is a function and the function is onto
  112.      *    returns false otherwise
  113.      */
  114.     public boolean isOnTo()    // column sum > 0 for all columns
  115.     {
  116.         for(int j = 0; j < matrix[0].length; j++){  
  117.             int count = 0;
  118.             for(int i = 0; i < matrix.length; i++){
  119.                 count += matrix[i][j];
  120.             }
  121.             if(count == 0) return false;
  122.         }
  123.         return true;
  124.     }
  125.  
  126.     /*
  127.      *     returns true if the matrix is a function and the function is bijective
  128.      *              that is both one to one and onto
  129.      *     returns false otherwise
  130.      */
  131.     public boolean isBijective()
  132.     {
  133.         return isFunction() && isOnTo() && isOneToOne();
  134.     }
  135.  
  136.     /*
  137.      *   precondition:  comp is a function.
  138.      *  
  139.      *   returns a new FunctionsChapter3StylePart2 Object.
  140.      *   The domain of the new Object is this.domain
  141.      *   The coDomain of the new Object is comp.coDomain
  142.      *  
  143.      *   The new function is the composition: relation o b = b ( relation )
  144.      *  
  145.      *   See the tester for more information
  146.      */
  147.     public FunctionsChapter3StylePart2 composition(int[][] comp)
  148.     {
  149.         int[][] a = new int[matrix.length][comp[0].length];
  150.  
  151.         for(int i = 0; i < matrix.length; i++){
  152.             for(int j = 0; j < matrix[0].length; j++){
  153.                 if(matrix[i][j] > 0){
  154.                     for(int k = 0; k < comp[0].length; k++){
  155.                         if(comp[j][k] > 0){
  156.                             a[i][k] = 1;
  157.                         }
  158.                     }
  159.                 }
  160.             }
  161.         }
  162.  
  163.         FunctionsChapter3StylePart2 two = new FunctionsChapter3StylePart2(a);
  164.         return two;
  165.     }
  166.  
  167.     /*
  168.      *   precondition:  relation is a function.
  169.      *   rel does not have to be both 1-1 and onto
  170.      *   the inverse does not need to be a function
  171.      */
  172.     public int[][] getInverse()
  173.     {
  174.         int[][] temp = new int[matrix.length][matrix[0].length];
  175.         for(int i = 0; i < matrix.length; i++){
  176.             for(int j = 0; j < matrix[0].length; j++){
  177.                 temp[i][j] = matrix[j][i];  
  178.             }
  179.         }
  180.         return temp;
  181.     }
  182.  
  183.     /*
  184.      * A relation is reflexive if (x, x) in R for every x in X
  185.      *
  186.      *       returns true if the current relation is reflexive
  187.      *       returns false otherwise
  188.      *      
  189.      *       You shuld not assume the matrix is a square matrix.
  190.      */
  191.     public boolean isReflexive()
  192.     {
  193.         for(int i = 0; i < matrix.length; i++){
  194.             for(int j = 0; j < matrix[0].length; j++){
  195.                 if(i == j && i < matrix[0].length && j < matrix.length){
  196.                     if(matrix[i][j] == 0)
  197.                         return false;
  198.                 }
  199.             }
  200.         }
  201.         return true;
  202.     }
  203.  
  204.     /*
  205.      *       A relation is symmetric if
  206.      *       for all x, y in X, if (x,y) in R, then (y,x) in R
  207.      *
  208.      *       returns true if the current relation is symmetric
  209.      *       returns false otherwise
  210.      */
  211.     public boolean isSymmetric()
  212.     {
  213.         for(int i = 0; i < matrix.length; i++){
  214.             for(int j = 0; j < matrix[0].length; j++){
  215.                 if(!((matrix[i][j] > 0 && matrix[j][i] > 0)|| (matrix[i][j] == 0 && matrix[j][i] == 0)))
  216.                     return false;
  217.             }
  218.         }
  219.         return true;
  220.     }
  221.  
  222.     /*
  223.      *       A relation is Antisymmetric if
  224.      *       for all x, y in X, if (x,y) in R, and (y,x) in R, then x = y
  225.      *
  226.      *    returns true if the current relation is Antisymmetric
  227.      *    returns false otherwise
  228.      */
  229.     public boolean isAntiSymmetric()
  230.     {
  231.         for(int i = 0; i < matrix.length; i++){
  232.             for(int j = 0; j < matrix[0].length; j++){
  233.                 if(i != j && (matrix[i][j] > 0 && matrix[j][i] > 0))
  234.                     return false;
  235.             }
  236.         }
  237.         return true;
  238.     }
  239.  
  240.     /*
  241.      *       A relation is transitive:
  242.      *       if (a,b) and (b,c) then (a,c)
  243.      *
  244.      *       returns true if the current relation is transitive
  245.      *       returns false otherwise
  246.      */
  247.     public boolean isTransitive()
  248.     {
  249.         int[][] a = Matrix.product(matrix,matrix);
  250.        
  251.         for(int i = 0; i < matrix.length; i++){
  252.             for(int j = 0; j < matrix[0].length; j++){
  253.                 if(a[i][j] > 0 && matrix[i][j] == 0)
  254.                     return false;
  255.             }
  256.         }
  257.         return true;
  258.     }
  259.  
  260.     /*
  261.      *    returns true is the relation is an Equivalence Relation
  262.      *    returns false otherwise
  263.      */
  264.     public boolean isEquivalenceRelation()
  265.     {
  266.         return isReflexive() && isTransitive() && isSymmetric();
  267.     }
  268.  
  269.     /*
  270.      *    returns true is the relation is an Partially Order
  271.      *    returns false otherwise
  272.      */
  273.     public boolean isPartiallyOrder()
  274.     {
  275.         return isReflexive() && isTransitive() && isAntiSymmetric();
  276.     }
  277.  
  278.     /*
  279.      *     look at the tester
  280.      *
  281.      *    returns [[a, c, ....d], [...], ...[]]
  282.      */
  283.     public String toString()
  284.     {
  285.         String str = "[";
  286.         for(int i = 0; i < matrix.length-1; i++){
  287.             str += "[";
  288.             for(int j = 0; j < matrix[0].length-1; j++){
  289.                 str += Integer.toString(matrix[i][j]) + ", ";
  290.             }
  291.             str += Integer.toString(matrix[i][matrix[0].length-1]) + "], ";
  292.         }
  293.         str += "[";
  294.         for(int j = 0; j < matrix[0].length-1; j++){
  295.             str += Integer.toString(matrix[matrix.length-1][j]) + ", ";
  296.         }
  297.         str += Integer.toString(matrix[matrix.length-1][matrix[0].length-1]) + "]]";
  298.  
  299.         return str;
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement