Commit b951cf43 authored by Jim Newton's avatar Jim Newton

update

parent 6915ebcc
......@@ -33,26 +33,7 @@ object FixedPoint {
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
)
)
}
}
......@@ -69,4 +69,9 @@ object Calculus {
else
limit(sumRectangles, (right - left) / 2, test)(0.0)
}
def main(argv:Array[String]):Unit = {
println(almostEqual(0.001)(0, limit(cos,0.1,almostEqual(0.0001))(Pi/2)))
}
}
import scala.math._
def fixedPoint[A](h:A=>A,x0:A):A = {
val v=h(x0)
def fixedPoint[A](h: A => A, x0: A): A = {
val v = h(x0)
if (x0 == v)
x0
else
fixedPoint(h,v)
fixedPoint(h, v)
}
def g(data:List[Char]):List[Char] = {
def recur(data:List[Char],acc:List[Char]):List[Char] = {
def g3(data: List[Char], open: Char, close: Char): List[Char] = {
def recur(data: List[Char], acc: List[Char]): List[Char] = {
data match {
case op::cl::tail =>
if (op == '(' && cl == ')')
recur(tail,acc)
case op :: cl :: tail =>
if (op == open && cl == close)
recur(tail, acc)
else
recur(cl::tail,op::acc)
recur(cl :: tail, op :: acc)
case _ => (data ++ acc).reverse
}
}
recur(data,List())
recur(data, List())
}
def f(data:String):String = {
g(data.toList).mkString
def g(data: List[Char]): List[Char] = {
g3(data, '(', ')')
}
def f(data: String): String = {
g(data.toList).mkString
}
f(f(f(")()x((z))((())())y(")))
f("x")
g(")()x((z))((())())y(".toList)
fixedPoint(f,")()x((z))((())())y(")
fixedPoint(f, ")()x((z))((())())y(")
fixedPoint(g, ")()x((z))((())())y(".toList)
fixedPoint( g3(_,'(',')'),
")()x((z))((())())y(".toList)
fixedPoint( g3(_,'[',']'),
"][]x[[z]][[[]][]]y[".toList).mkString
......@@ -23,13 +23,20 @@ def derivative(f:Double=>Double,dx:Double,test:(Double,Double)=>Boolean)(x:Doubl
limit(estimate,dx:Double,test)(0)
}
def integral(f:Double=>Double,left:Double,right:Double,test:(Double,Double)=>Boolean):Double = {
def sumRectangles(partitionWidth: Double): Double = {
val numPartitions = ((right - left) / partitionWidth).floor.toInt
(1 to numPartitions).foldLeft(0.0)( (acc, i) => acc + f(left + i * partitionWidth) * partitionWidth )
}
limit(sumRectangles,(right - left)/2,test)(0)
//limit(sumRectangles,(right - left)/2,test)(0)
if (right == left)
0.0
else if (right < left)
- integral(f, right, left, test)
else
limit(sumRectangles,(right - left)/2,test)(0)
}
for {n <- 0 to 20
......@@ -37,4 +44,4 @@ for {n <- 0 to 20
s1 = sin(x)
s2 = integral(cos,0,x,almostEqual(0.00001))
delta = s1 - s2
} println(s"$x delta= $delta")
\ No newline at end of file
} println(s"$x delta= $delta")
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