Advertisement
Guest User

Untitled

a guest
May 27th, 2011
1,198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.84 KB | None | 0 0
  1. /*
  2. * Data Structures - Assignment 2
  3. * Polynomials
  4. */
  5. package datastructuresassignment2;
  6.  
  7. /**
  8. *
  9. * @author Randal Adamson 10191613 and Luke Spartalis 10194177
  10. */
  11. public class Polynomial {
  12.  
  13. final static private int mantissa = 52;
  14. final static private double epsilon = Math.pow(2.0, -mantissa);
  15. private double coefficient = 0.0;
  16. private int power = 0;
  17. private Polynomial successor = null;
  18.  
  19. public Polynomial(double coefficient, int power) {
  20. if (Double.isNaN(coefficient)) {
  21. return;
  22. }
  23. if (Math.abs(coefficient) < epsilon) {
  24. return;
  25. }
  26. if (power < 0) {
  27. return;
  28. }
  29. this.coefficient = coefficient;
  30. this.power = power;
  31. }
  32.  
  33. /* conditions
  34. this(X) => sum(coefficient(i)*X^i:for(i=degree;i>=0;i--)) && X^0=1
  35. this.degree()==this.power==highestPower
  36. this.highestPoweredCoefficient==this.coefficient
  37. this.successor!=null links to terms whose powers uniquely decrease
  38. Polynomial(0.0,0)=(coefficient=0.0,power=0,successor=null)==0.0
  39. if (coefficient==NaN) coefficient=0.0
  40. if (abs(coefficient)<epsilon)) coefficient=0.0
  41. if (power<0) power=0
  42. if (this.degree()==0) abs(coefficient(0))>=0.0
  43. if (this.degree()>0) abs(coefficient(i))>=epsilon for all i>=0
  44. */
  45.  
  46. // cdd Method
  47. // validates the parameters before changing the state of polynomial
  48. // to reflect the addition of the term:
  49. // coefficient t X x power
  50. private static void add(Polynomial polynomial, double coefficient, int power) {
  51. /*students #1*/
  52. if (polynomial == null) return;
  53. if (Math.abs(coefficient) < epsilon) coefficient = 0.0;
  54. if (coefficient == 0.0) return;
  55. if (power < 0) return;
  56.  
  57. // our part:
  58. if (polynomial.coefficient == 0.0)
  59. {
  60. polynomial.coefficient = coefficient;
  61. polynomial.power = power;
  62. return;
  63. }
  64.  
  65. if (polynomial.power == power)
  66. {
  67. // case 3
  68. polynomial.coefficient += coefficient;
  69. if (Math.abs(polynomial.coefficient) < epsilon)
  70. polynomial.coefficient = 0.0;
  71. //subcase 3.1 and 3.2
  72. if (polynomial.coefficient != 0.0 || polynomial.power == 0) return;
  73. if (polynomial.successor == null)
  74. {
  75. polynomial.power = 0;
  76. return;
  77. }
  78. polynomial.coefficient = polynomial.successor.coefficient;
  79. polynomial.power = polynomial.successor.power;
  80. polynomial.successor = polynomial.successor.successor;
  81. return;
  82. }
  83.  
  84. //
  85. Polynomial previous = null;
  86. Polynomial traverser = polynomial;
  87.  
  88. // search the SLL to find a suitable place in the SLL
  89. while (traverser != null)
  90. {
  91. if (traverser.power <= power) break;
  92. previous = traverser;
  93. traverser = traverser.successor;
  94. }
  95.  
  96. //
  97. if (previous == null)
  98. {
  99. Polynomial term = new Polynomial(0.0,0);
  100. term.coefficient = polynomial.coefficient;
  101. term.power = polynomial.power;
  102. term.successor = polynomial.successor;
  103. polynomial.coefficient = coefficient;
  104. polynomial.power = power;
  105. polynomial.successor = term;
  106. return;
  107. }
  108. //
  109. if (traverser == null || traverser.power < power)
  110. {
  111. Polynomial term = new Polynomial(0.0,0);
  112. term.coefficient = coefficient;
  113. term.power = power;
  114. term.successor = traverser;
  115. previous.successor = term;
  116. return;
  117. }
  118. coefficient += traverser.coefficient;
  119. if (Math.abs(coefficient) < epsilon) coefficient = 0.0;
  120. if (coefficient == 0.0)
  121. previous.successor = traverser.successor;
  122. else
  123. traverser.coefficient = coefficient;
  124. }
  125.  
  126. // cardinality Method
  127. // yields the number of terms in this polynomial
  128. final public int cardinality() {
  129. int count = 1;
  130.  
  131. Polynomial traverser = this.successor;
  132.  
  133. while (traverser != null) {
  134. count++;
  135. traverser = traverser.successor;
  136. }
  137. return count;
  138. }
  139.  
  140. // clone Method
  141. // yields a link-by-link copy of this polynomial
  142. final public Polynomial clone() {
  143. Polynomial result = new Polynomial(0.0, 0);
  144.  
  145. result.coefficient = this.coefficient;
  146.  
  147. result.power = this.power;
  148.  
  149. Polynomial traverserThis = this;
  150.  
  151. Polynomial traverserResult = result;
  152.  
  153. while (traverserThis.successor != null) {
  154. traverserResult.successor = new Polynomial(0.0, 0);
  155. traverserThis = traverserThis.successor;
  156. traverserResult = traverserResult.successor;
  157. traverserResult.coefficient = traverserThis.coefficient;
  158. traverserResult.power = traverserThis.power;
  159. }
  160.  
  161. return result;
  162. }
  163.  
  164. // coefficient Method
  165. // yields the coefficient of the term with that power
  166. // in this polynomial
  167. final public double coefficient(int power) {
  168. if (power < 0) {
  169. return 0.0;
  170. }
  171.  
  172. Polynomial traverser = this;
  173.  
  174. do {
  175. if (traverser.power < power) {
  176. return 0.0;
  177. }
  178. if (traverser.power == power) {
  179. return traverser.coefficient;
  180. }
  181. traverser = traverser.successor;
  182. } while (traverser != null);
  183.  
  184. return 0.0;
  185. }
  186.  
  187. // composite Method
  188. // validates the parameter and yields the polynomial result
  189. // for the operation: this(that(x))
  190. /**
  191. * validates the parameter and yields the polynomial result
  192. * for the operation: this(that(x))
  193. *
  194. * @param Polynomial that
  195. *
  196. * @return Polynomial outcome
  197. */
  198. final public Polynomial composite(Polynomial that) {
  199. /*students #2*/
  200. if (that == null) {
  201. return null;
  202. }
  203.  
  204. // setup a clone for this Polynomial
  205. Polynomial thisClone = this.clone();
  206. // setup a clone for that Polynomial
  207. Polynomial thatClone = that.clone();
  208. // setup a provisional (temporary) Polynomial
  209. Polynomial provisional = new Polynomial(0.0, 0);
  210. // store the outcome (result) in this Polynomial
  211. Polynomial outcome = new Polynomial(0.0, 0);
  212.  
  213. // formula is: this(that) = this.coeff(n).times(that(n)) + this.coeff(n-1).times(that(n-1))
  214.  
  215. while(thisClone != null)
  216. {
  217. // expand this power by the coefficient
  218. for( int i = 1; i <= thisClone.power; i++)
  219. {
  220. provisional.coefficient = thisClone.coefficient;
  221. provisional = provisional.times(thatClone);
  222. }
  223. // add to the outcome
  224. outcome = outcome.plus(provisional);
  225. // clear the provisional for the next loop
  226. provisional = new Polynomial(0.0,0);
  227. // move to the next element
  228. thisClone = thisClone.successor;
  229. }
  230.  
  231.  
  232. return outcome;
  233. }
  234.  
  235.  
  236. // degree Method
  237. // yields the degree (highest power) of this polynomial
  238. final public int degree() {
  239. return this.power;
  240. }
  241.  
  242. // differentiate method
  243. // yields the polynomial result for the operation:
  244. // d/dx(this(x))
  245. final public Polynomial differentiate() {
  246. /*students #3*/
  247. // setup traverser Polynomial
  248. Polynomial traverser = this;
  249. // setup provisional (temporary) Polynomial
  250. Polynomial provisional = new Polynomial(0.0,0);
  251. // setup outcome (result) Polynomial
  252. Polynomial outcome = new Polynomial(0.0,0);
  253.  
  254. if (this.power == 0)
  255. {
  256. return new Polynomial(0.0,0);
  257. }
  258. else
  259. {
  260. // search the Polynomial with traverser
  261. while (traverser!=null)
  262. {
  263. // Power > 1
  264. if(traverser.power >1)
  265. {
  266. provisional.coefficient = traverser.coefficient * traverser.power;
  267. provisional.power = traverser.power - 1;
  268.  
  269. }
  270. // Power = 1
  271. if(traverser.power ==1)
  272. {
  273. provisional.coefficient = traverser.coefficient;
  274. provisional.power = 0;
  275. }
  276. // Power = 0
  277. if(traverser.power ==0)
  278. {
  279. provisional.coefficient =0.0;
  280. provisional.power =0;
  281. }
  282. // move the traverser to the next element
  283. traverser = traverser.successor;
  284. // add the provisional to the ouctome Polynomial
  285. outcome = outcome.plus(provisional);
  286. }
  287. }
  288. return outcome;
  289. }
  290.  
  291. // dividedBy method
  292. // validates the parameter and yields the quotient and the
  293. // remainder (as a 2-array of polynomials) for
  294. // the operation: this(x) / that(x)
  295. final public Polynomial[] dividedBy(Polynomial that) {
  296. if (that == null) {
  297. return null;
  298. }
  299. if (that.coefficient == 0.0) {
  300. return null;
  301. }
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308. // Student Part
  309.  
  310.  
  311. double powerMaxM = that.powerMax();
  312.  
  313. int provisionalpower = (int) (this.degree() - powerMaxM);
  314.  
  315. // setup a provisional (temporary) Polynomial
  316.  
  317. // setup two doubles used to divide the quotient coefficient
  318. Polynomial quotient = new Polynomial(0.0, provisionalpower);
  319. Polynomial reminder = this.clone();
  320.  
  321.  
  322. for (int i = this.degree(); i >= powerMaxM; i--) {
  323.  
  324. quotient.coefficient = reminder.coefficient / that.coefficient;
  325.  
  326.  
  327. reminder = this.minus(that.times(quotient));
  328.  
  329.  
  330.  
  331. }
  332.  
  333.  
  334. // end Student Part
  335.  
  336. Polynomial[] result = new Polynomial[2];
  337. result[0] = quotient;
  338. result[1] = reminder;
  339. return result;
  340. }
  341.  
  342.  
  343.  
  344. // equals Method
  345. // yields true when this polynomial is a hard-copy
  346. // of that polynomial; and false otherwise
  347. final public boolean equals(Polynomial that) {
  348. if (that == null) {
  349. return false;
  350. }
  351.  
  352. if (this.coefficient != that.coefficient) {
  353. return false;
  354. }
  355.  
  356. if (this.power != that.power) {
  357. return false;
  358. }
  359.  
  360. if (this.successor == null && that.successor == null) {
  361. return true;
  362. }
  363.  
  364. if (this.successor == null && that.successor != null) {
  365. return false;
  366. }
  367.  
  368. if (this.successor != null && that.successor == null) {
  369. return false;
  370. }
  371.  
  372. return this.successor.equals(that.successor);
  373. }
  374.  
  375. // evaluate Method
  376. // validates the parameter and yields the double result
  377. // for the operation this(variable).
  378. final public double evaluate(double variable) {
  379. /*students #5 */
  380. if (Double.isNaN(variable)) {
  381. variable = 0.0;
  382. }
  383. if (Math.abs(variable) < epsilon) {
  384. variable = 0.0;
  385. }
  386. double value = 0.0;
  387.  
  388. // Student Part
  389.  
  390. // Setup a Polynomial for this Clone (pointer)
  391. Polynomial thisClone = this.clone();
  392. // search through this clone
  393. while(thisClone !=null)
  394. {
  395. // Power > 0
  396. if(thisClone.power > 0)
  397. {
  398. // append the clone's coeffeicient multiplied by the power
  399. value += thisClone.coefficient * Math.pow(variable, thisClone.power);
  400. }
  401. // Power is not > 0
  402. else
  403. {
  404. // append the clone's coefficient
  405. value += thisClone.coefficient;
  406. }
  407. // move the clone to the successor for the loop
  408. thisClone = thisClone.successor;
  409. }
  410.  
  411. // End Student Part
  412.  
  413. return value;
  414. }
  415.  
  416. // integrate Method
  417. // yields the polynomial result for the operation
  418. // [ this(x) x dx
  419. final public Polynomial integrate() {
  420. /*students #6*/
  421. if (this.coefficient == 0.0) {
  422. return new Polynomial(0.0, 0);
  423. }
  424. Polynomial result = this.clone();
  425.  
  426. // Student Part
  427.  
  428. // Setup outcome (answer) Polynomial
  429. Polynomial outcome = new Polynomial(0.0, 0);
  430. // Setup provisional (temporary) Polynomial
  431. Polynomial provisional = new Polynomial(0.0, 0);
  432.  
  433. while(result!=null)
  434. {
  435. // add 1 to the power for provisional
  436. provisional.power = result.power + 1;
  437. // make provisional coefficient equal
  438. // the result coefficient divided by
  439. // the new provisional power
  440. // provisional.coefficient = result.coefficient / provisional.power;
  441. provisional.coefficient = result.coefficient / (result.power +1);
  442. // move the result to the successor element
  443. result = result.successor;
  444. // add the provisional to the outcome and loop
  445. outcome = outcome.plus(provisional);
  446. }
  447.  
  448. // End Student Part
  449.  
  450. return result;
  451. }
  452.  
  453. // minus Method
  454. // yields a hard-copy of the result of this polynomial
  455. // subtract that polynomial
  456. final public Polynomial minus(Polynomial that) {
  457. if (that == null) {
  458. return null;
  459. }
  460.  
  461. if (this.equals(that)) {
  462. return new Polynomial(0.0, 0);
  463. }
  464.  
  465. Polynomial result = this.clone();
  466.  
  467. if (that.coefficient == 0.0) {
  468. return result;
  469. }
  470.  
  471. Polynomial traverser = that;
  472.  
  473. do {
  474. add(result, -traverser.coefficient, traverser.power);
  475. traverser = traverser.successor;
  476. } while (traverser != null);
  477.  
  478. return result;
  479. }
  480.  
  481. // plus Method
  482. // yields a hard-copy of the result of this polynomial
  483. // and that polynomial
  484. final public Polynomial plus(Polynomial that) {
  485. if (that == null) {
  486. return null;
  487. }
  488.  
  489. if (this.coefficient == 0.0) {
  490. return that.clone();
  491. }
  492.  
  493. Polynomial result = this.clone();
  494.  
  495. if (that.coefficient == 0.0) {
  496. return result;
  497. }
  498.  
  499. Polynomial traverser = that;
  500.  
  501. do {
  502. add(result, traverser.coefficient, traverser.power);
  503. traverser = traverser.successor;
  504. } while (traverser != null);
  505.  
  506. return result;
  507. }
  508.  
  509. // powerMax Method
  510. // yields the highest power of this polynomial by traversing all
  511. // the terms
  512. final public int powerMax() {
  513. int max = Integer.MIN_VALUE;
  514.  
  515. Polynomial traverser = this;
  516.  
  517. do {
  518. if (max < traverser.power) {
  519. max = traverser.power;
  520. }
  521.  
  522. traverser = traverser.successor;
  523.  
  524. } while (traverser != null);
  525.  
  526. return max;
  527. }
  528.  
  529. // powerMin Method
  530. // yields the least power of this polynomial by traversing all
  531. // the terms
  532. final public int powerMin() {
  533. int min = Integer.MAX_VALUE;
  534.  
  535. Polynomial traverser = this;
  536.  
  537. do {
  538. if (min > traverser.power) {
  539. min = traverser.power;
  540. }
  541. traverser = traverser.successor;
  542. } while (traverser != null);
  543.  
  544. return min;
  545. }
  546.  
  547. // times Method
  548. // validates the parameter and yields the polynomial result
  549. // for the operation this(x) x that(x)
  550. final public Polynomial times(Polynomial that) {
  551. /*students #7*/
  552. if (that == null)
  553. {
  554. return null;
  555. }
  556.  
  557. Polynomial result = new Polynomial(0.0, 0);
  558.  
  559. // Student Part
  560.  
  561. // Setup new polynomial for this clone
  562. Polynomial thisClone = this.clone();
  563. // Setup new polynomial for that clone
  564. Polynomial thatClone = that.clone();
  565. // Setup new provisional (temporary) polynomial
  566. Polynomial provisional = new Polynomial(0.0, 0);
  567.  
  568. // search through this clone
  569. while (thisClone != null)
  570. {
  571. // search through that clone
  572. while (thatClone != null)
  573. {
  574. // Either clone's coefficient is equal to zero (0)
  575. if (thisClone.coefficient == 0.0 || thatClone.coefficient == 0.0)
  576. {
  577. // make the provisional coefficient zero
  578. provisional.coefficient = 0.0;
  579. // make the provisional power zero
  580. provisional.power = 0;
  581. }
  582. // otherwise:
  583. else
  584. {
  585. // make the provisional coefficient equal this clone's coefficient
  586. // multiplied by that clone's coefficient
  587. provisional.coefficient = thisClone.coefficient * thatClone.coefficient;
  588. // make the provisional power equal to this clone's power
  589. // multiplied by that clone's power
  590. provisional.power = thisClone.power + thatClone.power;
  591. }
  592. // add the provisional to the result
  593. result = result.plus(provisional);
  594. // make that clone it's successor
  595. thatClone = thatClone.successor;
  596. }
  597. // clear that clone for next loop
  598. thatClone = that.clone();
  599. // make this clone it's successor
  600. thisClone = thisClone.successor;
  601. }
  602.  
  603. // End Student Part
  604.  
  605. return result;
  606. }
  607.  
  608. // @Override
  609. // toString Method
  610. // yields a String representation of all the terms
  611. // of this polynomial
  612. final public String toString() {
  613. String string = "" + this.coefficient + (this.power == 0 ? "" : "*X^" + this.power);
  614.  
  615. Polynomial traverser = this.successor;
  616.  
  617. while (traverser != null) {
  618. string += (traverser.coefficient > 0.0 ? "+" : "")
  619. + traverser.coefficient
  620. + (traverser.power == 0 ? "" : "*X^"
  621. + traverser.power);
  622. traverser = traverser.successor;
  623. }
  624.  
  625. return string;
  626. }
  627.  
  628. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement