Last active
June 5, 2021 10:06
-
-
Save taketo1024/e1e1b92b0c5c9c441bd870476356204e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
typealias R = Double | |
let step = 10 | |
func unitRand() -> R { | |
R(arc4random_uniform(UINT32_MAX)) / R(UINT32_MAX) | |
} | |
func forAnySmall(_ p: (R) -> Bool) -> Bool { | |
for i in 0 ..< step { | |
let ε = R(step - i) / R(step) | |
if !p(ε) { | |
return false | |
} | |
} | |
return true | |
} | |
func existsSmall(_ p: (R) -> Bool) -> Bool { | |
for i in 0 ..< step { | |
let δ = R(step - i) / R(step) | |
if p(δ) { | |
return true | |
} | |
} | |
return false | |
} | |
func forAnyInRange(_ from: R, _ to: R, p: ((R) -> Bool)) -> Bool { | |
for _ in 0 ..< step { | |
let x = from + unitRand() * (to - from) | |
if !p(x) { | |
return false | |
} | |
} | |
return true | |
} | |
func isContinuous(_ f: (R) -> R, at a: R) -> Bool { | |
forAnySmall { ε in | |
existsSmall { δ in | |
forAnyInRange(a - δ, a + δ) { x in | |
abs(f(x) - f(a)) < ε | |
} | |
} | |
} | |
} | |
print( isContinuous( {x in pow(x, 2)}, at: 0) ) | |
// true: x^2 is continuous at x = 0 | |
print( isContinuous( {x in floor(x)}, at: 1) ) | |
// false: floor func is not continuous at x = 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment