Commit 8b43bdc0 authored by Jim Newton's avatar Jim Newton

live coding examples

parent f24ef619
import scala.math._
// find a root of the given function within the given range
def binSearch(left:Double,right:Double,f:Double=>Double,epsilon:Double):Option[Double] = {
def recur(left: Double, right: Double): Option[Double] = {
val m = (right + left) / 2.0
val fm = f(m)
if (abs(fm) < epsilon)
Some(m)
else if (fm < 0)
recur(m, right)
else
recur(left, m)
}
if ( f(left) <= 0 && f(right) >=0 )
recur(left,right)
else if (f(left) >=0 && f(right) <=0 )
recur(right,left)
else
None
}
binSearch(-Pi/10, Pi/20, sin, .0001)
binSearch(-Pi/10, Pi/20, x => 10 - x*x, .0001)
\ No newline at end of file
import scala.math._
def limit(f:Double=>Double,x0:Double,h:Double,test:(Double,Double)=>Boolean):Double = {
def recur(h:Double):Double = {
val f1 = f(x0 + h)
val f2 = f(x0 + h / 2)
if (test(f1, f2))
f2
else
recur( h / 2)
}
recur(h)
}
def almostEqual(epsilon:Double)
(a:Double,b:Double):Boolean = {
abs(a - b) < epsilon
}
def derivative(f:Double=>Double,dx:Double,test:(Double,Double)=>Boolean)
(x:Double):Double = {
def estimate(h:Double):Double = {
(f(x+h) - f(x)) / h
}
limit(estimate,0,dx,test)
}
val cos_est = derivative(sin,0.1,almostEqual(0.000001))(_)
for {n <- 0 to 20
x = -Pi + n*2 * Pi / 20
c1 = cos(x)
c2 = cos_est(x)
delta = c1 - c2
} println(s"delta = $delta")
type BooleanBinary = (Double,Double)=>Boolean
def integral(f:Double=>Double, left:Double, right:Double, test:BooleanBinary):Double = {
def sumRectangles(partitionWidth:Double):Double = {
val numPartitions = ((right-left)/partitionWidth).floor.toInt
(1 to numPartitions).foldLeft(0.0){
(acc,i) => acc + partitionWidth * f(left + i*partitionWidth)
}
}
limit(sumRectangles,0.0,(right-left)/2,test)
}
val sin_est = (x => integral(cos,0.0,x,almostEqual(0.0001)))
for {n <- 0 to 20
x = 0 + n*2 * Pi / 20
c1 = sin(x)
c2 = sin_est(x)
delta = c1 - c2
} println(s"delta = $delta")
......@@ -23,7 +23,6 @@ def derivative(f:Double=>Double,dx:Double,test:(Double,Double)=>Boolean)(x:Doubl
limit(estimate,dx:Double,test)(0)
}
val cos_est:Double=>Double = derivative(sin,0.01,almostEqual(0.000001)) // cos
val poly_est:Double=>Double = derivative(x => x*x + 3*x + 1,0.01,almostEqual(0.00001)) // 2*x + 3
......@@ -34,6 +33,5 @@ for {n <- 0 to 20
delta = c1 - c2
} println(s"$x delta= $delta")
//derivative(sin)(Pi/2)
//derivative(x => x*x + 3*x + 1)(1)
\ No newline at end of file
def fixedPoint[A](f:A=>A,data:A,goodEnough:(A,A)=>Boolean):A = {
val v = f(data)
if (goodEnough(v,data))
data
else
fixedPoint(f,v,goodEnough)
}
def g1(data:List[Char]):List[Char] = {
def recur(data:List[Char],acc:List[Char]):List[Char] = {
data match {
case c1 :: c2 :: cs =>
if (c1 == '(' && c2 == ')')
recur(cs,acc)
else
recur(c2::cs,c1::acc)
case c::cs =>
recur(cs,c::acc)
case List() =>
acc.reverse
}
}
recur(data,List())
}
def g(data:List[Char]):List[Char] = {
def cmp(a:List[Char],b:List[Char]):Boolean = {
a == b
}
fixedPoint(g1, data, cmp)
}
def f(data:String):String = {
g(data.toList).mkString
}
f(")()x((z))((())())y(")
......@@ -36,7 +36,6 @@ def integral(f:Double=>Double,left:Double,right:Double,test:(Double,Double)=>Boo
- integral(f, right, left, test)
else
limit(sumRectangles,(right - left)/2,test)(0)
}
for {n <- 0 to 20
......
import lecture.Polynomial._
Map(1->1, 2->1, 3->3).keys
val p1 = Map(1->11.3, 2->10.3, 3-> -3.1)
val p2 = Map(0->2.1, 2->10.3, 1-> -3.0)
val exponents = p1.keys ++ p2.keys
p1.get(2)
p1.get(0)
type POLY = Map[Int,Double]
def plus(p1:POLY, p2:POLY):POLY = {
val exponents = p1.keys ++ p2.keys
exponents.map{
e => (p1.get(e),p2.get(e)) match {
case (Some(c1),Some(c2)) => e -> (c1+c2)
case (None,Some(c2)) => e -> c2
case (Some(c1),None) => e -> c1
}
}.toMap
}
plus(p1,p2)
import scala.math.pow
def evaluate(p:POLY, x:Double):Double = {
p.foldLeft(0.0){
case (acc,(e,c)) => acc + pow(x,e) * c
}
}
evaluate(p1,0.0)
evaluate(p2,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