25 lines
717 B
Scala
25 lines
717 B
Scala
package Assignment8
|
|
|
|
import utils.timeVerbose
|
|
|
|
import scala.collection.parallel.CollectionConverters._
|
|
|
|
object Ex1 extends App {
|
|
def integrate(a: Double, b: Double, nIntervals: Int, f: (Double => Double)): Double = {
|
|
val dx: Double = (b - a) / nIntervals
|
|
//val y: Double = (1 until nIntervals).map(i => f(i * dx + a)).sum
|
|
//val y: Double = (1 until nIntervals).view.map(i => f(i * dx + a)).sum
|
|
val y: Double = (1 until nIntervals).par.map(i => f(i * dx + a)).sum
|
|
return (2 * y + f(a) + f(b)) * dx / 2
|
|
}
|
|
|
|
println(integrate(1, 2, 500, math.sin))
|
|
|
|
println(integrate(0, 1, 500, math.sin _ compose math.cos))
|
|
|
|
timeVerbose {
|
|
val i = integrate(0, 1, math.pow(20, 6).toInt, math.sin)
|
|
}
|
|
}
|
|
|