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) | ||||
|   } | ||||
| } | ||||
|  | ||||
							
								
								
									
										57
									
								
								src/utils/package.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/utils/package.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
| /** | ||||
|  * Some useful functions for Scala. | ||||
|  * | ||||
|  * @author Pierre-André Mudry | ||||
|  * @version 1.0 | ||||
|  */ | ||||
|  | ||||
| import java.io.{BufferedOutputStream, File, FileOutputStream} | ||||
| import scala.io.{Codec, Source} | ||||
|  | ||||
| package object utils { | ||||
|  | ||||
| 	/** | ||||
| 	 * There are functions for micro-benchmarking, but you should not | ||||
| 	 * rely on those functions for measuring short durations! | ||||
| 	 */ | ||||
|  | ||||
| 	// Measure the time for a block of code to run, approximately | ||||
| 	def timeVerbose(f: ⇒ Unit) = { | ||||
| 		println("[Time] Start of measure") | ||||
| 		val duration = time(f) | ||||
| 		println(s"[Time] Block duration was $duration ms\n") | ||||
| 		duration | ||||
| 	} | ||||
|  | ||||
| 	// With a customized error message | ||||
| 	def timeVerbose(s: String)(f: ⇒ Unit) = { | ||||
| 		println(s"[$s] Start of measure") | ||||
| 		val duration = time(f) | ||||
| 		println(s"[$s] Block duration was $duration ms\n") | ||||
| 		duration | ||||
| 	} | ||||
|  | ||||
| 	// Measure the time for a block of code to run, approximately | ||||
| 	def time(f: ⇒ Unit) = { | ||||
| 		val start = System.currentTimeMillis | ||||
| 		f // Execute the block | ||||
| 		val duration = System.currentTimeMillis - start | ||||
| 		duration | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * A class for reading and writing to files easily | ||||
| 	 */ | ||||
| 	implicit class RichFile(file: File) { | ||||
| 		def read() = Source.fromFile(file)(Codec.UTF8).mkString | ||||
|  | ||||
| 		def write(data: String) { | ||||
| 			val fos = new BufferedOutputStream(new FileOutputStream(file)) | ||||
| 			try { | ||||
| 				fos.write(data.getBytes("UTF-8")) | ||||
| 			} finally { | ||||
| 				fos.close | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user