Advertisement
Guest User

big69

a guest
Feb 24th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.74 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. * You may assume all matrices will be square matrices
  9. */
  10.  
  11. import java.util.*;
  12. import java.lang.Math;
  13. /**
  14. *
  15. * @author
  16. * @version (a version number or a date)
  17. */
  18. public class FunctionsChapter3StylePart2
  19. {
  20. private int[][]matrix;
  21. /* add your own instance variables
  22. * You may assume all matrices in this problem will be square matrices n=unless stated otherwise
  23. * that is r.length == r[m].length for all m: 0 <= m < r.length
  24. *
  25. * isReflexive does NOT assume a square matrix
  26. */
  27.  
  28. public FunctionsChapter3StylePart2(int[][] r)
  29. {
  30. /* build your own constructor */
  31. matrix = r;
  32. }
  33.  
  34. public int getNumRows()
  35. {
  36. return matrix.length;
  37. }
  38.  
  39. public int getNumCols()
  40. {
  41. return matrix[0].length;
  42. }
  43.  
  44. /*
  45. * replaces the current relation instance variable with r
  46. *
  47. * YES - this method gets used in the my (stipulator) tester
  48. */
  49. public void setRelation(int[][] r)
  50. {
  51. /* add some code */
  52. matrix = r;
  53. }
  54.  
  55. /*
  56. * returns the current relation instance variable
  57. */
  58. public int[][] getRelation()
  59. {
  60. return matrix;
  61. }
  62.  
  63. /*
  64. * retruns the number of Order Pairs in the relation
  65. * that is, the number of one's (1) in the Matrix
  66. */
  67. public int getSize()
  68. {
  69. int big69 = 0;
  70. for(int r = 0; r < matrix.length; r++)
  71. {
  72. for(int c = 0; c < matrix[r].length; c++)
  73. {
  74. if(matrix[r][c]==1)
  75. {
  76. big69++;
  77. }
  78. }
  79. }
  80. return big69;
  81. }
  82.  
  83. /*
  84. * You may NOT assume all matrices in this problem will be square matrices
  85. * that is r.length may NOT equal r[m].length for all m: 0 <= m < r.length
  86. *
  87. * f is a function if
  88. * for each x in X, there is exactly one y in Y with (x,y) in f
  89. * returns true if the matrix forms a function
  90. * returns false otherwise
  91. */
  92. public boolean isFunction()
  93. {
  94. /*int[][] a = { {1, 1, 1}, {0, 1, 1} }; not a function
  95.  
  96. int[][] b = { {0, 1}, {1, 0}, {0, 1} };yes it is a function */
  97. boolean oneOne = false;
  98. for(int r = 0; r < matrix.length; r++)
  99. {
  100. for(int c = 0; c < matrix[r].length; c++)
  101. {
  102. if(matrix[r][c]==1 && oneOne == false)
  103. {
  104. oneOne=true;
  105. }
  106. else if(matrix[r][c]==1 && oneOne == true)
  107. {
  108. return false;
  109. }
  110. }
  111. oneOne=false;
  112. }
  113. return true;
  114. }
  115.  
  116. /*
  117. * You may NOT assume all matrices in this problem will be square matrices
  118. * that is r.length may NOT equal r[m].length for all m: 0 <= m < r.length
  119. *
  120. * A function f from X to Y is said to be one to one if
  121. * for each y in Y, there is at most one x in X with f(x) = y
  122. *
  123. * returns true if the matrix is a function and the function is one to one
  124. * returns false otherwise
  125. */
  126. public boolean isOneToOne() // column sum is one for every column
  127. {
  128. int numCols=0;
  129. int tempCols=0;
  130. for(int r = 0; r < matrix.length; r++)
  131. {
  132. for(int c = 0; c < matrix[r].length; c++)
  133. {
  134. tempCols++;
  135. }
  136. if(tempCols>numCols)
  137. {
  138. numCols=tempCols;
  139. }
  140. tempCols=0;
  141. }
  142. int[]columnSum = new int[numCols];
  143. for(int r = 0; r < matrix.length; r++)
  144. {
  145. for(int c = 0; c < matrix[r].length; c++)
  146. {
  147. if(matrix[r][c]==1)
  148. {
  149. columnSum[c]+=1;
  150. }
  151. }
  152. }
  153. for(int i = 0; i < columnSum.length; i++)
  154. {
  155. if(columnSum[i]>1)
  156. {
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162.  
  163. /*
  164. * You may NOT assume all matrices in this problem will be square matrices
  165. * that is r.length may NOT equal r[m].length for all m: 0 <= m < r.length
  166. *
  167. * A function from X to Y is said to be onto if
  168. * the range of f == Y
  169. *
  170. * returns true if the matrix is a function and the function is onto
  171. * returns false otherwise
  172. */
  173. public boolean isOnTo() // column sum > 0 for all columns
  174. {
  175. int numCols=0;
  176. int tempCols=0;
  177. for(int r = 0; r < matrix.length; r++)
  178. {
  179. for(int c = 0; c < matrix[r].length; c++)
  180. {
  181. tempCols++;
  182. }
  183. if(tempCols>numCols)
  184. {
  185. numCols=tempCols;
  186. }
  187. tempCols=0;
  188. }
  189. int[]columnSum = new int[numCols];
  190. for(int r = 0; r < matrix.length; r++)
  191. {
  192. for(int c = 0; c < matrix[r].length; c++)
  193. {
  194. if(matrix[r][c]==1)
  195. {
  196. columnSum[c]+=1;
  197. }
  198. }
  199. }
  200. for(int i = 0; i < columnSum.length; i++)
  201. {
  202. if(columnSum[i]==0)
  203. {
  204. return false;
  205. }
  206. }
  207. return true;
  208. }
  209.  
  210. /*
  211. * You may NOT assume all matrices in this problem will be square matrices
  212. * that is r.length may NOT equal r[m].length for all m: 0 <= m < r.length
  213. *
  214. * returns true if the matrix is a function and the function is bijective
  215. * that is both one to one and onto
  216. * returns false otherwise
  217. */
  218. public boolean isBijective()
  219. {
  220. return isFunction()&&isOneToOne()&&isOnTo();
  221. }
  222.  
  223. /*
  224. * precondition: comp is a function.
  225. *
  226. * returns a new FunctionsChapter3StylePart2 Object.
  227. * The domain of the new Object is this.domain
  228. * The coDomain of the new Object is comp.coDomain
  229. *
  230. * The new function is the composition: b o relation = b ( relation )
  231. *
  232. * See the tester for more information
  233. */
  234. public FunctionsChapter3StylePart2 composition(int[][] comp)
  235. {
  236. /*
  237. * int[][] a = { {1, 0}, {0, 1}, {1, 1} }; (1,1), (2,2) (3,1), (3,2)
  238. FunctionsChapter3StylePart2 f1 = new FunctionsChapter3StylePart2(a);
  239.  
  240. int[][] b = { {1, 1, 0}, {0, 1, 1} }; (1,1) (1,2), (2,2), (2,3)
  241. FunctionsChapter3StylePart2 f2 = f1.composition(b);
  242. //1,2,3
  243. //1,2,3
  244. int[][] ans = { {1, 1, 0}, {0, 1, 1}, {1, 1, 1} };
  245. int[][] mat = f2.getRelation();
  246.  
  247. for (int m = 0; m < ans.length; m++)
  248. for (int n = 0; n < ans[0].length; n++)
  249. assertEquals(ans[m][n], mat[m][n]);
  250. */
  251. //f1=(1,1), (2,2) (3,1), (3,2)
  252. //f2=(1,1) (1,2), (2,2), (2,3)
  253. //ans=(1,1) (1,2) (2,2) (2,3) (3,1) (3,2) (3,3)
  254. /*
  255. ArrayList<Integer> domain = new ArrayList<Integer>();
  256. ArrayList<Integer> coDomain = new ArrayList<Integer>();
  257. for(int r = 0; r < matrix.length; r++)
  258. {
  259. for(int c = 0; c < matrix[r].length; c++)
  260. {
  261. if(matrix[r][c]==1&&!domain.contains(new Integer(r)))
  262. {
  263. domain.add(new Integer(r));
  264. }
  265. }
  266. }
  267. for(int r = 0; r < comp.length; r++)
  268. {
  269. for(int c = 0; c < comp[r].length; c++)
  270. {
  271. if(comp[r][c]==1&&!coDomain.contains(new Integer(c)))
  272. {
  273. coDomain.add(new Integer(c));
  274. }
  275. }
  276. }
  277. */
  278. return new FunctionsChapter3StylePart2(comp);
  279. /*returns a new FunctionsChapter3StylePart2 Object.
  280. The domain of the new Object is this.domain
  281. The coDomain of the new Object is comp.coDomain*/
  282. }
  283.  
  284. /*
  285. * ALERT: Not the inverse of the matrix, but the function inverse (or matrix transposed)
  286. *
  287. * precondition: relation is a function.
  288. * rel does not have to be both 1-1 and onto
  289. * the inverse does not need to be a function
  290. */
  291. public int[][] getInverse() // not the inverse of the matrix, but function inverse!!!!!
  292. {
  293. return new int[7][13];
  294. }
  295.  
  296. /*
  297. * A relation is reflexive if (x, x) in R for every x in X
  298. *
  299. * returns true if the current relation is reflexive
  300. * returns false otherwise
  301. *
  302. * You shuld NOT assume the matrix is a square matrix. If the matrix is not a square, return false
  303. */
  304. public boolean isReflexive()
  305. {
  306. return Math.random() < 0.5;
  307. }
  308.  
  309. /*
  310. * A relation is symmetric if
  311. * for all x, y in X, if (x,y) in R, then (y,x) in R
  312. *
  313. * returns true if the current relation is symmetric
  314. * returns false otherwise
  315. */
  316. public boolean isSymmetric()
  317. {
  318. return Math.random() < 0.5;
  319. }
  320.  
  321. /*
  322. * A relation is Antisymmetric if
  323. * for all x, y in X, if (x,y) in R, and (y,x) in R, then x = y
  324. *
  325. * returns true if the current relation is Antisymmetric
  326. * returns false otherwise
  327. */
  328. public boolean isAntiSymmetric()
  329. {
  330. return Math.random() < 0.5;
  331. }
  332.  
  333. /*
  334. * A relation is transitive:
  335. * if (a,b) and (b,c) then (a,c)
  336. *
  337. * returns true if the current relation is transitive
  338. * returns false otherwise
  339. */
  340. public boolean isTransitive()
  341. {
  342. return Math.random() < 0.5;
  343. }
  344.  
  345. /*
  346. * returns true is the relation is an Equivalence Relation
  347. * returns false otherwise
  348. */
  349. public boolean isEquivalenceRelation()
  350. {
  351. return Math.random() < 0.5;
  352. }
  353.  
  354. /*
  355. * returns true is the relation is an Partially Order
  356. * returns false otherwise
  357. */
  358. public boolean isPartiallyOrder()
  359. {
  360. return Math.random() < 0.5;
  361. }
  362.  
  363. /*
  364. * look at the tester
  365. *
  366. * returns [[a, c, ....d], [...], ...[]]
  367. */
  368. public String toString()
  369. {
  370. return "";
  371. }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement