Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* implewment (almost) ALL the methods in this class.
- *
- *
- * This project is based on Chapter 3, section 5, Matrices of Relations
- *
- * Good luck
- */
- import java.util.*;
- import java.lang.Math;
- /**
- *
- * @author
- * @version (a version number or a date)
- */
- public class FunctionsChapter3StylePart2
- {
- int[][] matrix;
- public FunctionsChapter3StylePart2(int[][] r)
- {
- matrix = r;
- }
- public int getNumRows()
- {
- return matrix.length;
- }
- public int getNumCols()
- {
- return matrix[0].length;
- }
- /*
- * replaces the current relation instance variable with r
- *
- * YES - this method gets used in the my (stipulator) tester
- */
- public void setRelation(int[][] r)
- {
- matrix = r;
- }
- /*
- * returns the current relation instance variable
- */
- public int[][] getRelation()
- {
- return matrix;
- }
- /*
- * retruns the number of Order Pairs in the relation
- * that is, the number of one's (1) in the Matrix
- */
- public int getSize()
- {
- int count = 0;
- for(int i = 0; i < matrix.length; i++){
- for(int j = 0; j < matrix[0].length; j++){
- if(matrix[i][j] == 1){
- count++;
- }
- }
- }
- return count;
- }
- /*
- * f is a function if
- * for each x in X, there is exactly one y in Y with (x,y) in f
- * returns true if the matrix forms a function
- * returns false otherwise
- */
- public boolean isFunction()
- {
- for(int i = 0; i < matrix.length; i++){
- int count = 0;
- for(int j = 0; j < matrix[0].length; j++){
- count += matrix[i][j];
- }
- if(count != 1) return false;
- }
- return true;
- }
- /*
- * A function f from X to Y is said to be one to one if
- * for each y in Y, there is at most one x in X with f(x) = y
- *
- * returns true if the matrix is a function and the function is one to one
- * returns false otherwise
- */
- public boolean isOneToOne() // column sum is one for every column
- {
- for(int j = 0; j < matrix[0].length; j++){
- int count = 0;
- for(int i = 0; i < matrix.length; i++){
- count += matrix[i][j];
- }
- if(count != 1) return false;
- }
- return true;
- }
- /*
- * A function from X to Y is said to be onto if
- * the range of f == Y
- *
- * returns true if the matrix is a function and the function is onto
- * returns false otherwise
- */
- public boolean isOnTo() // column sum > 0 for all columns
- {
- for(int j = 0; j < matrix[0].length; j++){
- int count = 0;
- for(int i = 0; i < matrix.length; i++){
- count += matrix[i][j];
- }
- if(count == 0) return false;
- }
- return true;
- }
- /*
- * returns true if the matrix is a function and the function is bijective
- * that is both one to one and onto
- * returns false otherwise
- */
- public boolean isBijective()
- {
- return isFunction() && isOnTo() && isOneToOne();
- }
- /*
- * precondition: comp is a function.
- *
- * returns a new FunctionsChapter3StylePart2 Object.
- * The domain of the new Object is this.domain
- * The coDomain of the new Object is comp.coDomain
- *
- * The new function is the composition: relation o b = b ( relation )
- *
- * See the tester for more information
- */
- public FunctionsChapter3StylePart2 composition(int[][] comp)
- {
- int[][] a = new int[matrix.length][comp[0].length];
- for(int i = 0; i < matrix.length; i++){
- for(int j = 0; j < matrix[0].length; j++){
- if(matrix[i][j] > 0){
- for(int k = 0; k < comp[0].length; k++){
- if(comp[j][k] > 0){
- a[i][k] = 1;
- }
- }
- }
- }
- }
- FunctionsChapter3StylePart2 two = new FunctionsChapter3StylePart2(a);
- return two;
- }
- /*
- * precondition: relation is a function.
- * rel does not have to be both 1-1 and onto
- * the inverse does not need to be a function
- */
- public int[][] getInverse()
- {
- int[][] temp = new int[matrix.length][matrix[0].length];
- for(int i = 0; i < matrix.length; i++){
- for(int j = 0; j < matrix[0].length; j++){
- temp[i][j] = matrix[j][i];
- }
- }
- return temp;
- }
- /*
- * A relation is reflexive if (x, x) in R for every x in X
- *
- * returns true if the current relation is reflexive
- * returns false otherwise
- *
- * You shuld not assume the matrix is a square matrix.
- */
- public boolean isReflexive()
- {
- for(int i = 0; i < matrix.length; i++){
- for(int j = 0; j < matrix[0].length; j++){
- if(i == j && i < matrix[0].length && j < matrix.length){
- if(matrix[i][j] == 0)
- return false;
- }
- }
- }
- return true;
- }
- /*
- * A relation is symmetric if
- * for all x, y in X, if (x,y) in R, then (y,x) in R
- *
- * returns true if the current relation is symmetric
- * returns false otherwise
- */
- public boolean isSymmetric()
- {
- for(int i = 0; i < matrix.length; i++){
- for(int j = 0; j < matrix[0].length; j++){
- if(!((matrix[i][j] > 0 && matrix[j][i] > 0)|| (matrix[i][j] == 0 && matrix[j][i] == 0)))
- return false;
- }
- }
- return true;
- }
- /*
- * A relation is Antisymmetric if
- * for all x, y in X, if (x,y) in R, and (y,x) in R, then x = y
- *
- * returns true if the current relation is Antisymmetric
- * returns false otherwise
- */
- public boolean isAntiSymmetric()
- {
- for(int i = 0; i < matrix.length; i++){
- for(int j = 0; j < matrix[0].length; j++){
- if(i != j && (matrix[i][j] > 0 && matrix[j][i] > 0))
- return false;
- }
- }
- return true;
- }
- /*
- * A relation is transitive:
- * if (a,b) and (b,c) then (a,c)
- *
- * returns true if the current relation is transitive
- * returns false otherwise
- */
- public boolean isTransitive()
- {
- int[][] a = Matrix.product(matrix,matrix);
- for(int i = 0; i < matrix.length; i++){
- for(int j = 0; j < matrix[0].length; j++){
- if(a[i][j] > 0 && matrix[i][j] == 0)
- return false;
- }
- }
- return true;
- }
- /*
- * returns true is the relation is an Equivalence Relation
- * returns false otherwise
- */
- public boolean isEquivalenceRelation()
- {
- return isReflexive() && isTransitive() && isSymmetric();
- }
- /*
- * returns true is the relation is an Partially Order
- * returns false otherwise
- */
- public boolean isPartiallyOrder()
- {
- return isReflexive() && isTransitive() && isAntiSymmetric();
- }
- /*
- * look at the tester
- *
- * returns [[a, c, ....d], [...], ...[]]
- */
- public String toString()
- {
- String str = "[";
- for(int i = 0; i < matrix.length-1; i++){
- str += "[";
- for(int j = 0; j < matrix[0].length-1; j++){
- str += Integer.toString(matrix[i][j]) + ", ";
- }
- str += Integer.toString(matrix[i][matrix[0].length-1]) + "], ";
- }
- str += "[";
- for(int j = 0; j < matrix[0].length-1; j++){
- str += Integer.toString(matrix[matrix.length-1][j]) + ", ";
- }
- str += Integer.toString(matrix[matrix.length-1][matrix[0].length-1]) + "]]";
- return str;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement