...

Commits (2)
 // Copyright (c) 2020 EPITA Research and Development Laboratory // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package lecture object Polynomial { import scala.math.{pow,sqrt} // We represent a polynomial such as 4x^3 - 2x +4 // as Map(3->4.0, 1->-1.0, 0->4.0) type POLY = Map[Int,Double] // given a polynomial and a numerical value, evaluate the polynomial. // E.g., evaluate(Map(2->2.2, 0-> -1.1),4.3)) // ==> 2.2*pow(4.3,2) - 1.1*pow(4.3,0) def evaluate(a:POLY,x:Double):Double = { a.foldLeft(0.0){case (acc,(e,c)) => acc + c * pow(x,e)} } // given two polynomials, add them to form a new polynomial // e.g., plus(Map(2->2.2, 0-> -1.1),Map(3->3.3, 0-> -1.1)) // ==> Map(3->3.3, 2->2.2, 0-> -2.2) def plus(a:POLY,b:POLY):POLY = { (a.keys ++ b.keys).map { e => (a.get(e), b.get(e)) match { case (Some(c1), Some(c2)) => e -> (c1+c2) case (Some(c1), None) => e -> c1 case (None, Some(c2)) => e -> c2 } }.toMap } // detect whether two polynomials are close enough to qualify // as equal, i.e., their RMS is less than epsilon def almostEqual(epsilon:Double)(a:POLY,b:POLY):Boolean = { // RMS mean of difference sqrt((a.keys ++ b.keys).foldLeft(0.0) { (acc, e) => val diff = a.getOrElse(e, 0.0) - b.getOrElse(e, 0.0) diff * diff }) < epsilon } def main(argv:Array[String]):Unit = { () } }
 import scala.math._ import lecture.MetroData._ //stationPositions //stationPositions.map(triple => triple._1) //stationPositions.map(triple => triple._1).distinct // //stationPositions.map(triple => triple._1) // .distinct // .filter(name => ! name.toList.contains(' ')) stationPositions.filter(triple => triple._2 > 300) .map(_._1) stationPositions.toList .filter(triple => triple._2 > 300) .splitAt(3)
 // Copyright (c) 2020 EPITA Research and Development Laboratory // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import homework.BinarySearch._ import lecture.BinarySearch._ import org.scalatest.FunSuite import scala.math._ class BinarySearchTestSuite extends FunSuite { def almostEqual(tolerance:Double)(x:Double, y:Double):Boolean = { abs(x - y ) <= tolerance } test("tolerance") { assert(almostEqual(0.0)(1.0, 1.0)) assert(almostEqual(0.5)(1.1, 1.2)) assert(almostEqual(0.5)(100.1, 100.2)) assert(almostEqual(0.1)(1000.01, 1000.05)) assert(!almostEqual(0.01)(0.001, 0.101)) } test("bin search") { assert(None != binSearch(-1.5, 1.0, sin, 0.0001, 20)) assert(None != binSearch(-1.0, 1.0, sin, 0.000001, 30)) // given function does not equal 0.0 in the given interval assert(None == binSearch(1.0, 2.0, (x: Double) => 3.0, 0.001, 20)) assert(None != binSearch(-1.0, 1.0, sin, 0.1, 0.0001, 20)) // interval reversed assert(None != binSearch(1.0, -1.0, sin, 0.1, 0.0001, 20)) // given function does not equal 20.3 in the given interval assert(None == binSearch(1.0, -1.0, sin, 20.3, 0.001, 20)) // not enough iterations allowed assert(None == binSearch(-0.9, 0.72, sin, 0.000001, 2)) } test("boolean search") { def optionAlmostEqual(test: (Double, Double) => Boolean): (Option[Double], Option[Double]) => Boolean = { (op1, op2) => { (op1, op2) match { case (None,None) => true case (Some(a), Some(b)) => test(a, b) case _ => false } } } def ae(delta: Double): (Option[Double], Option[Double]) => Boolean = { optionAlmostEqual(almostEqual(delta)) } // check for maxDepth exceeded assert(None == binSearchByBoolean(0.0, 100.0, _>= 5.0, 0.01, 3)) assert(ae(0.1)(None,binSearchByBoolean(0.0, 100.0, _>= 5.0, 0.01, 2))) // check for successfully finding the switching point assert(ae(0.1)(Some(3.4), binSearchByBoolean(0.3, 52.1, _ > 3.4, 0.01, 1000))) // check for no such result in range assert(None == binSearchByBoolean(10.0, 20.0, _ > 3.4, 0.01, 100)) } }
 // Copyright (c) 2020 EPITA Research and Development Laboratory // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import homework.DoubleIntegral._ import lecture.Calculus.almostEqual import org.scalatest.FunSuite import scala.math._ class DoubleIntegralTestSuite extends FunSuite { test("slow double integral 1") { assert(0.0 == slowDoubleIntegral((x, y) => 0.0, -1.0, -1.0, 1.0, 1.0, _ == _)) } test("slow double integral 2") { assert(1.0 == slowDoubleIntegral((x, y) => 1.0, 0.0, 0.0, 1.0, 1.0, almostEqual(0.01))) } test("slow double integral 3") { // taken from http://www.stankova.net/statistics_2012/double_integration.pdf assert(almostEqual(0.01)(57.0, slowDoubleIntegral((x: Double, y: Double) => 1 + 8 * x * y, 0.0, 1.0, 3.0, 2.0, almostEqual(0.001)))) } test("slow double integral 4") { assert(almostEqual(0.1)(1.0, slowDoubleIntegral((x, y) => x * y, 0.0, 0.0, 1.0, 2.0, almostEqual(0.001)))) } test("slow double integral 5") { assert(almostEqual(0.01)(0.5, slowDoubleIntegral((x, y) => y * sin(x), 0.0, 0.0, (Pi / 2), 1.0, almostEqual(0.001)))) } test("slow double integral 6") { // example from https://mathinsight.org/double_integral_examples assert(almostEqual(0.01)(2.0 / 3, slowDoubleIntegral((x, y) => x * y * y, 0.0, 0.0, 2.0, 1.0, almostEqual(0.0001)))) } }
 // Copyright (c) 2020 EPITA Research and Development Laboratory // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import homework.FixedPoint._ import lecture.Calculus.almostEqual import org.scalatest.FunSuite class FixedPointTestSuite extends FunSuite { test("fixed point") { assert(almostEqual(0.01)(5.0, squareRoot(25.0))) } }