added assignment 8 ex 1
This commit is contained in:
		
							
								
								
									
										24
									
								
								src/Assignment8/Ex1.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/Assignment8/Ex1.scala
									
									
									
									
									
										Normal 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) | ||||
|   } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user