Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 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. {
  62. for(int j = 0; j < matrix[0].length; j++)
  63. {
  64. if(matrix[i][j] == 1)
  65. {
  66. count++;
  67. }
  68. }
  69. }
  70. return count;
  71. }
  72.  
  73. /*
  74. * f is a function if
  75. * for each x in X, there is exactly one y in Y with (x,y) in f
  76. * returns true if the matrix forms a function
  77. * returns false otherwise
  78. */
  79. public boolean isFunction()
  80. {
  81. for(int i = 0; i < matrix.length; i++)
  82. {
  83. int count = 0;
  84. for(int j = 0; j < matrix[0].length; j++)
  85. {
  86. count += matrix[i][j];
  87. }
  88. if(count != 1)
  89. {return false;
  90. }
  91. }
  92. return true;
  93. }
  94.  
  95. /*
  96. * A function f from X to Y is said to be one to one if
  97. * for each y in Y, there is at most one x in X with f(x) = y
  98. *
  99. * returns true if the matrix is a function and the function is one to one
  100. * returns false otherwise
  101. */
  102. public boolean isOneToOne() // column sum is one for every column
  103. {
  104. for(int j = 0; j < matrix[0].length; j++)
  105. {
  106. int count = 0;
  107. for(int i = 0; i < matrix.length; i++)
  108. {
  109. count += matrix[i][j];
  110. }
  111. if(count != 1)
  112. {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118.  
  119. /*
  120. * A function from X to Y is said to be onto if
  121. * the range of f == Y
  122. *
  123. * returns true if the matrix is a function and the function is onto
  124. * returns false otherwise
  125. */
  126. public boolean isOnTo() // column sum > 0 for all columns
  127. {
  128. for(int j = 0; j < matrix[0].length; j++)
  129. {
  130. int count = 0;
  131. for(int i = 0; i < matrix.length; i++)
  132. {
  133. count += matrix[i][j];
  134. }
  135. if(count == 0)
  136. {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142.  
  143. /*
  144. * returns true if the matrix is a function and the function is bijective
  145. * that is both one to one and onto
  146. * returns false otherwise
  147. */
  148. public boolean isBijective()
  149. {
  150. return isFunction() && isOnTo() && isOneToOne();
  151. }
  152.  
  153. /*
  154. * precondition: comp is a function.
  155. *
  156. * returns a new FunctionsChapter3StylePart2 Object.
  157. * The domain of the new Object is this.domain
  158. * The coDomain of the new Object is comp.coDomain
  159. *
  160. * The new function is the composition: relation o b = b ( relation )
  161. *
  162. * See the tester for more information
  163. */
  164. public FunctionsChapter3StylePart2 composition(int[][] comp)
  165. {
  166. int[][] a = new int[matrix.length][comp[0].length];
  167.  
  168. for(int i = 0; i < matrix.length; i++)
  169. {
  170. for(int j = 0; j < matrix[0].length; j++)
  171. {
  172. if(matrix[i][j] > 0)
  173. {
  174. for(int k = 0; k < comp[0].length; k++)
  175. {
  176. if(comp[j][k] > 0)
  177. {
  178. a[i][k] = 1;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. FunctionsChapter3StylePart2 two = new FunctionsChapter3StylePart2(a);
  185. return two;
  186. }
  187.  
  188. /*
  189. * precondition: relation is a function.
  190. * rel does not have to be both 1-1 and onto
  191. * the inverse does not need to be a function
  192. */
  193. public int[][] getInverse()
  194. {
  195. int[][] temp = new int[matrix.length][matrix[0].length];
  196. for(int i = 0; i < matrix.length; i++){
  197. for(int j = 0; j < matrix[0].length; j++){
  198. temp[i][j] = matrix[j][i];
  199. }
  200. }
  201. return temp;
  202. }
  203.  
  204. /*
  205. * A relation is reflexive if (x, x) in R for every x in X
  206. *
  207. * returns true if the current relation is reflexive
  208. * returns false otherwise
  209. *
  210. * You shuld not assume the matrix is a square matrix.
  211. */
  212. public boolean isReflexive()
  213. {
  214. for(int i = 0; i < matrix.length; i++)
  215. {
  216. for(int j = 0; j < matrix[0].length; j++)
  217. {
  218. if(i == j && i < matrix[0].length && j < matrix.length)
  219. {
  220. if(matrix[i][j] == 0)
  221. {
  222. return false;
  223. }
  224. }
  225. }
  226. }
  227. return true;
  228. }
  229.  
  230. /*
  231. * A relation is symmetric if
  232. * for all x, y in X, if (x,y) in R, then (y,x) in R
  233. *
  234. * returns true if the current relation is symmetric
  235. * returns false otherwise
  236. */
  237. public boolean isSymmetric()
  238. {
  239. for(int i = 0; i < matrix.length; i++)
  240. {
  241. for(int j = 0; j < matrix[0].length; j++)
  242. {
  243. if(!((matrix[i][j] > 0 && matrix[j][i] > 0)|| (matrix[i][j] == 0 && matrix[j][i] == 0)))
  244. {
  245. return false;
  246. }
  247. }
  248. }
  249. return true;
  250. }
  251.  
  252. /*
  253. * A relation is Antisymmetric if
  254. * for all x, y in X, if (x,y) in R, and (y,x) in R, then x = y
  255. *
  256. * returns true if the current relation is Antisymmetric
  257. * returns false otherwise
  258. */
  259. public boolean isAntiSymmetric()
  260. {
  261. for(int i = 0; i < matrix.length; i++)
  262. {
  263. for(int j = 0; j < matrix[0].length; j++)
  264. {
  265. if(i != j && (matrix[i][j] > 0 && matrix[j][i] > 0))
  266. {
  267. return false;
  268. }
  269. }
  270. }
  271. return true;
  272. }
  273.  
  274. /*
  275. * A relation is transitive:
  276. * if (a,b) and (b,c) then (a,c)
  277. *
  278. * returns true if the current relation is transitive
  279. * returns false otherwise
  280. */
  281. public boolean isTransitive()
  282. {
  283. int[][] a = Matrix.product(matrix,matrix);
  284.  
  285. for(int i = 0; i < matrix.length; i++)
  286. {
  287. for(int j = 0; j < matrix[0].length; j++)
  288. {
  289. if(a[i][j] > 0 && matrix[i][j] == 0)
  290. {
  291. return false;
  292. }
  293. }
  294. }
  295. return true;
  296. }
  297.  
  298. /*
  299. * returns true is the relation is an Equivalence Relation
  300. * returns false otherwise
  301. */
  302. public boolean isEquivalenceRelation()
  303. {
  304. return isReflexive() && isTransitive() && isSymmetric();
  305. }
  306.  
  307. /*
  308. * returns true is the relation is an Partially Order
  309. * returns false otherwise
  310. */
  311. public boolean isPartiallyOrder()
  312. {
  313. return isReflexive() && isTransitive() && isAntiSymmetric();
  314. }
  315.  
  316. /*
  317. * look at the tester
  318. *
  319. * returns [[a, c, ....d], [...], ...[]]
  320. */
  321. public String toString()
  322. {
  323. String str = "[";
  324. for(int i = 0; i < matrix.length-1; i++)
  325. {
  326. str += "[";
  327. for(int j = 0; j < matrix[0].length-1; j++)
  328. {
  329. str += Integer.toString(matrix[i][j]) + ", ";
  330. }
  331. str += Integer.toString(matrix[i][matrix[0].length-1]) + "], ";
  332. }
  333. str += "[";
  334. for(int j = 0; j < matrix[0].length-1; j++)
  335. {
  336. str += Integer.toString(matrix[matrix.length-1][j]) + ", ";
  337. }
  338. str += Integer.toString(matrix[matrix.length-1][matrix[0].length-1]) + "]]";
  339.  
  340. return str;
  341. }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement