(** This file is part of the Coquelicot formalization of real analysis in Coq: http://coquelicot.saclay.inria.fr/  Copyright (C) 2011-2015 Sylvie Boldo #<br /># Copyright (C) 2011-2015 Catherine Lelay #<br /># Copyright (C) 2011-2015 Guillaume Melquiond  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the COPYING file for more details. *)
(** This file describes results about differentiability in [R x R]. This includes the [Schwarz] theorem and the 2D Taylor-Lagrange inequality. *)
(** * Differentiability *)
Definition differentiable_pt_lim (f : R -> R -> R) (x y : R) (lx ly : R) :=
forall eps : posreal, locally_2d (fun u v =>
Rabs (f u v - f x y - (lx * (u - x) + ly * (v - y))) <= eps * Rmax (Rabs (u - x)) (Rabs (v - y))) x y.
Definition differentiable_pt (f : R -> R -> R) (x y : R) :=
exists lx, exists ly, differentiable_pt_lim f x y lx ly.
(** * Operations *)
(** * Partial derivatives *)
Definition partial_derive (m k : nat) (f : R -> R -> R) : R -> R -> R :=
fun x y => Derive_n (fun t => Derive_n (fun z => f t z) k y) m x.
Definition differential (p : nat) (f : R -> R -> R) (x y dx dy : R) : R :=
sum_f_R0
(fun m =>
C p m *
partial_derive m (p - m)%nat f x y *
dx ^ m * dy ^ (p - m)%nat)
p.
Definition DL_pol (n : nat) (f : R -> R -> R) (x y dx dy : R) : R :=
sum_f_R0
(fun p =>
differential p f x y dx dy / INR (fact p))
n.
(** * Schwarz theorem *)
intros f p q r s x y H.
destruct H; rewrite H.
rewrite plus_0_l.
unfold partial_derive.
simpl.
rewrite -Derive_n_comp.
now apply Derive_n_ext.
rewrite plus_0_r.
unfold partial_derive.
simpl.
apply Derive_n_ext.
intros y0.
rewrite -Derive_n_comp.
now apply Derive_n_ext.
Qed.
(** * Iterated differential *)
Fixpoint ex_diff_n f n x y :=
continuity_2d_pt f x y /\
match n with
| O => True
| S n =>
ex_derive (fun z => f z y) x /\
ex_derive (fun z => f x z) y /\
ex_diff_n (fun u v => Derive (fun z => f z v) u) n x y /\
ex_diff_n (fun u v => Derive (fun z => f u z) v) n x y
end.

Lemma ex_diff_n_deriv: forall n p q, (p+q <= n)%nat -> forall f x y,
ex_diff_n f n x y-> ex_diff_n (partial_derive p q f) (n -(p+q)) x y.