added assignment 8 ex 1

This commit is contained in:
2025-05-05 17:35:43 +02:00
parent a16b70970f
commit 6954695a0d
2 changed files with 81 additions and 0 deletions

24
src/Assignment8/Ex1.scala Normal file
View File

@ -0,0 +1,24 @@
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)
}
}