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
- *
- * You may assume all matrices will be square matrices
- */
- import java.util.*;
- import java.lang.Math;
- /**
- *
- * @author
- * @version (a version number or a date)
- */
- public class FunctionsChapter3StylePart2
- {
- private int[][]matrix;
- /* add your own instance variables
- * You may assume all matrices in this problem will be square matrices n=unless stated otherwise
- * that is r.length == r[m].length for all m: 0 <= m < r.length
- *
- * isReflexive does NOT assume a square matrix
- */
- public FunctionsChapter3StylePart2(int[][] r)
- {
- /* build your own constructor */
- 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)
- {
- /* add some code */
- 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 big69 = 0;
- for(int r = 0; r < matrix.length; r++)
- {
- for(int c = 0; c < matrix[r].length; c++)
- {
- if(matrix[r][c]==1)
- {
- big69++;
- }
- }
- }
- return big69;
- }
- /*
- * You may NOT assume all matrices in this problem will be square matrices
- * that is r.length may NOT equal r[m].length for all m: 0 <= m < r.length
- *
- * 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()
- {
- /*int[][] a = { {1, 1, 1}, {0, 1, 1} }; not a function
- int[][] b = { {0, 1}, {1, 0}, {0, 1} };yes it is a function */
- boolean oneOne = false;
- for(int r = 0; r < matrix.length; r++)
- {
- for(int c = 0; c < matrix[r].length; c++)
- {
- if(matrix[r][c]==1 && oneOne == false)
- {
- oneOne=true;
- }
- else if(matrix[r][c]==1 && oneOne == true)
- {
- return false;
- }
- }
- oneOne=false;
- }
- return true;
- }
- /*
- * You may NOT assume all matrices in this problem will be square matrices
- * that is r.length may NOT equal r[m].length for all m: 0 <= m < r.length
- *
- * 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
- {
- int numCols=0;
- int tempCols=0;
- for(int r = 0; r < matrix.length; r++)
- {
- for(int c = 0; c < matrix[r].length; c++)
- {
- tempCols++;
- }
- if(tempCols>numCols)
- {
- numCols=tempCols;
- }
- tempCols=0;
- }
- int[]columnSum = new int[numCols];
- for(int r = 0; r < matrix.length; r++)
- {
- for(int c = 0; c < matrix[r].length; c++)
- {
- if(matrix[r][c]==1)
- {
- columnSum[c]+=1;
- }
- }
- }
- for(int i = 0; i < columnSum.length; i++)
- {
- if(columnSum[i]>1)
- {
- return false;
- }
- }
- return true;
- }
- /*
- * You may NOT assume all matrices in this problem will be square matrices
- * that is r.length may NOT equal r[m].length for all m: 0 <= m < r.length
- *
- * 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
- {
- int numCols=0;
- int tempCols=0;
- for(int r = 0; r < matrix.length; r++)
- {
- for(int c = 0; c < matrix[r].length; c++)
- {
- tempCols++;
- }
- if(tempCols>numCols)
- {
- numCols=tempCols;
- }
- tempCols=0;
- }
- int[]columnSum = new int[numCols];
- for(int r = 0; r < matrix.length; r++)
- {
- for(int c = 0; c < matrix[r].length; c++)
- {
- if(matrix[r][c]==1)
- {
- columnSum[c]+=1;
- }
- }
- }
- for(int i = 0; i < columnSum.length; i++)
- {
- if(columnSum[i]==0)
- {
- return false;
- }
- }
- return true;
- }
- /*
- * You may NOT assume all matrices in this problem will be square matrices
- * that is r.length may NOT equal r[m].length for all m: 0 <= m < r.length
- *
- * 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()&&isOneToOne()&&isOnTo();
- }
- /*
- * 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: b o relation = b ( relation )
- *
- * See the tester for more information
- */
- public FunctionsChapter3StylePart2 composition(int[][] comp)
- {
- /*
- * int[][] a = { {1, 0}, {0, 1}, {1, 1} }; (1,1), (2,2) (3,1), (3,2)
- FunctionsChapter3StylePart2 f1 = new FunctionsChapter3StylePart2(a);
- int[][] b = { {1, 1, 0}, {0, 1, 1} }; (1,1) (1,2), (2,2), (2,3)
- FunctionsChapter3StylePart2 f2 = f1.composition(b);
- //1,2,3
- //1,2,3
- int[][] ans = { {1, 1, 0}, {0, 1, 1}, {1, 1, 1} };
- int[][] mat = f2.getRelation();
- for (int m = 0; m < ans.length; m++)
- for (int n = 0; n < ans[0].length; n++)
- assertEquals(ans[m][n], mat[m][n]);
- */
- //f1=(1,1), (2,2) (3,1), (3,2)
- //f2=(1,1) (1,2), (2,2), (2,3)
- //ans=(1,1) (1,2) (2,2) (2,3) (3,1) (3,2) (3,3)
- /*
- ArrayList<Integer> domain = new ArrayList<Integer>();
- ArrayList<Integer> coDomain = new ArrayList<Integer>();
- for(int r = 0; r < matrix.length; r++)
- {
- for(int c = 0; c < matrix[r].length; c++)
- {
- if(matrix[r][c]==1&&!domain.contains(new Integer(r)))
- {
- domain.add(new Integer(r));
- }
- }
- }
- for(int r = 0; r < comp.length; r++)
- {
- for(int c = 0; c < comp[r].length; c++)
- {
- if(comp[r][c]==1&&!coDomain.contains(new Integer(c)))
- {
- coDomain.add(new Integer(c));
- }
- }
- }
- */
- return new FunctionsChapter3StylePart2(comp);
- /*returns a new FunctionsChapter3StylePart2 Object.
- The domain of the new Object is this.domain
- The coDomain of the new Object is comp.coDomain*/
- }
- /*
- * ALERT: Not the inverse of the matrix, but the function inverse (or matrix transposed)
- *
- * 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() // not the inverse of the matrix, but function inverse!!!!!
- {
- return new int[7][13];
- }
- /*
- * 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. If the matrix is not a square, return false
- */
- public boolean isReflexive()
- {
- return Math.random() < 0.5;
- }
- /*
- * 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()
- {
- return Math.random() < 0.5;
- }
- /*
- * 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()
- {
- return Math.random() < 0.5;
- }
- /*
- * 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()
- {
- return Math.random() < 0.5;
- }
- /*
- * returns true is the relation is an Equivalence Relation
- * returns false otherwise
- */
- public boolean isEquivalenceRelation()
- {
- return Math.random() < 0.5;
- }
- /*
- * returns true is the relation is an Partially Order
- * returns false otherwise
- */
- public boolean isPartiallyOrder()
- {
- return Math.random() < 0.5;
- }
- /*
- * look at the tester
- *
- * returns [[a, c, ....d], [...], ...[]]
- */
- public String toString()
- {
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement