Question: Please solve the 3 functions using Scala! object poly { def roots(p: (Double, Double, Double)): Option[(Double, Double)] = { // = None if p has
Please solve the 3 functions using Scala!
object poly { def roots(p: (Double, Double, Double)): Option[(Double, Double)] = { // = None if p has no real roots // = Some((r1, r2)) where p(r1) == p(r2) == 0 } def deriv(p: (Double, Double, Double)): (Double, Double, Double) = // = derivative of p (which should be degree 1 def eval(a: Double, p: (Double, Double, Double)): Double = // = p(a) }
object PolyTest extends App { val p = (3.0, 9.0, -30.0) // = (3x - 6) * (x + 5) println("eval(6, p) = " + poly.eval(6, p)) println("eval(2, p) = " + poly.eval(2, p)) println("eval(-5, p) = " + poly.eval(-5, p)) println("roots(p) = " + poly.roots(p)) println("deriv(p) = " + poly.deriv(p)) println("deriv2(p) = " + poly.deriv(poly.deriv(p))) }A quadratic polynomial (i.e. a polynomial of the form ax + bx + c) can be represented as a 3-tuple in Scala. For example: 3x2 + 9x p 30 = (3x 6) (x + 5) = (3.0, 9.0, -30.0) Complete the implementation of poly.scala and test it with Poly Test.scala. Here is the expected output: eval (6, p) 132.0 eval (2, p) 0.0 eval(-5, p) = 0.0 roots (p) Some ( (2.0,-5.0)) deriv (p) (0.0,6.0,9.0) deriv2 (p) (0.0,0.0,6.0) = A quadratic polynomial (i.e. a polynomial of the form ax + bx + c) can be represented as a 3-tuple in Scala. For example: 3x2 + 9x p 30 = (3x 6) (x + 5) = (3.0, 9.0, -30.0) Complete the implementation of poly.scala and test it with Poly Test.scala. Here is the expected output: eval (6, p) 132.0 eval (2, p) 0.0 eval(-5, p) = 0.0 roots (p) Some ( (2.0,-5.0)) deriv (p) (0.0,6.0,9.0) deriv2 (p) (0.0,0.0,6.0) =
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
