Commit d1401116 authored by Jim Newton's avatar Jim Newton

added lecture 2 files

parent 7e2ddc63
// 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 FixedPoint {
def fixedPoint[A](initial:A, f:A=>A, goodEnough:(A,A)=>Boolean):A = {
def improve(current:A):A = {
val nextResult = f(current)
if (goodEnough(current,nextResult))
current
else
improve(nextResult)
}
improve(initial)
}
def flatten(xs:List[Int]):List[Int] = {
xs match {
case List() => xs
case head::Nil => xs
case x1::x2::tail => if (x1 <= x2)
x1 :: flatten(x2::tail)
else
flatten(x1::Math.abs(x2)+Math.abs(x1)::tail)
}
}
def main(argv:Array[String]):Unit = {
println(
fixedPoint(List(-16, 0, -1, 7, 17, 5, -12, 16),
flatten,
(x:List[Int],y:List[Int])=> x==y
)
)
}
}
// 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 BinarySearch {
import scala.math._
def binSearch(left:Double, right:Double, f:Double=>Double, target:Double, threshold:Double,maxDepth:Int):Option[Double] = {
// find where f(x) = target +/- threshold
binSearch(left, right, x=>f(x)-target, threshold,maxDepth)
}
def binSearch(left:Double, right:Double, f:Double=>Double, threshold:Double ,maxDepth:Int):Option[Double] = {
// find where f(x) = 0.0 +/- epsilon
val epsilon = abs(threshold * (f(left)-f(right)))
def recur(left:Double, right:Double, depth:Int):Option[Double] = {
val mid = (left + right)/2.0
val fm = f(mid)
val fl = f(left)
val fr = f(right)
if (depth >= maxDepth)
None
else if (abs(fm) < epsilon)
Some(mid)
else if (fm < 0)
recur(mid,right,depth+1)
else
recur(left,mid,depth+1)
}
if ( f(left) <= 0.0 && f(right) >= 0.0)
recur(left,right,0)
else if (f(left) >= 0.0 && f(right) <= 0.0)
recur(right,left,0)
else
None
}
def main(argv:Array[String]) = {
}
}
// 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
import scala.math._
object Calculus {
def almostEqual(tolerance:Double)(x:Double, y:Double):Boolean = {
abs(x - y ) <= tolerance
}
def limit(f: Double => Double, delta: Double, test: (Double, Double) => Boolean)(x: Double): Double = {
@scala.annotation.tailrec
def recur(h: Double, previous: Double): Double = {
val improved = f(x + h)
if (test(improved, previous)) {
improved
}
else
recur(h / 2, improved)
}
recur(delta / 2, f(x + delta))
}
def derivative(f: Double => Double, delta: Double, test: (Double, Double) => Boolean)(x: Double):Double = {
def estimate(h: Double): Double = {
(f(x + h) - f(x)) / h
}
// f(x+h) - f(x)
// derivative = Limit ------------- // which is a function of x
// h->0.0 h
limit(estimate, delta, test)(0.0)
}
def integral(f:Double=>Double, left:Double, right:Double, test:(Double,Double)=>Boolean):Double = {
def sumRectangles(partitionWidth: Double): Double = {
val numPartitions = floor((right - left) / partitionWidth).toInt
(0 until numPartitions).foldLeft(0.0) {
(area, partition) => area + partitionWidth * f(left + partition * partitionWidth)
}
}
if (right == left)
0.0
else if (right < left)
-1 * integral(f, right, left, test)
else
limit(sumRectangles, (right - left) / 2, test)(0.0)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment