Up to index of Isabelle/HOL/Decision_Procs
theory Approximation(* Author: Johannes Hoelzl, TU Muenchen
Coercions removed by Dmitriy Traytel *)
header {* Prove Real Valued Inequalities by Computation *}
theory Approximation
imports
Complex_Main
"~~/src/HOL/Library/Float"
"~~/src/HOL/Library/Reflection"
"~~/src/HOL/Decision_Procs/Dense_Linear_Order"
"~~/src/HOL/Library/Efficient_Nat"
begin
section "Horner Scheme"
subsection {* Define auxiliary helper @{text horner} function *}
primrec horner :: "(nat => nat) => (nat => nat => nat) => nat => nat => nat => real => real" where
"horner F G 0 i k x = 0" |
"horner F G (Suc n) i k x = 1 / k - x * horner F G n (F i) (G i k) x"
lemma horner_schema': fixes x :: real and a :: "nat => real"
shows "a 0 - x * (∑ i=0..<n. (-1)^i * a (Suc i) * x^i) = (∑ i=0..<Suc n. (-1)^i * a i * x^i)"
proof -
have shift_pow: "!!i. - (x * ((-1)^i * a (Suc i) * x ^ i)) = (-1)^(Suc i) * a (Suc i) * x ^ (Suc i)" by auto
show ?thesis unfolding setsum_right_distrib shift_pow diff_minus setsum_negf[symmetric] setsum_head_upt_Suc[OF zero_less_Suc]
setsum_reindex[OF inj_Suc, unfolded comp_def, symmetric, of "λ n. (-1)^n *a n * x^n"] by auto
qed
lemma horner_schema: fixes f :: "nat => nat" and G :: "nat => nat => nat" and F :: "nat => nat"
assumes f_Suc: "!!n. f (Suc n) = G ((F ^^ n) s) (f n)"
shows "horner F G n ((F ^^ j') s) (f j') x = (∑ j = 0..< n. -1 ^ j * (1 / (f (j' + j))) * x ^ j)"
proof (induct n arbitrary: i k j')
case (Suc n)
show ?case unfolding horner.simps Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc]
using horner_schema'[of "λ j. 1 / (f (j' + j))"] by auto
qed auto
lemma horner_bounds':
fixes lb :: "nat => nat => nat => float => float" and ub :: "nat => nat => nat => float => float"
assumes "0 ≤ real x" and f_Suc: "!!n. f (Suc n) = G ((F ^^ n) s) (f n)"
and lb_0: "!! i k x. lb 0 i k x = 0"
and lb_Suc: "!! n i k x. lb (Suc n) i k x = lapprox_rat prec 1 k - x * (ub n (F i) (G i k) x)"
and ub_0: "!! i k x. ub 0 i k x = 0"
and ub_Suc: "!! n i k x. ub (Suc n) i k x = rapprox_rat prec 1 k - x * (lb n (F i) (G i k) x)"
shows "(lb n ((F ^^ j') s) (f j') x) ≤ horner F G n ((F ^^ j') s) (f j') x ∧
horner F G n ((F ^^ j') s) (f j') x ≤ (ub n ((F ^^ j') s) (f j') x)"
(is "?lb n j' ≤ ?horner n j' ∧ ?horner n j' ≤ ?ub n j'")
proof (induct n arbitrary: j')
case 0 thus ?case unfolding lb_0 ub_0 horner.simps by auto
next
case (Suc n)
have "?lb (Suc n) j' ≤ ?horner (Suc n) j'" unfolding lb_Suc ub_Suc horner.simps real_of_float_sub diff_minus
proof (rule add_mono)
show "(lapprox_rat prec 1 (f j')) ≤ 1 / (f j')" using lapprox_rat[of prec 1 "f j'"] by auto
from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct2] `0 ≤ real x`
show "- real (x * ub n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x) ≤
- (x * horner F G n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x)"
unfolding real_of_float_mult neg_le_iff_le by (rule mult_left_mono)
qed
moreover have "?horner (Suc n) j' ≤ ?ub (Suc n) j'" unfolding ub_Suc ub_Suc horner.simps real_of_float_sub diff_minus
proof (rule add_mono)
show "1 / (f j') ≤ (rapprox_rat prec 1 (f j'))" using rapprox_rat[of 1 "f j'" prec] by auto
from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct1] `0 ≤ real x`
show "- (x * horner F G n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x) ≤
- real (x * lb n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x)"
unfolding real_of_float_mult neg_le_iff_le by (rule mult_left_mono)
qed
ultimately show ?case by blast
qed
subsection "Theorems for floating point functions implementing the horner scheme"
text {*
Here @{term_type "f :: nat => nat"} is the sequence defining the Taylor series, the coefficients are
all alternating and reciprocs. We use @{term G} and @{term F} to describe the computation of @{term f}.
*}
lemma horner_bounds: fixes F :: "nat => nat" and G :: "nat => nat => nat"
assumes "0 ≤ real x" and f_Suc: "!!n. f (Suc n) = G ((F ^^ n) s) (f n)"
and lb_0: "!! i k x. lb 0 i k x = 0"
and lb_Suc: "!! n i k x. lb (Suc n) i k x = lapprox_rat prec 1 k - x * (ub n (F i) (G i k) x)"
and ub_0: "!! i k x. ub 0 i k x = 0"
and ub_Suc: "!! n i k x. ub (Suc n) i k x = rapprox_rat prec 1 k - x * (lb n (F i) (G i k) x)"
shows "(lb n ((F ^^ j') s) (f j') x) ≤ (∑j=0..<n. -1 ^ j * (1 / (f (j' + j))) * (x ^ j))" (is "?lb") and
"(∑j=0..<n. -1 ^ j * (1 / (f (j' + j))) * (x ^ j)) ≤ (ub n ((F ^^ j') s) (f j') x)" (is "?ub")
proof -
have "?lb ∧ ?ub"
using horner_bounds'[where lb=lb, OF `0 ≤ real x` f_Suc lb_0 lb_Suc ub_0 ub_Suc]
unfolding horner_schema[where f=f, OF f_Suc] .
thus "?lb" and "?ub" by auto
qed
lemma horner_bounds_nonpos: fixes F :: "nat => nat" and G :: "nat => nat => nat"
assumes "real x ≤ 0" and f_Suc: "!!n. f (Suc n) = G ((F ^^ n) s) (f n)"
and lb_0: "!! i k x. lb 0 i k x = 0"
and lb_Suc: "!! n i k x. lb (Suc n) i k x = lapprox_rat prec 1 k + x * (ub n (F i) (G i k) x)"
and ub_0: "!! i k x. ub 0 i k x = 0"
and ub_Suc: "!! n i k x. ub (Suc n) i k x = rapprox_rat prec 1 k + x * (lb n (F i) (G i k) x)"
shows "(lb n ((F ^^ j') s) (f j') x) ≤ (∑j=0..<n. (1 / (f (j' + j))) * real x ^ j)" (is "?lb") and
"(∑j=0..<n. (1 / (f (j' + j))) * real x ^ j) ≤ (ub n ((F ^^ j') s) (f j') x)" (is "?ub")
proof -
{ fix x y z :: float have "x - y * z = x + - y * z"
by (cases x, cases y, cases z, simp add: plus_float.simps minus_float_def uminus_float.simps times_float.simps algebra_simps)
} note diff_mult_minus = this
{ fix x :: float have "- (- x) = x" by (cases x, auto simp add: uminus_float.simps) } note minus_minus = this
have move_minus: "(-x) = -1 * real x" by auto (* coercion "inside" is necessary *)
have sum_eq: "(∑j=0..<n. (1 / (f (j' + j))) * real x ^ j) =
(∑j = 0..<n. -1 ^ j * (1 / (f (j' + j))) * real (- x) ^ j)"
proof (rule setsum_cong, simp)
fix j assume "j ∈ {0 ..< n}"
show "1 / (f (j' + j)) * real x ^ j = -1 ^ j * (1 / (f (j' + j))) * real (- x) ^ j"
unfolding move_minus power_mult_distrib mult_assoc[symmetric]
unfolding mult_commute unfolding mult_assoc[of "-1 ^ j", symmetric] power_mult_distrib[symmetric]
by auto
qed
have "0 ≤ real (-x)" using assms by auto
from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec
and lb="λ n i k x. lb n i k (-x)" and ub="λ n i k x. ub n i k (-x)", unfolded lb_Suc ub_Suc diff_mult_minus,
OF this f_Suc lb_0 refl ub_0 refl]
show "?lb" and "?ub" unfolding minus_minus sum_eq
by auto
qed
subsection {* Selectors for next even or odd number *}
text {*
The horner scheme computes alternating series. To get the upper and lower bounds we need to
guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}.
*}
definition get_odd :: "nat => nat" where
"get_odd n = (if odd n then n else (Suc n))"
definition get_even :: "nat => nat" where
"get_even n = (if even n then n else (Suc n))"
lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto)
lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto)
lemma get_odd_ex: "∃ k. Suc k = get_odd n ∧ odd (Suc k)"
proof (cases "odd n")
case True hence "0 < n" by (rule odd_pos)
from gr0_implies_Suc[OF this] obtain k where "Suc k = n" by auto
thus ?thesis unfolding get_odd_def if_P[OF True] using True[unfolded `Suc k = n`[symmetric]] by blast
next
case False hence "odd (Suc n)" by auto
thus ?thesis unfolding get_odd_def if_not_P[OF False] by blast
qed
lemma get_even_double: "∃i. get_even n = 2 * i" using get_even[unfolded even_mult_two_ex] .
lemma get_odd_double: "∃i. get_odd n = 2 * i + 1" using get_odd[unfolded odd_Suc_mult_two_ex] by auto
section "Power function"
definition float_power_bnds :: "nat => float => float => float * float" where
"float_power_bnds n l u = (if odd n ∨ 0 < l then (l ^ n, u ^ n)
else if u < 0 then (u ^ n, l ^ n)
else (0, (max (-l) u) ^ n))"
lemma float_power_bnds: fixes x :: real
assumes "(l1, u1) = float_power_bnds n l u" and "x ∈ {l .. u}"
shows "x ^ n ∈ {l1..u1}"
proof (cases "even n")
case True
show ?thesis
proof (cases "0 < l")
case True hence "odd n ∨ 0 < l" and "0 ≤ real l" unfolding less_float_def by auto
have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n ∨ 0 < l`] by auto
have "real l ^ n ≤ x ^ n" and "x ^ n ≤ real u ^ n " using `0 ≤ real l` and assms unfolding atLeastAtMost_iff using power_mono[of l x] power_mono[of x u] by auto
thus ?thesis using assms `0 < l` unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
next
case False hence P: "¬ (odd n ∨ 0 < l)" using `even n` by auto
show ?thesis
proof (cases "u < 0")
case True hence "0 ≤ - real u" and "- real u ≤ - x" and "0 ≤ - x" and "-x ≤ - real l" using assms unfolding less_float_def by auto
hence "real u ^ n ≤ x ^ n" and "x ^ n ≤ real l ^ n" using power_mono[of "-x" "-real l" n] power_mono[of "-real u" "-x" n]
unfolding power_minus_even[OF `even n`] by auto
moreover have u1: "u1 = l ^ n" and l1: "l1 = u ^ n" using assms unfolding float_power_bnds_def if_not_P[OF P] if_P[OF True] by auto
ultimately show ?thesis using float_power by auto
next
case False
have "¦x¦ ≤ real (max (-l) u)"
proof (cases "-l ≤ u")
case True thus ?thesis unfolding max_def if_P[OF True] using assms unfolding le_float_def by auto
next
case False thus ?thesis unfolding max_def if_not_P[OF False] using assms unfolding le_float_def by auto
qed
hence x_abs: "¦x¦ ≤ ¦real (max (-l) u)¦" by auto
have u1: "u1 = (max (-l) u) ^ n" and l1: "l1 = 0" using assms unfolding float_power_bnds_def if_not_P[OF P] if_not_P[OF False] by auto
show ?thesis unfolding atLeastAtMost_iff l1 u1 float_power using zero_le_even_power[OF `even n`] power_mono_even[OF `even n` x_abs] by auto
qed
qed
next
case False hence "odd n ∨ 0 < l" by auto
have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n ∨ 0 < l`] by auto
have "real l ^ n ≤ x ^ n" and "x ^ n ≤ real u ^ n " using assms unfolding atLeastAtMost_iff using power_mono_odd[OF False] by auto
thus ?thesis unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
qed
lemma bnds_power: "∀ (x::real) l u. (l1, u1) = float_power_bnds n l u ∧ x ∈ {l .. u} --> l1 ≤ x ^ n ∧ x ^ n ≤ u1"
using float_power_bnds by auto
section "Square root"
text {*
The square root computation is implemented as newton iteration. As first first step we use the
nearest power of two greater than the square root.
*}
fun sqrt_iteration :: "nat => nat => float => float" where
"sqrt_iteration prec 0 (Float m e) = Float 1 ((e + bitlen m) div 2 + 1)" |
"sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x
in Float 1 -1 * (y + float_divr prec x y))"
function ub_sqrt lb_sqrt :: "nat => float => float" where
"ub_sqrt prec x = (if 0 < x then (sqrt_iteration prec prec x)
else if x < 0 then - lb_sqrt prec (- x)
else 0)" |
"lb_sqrt prec x = (if 0 < x then (float_divl prec x (sqrt_iteration prec prec x))
else if x < 0 then - ub_sqrt prec (- x)
else 0)"
by pat_completeness auto
termination by (relation "measure (λ v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def)
declare lb_sqrt.simps[simp del]
declare ub_sqrt.simps[simp del]
lemma sqrt_ub_pos_pos_1:
assumes "sqrt x < b" and "0 < b" and "0 < x"
shows "sqrt x < (b + x / b)/2"
proof -
from assms have "0 < (b - sqrt x) ^ 2 " by simp
also have "… = b ^ 2 - 2 * b * sqrt x + (sqrt x) ^ 2" by algebra
also have "… = b ^ 2 - 2 * b * sqrt x + x" using assms by (simp add: real_sqrt_pow2)
finally have "0 < b ^ 2 - 2 * b * sqrt x + x" by assumption
hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms
by (simp add: field_simps power2_eq_square)
thus ?thesis by (simp add: field_simps)
qed
lemma sqrt_iteration_bound: assumes "0 < real x"
shows "sqrt x < (sqrt_iteration prec n x)"
proof (induct n)
case 0
show ?case
proof (cases x)
case (Float m e)
hence "0 < m" using float_pos_m_pos[unfolded less_float_def] assms by auto
hence "0 < sqrt m" by auto
have int_nat_bl: "(nat (bitlen m)) = bitlen m" using bitlen_ge0 by auto
have "x = (m / 2^nat (bitlen m)) * pow2 (e + (nat (bitlen m)))"
unfolding pow2_add pow2_int Float real_of_float_simp by auto
also have "… < 1 * pow2 (e + nat (bitlen m))"
proof (rule mult_strict_right_mono, auto)
show "real m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2]
unfolding real_of_int_less_iff[of m, symmetric] by auto
qed
finally have "sqrt x < sqrt (pow2 (e + bitlen m))" unfolding int_nat_bl by auto
also have "… ≤ pow2 ((e + bitlen m) div 2 + 1)"
proof -
let ?E = "e + bitlen m"
have E_mod_pow: "pow2 (?E mod 2) < 4"
proof (cases "?E mod 2 = 1")
case True thus ?thesis by auto
next
case False
have "0 ≤ ?E mod 2" by auto
have "?E mod 2 < 2" by auto
from this[THEN zless_imp_add1_zle]
have "?E mod 2 ≤ 0" using False by auto
from xt1(5)[OF `0 ≤ ?E mod 2` this]
show ?thesis by auto
qed
hence "sqrt (pow2 (?E mod 2)) < sqrt (2 * 2)" by auto
hence E_mod_pow: "sqrt (pow2 (?E mod 2)) < 2" unfolding real_sqrt_abs2 by auto
have E_eq: "pow2 ?E = pow2 (?E div 2 + ?E div 2 + ?E mod 2)" by auto
have "sqrt (pow2 ?E) = sqrt (pow2 (?E div 2) * pow2 (?E div 2) * pow2 (?E mod 2))"
unfolding E_eq unfolding pow2_add ..
also have "… = pow2 (?E div 2) * sqrt (pow2 (?E mod 2))"
unfolding real_sqrt_mult[of _ "pow2 (?E mod 2)"] real_sqrt_abs2 by auto
also have "… < pow2 (?E div 2) * 2"
by (rule mult_strict_left_mono, auto intro: E_mod_pow)
also have "… = pow2 (?E div 2 + 1)" unfolding add_commute[of _ 1] pow2_add1 by auto
finally show ?thesis by auto
qed
finally show ?thesis
unfolding Float sqrt_iteration.simps real_of_float_simp by auto
qed
next
case (Suc n)
let ?b = "sqrt_iteration prec n x"
have "0 < sqrt x" using `0 < real x` by auto
also have "… < real ?b" using Suc .
finally have "sqrt x < (?b + x / ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < real x`] by auto
also have "… ≤ (?b + (float_divr prec x ?b))/2" by (rule divide_right_mono, auto simp add: float_divr)
also have "… = (Float 1 -1) * (?b + (float_divr prec x ?b))" by auto
finally show ?case unfolding sqrt_iteration.simps Let_def real_of_float_mult real_of_float_add right_distrib .
qed
lemma sqrt_iteration_lower_bound: assumes "0 < real x"
shows "0 < real (sqrt_iteration prec n x)" (is "0 < ?sqrt")
proof -
have "0 < sqrt x" using assms by auto
also have "… < ?sqrt" using sqrt_iteration_bound[OF assms] .
finally show ?thesis .
qed
lemma lb_sqrt_lower_bound: assumes "0 ≤ real x"
shows "0 ≤ real (lb_sqrt prec x)"
proof (cases "0 < x")
case True hence "0 < real x" and "0 ≤ x" using `0 ≤ real x` unfolding less_float_def le_float_def by auto
hence "0 < sqrt_iteration prec prec x" unfolding less_float_def using sqrt_iteration_lower_bound by auto
hence "0 ≤ real (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 ≤ x`] unfolding le_float_def by auto
thus ?thesis unfolding lb_sqrt.simps using True by auto
next
case False with `0 ≤ real x` have "real x = 0" unfolding less_float_def by auto
thus ?thesis unfolding lb_sqrt.simps less_float_def by auto
qed
lemma bnds_sqrt':
shows "sqrt x ∈ {(lb_sqrt prec x) .. (ub_sqrt prec x) }"
proof -
{ fix x :: float assume "0 < x"
hence "0 < real x" and "0 ≤ real x" unfolding less_float_def by auto
hence sqrt_gt0: "0 < sqrt x" by auto
hence sqrt_ub: "sqrt x < sqrt_iteration prec prec x" using sqrt_iteration_bound by auto
have "(float_divl prec x (sqrt_iteration prec prec x)) ≤
x / (sqrt_iteration prec prec x)" by (rule float_divl)
also have "… < x / sqrt x"
by (rule divide_strict_left_mono[OF sqrt_ub `0 < real x`
mult_pos_pos[OF order_less_trans[OF sqrt_gt0 sqrt_ub] sqrt_gt0]])
also have "… = sqrt x"
unfolding inverse_eq_iff_eq[of _ "sqrt x", symmetric]
sqrt_divide_self_eq[OF `0 ≤ real x`, symmetric] by auto
finally have "lb_sqrt prec x ≤ sqrt x"
unfolding lb_sqrt.simps if_P[OF `0 < x`] by auto }
note lb = this
{ fix x :: float assume "0 < x"
hence "0 < real x" unfolding less_float_def by auto
hence "0 < sqrt x" by auto
hence "sqrt x < sqrt_iteration prec prec x"
using sqrt_iteration_bound by auto
hence "sqrt x ≤ ub_sqrt prec x"
unfolding ub_sqrt.simps if_P[OF `0 < x`] by auto }
note ub = this
show ?thesis
proof (cases "0 < x")
case True with lb ub show ?thesis by auto
next case False show ?thesis
proof (cases "real x = 0")
case True thus ?thesis
by (auto simp add: less_float_def lb_sqrt.simps ub_sqrt.simps)
next
case False with `¬ 0 < x` have "x < 0" and "0 < -x"
by (auto simp add: less_float_def)
with `¬ 0 < x`
show ?thesis using lb[OF `0 < -x`] ub[OF `0 < -x`]
by (auto simp add: real_sqrt_minus lb_sqrt.simps ub_sqrt.simps)
qed qed
qed
lemma bnds_sqrt: "∀ (x::real) lx ux. (l, u) = (lb_sqrt prec lx, ub_sqrt prec ux) ∧ x ∈ {lx .. ux} --> l ≤ sqrt x ∧ sqrt x ≤ u"
proof ((rule allI) +, rule impI, erule conjE, rule conjI)
fix x :: real fix lx ux
assume "(l, u) = (lb_sqrt prec lx, ub_sqrt prec ux)"
and x: "x ∈ {lx .. ux}"
hence l: "l = lb_sqrt prec lx " and u: "u = ub_sqrt prec ux" by auto
have "sqrt lx ≤ sqrt x" using x by auto
from order_trans[OF _ this]
show "l ≤ sqrt x" unfolding l using bnds_sqrt'[of lx prec] by auto
have "sqrt x ≤ sqrt ux" using x by auto
from order_trans[OF this]
show "sqrt x ≤ u" unfolding u using bnds_sqrt'[of ux prec] by auto
qed
section "Arcus tangens and π"
subsection "Compute arcus tangens series"
text {*
As first step we implement the computation of the arcus tangens series. This is only valid in the range
@{term "{-1 :: real .. 1}"}. This is used to compute π and then the entire arcus tangens.
*}
fun ub_arctan_horner :: "nat => nat => nat => float => float"
and lb_arctan_horner :: "nat => nat => nat => float => float" where
"ub_arctan_horner prec 0 k x = 0"
| "ub_arctan_horner prec (Suc n) k x =
(rapprox_rat prec 1 k) - x * (lb_arctan_horner prec n (k + 2) x)"
| "lb_arctan_horner prec 0 k x = 0"
| "lb_arctan_horner prec (Suc n) k x =
(lapprox_rat prec 1 k) - x * (ub_arctan_horner prec n (k + 2) x)"
lemma arctan_0_1_bounds': assumes "0 ≤ real x" "real x ≤ 1" and "even n"
shows "arctan x ∈ {(x * lb_arctan_horner prec n 1 (x * x)) .. (x * ub_arctan_horner prec (Suc n) 1 (x * x))}"
proof -
let "?c i" = "-1^i * (1 / (i * 2 + (1::nat)) * real x ^ (i * 2 + 1))"
let "?S n" = "∑ i=0..<n. ?c i"
have "0 ≤ real (x * x)" by auto
from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto
have "arctan x ∈ { ?S n .. ?S (Suc n) }"
proof (cases "real x = 0")
case False
hence "0 < real x" using `0 ≤ real x` by auto
hence prem: "0 < 1 / (0 * 2 + (1::nat)) * real x ^ (0 * 2 + 1)" by auto
have "¦ real x ¦ ≤ 1" using `0 ≤ real x` `real x ≤ 1` by auto
from mp[OF summable_Leibniz(2)[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]] prem, THEN spec, of m, unfolded `2 * m = n`]
show ?thesis unfolding arctan_series[OF `¦ real x ¦ ≤ 1`] Suc_eq_plus1 .
qed auto
note arctan_bounds = this[unfolded atLeastAtMost_iff]
have F: "!!n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto
note bounds = horner_bounds[where s=1 and f="λi. 2 * i + 1" and j'=0
and lb="λn i k x. lb_arctan_horner prec n k x"
and ub="λn i k x. ub_arctan_horner prec n k x",
OF `0 ≤ real (x*x)` F lb_arctan_horner.simps ub_arctan_horner.simps]
{ have "(x * lb_arctan_horner prec n 1 (x*x)) ≤ ?S n"
using bounds(1) `0 ≤ real x`
unfolding real_of_float_mult power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric]
unfolding mult_commute[where 'a=real] mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"]
by (auto intro!: mult_left_mono)
also have "… ≤ arctan x" using arctan_bounds ..
finally have "(x * lb_arctan_horner prec n 1 (x*x)) ≤ arctan x" . }
moreover
{ have "arctan x ≤ ?S (Suc n)" using arctan_bounds ..
also have "… ≤ (x * ub_arctan_horner prec (Suc n) 1 (x*x))"
using bounds(2)[of "Suc n"] `0 ≤ real x`
unfolding real_of_float_mult power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric]
unfolding mult_commute[where 'a=real] mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"]
by (auto intro!: mult_left_mono)
finally have "arctan x ≤ (x * ub_arctan_horner prec (Suc n) 1 (x*x))" . }
ultimately show ?thesis by auto
qed
lemma arctan_0_1_bounds: assumes "0 ≤ real x" "real x ≤ 1"
shows "arctan x ∈ {(x * lb_arctan_horner prec (get_even n) 1 (x * x)) .. (x * ub_arctan_horner prec (get_odd n) 1 (x * x))}"
proof (cases "even n")
case True
obtain n' where "Suc n' = get_odd n" and "odd (Suc n')" using get_odd_ex by auto
hence "even n'" unfolding even_Suc by auto
have "arctan x ≤ x * ub_arctan_horner prec (get_odd n) 1 (x * x)"
unfolding `Suc n' = get_odd n`[symmetric] using arctan_0_1_bounds'[OF `0 ≤ real x` `real x ≤ 1` `even n'`] by auto
moreover
have "x * lb_arctan_horner prec (get_even n) 1 (x * x) ≤ arctan x"
unfolding get_even_def if_P[OF True] using arctan_0_1_bounds'[OF `0 ≤ real x` `real x ≤ 1` `even n`] by auto
ultimately show ?thesis by auto
next
case False hence "0 < n" by (rule odd_pos)
from gr0_implies_Suc[OF this] obtain n' where "n = Suc n'" ..
from False[unfolded this even_Suc]
have "even n'" and "even (Suc (Suc n'))" by auto
have "get_odd n = Suc n'" unfolding get_odd_def if_P[OF False] using `n = Suc n'` .
have "arctan x ≤ x * ub_arctan_horner prec (get_odd n) 1 (x * x)"
unfolding `get_odd n = Suc n'` using arctan_0_1_bounds'[OF `0 ≤ real x` `real x ≤ 1` `even n'`] by auto
moreover
have "(x * lb_arctan_horner prec (get_even n) 1 (x * x)) ≤ arctan x"
unfolding get_even_def if_not_P[OF False] unfolding `n = Suc n'` using arctan_0_1_bounds'[OF `0 ≤ real x` `real x ≤ 1` `even (Suc (Suc n'))`] by auto
ultimately show ?thesis by auto
qed
subsection "Compute π"
definition ub_pi :: "nat => float" where
"ub_pi prec = (let A = rapprox_rat prec 1 5 ;
B = lapprox_rat prec 1 239
in ((Float 1 2) * ((Float 1 2) * A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (A * A)) -
B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1 (B * B)))))"
definition lb_pi :: "nat => float" where
"lb_pi prec = (let A = lapprox_rat prec 1 5 ;
B = rapprox_rat prec 1 239
in ((Float 1 2) * ((Float 1 2) * A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (A * A)) -
B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1 (B * B)))))"
lemma pi_boundaries: "pi ∈ {(lb_pi n) .. (ub_pi n)}"
proof -
have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))" unfolding machin[symmetric] by auto
{ fix prec n :: nat fix k :: int assume "1 < k" hence "0 ≤ k" and "0 < k" and "1 ≤ k" by auto
let ?k = "rapprox_rat prec 1 k"
have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
have "0 ≤ real ?k" by (rule order_trans[OF _ rapprox_rat], auto simp add: `0 ≤ k`)
have "real ?k ≤ 1" unfolding rapprox_rat.simps(2)[OF zero_le_one `0 < k`]
by (rule rapprox_posrat_le1, auto simp add: `0 < k` `1 ≤ k`)
have "1 / k ≤ ?k" using rapprox_rat[where x=1 and y=k] by auto
hence "arctan (1 / k) ≤ arctan ?k" by (rule arctan_monotone')
also have "… ≤ (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))"
using arctan_0_1_bounds[OF `0 ≤ real ?k` `real ?k ≤ 1`] by auto
finally have "arctan (1 / k) ≤ ?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k)" .
} note ub_arctan = this
{ fix prec n :: nat fix k :: int assume "1 < k" hence "0 ≤ k" and "0 < k" by auto
let ?k = "lapprox_rat prec 1 k"
have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
have "1 / k ≤ 1" using `1 < k` by auto
have "!!n. 0 ≤ real ?k" using lapprox_rat_bottom[where x=1 and y=k, OF zero_le_one `0 < k`] by (auto simp add: `1 div k = 0`)
have "!!n. real ?k ≤ 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / k ≤ 1`)
have "?k ≤ 1 / k" using lapprox_rat[where x=1 and y=k] by auto
have "?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k) ≤ arctan ?k"
using arctan_0_1_bounds[OF `0 ≤ real ?k` `real ?k ≤ 1`] by auto
also have "… ≤ arctan (1 / k)" using `?k ≤ 1 / k` by (rule arctan_monotone')
finally have "?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k) ≤ arctan (1 / k)" .
} note lb_arctan = this
have "pi ≤ ub_pi n"
unfolding ub_pi_def machin_pi Let_def real_of_float_mult real_of_float_sub unfolding Float_num
using lb_arctan[of 239] ub_arctan[of 5]
by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
moreover
have "lb_pi n ≤ pi"
unfolding lb_pi_def machin_pi Let_def real_of_float_mult real_of_float_sub Float_num
using lb_arctan[of 5] ub_arctan[of 239]
by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
ultimately show ?thesis by auto
qed
subsection "Compute arcus tangens in the entire domain"
function lb_arctan :: "nat => float => float" and ub_arctan :: "nat => float => float" where
"lb_arctan prec x = (let ub_horner = λ x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ;
lb_horner = λ x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)
in (if x < 0 then - ub_arctan prec (-x) else
if x ≤ Float 1 -1 then lb_horner x else
if x ≤ Float 1 1 then Float 1 1 * lb_horner (float_divl prec x (1 + ub_sqrt prec (1 + x * x)))
else (let inv = float_divr prec 1 x
in if inv > 1 then 0
else lb_pi prec * Float 1 -1 - ub_horner inv)))"
| "ub_arctan prec x = (let lb_horner = λ x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ;
ub_horner = λ x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)
in (if x < 0 then - lb_arctan prec (-x) else
if x ≤ Float 1 -1 then ub_horner x else
if x ≤ Float 1 1 then let y = float_divr prec x (1 + lb_sqrt prec (1 + x * x))
in if y > 1 then ub_pi prec * Float 1 -1
else Float 1 1 * ub_horner y
else ub_pi prec * Float 1 -1 - lb_horner (float_divl prec 1 x)))"
by pat_completeness auto
termination by (relation "measure (λ v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def)
declare ub_arctan_horner.simps[simp del]
declare lb_arctan_horner.simps[simp del]
lemma lb_arctan_bound': assumes "0 ≤ real x"
shows "lb_arctan prec x ≤ arctan x"
proof -
have "¬ x < 0" and "0 ≤ x" unfolding less_float_def le_float_def using `0 ≤ real x` by auto
let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
show ?thesis
proof (cases "x ≤ Float 1 -1")
case True hence "real x ≤ 1" unfolding le_float_def Float_num by auto
show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `¬ x < 0`] if_P[OF True]
using arctan_0_1_bounds[OF `0 ≤ real x` `real x ≤ 1`] by auto
next
case False hence "0 < real x" unfolding le_float_def Float_num by auto
let ?R = "1 + sqrt (1 + real x * real x)"
let ?fR = "1 + ub_sqrt prec (1 + x * x)"
let ?DIV = "float_divl prec x ?fR"
have sqr_ge0: "0 ≤ 1 + real x * real x" using sum_power2_ge_zero[of 1 "real x", unfolded numeral_2_eq_2] by auto
hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
have "sqrt (1 + x * x) ≤ ub_sqrt prec (1 + x * x)"
using bnds_sqrt'[of "1 + x * x"] by auto
hence "?R ≤ ?fR" by auto
hence "0 < ?fR" and "0 < real ?fR" unfolding less_float_def using `0 < ?R` by auto
have monotone: "(float_divl prec x ?fR) ≤ x / ?R"
proof -
have "?DIV ≤ real x / ?fR" by (rule float_divl)
also have "… ≤ x / ?R" by (rule divide_left_mono[OF `?R ≤ ?fR` `0 ≤ real x` mult_pos_pos[OF order_less_le_trans[OF divisor_gt0 `?R ≤ real ?fR`] divisor_gt0]])
finally show ?thesis .
qed
show ?thesis
proof (cases "x ≤ Float 1 1")
case True
have "x ≤ sqrt (1 + x * x)" using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto
also have "… ≤ (ub_sqrt prec (1 + x * x))"
using bnds_sqrt'[of "1 + x * x"] by auto
finally have "real x ≤ ?fR" by auto
moreover have "?DIV ≤ real x / ?fR" by (rule float_divl)
ultimately have "real ?DIV ≤ 1" unfolding divide_le_eq_1_pos[OF `0 < real ?fR`, symmetric] by auto
have "0 ≤ real ?DIV" using float_divl_lower_bound[OF `0 ≤ x` `0 < ?fR`] unfolding le_float_def by auto
have "(Float 1 1 * ?lb_horner ?DIV) ≤ 2 * arctan (float_divl prec x ?fR)" unfolding real_of_float_mult[of "Float 1 1"] Float_num
using arctan_0_1_bounds[OF `0 ≤ real ?DIV` `real ?DIV ≤ 1`] by auto
also have "… ≤ 2 * arctan (x / ?R)"
using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
also have "2 * arctan (x / ?R) = arctan x" using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left .
finally show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `¬ x < 0`] if_not_P[OF `¬ x ≤ Float 1 -1`] if_P[OF True] .
next
case False
hence "2 < real x" unfolding le_float_def Float_num by auto
hence "1 ≤ real x" by auto
let "?invx" = "float_divr prec 1 x"
have "0 ≤ arctan x" using arctan_monotone'[OF `0 ≤ real x`] using arctan_tan[of 0, unfolded tan_zero] by auto
show ?thesis
proof (cases "1 < ?invx")
case True
show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `¬ x < 0`] if_not_P[OF `¬ x ≤ Float 1 -1`] if_not_P[OF False] if_P[OF True]
using `0 ≤ arctan x` by auto
next
case False
hence "real ?invx ≤ 1" unfolding less_float_def by auto
have "0 ≤ real ?invx" by (rule order_trans[OF _ float_divr], auto simp add: `0 ≤ real x`)
have "1 / x ≠ 0" and "0 < 1 / x" using `0 < real x` by auto
have "arctan (1 / x) ≤ arctan ?invx" unfolding real_of_float_1[symmetric] by (rule arctan_monotone', rule float_divr)
also have "… ≤ (?ub_horner ?invx)" using arctan_0_1_bounds[OF `0 ≤ real ?invx` `real ?invx ≤ 1`] by auto
finally have "pi / 2 - (?ub_horner ?invx) ≤ arctan x"
using `0 ≤ arctan x` arctan_inverse[OF `1 / x ≠ 0`]
unfolding real_sgn_pos[OF `0 < 1 / real x`] le_diff_eq by auto
moreover
have "lb_pi prec * Float 1 -1 ≤ pi / 2" unfolding real_of_float_mult Float_num times_divide_eq_right mult_1_left using pi_boundaries by auto
ultimately
show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `¬ x < 0`] if_not_P[OF `¬ x ≤ Float 1 -1`] if_not_P[OF `¬ x ≤ Float 1 1`] if_not_P[OF False]
by auto
qed
qed
qed
qed
lemma ub_arctan_bound': assumes "0 ≤ real x"
shows "arctan x ≤ ub_arctan prec x"
proof -
have "¬ x < 0" and "0 ≤ x" unfolding less_float_def le_float_def using `0 ≤ real x` by auto
let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
show ?thesis
proof (cases "x ≤ Float 1 -1")
case True hence "real x ≤ 1" unfolding le_float_def Float_num by auto
show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `¬ x < 0`] if_P[OF True]
using arctan_0_1_bounds[OF `0 ≤ real x` `real x ≤ 1`] by auto
next
case False hence "0 < real x" unfolding le_float_def Float_num by auto
let ?R = "1 + sqrt (1 + real x * real x)"
let ?fR = "1 + lb_sqrt prec (1 + x * x)"
let ?DIV = "float_divr prec x ?fR"
have sqr_ge0: "0 ≤ 1 + real x * real x" using sum_power2_ge_zero[of 1 "real x", unfolded numeral_2_eq_2] by auto
hence "0 ≤ real (1 + x*x)" by auto
hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
have "lb_sqrt prec (1 + x * x) ≤ sqrt (1 + x * x)"
using bnds_sqrt'[of "1 + x * x"] by auto
hence "?fR ≤ ?R" by auto
have "0 < real ?fR" unfolding real_of_float_add real_of_float_1 by (rule order_less_le_trans[OF zero_less_one], auto simp add: lb_sqrt_lower_bound[OF `0 ≤ real (1 + x*x)`])
have monotone: "x / ?R ≤ (float_divr prec x ?fR)"
proof -
from divide_left_mono[OF `?fR ≤ ?R` `0 ≤ real x` mult_pos_pos[OF divisor_gt0 `0 < real ?fR`]]
have "x / ?R ≤ x / ?fR" .
also have "… ≤ ?DIV" by (rule float_divr)
finally show ?thesis .
qed
show ?thesis
proof (cases "x ≤ Float 1 1")
case True
show ?thesis
proof (cases "?DIV > 1")
case True
have "pi / 2 ≤ ub_pi prec * Float 1 -1" unfolding real_of_float_mult Float_num times_divide_eq_right mult_1_left using pi_boundaries by auto
from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le]
show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `¬ x < 0`] if_not_P[OF `¬ x ≤ Float 1 -1`] if_P[OF `x ≤ Float 1 1`] if_P[OF True] .
next
case False
hence "real ?DIV ≤ 1" unfolding less_float_def by auto
have "0 ≤ x / ?R" using `0 ≤ real x` `0 < ?R` unfolding zero_le_divide_iff by auto
hence "0 ≤ real ?DIV" using monotone by (rule order_trans)
have "arctan x = 2 * arctan (x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left .
also have "… ≤ 2 * arctan (?DIV)"
using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
also have "… ≤ (Float 1 1 * ?ub_horner ?DIV)" unfolding real_of_float_mult[of "Float 1 1"] Float_num
using arctan_0_1_bounds[OF `0 ≤ real ?DIV` `real ?DIV ≤ 1`] by auto
finally show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `¬ x < 0`] if_not_P[OF `¬ x ≤ Float 1 -1`] if_P[OF `x ≤ Float 1 1`] if_not_P[OF False] .
qed
next
case False
hence "2 < real x" unfolding le_float_def Float_num by auto
hence "1 ≤ real x" by auto
hence "0 < real x" by auto
hence "0 < x" unfolding less_float_def by auto
let "?invx" = "float_divl prec 1 x"
have "0 ≤ arctan x" using arctan_monotone'[OF `0 ≤ real x`] using arctan_tan[of 0, unfolded tan_zero] by auto
have "real ?invx ≤ 1" unfolding less_float_def by (rule order_trans[OF float_divl], auto simp add: `1 ≤ real x` divide_le_eq_1_pos[OF `0 < real x`])
have "0 ≤ real ?invx" unfolding real_of_float_0[symmetric] by (rule float_divl_lower_bound[unfolded le_float_def], auto simp add: `0 < x`)
have "1 / x ≠ 0" and "0 < 1 / x" using `0 < real x` by auto
have "(?lb_horner ?invx) ≤ arctan (?invx)" using arctan_0_1_bounds[OF `0 ≤ real ?invx` `real ?invx ≤ 1`] by auto
also have "… ≤ arctan (1 / x)" unfolding real_of_float_1[symmetric] by (rule arctan_monotone', rule float_divl)
finally have "arctan x ≤ pi / 2 - (?lb_horner ?invx)"
using `0 ≤ arctan x` arctan_inverse[OF `1 / x ≠ 0`]
unfolding real_sgn_pos[OF `0 < 1 / x`] le_diff_eq by auto
moreover
have "pi / 2 ≤ ub_pi prec * Float 1 -1" unfolding real_of_float_mult Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto
ultimately
show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `¬ x < 0`] if_not_P[OF `¬ x ≤ Float 1 -1`] if_not_P[OF `¬ x ≤ Float 1 1`] if_not_P[OF False]
by auto
qed
qed
qed
lemma arctan_boundaries:
"arctan x ∈ {(lb_arctan prec x) .. (ub_arctan prec x)}"
proof (cases "0 ≤ x")
case True hence "0 ≤ real x" unfolding le_float_def by auto
show ?thesis using ub_arctan_bound'[OF `0 ≤ real x`] lb_arctan_bound'[OF `0 ≤ real x`] unfolding atLeastAtMost_iff by auto
next
let ?mx = "-x"
case False hence "x < 0" and "0 ≤ real ?mx" unfolding le_float_def less_float_def by auto
hence bounds: "lb_arctan prec ?mx ≤ arctan ?mx ∧ arctan ?mx ≤ ub_arctan prec ?mx"
using ub_arctan_bound'[OF `0 ≤ real ?mx`] lb_arctan_bound'[OF `0 ≤ real ?mx`] by auto
show ?thesis unfolding real_of_float_minus arctan_minus lb_arctan.simps[where x=x] ub_arctan.simps[where x=x] Let_def if_P[OF `x < 0`]
unfolding atLeastAtMost_iff using bounds[unfolded real_of_float_minus arctan_minus] by auto
qed
lemma bnds_arctan: "∀ (x::real) lx ux. (l, u) = (lb_arctan prec lx, ub_arctan prec ux) ∧ x ∈ {lx .. ux} --> l ≤ arctan x ∧ arctan x ≤ u"
proof (rule allI, rule allI, rule allI, rule impI)
fix x :: real fix lx ux
assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) ∧ x ∈ {lx .. ux}"
hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x ∈ {lx .. ux}" by auto
{ from arctan_boundaries[of lx prec, unfolded l]
have "l ≤ arctan lx" by (auto simp del: lb_arctan.simps)
also have "… ≤ arctan x" using x by (auto intro: arctan_monotone')
finally have "l ≤ arctan x" .
} moreover
{ have "arctan x ≤ arctan ux" using x by (auto intro: arctan_monotone')
also have "… ≤ u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps)
finally have "arctan x ≤ u" .
} ultimately show "l ≤ arctan x ∧ arctan x ≤ u" ..
qed
section "Sinus and Cosinus"
subsection "Compute the cosinus and sinus series"
fun ub_sin_cos_aux :: "nat => nat => nat => nat => float => float"
and lb_sin_cos_aux :: "nat => nat => nat => nat => float => float" where
"ub_sin_cos_aux prec 0 i k x = 0"
| "ub_sin_cos_aux prec (Suc n) i k x =
(rapprox_rat prec 1 k) - x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)"
| "lb_sin_cos_aux prec 0 i k x = 0"
| "lb_sin_cos_aux prec (Suc n) i k x =
(lapprox_rat prec 1 k) - x * (ub_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)"
lemma cos_aux:
shows "(lb_sin_cos_aux prec n 1 1 (x * x)) ≤ (∑ i=0..<n. -1^i * (1/real (fact (2 * i))) * x ^(2 * i))" (is "?lb")
and "(∑ i=0..<n. -1^i * (1/real (fact (2 * i))) * x^(2 * i)) ≤ (ub_sin_cos_aux prec n 1 1 (x * x))" (is "?ub")
proof -
have "0 ≤ real (x * x)" unfolding real_of_float_mult by auto
let "?f n" = "fact (2 * n)"
{ fix n
have F: "!!m. ((λi. i + 2) ^^ n) m = m + 2 * n" by (induct n arbitrary: m, auto)
have "?f (Suc n) = ?f n * ((λi. i + 2) ^^ n) 1 * (((λi. i + 2) ^^ n) 1 + 1)"
unfolding F by auto } note f_eq = this
from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
OF `0 ≤ real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "real x"])
qed
lemma cos_boundaries: assumes "0 ≤ real x" and "x ≤ pi / 2"
shows "cos x ∈ {(lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) .. (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))}"
proof (cases "real x = 0")
case False hence "real x ≠ 0" by auto
hence "0 < x" and "0 < real x" using `0 ≤ real x` unfolding less_float_def by auto
have "0 < x * x" using `0 < x` unfolding less_float_def real_of_float_mult real_of_float_0
using mult_pos_pos[where a="real x" and b="real x"] by auto
{ fix x n have "(∑ i=0..<n. -1^i * (1/real (fact (2 * i))) * x ^ (2 * i))
= (∑ i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * x ^ i)" (is "?sum = ?ifsum")
proof -
have "?sum = ?sum + (∑ j = 0 ..< n. 0)" by auto
also have "… =
(∑ j = 0 ..< n. -1 ^ ((2 * j) div 2) / (real (fact (2 * j))) * x ^(2 * j)) + (∑ j = 0 ..< n. 0)" by auto
also have "… = (∑ i = 0 ..< 2 * n. if even i then -1 ^ (i div 2) / (real (fact i)) * x ^ i else 0)"
unfolding sum_split_even_odd ..
also have "… = (∑ i = 0 ..< 2 * n. (if even i then -1 ^ (i div 2) / (real (fact i)) else 0) * x ^ i)"
by (rule setsum_cong2) auto
finally show ?thesis by assumption
qed } note morph_to_if_power = this
{ fix n :: nat assume "0 < n"
hence "0 < 2 * n" by auto
obtain t where "0 < t" and "t < real x" and
cos_eq: "cos x = (∑ i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * (real x) ^ i)
+ (cos (t + 1/2 * (2 * n) * pi) / real (fact (2*n))) * (real x)^(2*n)"
(is "_ = ?SUM + ?rest / ?fact * ?pow")
using Maclaurin_cos_expansion2[OF `0 < real x` `0 < 2 * n`]
unfolding cos_coeff_def by auto
have "cos t * -1^n = cos t * cos (n * pi) + sin t * sin (n * pi)" by auto
also have "… = cos (t + n * pi)" using cos_add by auto
also have "… = ?rest" by auto
finally have "cos t * -1^n = ?rest" .
moreover
have "t ≤ pi / 2" using `t < real x` and `x ≤ pi / 2` by auto
hence "0 ≤ cos t" using `0 < t` and cos_ge_zero by auto
ultimately have even: "even n ==> 0 ≤ ?rest" and odd: "odd n ==> 0 ≤ - ?rest " by auto
have "0 < ?fact" by auto
have "0 < ?pow" using `0 < real x` by auto
{
assume "even n"
have "(lb_sin_cos_aux prec n 1 1 (x * x)) ≤ ?SUM"
unfolding morph_to_if_power[symmetric] using cos_aux by auto
also have "… ≤ cos x"
proof -
from even[OF `even n`] `0 < ?fact` `0 < ?pow`
have "0 ≤ (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
thus ?thesis unfolding cos_eq by auto
qed
finally have "(lb_sin_cos_aux prec n 1 1 (x * x)) ≤ cos x" .
} note lb = this
{
assume "odd n"
have "cos x ≤ ?SUM"
proof -
from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
have "0 ≤ (- ?rest) / ?fact * ?pow"
by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
thus ?thesis unfolding cos_eq by auto
qed
also have "… ≤ (ub_sin_cos_aux prec n 1 1 (x * x))"
unfolding morph_to_if_power[symmetric] using cos_aux by auto
finally have "cos x ≤ (ub_sin_cos_aux prec n 1 1 (x * x))" .
} note ub = this and lb
} note ub = this(1) and lb = this(2)
have "cos x ≤ (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] .
moreover have "(lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) ≤ cos x"
proof (cases "0 < get_even n")
case True show ?thesis using lb[OF True get_even] .
next
case False
hence "get_even n = 0" by auto
have "- (pi / 2) ≤ x" by (rule order_trans[OF _ `0 < real x`[THEN less_imp_le]], auto)
with `x ≤ pi / 2`
show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps real_of_float_minus real_of_float_0 using cos_ge_zero by auto
qed
ultimately show ?thesis by auto
next
case True
show ?thesis
proof (cases "n = 0")
case True
thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `real x = 0` lapprox_rat[where x="-1" and y=1] by auto
next
case False with not0_implies_Suc obtain m where "n = Suc m" by blast
thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `real x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto)
qed
qed
lemma sin_aux: assumes "0 ≤ real x"
shows "(x * lb_sin_cos_aux prec n 2 1 (x * x)) ≤ (∑ i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * x^(2 * i + 1))" (is "?lb")
and "(∑ i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * x^(2 * i + 1)) ≤ (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub")
proof -
have "0 ≤ real (x * x)" unfolding real_of_float_mult by auto
let "?f n" = "fact (2 * n + 1)"
{ fix n
have F: "!!m. ((λi. i + 2) ^^ n) m = m + 2 * n" by (induct n arbitrary: m, auto)
have "?f (Suc n) = ?f n * ((λi. i + 2) ^^ n) 2 * (((λi. i + 2) ^^ n) 2 + 1)"
unfolding F by auto } note f_eq = this
from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
OF `0 ≤ real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
show "?lb" and "?ub" using `0 ≤ real x` unfolding real_of_float_mult
unfolding power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric]
unfolding mult_commute[where 'a=real]
by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "real x"])
qed
lemma sin_boundaries: assumes "0 ≤ real x" and "x ≤ pi / 2"
shows "sin x ∈ {(x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) .. (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))}"
proof (cases "real x = 0")
case False hence "real x ≠ 0" by auto
hence "0 < x" and "0 < real x" using `0 ≤ real x` unfolding less_float_def by auto
have "0 < x * x" using `0 < x` unfolding less_float_def real_of_float_mult real_of_float_0
using mult_pos_pos[where a="real x" and b="real x"] by auto
{ fix x n have "(∑ j = 0 ..< n. -1 ^ (((2 * j + 1) - Suc 0) div 2) / (real (fact (2 * j + 1))) * x ^(2 * j + 1))
= (∑ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * x ^ i)" (is "?SUM = _")
proof -
have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto
have "?SUM = (∑ j = 0 ..< n. 0) + ?SUM" by auto
also have "… = (∑ i = 0 ..< 2 * n. if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i)) * x ^ i)"
unfolding sum_split_even_odd ..
also have "… = (∑ i = 0 ..< 2 * n. (if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i))) * x ^ i)"
by (rule setsum_cong2) auto
finally show ?thesis by assumption
qed } note setsum_morph = this
{ fix n :: nat assume "0 < n"
hence "0 < 2 * n + 1" by auto
obtain t where "0 < t" and "t < real x" and
sin_eq: "sin x = (∑ i = 0 ..< 2 * n + 1. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i)
+ (sin (t + 1/2 * (2 * n + 1) * pi) / real (fact (2*n + 1))) * (real x)^(2*n + 1)"
(is "_ = ?SUM + ?rest / ?fact * ?pow")
using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < real x`]
unfolding sin_coeff_def by auto
have "?rest = cos t * -1^n" unfolding sin_add cos_add real_of_nat_add left_distrib right_distrib by auto
moreover
have "t ≤ pi / 2" using `t < real x` and `x ≤ pi / 2` by auto
hence "0 ≤ cos t" using `0 < t` and cos_ge_zero by auto
ultimately have even: "even n ==> 0 ≤ ?rest" and odd: "odd n ==> 0 ≤ - ?rest " by auto
have "0 < ?fact" by (simp del: fact_Suc)
have "0 < ?pow" using `0 < real x` by (rule zero_less_power)
{
assume "even n"
have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) ≤
(∑ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i)"
using sin_aux[OF `0 ≤ real x`] unfolding setsum_morph[symmetric] by auto
also have "… ≤ ?SUM" by auto
also have "… ≤ sin x"
proof -
from even[OF `even n`] `0 < ?fact` `0 < ?pow`
have "0 ≤ (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
thus ?thesis unfolding sin_eq by auto
qed
finally have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) ≤ sin x" .
} note lb = this
{
assume "odd n"
have "sin x ≤ ?SUM"
proof -
from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
have "0 ≤ (- ?rest) / ?fact * ?pow"
by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
thus ?thesis unfolding sin_eq by auto
qed
also have "… ≤ (∑ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i)"
by auto
also have "… ≤ (x * ub_sin_cos_aux prec n 2 1 (x * x))"
using sin_aux[OF `0 ≤ real x`] unfolding setsum_morph[symmetric] by auto
finally have "sin x ≤ (x * ub_sin_cos_aux prec n 2 1 (x * x))" .
} note ub = this and lb
} note ub = this(1) and lb = this(2)
have "sin x ≤ (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] .
moreover have "(x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) ≤ sin x"
proof (cases "0 < get_even n")
case True show ?thesis using lb[OF True get_even] .
next
case False
hence "get_even n = 0" by auto
with `x ≤ pi / 2` `0 ≤ real x`
show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps real_of_float_minus real_of_float_0 using sin_ge_zero by auto
qed
ultimately show ?thesis by auto
next
case True
show ?thesis
proof (cases "n = 0")
case True
thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `real x = 0` lapprox_rat[where x="-1" and y=1] by auto
next
case False with not0_implies_Suc obtain m where "n = Suc m" by blast
thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `real x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto)
qed
qed
subsection "Compute the cosinus in the entire domain"
definition lb_cos :: "nat => float => float" where
"lb_cos prec x = (let
horner = λ x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ;
half = λ x. if x < 0 then - 1 else Float 1 1 * x * x - 1
in if x < Float 1 -1 then horner x
else if x < 1 then half (horner (x * Float 1 -1))
else half (half (horner (x * Float 1 -2))))"
definition ub_cos :: "nat => float => float" where
"ub_cos prec x = (let
horner = λ x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ;
half = λ x. Float 1 1 * x * x - 1
in if x < Float 1 -1 then horner x
else if x < 1 then half (horner (x * Float 1 -1))
else half (half (horner (x * Float 1 -2))))"
lemma lb_cos: assumes "0 ≤ real x" and "x ≤ pi"
shows "cos x ∈ {(lb_cos prec x) .. (ub_cos prec x)}" (is "?cos x ∈ {(?lb x) .. (?ub x) }")
proof -
{ fix x :: real
have "cos x = cos (x / 2 + x / 2)" by auto
also have "… = cos (x / 2) * cos (x / 2) + sin (x / 2) * sin (x / 2) - sin (x / 2) * sin (x / 2) + cos (x / 2) * cos (x / 2) - 1"
unfolding cos_add by auto
also have "… = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra
finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" .
} note x_half = this[symmetric]
have "¬ x < 0" using `0 ≤ real x` unfolding less_float_def by auto
let "?ub_horner x" = "ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x)"
let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)"
let "?ub_half x" = "Float 1 1 * x * x - 1"
let "?lb_half x" = "if x < 0 then - 1 else Float 1 1 * x * x - 1"
show ?thesis
proof (cases "x < Float 1 -1")
case True hence "x ≤ pi / 2" unfolding less_float_def using pi_ge_two by auto
show ?thesis unfolding lb_cos_def[where x=x] ub_cos_def[where x=x] if_not_P[OF `¬ x < 0`] if_P[OF `x < Float 1 -1`] Let_def
using cos_boundaries[OF `0 ≤ real x` `x ≤ pi / 2`] .
next
case False
{ fix y x :: float let ?x2 = "(x * Float 1 -1)"
assume "y ≤ cos ?x2" and "-pi ≤ x" and "x ≤ pi"
hence "- (pi / 2) ≤ ?x2" and "?x2 ≤ pi / 2" using pi_ge_two unfolding real_of_float_mult Float_num by auto
hence "0 ≤ cos ?x2" by (rule cos_ge_zero)
have "(?lb_half y) ≤ cos x"
proof (cases "y < 0")
case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto
next
case False
hence "0 ≤ real y" unfolding less_float_def by auto
from mult_mono[OF `y ≤ cos ?x2` `y ≤ cos ?x2` `0 ≤ cos ?x2` this]
have "real y * real y ≤ cos ?x2 * cos ?x2" .
hence "2 * real y * real y ≤ 2 * cos ?x2 * cos ?x2" by auto
hence "2 * real y * real y - 1 ≤ 2 * cos (x / 2) * cos (x / 2) - 1" unfolding Float_num real_of_float_mult by auto
thus ?thesis unfolding if_not_P[OF False] x_half Float_num real_of_float_mult real_of_float_sub by auto
qed
} note lb_half = this
{ fix y x :: float let ?x2 = "(x * Float 1 -1)"
assume ub: "cos ?x2 ≤ y" and "- pi ≤ x" and "x ≤ pi"
hence "- (pi / 2) ≤ ?x2" and "?x2 ≤ pi / 2" using pi_ge_two unfolding real_of_float_mult Float_num by auto
hence "0 ≤ cos ?x2" by (rule cos_ge_zero)
have "cos x ≤ (?ub_half y)"
proof -
have "0 ≤ real y" using `0 ≤ cos ?x2` ub by (rule order_trans)
from mult_mono[OF ub ub this `0 ≤ cos ?x2`]
have "cos ?x2 * cos ?x2 ≤ real y * real y" .
hence "2 * cos ?x2 * cos ?x2 ≤ 2 * real y * real y" by auto
hence "2 * cos (x / 2) * cos (x / 2) - 1 ≤ 2 * real y * real y - 1" unfolding Float_num real_of_float_mult by auto
thus ?thesis unfolding x_half real_of_float_mult Float_num real_of_float_sub by auto
qed
} note ub_half = this
let ?x2 = "x * Float 1 -1"
let ?x4 = "x * Float 1 -1 * Float 1 -1"
have "-pi ≤ x" using pi_ge_zero[THEN le_imp_neg_le, unfolded minus_zero] `0 ≤ real x` by (rule order_trans)
show ?thesis
proof (cases "x < 1")
case True hence "real x ≤ 1" unfolding less_float_def by auto
have "0 ≤ real ?x2" and "?x2 ≤ pi / 2" using pi_ge_two `0 ≤ real x` unfolding real_of_float_mult Float_num using assms by auto
from cos_boundaries[OF this]
have lb: "(?lb_horner ?x2) ≤ ?cos ?x2" and ub: "?cos ?x2 ≤ (?ub_horner ?x2)" by auto
have "(?lb x) ≤ ?cos x"
proof -
from lb_half[OF lb `-pi ≤ x` `x ≤ pi`]
show ?thesis unfolding lb_cos_def[where x=x] Let_def using `¬ x < 0` `¬ x < Float 1 -1` `x < 1` by auto
qed
moreover have "?cos x ≤ (?ub x)"
proof -
from ub_half[OF ub `-pi ≤ x` `x ≤ pi`]
show ?thesis unfolding ub_cos_def[where x=x] Let_def using `¬ x < 0` `¬ x < Float 1 -1` `x < 1` by auto
qed
ultimately show ?thesis by auto
next
case False
have "0 ≤ real ?x4" and "?x4 ≤ pi / 2" using pi_ge_two `0 ≤ real x` `x ≤ pi` unfolding real_of_float_mult Float_num by auto
from cos_boundaries[OF this]
have lb: "(?lb_horner ?x4) ≤ ?cos ?x4" and ub: "?cos ?x4 ≤ (?ub_horner ?x4)" by auto
have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by (cases x, auto simp add: times_float.simps)
have "(?lb x) ≤ ?cos x"
proof -
have "-pi ≤ ?x2" and "?x2 ≤ pi" unfolding real_of_float_mult Float_num using pi_ge_two `0 ≤ real x` `x ≤ pi` by auto
from lb_half[OF lb_half[OF lb this] `-pi ≤ x` `x ≤ pi`, unfolded eq_4]
show ?thesis unfolding lb_cos_def[where x=x] if_not_P[OF `¬ x < 0`] if_not_P[OF `¬ x < Float 1 -1`] if_not_P[OF `¬ x < 1`] Let_def .
qed
moreover have "?cos x ≤ (?ub x)"
proof -
have "-pi ≤ ?x2" and "?x2 ≤ pi" unfolding real_of_float_mult Float_num using pi_ge_two `0 ≤ real x` ` x ≤ pi` by auto
from ub_half[OF ub_half[OF ub this] `-pi ≤ x` `x ≤ pi`, unfolded eq_4]
show ?thesis unfolding ub_cos_def[where x=x] if_not_P[OF `¬ x < 0`] if_not_P[OF `¬ x < Float 1 -1`] if_not_P[OF `¬ x < 1`] Let_def .
qed
ultimately show ?thesis by auto
qed
qed
qed
lemma lb_cos_minus: assumes "-pi ≤ x" and "real x ≤ 0"
shows "cos (real(-x)) ∈ {(lb_cos prec (-x)) .. (ub_cos prec (-x))}"
proof -
have "0 ≤ real (-x)" and "(-x) ≤ pi" using `-pi ≤ x` `real x ≤ 0` by auto
from lb_cos[OF this] show ?thesis .
qed
definition bnds_cos :: "nat => float => float => float * float" where
"bnds_cos prec lx ux = (let
lpi = round_down prec (lb_pi prec) ;
upi = round_up prec (ub_pi prec) ;
k = floor_fl (float_divr prec (lx + lpi) (2 * lpi)) ;
lx = lx - k * 2 * (if k < 0 then lpi else upi) ;
ux = ux - k * 2 * (if k < 0 then upi else lpi)
in if - lpi ≤ lx ∧ ux ≤ 0 then (lb_cos prec (-lx), ub_cos prec (-ux))
else if 0 ≤ lx ∧ ux ≤ lpi then (lb_cos prec ux, ub_cos prec lx)
else if - lpi ≤ lx ∧ ux ≤ lpi then (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0)
else if 0 ≤ lx ∧ ux ≤ 2 * lpi then (Float -1 0, max (ub_cos prec lx) (ub_cos prec (- (ux - 2 * lpi))))
else if -2 * lpi ≤ lx ∧ ux ≤ 0 then (Float -1 0, max (ub_cos prec (lx + 2 * lpi)) (ub_cos prec (-ux)))
else (Float -1 0, Float 1 0))"
lemma floor_int:
obtains k :: int where "real k = (floor_fl f)"
proof -
assume *: "!! k :: int. real k = (floor_fl f) ==> thesis"
obtain m e where fl: "Float m e = floor_fl f" by (cases "floor_fl f", auto)
from floor_pos_exp[OF this]
have "real (m* 2^(nat e)) = (floor_fl f)"
by (auto simp add: fl[symmetric] real_of_float_def pow2_def)
from *[OF this] show thesis by blast
qed
lemma float_remove_real_numeral[simp]: "(number_of k :: float) = (number_of k :: real)"
proof -
have "(number_of k :: float) = real k"
unfolding number_of_float_def real_of_float_def pow2_def by auto
also have "… = (number_of k :: int)"
by (simp add: number_of_is_id)
finally show ?thesis by auto
qed
lemma cos_periodic_nat[simp]: fixes n :: nat shows "cos (x + n * (2 * pi)) = cos x"
proof (induct n arbitrary: x)
case (Suc n)
have split_pi_off: "x + (Suc n) * (2 * pi) = (x + n * (2 * pi)) + 2 * pi"
unfolding Suc_eq_plus1 real_of_nat_add real_of_one left_distrib by auto
show ?case unfolding split_pi_off using Suc by auto
qed auto
lemma cos_periodic_int[simp]: fixes i :: int shows "cos (x + i * (2 * pi)) = cos x"
proof (cases "0 ≤ i")
case True hence i_nat: "real i = nat i" by auto
show ?thesis unfolding i_nat by auto
next
case False hence i_nat: "i = - real (nat (-i))" by auto
have "cos x = cos (x + i * (2 * pi) - i * (2 * pi))" by auto
also have "… = cos (x + i * (2 * pi))"
unfolding i_nat mult_minus_left diff_minus_eq_add by (rule cos_periodic_nat)
finally show ?thesis by auto
qed
lemma bnds_cos: "∀ (x::real) lx ux. (l, u) = bnds_cos prec lx ux ∧ x ∈ {lx .. ux} --> l ≤ cos x ∧ cos x ≤ u"
proof ((rule allI | rule impI | erule conjE) +)
fix x :: real fix lx ux
assume bnds: "(l, u) = bnds_cos prec lx ux" and x: "x ∈ {lx .. ux}"
let ?lpi = "round_down prec (lb_pi prec)"
let ?upi = "round_up prec (ub_pi prec)"
let ?k = "floor_fl (float_divr prec (lx + ?lpi) (2 * ?lpi))"
let ?lx = "lx - ?k * 2 * (if ?k < 0 then ?lpi else ?upi)"
let ?ux = "ux - ?k * 2 * (if ?k < 0 then ?upi else ?lpi)"
obtain k :: int where k: "k = real ?k" using floor_int .
have upi: "pi ≤ ?upi" and lpi: "?lpi ≤ pi"
using round_up[of "ub_pi prec" prec] pi_boundaries[of prec]
round_down[of prec "lb_pi prec"] by auto
hence "?lx ≤ x - k * (2 * pi) ∧ x - k * (2 * pi) ≤ ?ux"
using x by (cases "k = 0") (auto intro!: add_mono
simp add: diff_minus k[symmetric] less_float_def)
note lx = this[THEN conjunct1] and ux = this[THEN conjunct2]
hence lx_less_ux: "?lx ≤ real ?ux" by (rule order_trans)
{ assume "- ?lpi ≤ ?lx" and x_le_0: "x - k * (2 * pi) ≤ 0"
with lpi[THEN le_imp_neg_le] lx
have pi_lx: "- pi ≤ ?lx" and lx_0: "real ?lx ≤ 0"
by (simp_all add: le_float_def)
have "(lb_cos prec (- ?lx)) ≤ cos (real (- ?lx))"
using lb_cos_minus[OF pi_lx lx_0] by simp
also have "… ≤ cos (x + (-k) * (2 * pi))"
using cos_monotone_minus_pi_0'[OF pi_lx lx x_le_0]
by (simp only: real_of_float_minus real_of_int_minus
cos_minus diff_minus mult_minus_left)
finally have "(lb_cos prec (- ?lx)) ≤ cos x"
unfolding cos_periodic_int . }
note negative_lx = this
{ assume "0 ≤ ?lx" and pi_x: "x - k * (2 * pi) ≤ pi"
with lx
have pi_lx: "?lx ≤ pi" and lx_0: "0 ≤ real ?lx"
by (auto simp add: le_float_def)
have "cos (x + (-k) * (2 * pi)) ≤ cos ?lx"
using cos_monotone_0_pi'[OF lx_0 lx pi_x]
by (simp only: real_of_float_minus real_of_int_minus
cos_minus diff_minus mult_minus_left)
also have "… ≤ (ub_cos prec ?lx)"
using lb_cos[OF lx_0 pi_lx] by simp
finally have "cos x ≤ (ub_cos prec ?lx)"
unfolding cos_periodic_int . }
note positive_lx = this
{ assume pi_x: "- pi ≤ x - k * (2 * pi)" and "?ux ≤ 0"
with ux
have pi_ux: "- pi ≤ ?ux" and ux_0: "real ?ux ≤ 0"
by (simp_all add: le_float_def)
have "cos (x + (-k) * (2 * pi)) ≤ cos (real (- ?ux))"
using cos_monotone_minus_pi_0'[OF pi_x ux ux_0]
by (simp only: real_of_float_minus real_of_int_minus
cos_minus diff_minus mult_minus_left)
also have "… ≤ (ub_cos prec (- ?ux))"
using lb_cos_minus[OF pi_ux ux_0, of prec] by simp
finally have "cos x ≤ (ub_cos prec (- ?ux))"
unfolding cos_periodic_int . }
note negative_ux = this
{ assume "?ux ≤ ?lpi" and x_ge_0: "0 ≤ x - k * (2 * pi)"
with lpi ux
have pi_ux: "?ux ≤ pi" and ux_0: "0 ≤ real ?ux"
by (simp_all add: le_float_def)
have "(lb_cos prec ?ux) ≤ cos ?ux"
using lb_cos[OF ux_0 pi_ux] by simp
also have "… ≤ cos (x + (-k) * (2 * pi))"
using cos_monotone_0_pi'[OF x_ge_0 ux pi_ux]
by (simp only: real_of_float_minus real_of_int_minus
cos_minus diff_minus mult_minus_left)
finally have "(lb_cos prec ?ux) ≤ cos x"
unfolding cos_periodic_int . }
note positive_ux = this
show "l ≤ cos x ∧ cos x ≤ u"
proof (cases "- ?lpi ≤ ?lx ∧ ?ux ≤ 0")
case True with bnds
have l: "l = lb_cos prec (-?lx)"
and u: "u = ub_cos prec (-?ux)"
by (auto simp add: bnds_cos_def Let_def)
from True lpi[THEN le_imp_neg_le] lx ux
have "- pi ≤ x - k * (2 * pi)"
and "x - k * (2 * pi) ≤ 0"
by (auto simp add: le_float_def)
with True negative_ux negative_lx
show ?thesis unfolding l u by simp
next case False note 1 = this show ?thesis
proof (cases "0 ≤ ?lx ∧ ?ux ≤ ?lpi")
case True with bnds 1
have l: "l = lb_cos prec ?ux"
and u: "u = ub_cos prec ?lx"
by (auto simp add: bnds_cos_def Let_def)
from True lpi lx ux
have "0 ≤ x - k * (2 * pi)"
and "x - k * (2 * pi) ≤ pi"
by (auto simp add: le_float_def)
with True positive_ux positive_lx
show ?thesis unfolding l u by simp
next case False note 2 = this show ?thesis
proof (cases "- ?lpi ≤ ?lx ∧ ?ux ≤ ?lpi")
case True note Cond = this with bnds 1 2
have l: "l = min (lb_cos prec (-?lx)) (lb_cos prec ?ux)"
and u: "u = Float 1 0"
by (auto simp add: bnds_cos_def Let_def)
show ?thesis unfolding u l using negative_lx positive_ux Cond
by (cases "x - k * (2 * pi) < 0", simp_all add: real_of_float_min)
next case False note 3 = this show ?thesis
proof (cases "0 ≤ ?lx ∧ ?ux ≤ 2 * ?lpi")
case True note Cond = this with bnds 1 2 3
have l: "l = Float -1 0"
and u: "u = max (ub_cos prec ?lx) (ub_cos prec (- (?ux - 2 * ?lpi)))"
by (auto simp add: bnds_cos_def Let_def)
have "cos x ≤ real u"
proof (cases "x - k * (2 * pi) < pi")
case True hence "x - k * (2 * pi) ≤ pi" by simp
from positive_lx[OF Cond[THEN conjunct1] this]
show ?thesis unfolding u by (simp add: real_of_float_max)
next
case False hence "pi ≤ x - k * (2 * pi)" by simp
hence pi_x: "- pi ≤ x - k * (2 * pi) - 2 * pi" by simp
have "?ux ≤ 2 * pi" using Cond lpi by (auto simp add: le_float_def)
hence "x - k * (2 * pi) - 2 * pi ≤ 0" using ux by simp
have ux_0: "real (?ux - 2 * ?lpi) ≤ 0"
using Cond by (auto simp add: le_float_def)
from 2 and Cond have "¬ ?ux ≤ ?lpi" by auto
hence "- ?lpi ≤ ?ux - 2 * ?lpi" by (auto simp add: le_float_def)
hence pi_ux: "- pi ≤ (?ux - 2 * ?lpi)"
using lpi[THEN le_imp_neg_le] by (auto simp add: le_float_def)
have x_le_ux: "x - k * (2 * pi) - 2 * pi ≤ (?ux - 2 * ?lpi)"
using ux lpi by auto
have "cos x = cos (x + (-k) * (2 * pi) + (-1::int) * (2 * pi))"
unfolding cos_periodic_int ..
also have "… ≤ cos ((?ux - 2 * ?lpi))"
using cos_monotone_minus_pi_0'[OF pi_x x_le_ux ux_0]
by (simp only: real_of_float_minus real_of_int_minus real_of_one
number_of_Min diff_minus mult_minus_left mult_1_left)
also have "… = cos ((- (?ux - 2 * ?lpi)))"
unfolding real_of_float_minus cos_minus ..
also have "… ≤ (ub_cos prec (- (?ux - 2 * ?lpi)))"
using lb_cos_minus[OF pi_ux ux_0] by simp
finally show ?thesis unfolding u by (simp add: real_of_float_max)
qed
thus ?thesis unfolding l by auto
next case False note 4 = this show ?thesis
proof (cases "-2 * ?lpi ≤ ?lx ∧ ?ux ≤ 0")
case True note Cond = this with bnds 1 2 3 4
have l: "l = Float -1 0"
and u: "u = max (ub_cos prec (?lx + 2 * ?lpi)) (ub_cos prec (-?ux))"
by (auto simp add: bnds_cos_def Let_def)
have "cos x ≤ u"
proof (cases "-pi < x - k * (2 * pi)")
case True hence "-pi ≤ x - k * (2 * pi)" by simp
from negative_ux[OF this Cond[THEN conjunct2]]
show ?thesis unfolding u by (simp add: real_of_float_max)
next
case False hence "x - k * (2 * pi) ≤ -pi" by simp
hence pi_x: "x - k * (2 * pi) + 2 * pi ≤ pi" by simp
have "-2 * pi ≤ ?lx" using Cond lpi by (auto simp add: le_float_def)
hence "0 ≤ x - k * (2 * pi) + 2 * pi" using lx by simp
have lx_0: "0 ≤ real (?lx + 2 * ?lpi)"
using Cond lpi by (auto simp add: le_float_def)
from 1 and Cond have "¬ -?lpi ≤ ?lx" by auto
hence "?lx + 2 * ?lpi ≤ ?lpi" by (auto simp add: le_float_def)
hence pi_lx: "(?lx + 2 * ?lpi) ≤ pi"
using lpi[THEN le_imp_neg_le] by (auto simp add: le_float_def)
have lx_le_x: "(?lx + 2 * ?lpi) ≤ x - k * (2 * pi) + 2 * pi"
using lx lpi by auto
have "cos x = cos (x + (-k) * (2 * pi) + (1 :: int) * (2 * pi))"
unfolding cos_periodic_int ..
also have "… ≤ cos ((?lx + 2 * ?lpi))"
using cos_monotone_0_pi'[OF lx_0 lx_le_x pi_x]
by (simp only: real_of_float_minus real_of_int_minus real_of_one
number_of_Min diff_minus mult_minus_left mult_1_left)
also have "… ≤ (ub_cos prec (?lx + 2 * ?lpi))"
using lb_cos[OF lx_0 pi_lx] by simp
finally show ?thesis unfolding u by (simp add: real_of_float_max)
qed
thus ?thesis unfolding l by auto
next
case False with bnds 1 2 3 4 show ?thesis by (auto simp add: bnds_cos_def Let_def)
qed qed qed qed qed
qed
section "Exponential function"
subsection "Compute the series of the exponential function"
fun ub_exp_horner :: "nat => nat => nat => nat => float => float" and lb_exp_horner :: "nat => nat => nat => nat => float => float" where
"ub_exp_horner prec 0 i k x = 0" |
"ub_exp_horner prec (Suc n) i k x = rapprox_rat prec 1 (int k) + x * lb_exp_horner prec n (i + 1) (k * i) x" |
"lb_exp_horner prec 0 i k x = 0" |
"lb_exp_horner prec (Suc n) i k x = lapprox_rat prec 1 (int k) + x * ub_exp_horner prec n (i + 1) (k * i) x"
lemma bnds_exp_horner: assumes "real x ≤ 0"
shows "exp x ∈ { lb_exp_horner prec (get_even n) 1 1 x .. ub_exp_horner prec (get_odd n) 1 1 x }"
proof -
{ fix n
have F: "!! m. ((λi. i + 1) ^^ n) m = n + m" by (induct n, auto)
have "fact (Suc n) = fact n * ((λi. i + 1) ^^ n) 1" unfolding F by auto } note f_eq = this
note bounds = horner_bounds_nonpos[where f="fact" and lb="lb_exp_horner prec" and ub="ub_exp_horner prec" and j'=0 and s=1,
OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps]
{ have "lb_exp_horner prec (get_even n) 1 1 x ≤ (∑j = 0..<get_even n. 1 / real (fact j) * real x ^ j)"
using bounds(1) by auto
also have "… ≤ exp x"
proof -
obtain t where "¦t¦ ≤ ¦real x¦" and "exp x = (∑m = 0..<get_even n. real x ^ m / real (fact m)) + exp t / real (fact (get_even n)) * (real x) ^ (get_even n)"
using Maclaurin_exp_le by blast
moreover have "0 ≤ exp t / real (fact (get_even n)) * (real x) ^ (get_even n)"
by (auto intro!: mult_nonneg_nonneg divide_nonneg_pos simp add: get_even zero_le_even_power exp_gt_zero)
ultimately show ?thesis
using get_odd exp_gt_zero by (auto intro!: mult_nonneg_nonneg)
qed
finally have "lb_exp_horner prec (get_even n) 1 1 x ≤ exp x" .
} moreover
{
have x_less_zero: "real x ^ get_odd n ≤ 0"
proof (cases "real x = 0")
case True
have "(get_odd n) ≠ 0" using get_odd[THEN odd_pos] by auto
thus ?thesis unfolding True power_0_left by auto
next
case False hence "real x < 0" using `real x ≤ 0` by auto
show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq get_odd `real x < 0`)
qed
obtain t where "¦t¦ ≤ ¦real x¦" and "exp x = (∑m = 0..<get_odd n. (real x) ^ m / real (fact m)) + exp t / real (fact (get_odd n)) * (real x) ^ (get_odd n)"
using Maclaurin_exp_le by blast
moreover have "exp t / real (fact (get_odd n)) * (real x) ^ (get_odd n) ≤ 0"
by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero exp_gt_zero)
ultimately have "exp x ≤ (∑j = 0..<get_odd n. 1 / real (fact j) * real x ^ j)"
using get_odd exp_gt_zero by (auto intro!: mult_nonneg_nonneg)
also have "… ≤ ub_exp_horner prec (get_odd n) 1 1 x"
using bounds(2) by auto
finally have "exp x ≤ ub_exp_horner prec (get_odd n) 1 1 x" .
} ultimately show ?thesis by auto
qed
subsection "Compute the exponential function on the entire domain"
function ub_exp :: "nat => float => float" and lb_exp :: "nat => float => float" where
"lb_exp prec x = (if 0 < x then float_divl prec 1 (ub_exp prec (-x))
else let
horner = (λ x. let y = lb_exp_horner prec (get_even (prec + 2)) 1 1 x in if y ≤ 0 then Float 1 -2 else y)
in if x < - 1 then (case floor_fl x of (Float m e) => (horner (float_divl prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e))
else horner x)" |
"ub_exp prec x = (if 0 < x then float_divr prec 1 (lb_exp prec (-x))
else if x < - 1 then (case floor_fl x of (Float m e) =>
(ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e))
else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)"
by pat_completeness auto
termination by (relation "measure (λ v. let (prec, x) = sum_case id id v in (if 0 < x then 1 else 0))", auto simp add: less_float_def)
lemma exp_m1_ge_quarter: "(1 / 4 :: real) ≤ exp (- 1)"
proof -
have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto
have "1 / 4 = (Float 1 -2)" unfolding Float_num by auto
also have "… ≤ lb_exp_horner 1 (get_even 4) 1 1 (- 1)"
unfolding get_even_def eq4
by (auto simp add: lapprox_posrat_def rapprox_posrat_def normfloat.simps)
also have "… ≤ exp (- 1 :: float)" using bnds_exp_horner[where x="- 1"] by auto
finally show ?thesis unfolding real_of_float_minus real_of_float_1 .
qed
lemma lb_exp_pos: assumes "¬ 0 < x" shows "0 < lb_exp prec x"
proof -
let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
let "?horner x" = "let y = ?lb_horner x in if y ≤ 0 then Float 1 -2 else y"
have pos_horner: "!! x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x ≤ 0", auto simp add: le_float_def less_float_def)
moreover { fix x :: float fix num :: nat
have "0 < real (?horner x) ^ num" using `0 < ?horner x`[unfolded less_float_def real_of_float_0] by (rule zero_less_power)
also have "… = (?horner x) ^ num" using float_power by auto
finally have "0 < real ((?horner x) ^ num)" .
}
ultimately show ?thesis
unfolding lb_exp.simps if_not_P[OF `¬ 0 < x`] Let_def
by (cases "floor_fl x", cases "x < - 1", auto simp add: float_power le_float_def less_float_def)
qed
lemma exp_boundaries': assumes "x ≤ 0"
shows "exp x ∈ { (lb_exp prec x) .. (ub_exp prec x)}"
proof -
let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x"
have "real x ≤ 0" and "¬ x > 0" using `x ≤ 0` unfolding le_float_def less_float_def by auto
show ?thesis
proof (cases "x < - 1")
case False hence "- 1 ≤ real x" unfolding less_float_def by auto
show ?thesis
proof (cases "?lb_exp_horner x ≤ 0")
from `¬ x < - 1` have "- 1 ≤ real x" unfolding less_float_def by auto
hence "exp (- 1) ≤ exp x" unfolding exp_le_cancel_iff .
from order_trans[OF exp_m1_ge_quarter this]
have "Float 1 -2 ≤ exp x" unfolding Float_num .
moreover case True
ultimately show ?thesis using bnds_exp_horner `real x ≤ 0` `¬ x > 0` `¬ x < - 1` by auto
next
case False thus ?thesis using bnds_exp_horner `real x ≤ 0` `¬ x > 0` `¬ x < - 1` by (auto simp add: Let_def)
qed
next
case True
obtain m e where Float_floor: "floor_fl x = Float m e" by (cases "floor_fl x", auto)
let ?num = "nat (- m) * 2 ^ nat e"
have "real (floor_fl x) < - 1" using floor_fl `x < - 1` unfolding le_float_def less_float_def real_of_float_minus real_of_float_1 by (rule order_le_less_trans)
hence "real (floor_fl x) < 0" unfolding Float_floor real_of_float_simp using zero_less_pow2[of xe] by auto
hence "m < 0"
unfolding less_float_def real_of_float_0 Float_floor real_of_float_simp
unfolding pos_prod_lt[OF zero_less_pow2[of e], unfolded mult_commute] by auto
hence "1 ≤ - m" by auto
hence "0 < nat (- m)" by auto
moreover
have "0 ≤ e" using floor_pos_exp Float_floor[symmetric] by auto
hence "(0::nat) < 2 ^ nat e" by auto
ultimately have "0 < ?num" by auto
hence "real ?num ≠ 0" by auto
have e_nat: "(nat e) = e" using `0 ≤ e` by auto
have num_eq: "real ?num = - floor_fl x" using `0 < nat (- m)`
unfolding Float_floor real_of_float_minus real_of_float_simp real_of_nat_mult pow2_int[of "nat e", unfolded e_nat] real_of_nat_power by auto
have "0 < - floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] unfolding less_float_def num_eq[symmetric] real_of_float_0 real_of_nat_zero .
hence "real (floor_fl x) < 0" unfolding less_float_def by auto
have "exp x ≤ ub_exp prec x"
proof -
have div_less_zero: "real (float_divr prec x (- floor_fl x)) ≤ 0"
using float_divr_nonpos_pos_upper_bound[OF `x ≤ 0` `0 < - floor_fl x`] unfolding le_float_def real_of_float_0 .
have "exp x = exp (?num * (x / ?num))" using `real ?num ≠ 0` by auto
also have "… = exp (x / ?num) ^ ?num" unfolding exp_real_of_nat_mult ..
also have "… ≤ exp (float_divr prec x (- floor_fl x)) ^ ?num" unfolding num_eq
by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto
also have "… ≤ (?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num" unfolding float_power
by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto)
finally show ?thesis unfolding ub_exp.simps if_not_P[OF `¬ 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def .
qed
moreover
have "lb_exp prec x ≤ exp x"
proof -
let ?divl = "float_divl prec x (- Float m e)"
let ?horner = "?lb_exp_horner ?divl"
show ?thesis
proof (cases "?horner ≤ 0")
case False hence "0 ≤ real ?horner" unfolding le_float_def by auto
have div_less_zero: "real (float_divl prec x (- floor_fl x)) ≤ 0"
using `real (floor_fl x) < 0` `real x ≤ 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg)
have "(?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num ≤
exp (float_divl prec x (- floor_fl x)) ^ ?num" unfolding float_power
using `0 ≤ real ?horner`[unfolded Float_floor[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono)
also have "… ≤ exp (x / ?num) ^ ?num" unfolding num_eq
using float_divl by (auto intro!: power_mono simp del: real_of_float_minus)
also have "… = exp (?num * (x / ?num))" unfolding exp_real_of_nat_mult ..
also have "… = exp x" using `real ?num ≠ 0` by auto
finally show ?thesis
unfolding lb_exp.simps if_not_P[OF `¬ 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_not_P[OF False] by auto
next
case True
have "real (floor_fl x) ≠ 0" and "real (floor_fl x) ≤ 0" using `real (floor_fl x) < 0` by auto
from divide_right_mono_neg[OF floor_fl[of x] `real (floor_fl x) ≤ 0`, unfolded divide_self[OF `real (floor_fl x) ≠ 0`]]
have "- 1 ≤ x / (- floor_fl x)" unfolding real_of_float_minus by auto
from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]]
have "Float 1 -2 ≤ exp (x / (- floor_fl x))" unfolding Float_num .
hence "real (Float 1 -2) ^ ?num ≤ exp (x / (- floor_fl x)) ^ ?num"
by (auto intro!: power_mono simp add: Float_num)
also have "… = exp x" unfolding num_eq exp_real_of_nat_mult[symmetric] using `real (floor_fl x) ≠ 0` by auto
finally show ?thesis
unfolding lb_exp.simps if_not_P[OF `¬ 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_P[OF True] float_power .
qed
qed
ultimately show ?thesis by auto
qed
qed
lemma exp_boundaries: "exp x ∈ { lb_exp prec x .. ub_exp prec x }"
proof -
show ?thesis
proof (cases "0 < x")
case False hence "x ≤ 0" unfolding less_float_def le_float_def by auto
from exp_boundaries'[OF this] show ?thesis .
next
case True hence "-x ≤ 0" unfolding less_float_def le_float_def by auto
have "lb_exp prec x ≤ exp x"
proof -
from exp_boundaries'[OF `-x ≤ 0`]
have ub_exp: "exp (- real x) ≤ ub_exp prec (-x)" unfolding atLeastAtMost_iff real_of_float_minus by auto
have "float_divl prec 1 (ub_exp prec (-x)) ≤ 1 / ub_exp prec (-x)" using float_divl[where x=1] by auto
also have "… ≤ exp x"
using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp] exp_gt_zero, symmetric]]
unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto
finally show ?thesis unfolding lb_exp.simps if_P[OF True] .
qed
moreover
have "exp x ≤ ub_exp prec x"
proof -
have "¬ 0 < -x" using `0 < x` unfolding less_float_def by auto
from exp_boundaries'[OF `-x ≤ 0`]
have lb_exp: "lb_exp prec (-x) ≤ exp (- real x)" unfolding atLeastAtMost_iff real_of_float_minus by auto
have "exp x ≤ (1 :: float) / lb_exp prec (-x)"
using lb_exp[unfolded inverse_le_iff_le[OF exp_gt_zero lb_exp_pos[OF `¬ 0 < -x`, unfolded less_float_def real_of_float_0],
symmetric]]
unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide real_of_float_1 by auto
also have "… ≤ float_divr prec 1 (lb_exp prec (-x))" using float_divr .
finally show ?thesis unfolding ub_exp.simps if_P[OF True] .
qed
ultimately show ?thesis by auto
qed
qed
lemma bnds_exp: "∀ (x::real) lx ux. (l, u) = (lb_exp prec lx, ub_exp prec ux) ∧ x ∈ {lx .. ux} --> l ≤ exp x ∧ exp x ≤ u"
proof (rule allI, rule allI, rule allI, rule impI)
fix x::real and lx ux
assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) ∧ x ∈ {lx .. ux}"
hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x ∈ {lx .. ux}" by auto
{ from exp_boundaries[of lx prec, unfolded l]
have "l ≤ exp lx" by (auto simp del: lb_exp.simps)
also have "… ≤ exp x" using x by auto
finally have "l ≤ exp x" .
} moreover
{ have "exp x ≤ exp ux" using x by auto
also have "… ≤ u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps)
finally have "exp x ≤ u" .
} ultimately show "l ≤ exp x ∧ exp x ≤ u" ..
qed
section "Logarithm"
subsection "Compute the logarithm series"
fun ub_ln_horner :: "nat => nat => nat => float => float"
and lb_ln_horner :: "nat => nat => nat => float => float" where
"ub_ln_horner prec 0 i x = 0" |
"ub_ln_horner prec (Suc n) i x = rapprox_rat prec 1 (int i) - x * lb_ln_horner prec n (Suc i) x" |
"lb_ln_horner prec 0 i x = 0" |
"lb_ln_horner prec (Suc n) i x = lapprox_rat prec 1 (int i) - x * ub_ln_horner prec n (Suc i) x"
lemma ln_bounds:
assumes "0 ≤ x" and "x < 1"
shows "(∑i=0..<2*n. -1^i * (1 / real (i + 1)) * x ^ (Suc i)) ≤ ln (x + 1)" (is "?lb")
and "ln (x + 1) ≤ (∑i=0..<2*n + 1. -1^i * (1 / real (i + 1)) * x ^ (Suc i))" (is "?ub")
proof -
let "?a n" = "(1/real (n +1)) * x ^ (Suc n)"
have ln_eq: "(∑ i. -1^i * ?a i) = ln (x + 1)"
using ln_series[of "x + 1"] `0 ≤ x` `x < 1` by auto
have "norm x < 1" using assms by auto
have "?a ----> 0" unfolding Suc_eq_plus1[symmetric] inverse_eq_divide[symmetric]
using tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto
{ fix n have "0 ≤ ?a n" by (rule mult_nonneg_nonneg, auto intro!: mult_nonneg_nonneg simp add: `0 ≤ x`) }
{ fix n have "?a (Suc n) ≤ ?a n" unfolding inverse_eq_divide[symmetric]
proof (rule mult_mono)
show "0 ≤ x ^ Suc (Suc n)" by (auto intro!: mult_nonneg_nonneg simp add: `0 ≤ x`)
have "x ^ Suc (Suc n) ≤ x ^ Suc n * 1" unfolding power_Suc2 mult_assoc[symmetric]
by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto intro!: mult_nonneg_nonneg simp add: `0 ≤ x`)
thus "x ^ Suc (Suc n) ≤ x ^ Suc n" by auto
qed auto }
from summable_Leibniz'(2,4)[OF `?a ----> 0` `!!n. 0 ≤ ?a n`, OF `!!n. ?a (Suc n) ≤ ?a n`, unfolded ln_eq]
show "?lb" and "?ub" by auto
qed
lemma ln_float_bounds:
assumes "0 ≤ real x" and "real x < 1"
shows "x * lb_ln_horner prec (get_even n) 1 x ≤ ln (x + 1)" (is "?lb ≤ ?ln")
and "ln (x + 1) ≤ x * ub_ln_horner prec (get_odd n) 1 x" (is "?ln ≤ ?ub")
proof -
obtain ev where ev: "get_even n = 2 * ev" using get_even_double ..
obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double ..
let "?s n" = "-1^n * (1 / real (1 + n)) * (real x)^(Suc n)"
have "?lb ≤ setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 mult_assoc[symmetric] real_of_float_mult setsum_left_distrib[symmetric] unfolding mult_commute[of "real x"] ev
using horner_bounds(1)[where G="λ i k. Suc k" and F="λx. x" and f="λx. x" and lb="λn i k x. lb_ln_horner prec n k x" and ub="λn i k x. ub_ln_horner prec n k x" and j'=1 and n="2*ev",
OF `0 ≤ real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 ≤ real x`
by (rule mult_right_mono)
also have "… ≤ ?ln" using ln_bounds(1)[OF `0 ≤ real x` `real x < 1`] by auto
finally show "?lb ≤ ?ln" .
have "?ln ≤ setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 ≤ real x` `real x < 1`] by auto
also have "… ≤ ?ub" unfolding power_Suc2 mult_assoc[symmetric] real_of_float_mult setsum_left_distrib[symmetric] unfolding mult_commute[of "real x"] od
using horner_bounds(2)[where G="λ i k. Suc k" and F="λx. x" and f="λx. x" and lb="λn i k x. lb_ln_horner prec n k x" and ub="λn i k x. ub_ln_horner prec n k x" and j'=1 and n="2*od+1",
OF `0 ≤ real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 ≤ real x`
by (rule mult_right_mono)
finally show "?ln ≤ ?ub" .
qed
lemma ln_add: assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)"
proof -
have "x ≠ 0" using assms by auto
have "x + y = x * (1 + y / x)" unfolding right_distrib times_divide_eq_right nonzero_mult_divide_cancel_left[OF `x ≠ 0`] by auto
moreover
have "0 < y / x" using assms divide_pos_pos by auto
hence "0 < 1 + y / x" by auto
ultimately show ?thesis using ln_mult assms by auto
qed
subsection "Compute the logarithm of 2"
definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3
in (Float 1 -1 * ub_ln_horner prec (get_odd prec) 1 (Float 1 -1)) +
(third * ub_ln_horner prec (get_odd prec) 1 third))"
definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3
in (Float 1 -1 * lb_ln_horner prec (get_even prec) 1 (Float 1 -1)) +
(third * lb_ln_horner prec (get_even prec) 1 third))"
lemma ub_ln2: "ln 2 ≤ ub_ln2 prec" (is "?ub_ln2")
and lb_ln2: "lb_ln2 prec ≤ ln 2" (is "?lb_ln2")
proof -
let ?uthird = "rapprox_rat (max prec 1) 1 3"
let ?lthird = "lapprox_rat prec 1 3"
have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1)"
using ln_add[of "3 / 2" "1 / 2"] by auto
have lb3: "?lthird ≤ 1 / 3" using lapprox_rat[of prec 1 3] by auto
hence lb3_ub: "real ?lthird < 1" by auto
have lb3_lb: "0 ≤ real ?lthird" using lapprox_rat_bottom[of 1 3] by auto
have ub3: "1 / 3 ≤ ?uthird" using rapprox_rat[of 1 3] by auto
hence ub3_lb: "0 ≤ real ?uthird" by auto
have lb2: "0 ≤ real (Float 1 -1)" and ub2: "real (Float 1 -1) < 1" unfolding Float_num by auto
have "0 ≤ (1::int)" and "0 < (3::int)" by auto
have ub3_ub: "real ?uthird < 1" unfolding rapprox_rat.simps(2)[OF `0 ≤ 1` `0 < 3`]
by (rule rapprox_posrat_less1, auto)
have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto
have uthird_gt0: "0 < real ?uthird + 1" using ub3_lb by auto
have lthird_gt0: "0 < real ?lthird + 1" using lb3_lb by auto
show ?ub_ln2 unfolding ub_ln2_def Let_def real_of_float_add ln2_sum Float_num(4)[symmetric]
proof (rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2])
have "ln (1 / 3 + 1) ≤ ln (real ?uthird + 1)" unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto
also have "… ≤ ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird"
using ln_float_bounds(2)[OF ub3_lb ub3_ub] .
finally show "ln (1 / 3 + 1) ≤ ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird" .
qed
show ?lb_ln2 unfolding lb_ln2_def Let_def real_of_float_add ln2_sum Float_num(4)[symmetric]
proof (rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2])
have "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird ≤ ln (real ?lthird + 1)"
using ln_float_bounds(1)[OF lb3_lb lb3_ub] .
also have "… ≤ ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto
finally show "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird ≤ ln (1 / 3 + 1)" .
qed
qed
subsection "Compute the logarithm in the entire domain"
function ub_ln :: "nat => float => float option" and lb_ln :: "nat => float => float option" where
"ub_ln prec x = (if x ≤ 0 then None
else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x)))
else let horner = λx. x * ub_ln_horner prec (get_odd prec) 1 x in
if x ≤ Float 3 -1 then Some (horner (x - 1))
else if x < Float 1 1 then Some (horner (Float 1 -1) + horner (x * rapprox_rat prec 2 3 - 1))
else let l = bitlen (mantissa x) - 1 in
Some (ub_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l) - 1)))" |
"lb_ln prec x = (if x ≤ 0 then None
else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x)))
else let horner = λx. x * lb_ln_horner prec (get_even prec) 1 x in
if x ≤ Float 3 -1 then Some (horner (x - 1))
else if x < Float 1 1 then Some (horner (Float 1 -1) +
horner (max (x * lapprox_rat prec 2 3 - 1) 0))
else let l = bitlen (mantissa x) - 1 in
Some (lb_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l) - 1)))"
by pat_completeness auto
termination proof (relation "measure (λ v. let (prec, x) = sum_case id id v in (if x < 1 then 1 else 0))", auto)
fix prec x assume "¬ x ≤ 0" and "x < 1" and "float_divl (max prec (Suc 0)) 1 x < 1"
hence "0 < x" and "0 < max prec (Suc 0)" unfolding less_float_def le_float_def by auto
from float_divl_pos_less1_bound[OF `0 < x` `x < 1` `0 < max prec (Suc 0)`]
show False using `float_divl (max prec (Suc 0)) 1 x < 1` unfolding less_float_def le_float_def by auto
next
fix prec x assume "¬ x ≤ 0" and "x < 1" and "float_divr prec 1 x < 1"
hence "0 < x" unfolding less_float_def le_float_def by auto
from float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`, of prec]
show False using `float_divr prec 1 x < 1` unfolding less_float_def le_float_def by auto
qed
lemma ln_shifted_float: assumes "0 < m" shows "ln (Float m e) = ln 2 * (e + (bitlen m - 1)) + ln (Float m (- (bitlen m - 1)))"
proof -
let ?B = "2^nat (bitlen m - 1)"
have "0 < real m" and "!!X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m ≠ 0" using assms by auto
hence "0 ≤ bitlen m - 1" using bitlen_ge1[OF `m ≠ 0`] by auto
show ?thesis
proof (cases "0 ≤ e")
case True
show ?thesis unfolding normalized_float[OF `m ≠ 0`]
unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`]
unfolding real_of_float_ge0_exp[OF True] ln_mult[OF `0 < real m` `0 < 2^nat e`]
ln_realpow[OF `0 < 2`] algebra_simps using `0 ≤ bitlen m - 1` True by auto
next
case False hence "0 < -e" by auto
hence pow_gt0: "(0::real) < 2^nat (-e)" by auto
hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto
show ?thesis unfolding normalized_float[OF `m ≠ 0`]
unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`]
unfolding real_of_float_nge0_exp[OF False] ln_mult[OF `0 < real m` inv_gt0] ln_inverse[OF pow_gt0]
ln_realpow[OF `0 < 2`] algebra_simps using `0 ≤ bitlen m - 1` False by auto
qed
qed
lemma ub_ln_lb_ln_bounds': assumes "1 ≤ x"
shows "the (lb_ln prec x) ≤ ln x ∧ ln x ≤ the (ub_ln prec x)"
(is "?lb ≤ ?ln ∧ ?ln ≤ ?ub")
proof (cases "x < Float 1 1")
case True
hence "real (x - 1) < 1" and "real x < 2" unfolding less_float_def Float_num by auto
have "¬ x ≤ 0" and "¬ x < 1" using `1 ≤ x` unfolding less_float_def le_float_def by auto
hence "0 ≤ real (x - 1)" using `1 ≤ x` unfolding less_float_def Float_num by auto
have [simp]: "(Float 3 -1) = 3 / 2" by (simp add: real_of_float_def pow2_def)
show ?thesis
proof (cases "x ≤ Float 3 -1")
case True
show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def
using ln_float_bounds[OF `0 ≤ real (x - 1)` `real (x - 1) < 1`, of prec] `¬ x ≤ 0` `¬ x < 1` True
by auto
next
case False hence *: "3 / 2 < x" by (auto simp add: le_float_def)
with ln_add[of "3 / 2" "x - 3 / 2"]
have add: "ln x = ln (3 / 2) + ln (real x * 2 / 3)"
by (auto simp add: algebra_simps diff_divide_distrib)
let "?ub_horner x" = "x * ub_ln_horner prec (get_odd prec) 1 x"
let "?lb_horner x" = "x * lb_ln_horner prec (get_even prec) 1 x"
{ have up: "real (rapprox_rat prec 2 3) ≤ 1"
by (rule rapprox_rat_le1) simp_all
have low: "2 / 3 ≤ rapprox_rat prec 2 3"
by (rule order_trans[OF _ rapprox_rat]) simp
from mult_less_le_imp_less[OF * low] *
have pos: "0 < real (x * rapprox_rat prec 2 3 - 1)" by auto
have "ln (real x * 2/3)
≤ ln (real (x * rapprox_rat prec 2 3 - 1) + 1)"
proof (rule ln_le_cancel_iff[symmetric, THEN iffD1])
show "real x * 2 / 3 ≤ real (x * rapprox_rat prec 2 3 - 1) + 1"
using * low by auto
show "0 < real x * 2 / 3" using * by simp
show "0 < real (x * rapprox_rat prec 2 3 - 1) + 1" using pos by auto
qed
also have "… ≤ ?ub_horner (x * rapprox_rat prec 2 3 - 1)"
proof (rule ln_float_bounds(2))
from mult_less_le_imp_less[OF `real x < 2` up] low *
show "real (x * rapprox_rat prec 2 3 - 1) < 1" by auto
show "0 ≤ real (x * rapprox_rat prec 2 3 - 1)" using pos by auto
qed
finally have "ln x
≤ ?ub_horner (Float 1 -1)
+ ?ub_horner (x * rapprox_rat prec 2 3 - 1)"
using ln_float_bounds(2)[of "Float 1 -1" prec prec] add by auto }
moreover
{ let ?max = "max (x * lapprox_rat prec 2 3 - 1) 0"
have up: "lapprox_rat prec 2 3 ≤ 2/3"
by (rule order_trans[OF lapprox_rat], simp)
have low: "0 ≤ real (lapprox_rat prec 2 3)"
using lapprox_rat_bottom[of 2 3 prec] by simp
have "?lb_horner ?max
≤ ln (real ?max + 1)"
proof (rule ln_float_bounds(1))
from mult_less_le_imp_less[OF `real x < 2` up] * low
show "real ?max < 1" by (cases "real (lapprox_rat prec 2 3) = 0",
auto simp add: real_of_float_max)
show "0 ≤ real ?max" by (auto simp add: real_of_float_max)
qed
also have "… ≤ ln (real x * 2/3)"
proof (rule ln_le_cancel_iff[symmetric, THEN iffD1])
show "0 < real ?max + 1" by (auto simp add: real_of_float_max)
show "0 < real x * 2/3" using * by auto
show "real ?max + 1 ≤ real x * 2/3" using * up
by (cases "0 < real x * real (lapprox_posrat prec 2 3) - 1",
auto simp add: real_of_float_max min_max.sup_absorb1)
qed
finally have "?lb_horner (Float 1 -1) + ?lb_horner ?max
≤ ln x"
using ln_float_bounds(1)[of "Float 1 -1" prec prec] add by auto }
ultimately
show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def
using `¬ x ≤ 0` `¬ x < 1` True False by auto
qed
next
case False
hence "¬ x ≤ 0" and "¬ x < 1" "0 < x" "¬ x ≤ Float 3 -1"
using `1 ≤ x` unfolding less_float_def le_float_def real_of_float_simp pow2_def
by auto
show ?thesis
proof (cases x)
case (Float m e)
let ?s = "Float (e + (bitlen m - 1)) 0"
let ?x = "Float m (- (bitlen m - 1))"
have "0 < m" and "m ≠ 0" using float_pos_m_pos `0 < x` Float by auto
{
have "lb_ln2 prec * ?s ≤ ln 2 * (e + (bitlen m - 1))" (is "?lb2 ≤ _")
unfolding real_of_float_mult real_of_float_ge0_exp[OF order_refl] nat_0 power_0 mult_1_right
using lb_ln2[of prec]
proof (rule mult_right_mono)
have "1 ≤ Float m e" using `1 ≤ x` Float unfolding le_float_def by auto
from float_gt1_scale[OF this]
show "0 ≤ real (e + (bitlen m - 1))" by auto
qed
moreover
from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m ≠ 0`, symmetric]]
have "0 ≤ real (?x - 1)" and "real (?x - 1) < 1" by auto
from ln_float_bounds(1)[OF this]
have "(?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1) ≤ ln ?x" (is "?lb_horner ≤ _") by auto
ultimately have "?lb2 + ?lb_horner ≤ ln x"
unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
}
moreover
{
from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m ≠ 0`, symmetric]]
have "0 ≤ real (?x - 1)" and "real (?x - 1) < 1" by auto
from ln_float_bounds(2)[OF this]
have "ln ?x ≤ (?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1)" (is "_ ≤ ?ub_horner") by auto
moreover
have "ln 2 * (e + (bitlen m - 1)) ≤ ub_ln2 prec * ?s" (is "_ ≤ ?ub2")
unfolding real_of_float_mult real_of_float_ge0_exp[OF order_refl] nat_0 power_0 mult_1_right
using ub_ln2[of prec]
proof (rule mult_right_mono)
have "1 ≤ Float m e" using `1 ≤ x` Float unfolding le_float_def by auto
from float_gt1_scale[OF this]
show "0 ≤ real (e + (bitlen m - 1))" by auto
qed
ultimately have "ln x ≤ ?ub2 + ?ub_horner"
unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
}
ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps
unfolding if_not_P[OF `¬ x ≤ 0`] if_not_P[OF `¬ x < 1`] if_not_P[OF False] if_not_P[OF `¬ x ≤ Float 3 -1`] Let_def
unfolding scale.simps[of m e, unfolded Float[symmetric]] mantissa.simps[of m e, unfolded Float[symmetric]] real_of_float_add
by auto
qed
qed
lemma ub_ln_lb_ln_bounds: assumes "0 < x"
shows "the (lb_ln prec x) ≤ ln x ∧ ln x ≤ the (ub_ln prec x)"
(is "?lb ≤ ?ln ∧ ?ln ≤ ?ub")
proof (cases "x < 1")
case False hence "1 ≤ x" unfolding less_float_def le_float_def by auto
show ?thesis using ub_ln_lb_ln_bounds'[OF `1 ≤ x`] .
next
case True have "¬ x ≤ 0" using `0 < x` unfolding less_float_def le_float_def by auto
have "0 < real x" and "real x ≠ 0" using `0 < x` unfolding less_float_def by auto
hence A: "0 < 1 / real x" by auto
{
let ?divl = "float_divl (max prec 1) 1 x"
have A': "1 ≤ ?divl" using float_divl_pos_less1_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto
hence B: "0 < real ?divl" unfolding le_float_def by auto
have "ln ?divl ≤ ln (1 / x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto
hence "ln x ≤ - ln ?divl" unfolding nonzero_inverse_eq_divide[OF `real x ≠ 0`, symmetric] ln_inverse[OF `0 < real x`] by auto
from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le]
have "?ln ≤ - the (lb_ln prec ?divl)" unfolding real_of_float_minus by (rule order_trans)
} moreover
{
let ?divr = "float_divr prec 1 x"
have A': "1 ≤ ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto
hence B: "0 < real ?divr" unfolding le_float_def by auto
have "ln (1 / x) ≤ ln ?divr" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto
hence "- ln ?divr ≤ ln x" unfolding nonzero_inverse_eq_divide[OF `real x ≠ 0`, symmetric] ln_inverse[OF `0 < real x`] by auto
from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this
have "- the (ub_ln prec ?divr) ≤ ?ln" unfolding real_of_float_minus by (rule order_trans)
}
ultimately show ?thesis unfolding lb_ln.simps[where x=x] ub_ln.simps[where x=x]
unfolding if_not_P[OF `¬ x ≤ 0`] if_P[OF True] by auto
qed
lemma lb_ln: assumes "Some y = lb_ln prec x"
shows "y ≤ ln x" and "0 < real x"
proof -
have "0 < x"
proof (rule ccontr)
assume "¬ 0 < x" hence "x ≤ 0" unfolding le_float_def less_float_def by auto
thus False using assms by auto
qed
thus "0 < real x" unfolding less_float_def by auto
have "the (lb_ln prec x) ≤ ln x"