src/HOL/Library/Polynomial.thy
author haftmann
Mon, 06 Feb 2017 20:56:34 +0100
changeset 64990 c6a7de505796
parent 64861 9e8de30fd859
child 65346 673a7b3379ec
permissions -rw-r--r--
more explicit errors in pathological cases
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 39302
diff changeset
     1
(*  Title:      HOL/Library/Polynomial.thy
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     2
    Author:     Brian Huffman
41959
b460124855b8 tuned headers;
wenzelm
parents: 39302
diff changeset
     3
    Author:     Clemens Ballarin
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
     4
    Author:     Amine Chaieb
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
     5
    Author:     Florian Haftmann
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     6
*)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     7
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
     8
section \<open>Polynomials as type over a ring structure\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     9
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    10
theory Polynomial
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
    11
imports Main "~~/src/HOL/Deriv" "~~/src/HOL/Library/More_List"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
    12
  "~~/src/HOL/Library/Infinite_Set"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    13
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    14
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
    15
subsection \<open>Auxiliary: operations for lists (later) representing coefficients\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    16
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    17
definition cCons :: "'a::zero \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixr "##" 65)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    18
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    19
  "x ## xs = (if xs = [] \<and> x = 0 then [] else x # xs)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    20
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    21
lemma cCons_0_Nil_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    22
  "0 ## [] = []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    23
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    24
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    25
lemma cCons_Cons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    26
  "x ## y # ys = x # y # ys"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    27
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    28
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    29
lemma cCons_append_Cons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    30
  "x ## xs @ y # ys = x # xs @ y # ys"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    31
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    32
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    33
lemma cCons_not_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    34
  "x \<noteq> 0 \<Longrightarrow> x ## xs = x # xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    35
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    36
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    37
lemma strip_while_not_0_Cons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    38
  "strip_while (\<lambda>x. x = 0) (x # xs) = x ## strip_while (\<lambda>x. x = 0) xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    39
proof (cases "x = 0")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    40
  case False then show ?thesis by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    41
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    42
  case True show ?thesis
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    43
  proof (induct xs rule: rev_induct)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    44
    case Nil with True show ?case by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    45
  next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    46
    case (snoc y ys) then show ?case
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    47
      by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    48
  qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    49
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    50
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    51
lemma tl_cCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    52
  "tl (x ## xs) = xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    53
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    54
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61260
diff changeset
    55
subsection \<open>Definition of type \<open>poly\<close>\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    56
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 60686
diff changeset
    57
typedef (overloaded) 'a poly = "{f :: nat \<Rightarrow> 'a::zero. \<forall>\<^sub>\<infinity> n. f n = 0}"
63433
wenzelm
parents: 63317
diff changeset
    58
  morphisms coeff Abs_poly
wenzelm
parents: 63317
diff changeset
    59
  by (auto intro!: ALL_MOST)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    60
59487
adaa430fc0f7 default abstypes and default abstract equations make technical (no_code) annotation superfluous
haftmann
parents: 58881
diff changeset
    61
setup_lifting type_definition_poly
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    62
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    63
lemma poly_eq_iff: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45605
diff changeset
    64
  by (simp add: coeff_inject [symmetric] fun_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    65
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    66
lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    67
  by (simp add: poly_eq_iff)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    68
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    69
lemma MOST_coeff_eq_0: "\<forall>\<^sub>\<infinity> n. coeff p n = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    70
  using coeff [of p] by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    71
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    72
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
    73
subsection \<open>Degree of a polynomial\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    74
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    75
definition degree :: "'a::zero poly \<Rightarrow> nat"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    76
where
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    77
  "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    78
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    79
lemma coeff_eq_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    80
  assumes "degree p < n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    81
  shows "coeff p n = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    82
proof -
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    83
  have "\<exists>n. \<forall>i>n. coeff p i = 0"
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    84
    using MOST_coeff_eq_0 by (simp add: MOST_nat)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    85
  then have "\<forall>i>degree p. coeff p i = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    86
    unfolding degree_def by (rule LeastI_ex)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    87
  with assms show ?thesis by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    88
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    89
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    90
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    91
  by (erule contrapos_np, rule coeff_eq_0, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    92
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    93
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    94
  unfolding degree_def by (erule Least_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    95
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    96
lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    97
  unfolding degree_def by (drule not_less_Least, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    98
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    99
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   100
subsection \<open>The zero polynomial\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   101
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   102
instantiation poly :: (zero) zero
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   103
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   104
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   105
lift_definition zero_poly :: "'a poly"
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   106
  is "\<lambda>_. 0" by (rule MOST_I) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   107
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   108
instance ..
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   109
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   110
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   111
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   112
lemma coeff_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   113
  "coeff 0 n = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   114
  by transfer rule
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   115
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   116
lemma degree_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   117
  "degree 0 = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   118
  by (rule order_antisym [OF degree_le le0]) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   119
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   120
lemma leading_coeff_neq_0:
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   121
  assumes "p \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   122
  shows "coeff p (degree p) \<noteq> 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   123
proof (cases "degree p")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   124
  case 0
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   125
  from \<open>p \<noteq> 0\<close> have "\<exists>n. coeff p n \<noteq> 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   126
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   127
  then obtain n where "coeff p n \<noteq> 0" ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   128
  hence "n \<le> degree p" by (rule le_degree)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   129
  with \<open>coeff p n \<noteq> 0\<close> and \<open>degree p = 0\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   130
  show "coeff p (degree p) \<noteq> 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   131
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   132
  case (Suc n)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   133
  from \<open>degree p = Suc n\<close> have "n < degree p" by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   134
  hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   135
  then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   136
  from \<open>degree p = Suc n\<close> and \<open>n < i\<close> have "degree p \<le> i" by simp
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   137
  also from \<open>coeff p i \<noteq> 0\<close> have "i \<le> degree p" by (rule le_degree)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   138
  finally have "degree p = i" .
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   139
  with \<open>coeff p i \<noteq> 0\<close> show "coeff p (degree p) \<noteq> 0" by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   140
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   141
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   142
lemma leading_coeff_0_iff [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   143
  "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   144
  by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   145
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   146
lemma eq_zero_or_degree_less:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   147
  assumes "degree p \<le> n" and "coeff p n = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   148
  shows "p = 0 \<or> degree p < n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   149
proof (cases n)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   150
  case 0
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   151
  with \<open>degree p \<le> n\<close> and \<open>coeff p n = 0\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   152
  have "coeff p (degree p) = 0" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   153
  then have "p = 0" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   154
  then show ?thesis ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   155
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   156
  case (Suc m)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   157
  have "\<forall>i>n. coeff p i = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   158
    using \<open>degree p \<le> n\<close> by (simp add: coeff_eq_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   159
  then have "\<forall>i\<ge>n. coeff p i = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   160
    using \<open>coeff p n = 0\<close> by (simp add: le_less)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   161
  then have "\<forall>i>m. coeff p i = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   162
    using \<open>n = Suc m\<close> by (simp add: less_eq_Suc_le)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   163
  then have "degree p \<le> m"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   164
    by (rule degree_le)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   165
  then have "degree p < n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   166
    using \<open>n = Suc m\<close> by (simp add: less_Suc_eq_le)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   167
  then show ?thesis ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   168
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   169
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   170
lemma coeff_0_degree_minus_1: "coeff rrr dr = 0 \<Longrightarrow> degree rrr \<le> dr \<Longrightarrow> degree rrr \<le> dr - 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   171
  using eq_zero_or_degree_less by fastforce
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   172
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   173
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   174
subsection \<open>List-style constructor for polynomials\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   175
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   176
lift_definition pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
55415
05f5fdb8d093 renamed 'nat_{case,rec}' to '{case,rec}_nat'
blanchet
parents: 54856
diff changeset
   177
  is "\<lambda>a p. case_nat a (coeff p)"
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   178
  by (rule MOST_SucD) (simp add: MOST_coeff_eq_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   179
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   180
lemmas coeff_pCons = pCons.rep_eq
29455
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   181
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   182
lemma coeff_pCons_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   183
  "coeff (pCons a p) 0 = a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   184
  by transfer simp
29455
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   185
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   186
lemma coeff_pCons_Suc [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   187
  "coeff (pCons a p) (Suc n) = coeff p n"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   188
  by (simp add: coeff_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   189
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   190
lemma degree_pCons_le:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   191
  "degree (pCons a p) \<le> Suc (degree p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   192
  by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   193
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   194
lemma degree_pCons_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   195
  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   196
  apply (rule order_antisym [OF degree_pCons_le])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   197
  apply (rule le_degree, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   198
  done
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   199
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   200
lemma degree_pCons_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   201
  "degree (pCons a 0) = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   202
  apply (rule order_antisym [OF _ le0])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   203
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   204
  done
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   205
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
   206
lemma degree_pCons_eq_if [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   207
  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   208
  apply (cases "p = 0", simp_all)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   209
  apply (rule order_antisym [OF _ le0])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   210
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   211
  apply (rule order_antisym [OF degree_pCons_le])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   212
  apply (rule le_degree, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   213
  done
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   214
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   215
lemma pCons_0_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   216
  "pCons 0 0 = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   217
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   218
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   219
lemma pCons_eq_iff [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   220
  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   221
proof safe
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   222
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   223
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   224
  then show "a = b" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   225
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   226
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   227
  then have "\<forall>n. coeff (pCons a p) (Suc n) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   228
                 coeff (pCons b q) (Suc n)" by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   229
  then show "p = q" by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   230
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   231
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   232
lemma pCons_eq_0_iff [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   233
  "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   234
  using pCons_eq_iff [of a p 0 0] by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   235
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   236
lemma pCons_cases [cases type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   237
  obtains (pCons) a q where "p = pCons a q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   238
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   239
  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   240
    by transfer
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   241
       (simp_all add: MOST_inj[where f=Suc and P="\<lambda>n. p n = 0" for p] fun_eq_iff Abs_poly_inverse
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   242
                 split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   243
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   244
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   245
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   246
  assumes zero: "P 0"
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   247
  assumes pCons: "\<And>a p. a \<noteq> 0 \<or> p \<noteq> 0 \<Longrightarrow> P p \<Longrightarrow> P (pCons a p)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   248
  shows "P p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   249
proof (induct p rule: measure_induct_rule [where f=degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   250
  case (less p)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   251
  obtain a q where "p = pCons a q" by (rule pCons_cases)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   252
  have "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   253
  proof (cases "q = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   254
    case True
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   255
    then show "P q" by (simp add: zero)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   256
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   257
    case False
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   258
    then have "degree (pCons a q) = Suc (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   259
      by (rule degree_pCons_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   260
    then have "degree q < degree p"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   261
      using \<open>p = pCons a q\<close> by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   262
    then show "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   263
      by (rule less.hyps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   264
  qed
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   265
  have "P (pCons a q)"
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   266
  proof (cases "a \<noteq> 0 \<or> q \<noteq> 0")
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   267
    case True
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   268
    with \<open>P q\<close> show ?thesis by (auto intro: pCons)
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   269
  next
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   270
    case False
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   271
    with zero show ?thesis by simp
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   272
  qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   273
  then show ?case
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   274
    using \<open>p = pCons a q\<close> by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   275
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   276
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   277
lemma degree_eq_zeroE:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   278
  fixes p :: "'a::zero poly"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   279
  assumes "degree p = 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   280
  obtains a where "p = pCons a 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   281
proof -
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   282
  obtain a q where p: "p = pCons a q" by (cases p)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   283
  with assms have "q = 0" by (cases "q = 0") simp_all
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   284
  with p have "p = pCons a 0" by simp
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   285
  with that show thesis .
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   286
qed
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   287
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   288
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   289
subsection \<open>Quickcheck generator for polynomials\<close>
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   290
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   291
quickcheck_generator poly constructors: "0 :: _ poly", pCons
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   292
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   293
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   294
subsection \<open>List-style syntax for polynomials\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   295
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   296
syntax
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   297
  "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   298
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   299
translations
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   300
  "[:x, xs:]" == "CONST pCons x [:xs:]"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   301
  "[:x:]" == "CONST pCons x 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   302
  "[:x:]" <= "CONST pCons x (_constrain 0 t)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   303
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   304
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   305
subsection \<open>Representation of polynomials by lists of coefficients\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   306
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   307
primrec Poly :: "'a::zero list \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   308
where
54855
d700d054d022 convenient printing of polynomial values
haftmann
parents: 54489
diff changeset
   309
  [code_post]: "Poly [] = 0"
d700d054d022 convenient printing of polynomial values
haftmann
parents: 54489
diff changeset
   310
| [code_post]: "Poly (a # as) = pCons a (Poly as)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   311
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   312
lemma Poly_replicate_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   313
  "Poly (replicate n 0) = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   314
  by (induct n) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   315
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   316
lemma Poly_eq_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   317
  "Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   318
  by (induct as) (auto simp add: Cons_replicate_eq)
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   319
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   320
lemma Poly_append_replicate_zero [simp]:
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   321
  "Poly (as @ replicate n 0) = Poly as"
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   322
  by (induct as) simp_all
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   323
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   324
lemma Poly_snoc_zero [simp]:
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   325
  "Poly (as @ [0]) = Poly as"
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   326
  using Poly_append_replicate_zero [of as 1] by simp
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   327
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   328
lemma Poly_cCons_eq_pCons_Poly [simp]:
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   329
  "Poly (a ## p) = pCons a (Poly p)"
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   330
  by (simp add: cCons_def)
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   331
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   332
lemma Poly_on_rev_starting_with_0 [simp]:
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   333
  assumes "hd as = 0"
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   334
  shows "Poly (rev (tl as)) = Poly (rev as)"
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   335
  using assms by (cases as) simp_all
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   336
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   337
lemma degree_Poly: "degree (Poly xs) \<le> length xs"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   338
  by (induction xs) simp_all
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   339
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   340
lemma coeff_Poly_eq [simp]:
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   341
  "coeff (Poly xs) = nth_default 0 xs"
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   342
  by (induct xs) (simp_all add: fun_eq_iff coeff_pCons split: nat.splits)
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   343
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   344
definition coeffs :: "'a poly \<Rightarrow> 'a::zero list"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   345
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   346
  "coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   347
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   348
lemma coeffs_eq_Nil [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   349
  "coeffs p = [] \<longleftrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   350
  by (simp add: coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   351
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   352
lemma not_0_coeffs_not_Nil:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   353
  "p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   354
  by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   355
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   356
lemma coeffs_0_eq_Nil [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   357
  "coeffs 0 = []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   358
  by simp
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   359
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   360
lemma coeffs_pCons_eq_cCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   361
  "coeffs (pCons a p) = a ## coeffs p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   362
proof -
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   363
  { fix ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   364
    assume "\<forall>m\<in>set ms. m > 0"
55415
05f5fdb8d093 renamed 'nat_{case,rec}' to '{case,rec}_nat'
blanchet
parents: 54856
diff changeset
   365
    then have "map (case_nat x f) ms = map f (map (\<lambda>n. n - 1) ms)"
58199
5fbe474b5da8 explicit theory with additional, less commonly used list operations
haftmann
parents: 57862
diff changeset
   366
      by (induct ms) (auto split: nat.split)
5fbe474b5da8 explicit theory with additional, less commonly used list operations
haftmann
parents: 57862
diff changeset
   367
  }
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   368
  note * = this
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   369
  show ?thesis
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   370
    by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt del: upt_Suc)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   371
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   372
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   373
lemma length_coeffs: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = degree p + 1"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   374
  by (simp add: coeffs_def)
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
   375
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   376
lemma coeffs_nth:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   377
  assumes "p \<noteq> 0" "n \<le> degree p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   378
  shows   "coeffs p ! n = coeff p n"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   379
  using assms unfolding coeffs_def by (auto simp del: upt_Suc)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   380
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
   381
lemma coeff_in_coeffs:
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
   382
  "p \<noteq> 0 \<Longrightarrow> n \<le> degree p \<Longrightarrow> coeff p n \<in> set (coeffs p)"
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
   383
  using coeffs_nth [of p n, symmetric]
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
   384
  by (simp add: length_coeffs)
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
   385
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   386
lemma not_0_cCons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   387
  "p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   388
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   389
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   390
lemma Poly_coeffs [simp, code abstype]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   391
  "Poly (coeffs p) = p"
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   392
  by (induct p) auto
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   393
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   394
lemma coeffs_Poly [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   395
  "coeffs (Poly as) = strip_while (HOL.eq 0) as"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   396
proof (induct as)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   397
  case Nil then show ?case by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   398
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   399
  case (Cons a as)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   400
  have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   401
    using replicate_length_same [of as 0] by (auto dest: sym [of _ as])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   402
  with Cons show ?case by auto
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   403
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   404
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   405
lemma last_coeffs_not_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   406
  "p \<noteq> 0 \<Longrightarrow> last (coeffs p) \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   407
  by (induct p) (auto simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   408
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   409
lemma strip_while_coeffs [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   410
  "strip_while (HOL.eq 0) (coeffs p) = coeffs p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   411
  by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   412
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   413
lemma coeffs_eq_iff:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   414
  "p = q \<longleftrightarrow> coeffs p = coeffs q" (is "?P \<longleftrightarrow> ?Q")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   415
proof
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   416
  assume ?P then show ?Q by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   417
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   418
  assume ?Q
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   419
  then have "Poly (coeffs p) = Poly (coeffs q)" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   420
  then show ?P by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   421
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   422
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   423
lemma nth_default_coeffs_eq:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   424
  "nth_default 0 (coeffs p) = coeff p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   425
  by (simp add: fun_eq_iff coeff_Poly_eq [symmetric])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   426
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   427
lemma [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   428
  "coeff p = nth_default 0 (coeffs p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   429
  by (simp add: nth_default_coeffs_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   430
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   431
lemma coeffs_eqI:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   432
  assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   433
  assumes zero: "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   434
  shows "coeffs p = xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   435
proof -
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   436
  from coeff have "p = Poly xs" by (simp add: poly_eq_iff)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   437
  with zero show ?thesis by simp (cases xs, simp_all)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   438
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   439
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   440
lemma degree_eq_length_coeffs [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   441
  "degree p = length (coeffs p) - 1"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   442
  by (simp add: coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   443
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   444
lemma length_coeffs_degree:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   445
  "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   446
  by (induct p) (auto simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   447
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   448
lemma [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   449
  "coeffs 0 = []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   450
  by (fact coeffs_0_eq_Nil)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   451
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   452
lemma [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   453
  "coeffs (pCons a p) = a ## coeffs p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   454
  by (fact coeffs_pCons_eq_cCons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   455
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   456
instantiation poly :: ("{zero, equal}") equal
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   457
begin
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   458
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   459
definition
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   460
  [code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   461
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   462
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   463
  by standard (simp add: equal equal_poly_def coeffs_eq_iff)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   464
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   465
end
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   466
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   467
lemma [code nbe]: "HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   468
  by (fact equal_refl)
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   469
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   470
definition is_zero :: "'a::zero poly \<Rightarrow> bool"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   471
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   472
  [code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   473
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   474
lemma is_zero_null [code_abbrev]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   475
  "is_zero p \<longleftrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   476
  by (simp add: is_zero_def null_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   477
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   478
subsubsection \<open>Reconstructing the polynomial from the list\<close>
63145
703edebd1d92 isabelle update_cartouches -c -t;
wenzelm
parents: 63060
diff changeset
   479
  \<comment> \<open>contributed by Sebastiaan J.C. Joosten and René Thiemann\<close>
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   480
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   481
definition poly_of_list :: "'a::comm_monoid_add list \<Rightarrow> 'a poly"
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   482
where
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   483
  [simp]: "poly_of_list = Poly"
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   484
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   485
lemma poly_of_list_impl [code abstract]:
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   486
  "coeffs (poly_of_list as) = strip_while (HOL.eq 0) as"
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   487
  by simp
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   488
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   489
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   490
subsection \<open>Fold combinator for polynomials\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   491
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   492
definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   493
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   494
  "fold_coeffs f p = foldr f (coeffs p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   495
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   496
lemma fold_coeffs_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   497
  "fold_coeffs f 0 = id"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   498
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   499
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   500
lemma fold_coeffs_pCons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   501
  "f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   502
  by (simp add: fold_coeffs_def cCons_def fun_eq_iff)
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   503
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   504
lemma fold_coeffs_pCons_0_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   505
  "fold_coeffs f (pCons 0 0) = id"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   506
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   507
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   508
lemma fold_coeffs_pCons_coeff_not_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   509
  "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   510
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   511
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   512
lemma fold_coeffs_pCons_not_0_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   513
  "p \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   514
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   515
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   516
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   517
subsection \<open>Canonical morphism on polynomials -- evaluation\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   518
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   519
definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   520
where
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61260
diff changeset
   521
  "poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" \<comment> \<open>The Horner Schema\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   522
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   523
lemma poly_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   524
  "poly 0 x = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   525
  by (simp add: poly_def)
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   526
  
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   527
lemma poly_pCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   528
  "poly (pCons a p) x = a + x * poly p x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   529
  by (cases "p = 0 \<and> a = 0") (auto simp add: poly_def)
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   530
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   531
lemma poly_altdef: 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   532
  "poly p (x :: 'a :: {comm_semiring_0, semiring_1}) = (\<Sum>i\<le>degree p. coeff p i * x ^ i)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   533
proof (induction p rule: pCons_induct)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   534
  case (pCons a p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   535
    show ?case
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   536
    proof (cases "p = 0")
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   537
      case False
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   538
      let ?p' = "pCons a p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   539
      note poly_pCons[of a p x]
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   540
      also note pCons.IH
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   541
      also have "a + x * (\<Sum>i\<le>degree p. coeff p i * x ^ i) =
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   542
                 coeff ?p' 0 * x^0 + (\<Sum>i\<le>degree p. coeff ?p' (Suc i) * x^Suc i)"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   543
          by (simp add: field_simps sum_distrib_left coeff_pCons)
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   544
      also note sum_atMost_Suc_shift[symmetric]
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
   545
      also note degree_pCons_eq[OF \<open>p \<noteq> 0\<close>, of a, symmetric]
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   546
      finally show ?thesis .
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   547
   qed simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   548
qed simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   549
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   550
lemma poly_0_coeff_0: "poly p 0 = coeff p 0"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   551
  by (cases p) (auto simp: poly_altdef)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   552
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   553
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   554
subsection \<open>Monomials\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   555
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   556
lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   557
  is "\<lambda>a m n. if m = n then a else 0"
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   558
  by (simp add: MOST_iff_cofinite)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   559
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   560
lemma coeff_monom [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   561
  "coeff (monom a m) n = (if m = n then a else 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   562
  by transfer rule
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   563
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   564
lemma monom_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   565
  "monom a 0 = pCons a 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   566
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   567
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   568
lemma monom_Suc:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   569
  "monom a (Suc n) = pCons 0 (monom a n)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   570
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   571
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   572
lemma monom_eq_0 [simp]: "monom 0 n = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   573
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   574
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   575
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   576
  by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   577
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   578
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   579
  by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   580
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   581
lemma degree_monom_le: "degree (monom a n) \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   582
  by (rule degree_le, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   583
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   584
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   585
  apply (rule order_antisym [OF degree_monom_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   586
  apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   587
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   588
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   589
lemma coeffs_monom [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   590
  "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   591
  by (induct n) (simp_all add: monom_0 monom_Suc)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   592
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   593
lemma fold_coeffs_monom [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   594
  "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   595
  by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   596
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   597
lemma poly_monom:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   598
  fixes a x :: "'a::{comm_semiring_1}"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   599
  shows "poly (monom a n) x = a * x ^ n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   600
  by (cases "a = 0", simp_all)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   601
    (induct n, simp_all add: mult.left_commute poly_def)  
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   602
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   603
lemma monom_eq_iff': "monom c n = monom d m \<longleftrightarrow>  c = d \<and> (c = 0 \<or> n = m)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   604
  by (auto simp: poly_eq_iff)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   605
  
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   606
lemma monom_eq_const_iff: "monom c n = [:d:] \<longleftrightarrow> c = d \<and> (c = 0 \<or> n = 0)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   607
  using monom_eq_iff'[of c n d 0] by (simp add: monom_0)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   608
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   609
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   610
subsection \<open>Leading coefficient\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   611
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   612
abbreviation lead_coeff:: "'a::zero poly \<Rightarrow> 'a"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   613
  where "lead_coeff p \<equiv> coeff p (degree p)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   614
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   615
lemma lead_coeff_pCons[simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   616
  "p \<noteq> 0 \<Longrightarrow> lead_coeff (pCons a p) = lead_coeff p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   617
  "p = 0 \<Longrightarrow> lead_coeff (pCons a p) = a"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   618
  by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   619
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   620
lemma lead_coeff_monom [simp]: "lead_coeff (monom c n) = c"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   621
  by (cases "c = 0") (simp_all add: degree_monom_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   622
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   623
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   624
subsection \<open>Addition and subtraction\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   625
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   626
instantiation poly :: (comm_monoid_add) comm_monoid_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   627
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   628
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   629
lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   630
  is "\<lambda>p q n. coeff p n + coeff q n"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   631
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   632
  fix q p :: "'a poly"
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   633
  show "\<forall>\<^sub>\<infinity>n. coeff p n + coeff q n = 0"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   634
    using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   635
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   636
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   637
lemma coeff_add [simp]: "coeff (p + q) n = coeff p n + coeff q n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   638
  by (simp add: plus_poly.rep_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   639
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   640
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   641
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   642
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   643
  show "(p + q) + r = p + (q + r)"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57482
diff changeset
   644
    by (simp add: poly_eq_iff add.assoc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   645
  show "p + q = q + p"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57482
diff changeset
   646
    by (simp add: poly_eq_iff add.commute)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   647
  show "0 + p = p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   648
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   649
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   650
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   651
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   652
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   653
instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   654
begin
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   655
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   656
lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   657
  is "\<lambda>p q n. coeff p n - coeff q n"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   658
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   659
  fix q p :: "'a poly"
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   660
  show "\<forall>\<^sub>\<infinity>n. coeff p n - coeff q n = 0"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   661
    using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   662
qed
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   663
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   664
lemma coeff_diff [simp]: "coeff (p - q) n = coeff p n - coeff q n"
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   665
  by (simp add: minus_poly.rep_eq)
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   666
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   667
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   668
proof
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   669
  fix p q r :: "'a poly"
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   670
  show "p + q - p = q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   671
    by (simp add: poly_eq_iff)
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   672
  show "p - q - r = p - (q + r)"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   673
    by (simp add: poly_eq_iff diff_diff_eq)
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   674
qed
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   675
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   676
end
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   677
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   678
instantiation poly :: (ab_group_add) ab_group_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   679
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   680
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   681
lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   682
  is "\<lambda>p n. - coeff p n"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   683
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   684
  fix p :: "'a poly"
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   685
  show "\<forall>\<^sub>\<infinity>n. - coeff p n = 0"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   686
    using MOST_coeff_eq_0 by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   687
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   688
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   689
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   690
  by (simp add: uminus_poly.rep_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   691
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   692
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   693
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   694
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   695
  show "- p + p = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   696
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   697
  show "p - q = p + - q"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 52380
diff changeset
   698
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   699
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   700
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   701
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   702
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   703
lemma add_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   704
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   705
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   706
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   707
lemma minus_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   708
  "- pCons a p = pCons (- a) (- p)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   709
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   710
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   711
lemma diff_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   712
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   713
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   714
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   715
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   716
  by (rule degree_le, auto simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   717
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   718
lemma degree_add_le:
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   719
  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   720
  by (auto intro: order_trans degree_add_le_max)
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   721
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   722
lemma degree_add_less:
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   723
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   724
  by (auto intro: le_less_trans degree_add_le_max)
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   725
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   726
lemma degree_add_eq_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   727
  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   728
  apply (cases "q = 0", simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   729
  apply (rule order_antisym)
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   730
  apply (simp add: degree_add_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   731
  apply (rule le_degree)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   732
  apply (simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   733
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   734
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   735
lemma degree_add_eq_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   736
  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   737
  using degree_add_eq_right [of q p]
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57482
diff changeset
   738
  by (simp add: add.commute)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   739
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   740
lemma degree_minus [simp]:
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   741
  "degree (- p) = degree p"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   742
  unfolding degree_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   743
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   744
lemma lead_coeff_add_le:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   745
  assumes "degree p < degree q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   746
  shows "lead_coeff (p + q) = lead_coeff q" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   747
  using assms
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   748
  by (metis coeff_add coeff_eq_0 monoid_add_class.add.left_neutral degree_add_eq_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   749
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   750
lemma lead_coeff_minus:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   751
  "lead_coeff (- p) = - lead_coeff p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   752
  by (metis coeff_minus degree_minus)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   753
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   754
lemma degree_diff_le_max:
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   755
  fixes p q :: "'a :: ab_group_add poly"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   756
  shows "degree (p - q) \<le> max (degree p) (degree q)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   757
  using degree_add_le [where p=p and q="-q"]
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 52380
diff changeset
   758
  by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   759
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   760
lemma degree_diff_le:
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   761
  fixes p q :: "'a :: ab_group_add poly"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   762
  assumes "degree p \<le> n" and "degree q \<le> n"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   763
  shows "degree (p - q) \<le> n"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   764
  using assms degree_add_le [of p n "- q"] by simp
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   765
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   766
lemma degree_diff_less:
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   767
  fixes p q :: "'a :: ab_group_add poly"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   768
  assumes "degree p < n" and "degree q < n"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   769
  shows "degree (p - q) < n"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   770
  using assms degree_add_less [of p n "- q"] by simp
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   771
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   772
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   773
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   774
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   775
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   776
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   777
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   778
lemma minus_monom: "- monom a n = monom (-a) n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   779
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   780
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   781
lemma coeff_sum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   782
  by (cases "finite A", induct set: finite, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   783
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   784
lemma monom_sum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   785
  by (rule poly_eqI) (simp add: coeff_sum)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   786
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   787
fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   788
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   789
  "plus_coeffs xs [] = xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   790
| "plus_coeffs [] ys = ys"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   791
| "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   792
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   793
lemma coeffs_plus_eq_plus_coeffs [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   794
  "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   795
proof -
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   796
  { fix xs ys :: "'a list" and n
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   797
    have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   798
    proof (induct xs ys arbitrary: n rule: plus_coeffs.induct)
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   799
      case (3 x xs y ys n)
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   800
      then show ?case by (cases n) (auto simp add: cCons_def)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   801
    qed simp_all }
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   802
  note * = this
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   803
  { fix xs ys :: "'a list"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   804
    assume "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" and "ys \<noteq> [] \<Longrightarrow> last ys \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   805
    moreover assume "plus_coeffs xs ys \<noteq> []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   806
    ultimately have "last (plus_coeffs xs ys) \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   807
    proof (induct xs ys rule: plus_coeffs.induct)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   808
      case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   809
    qed simp_all }
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   810
  note ** = this
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   811
  show ?thesis
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   812
    apply (rule coeffs_eqI)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   813
    apply (simp add: * nth_default_coeffs_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   814
    apply (rule **)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   815
    apply (auto dest: last_coeffs_not_0)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   816
    done
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   817
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   818
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   819
lemma coeffs_uminus [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   820
  "coeffs (- p) = map (\<lambda>a. - a) (coeffs p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   821
  by (rule coeffs_eqI)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   822
    (simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   823
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   824
lemma [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   825
  fixes p q :: "'a::ab_group_add poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   826
  shows "p - q = p + - q"
59557
ebd8ecacfba6 establish unique preferred fact names
haftmann
parents: 59487
diff changeset
   827
  by (fact diff_conv_add_uminus)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   828
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   829
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   830
  apply (induct p arbitrary: q, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   831
  apply (case_tac q, simp, simp add: algebra_simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   832
  done
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   833
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   834
lemma poly_minus [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   835
  fixes x :: "'a::comm_ring"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   836
  shows "poly (- p) x = - poly p x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   837
  by (induct p) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   838
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   839
lemma poly_diff [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   840
  fixes x :: "'a::comm_ring"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   841
  shows "poly (p - q) x = poly p x - poly q x"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 52380
diff changeset
   842
  using poly_add [of p "- q" x] by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   843
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   844
lemma poly_sum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   845
  by (induct A rule: infinite_finite_induct) simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   846
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   847
lemma degree_sum_le: "finite S \<Longrightarrow> (\<And> p . p \<in> S \<Longrightarrow> degree (f p) \<le> n)
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   848
  \<Longrightarrow> degree (sum f S) \<le> n"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   849
proof (induct S rule: finite_induct)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   850
  case (insert p S)
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   851
  hence "degree (sum f S) \<le> n" "degree (f p) \<le> n" by auto
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   852
  thus ?case unfolding sum.insert[OF insert(1-2)] by (metis degree_add_le)
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   853
qed simp
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   854
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   855
lemma poly_as_sum_of_monoms': 
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   856
  assumes n: "degree p \<le> n" 
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   857
  shows "(\<Sum>i\<le>n. monom (coeff p i) i) = p"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   858
proof -
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   859
  have eq: "\<And>i. {..n} \<inter> {i} = (if i \<le> n then {i} else {})"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   860
    by auto
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   861
  show ?thesis
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   862
    using n by (simp add: poly_eq_iff coeff_sum coeff_eq_0 sum.If_cases eq 
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   863
                  if_distrib[where f="\<lambda>x. x * a" for a])
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   864
qed
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   865
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   866
lemma poly_as_sum_of_monoms: "(\<Sum>i\<le>degree p. monom (coeff p i) i) = p"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   867
  by (intro poly_as_sum_of_monoms' order_refl)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   868
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   869
lemma Poly_snoc: "Poly (xs @ [x]) = Poly xs + monom x (length xs)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   870
  by (induction xs) (simp_all add: monom_0 monom_Suc)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   871
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   872
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   873
subsection \<open>Multiplication by a constant, polynomial multiplication and the unit polynomial\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   874
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   875
lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   876
  is "\<lambda>a p n. a * coeff p n"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   877
proof -
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   878
  fix a :: 'a and p :: "'a poly" show "\<forall>\<^sub>\<infinity> i. a * coeff p i = 0"
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   879
    using MOST_coeff_eq_0[of p] by eventually_elim simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   880
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   881
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   882
lemma coeff_smult [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   883
  "coeff (smult a p) n = a * coeff p n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   884
  by (simp add: smult.rep_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   885
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   886
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   887
  by (rule degree_le, simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   888
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   889
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57482
diff changeset
   890
  by (rule poly_eqI, simp add: mult.assoc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   891
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   892
lemma smult_0_right [simp]: "smult a 0 = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   893
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   894
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   895
lemma smult_0_left [simp]: "smult 0 p = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   896
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   897
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   898
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   899
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   900
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   901
lemma smult_add_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   902
  "smult a (p + q) = smult a p + smult a q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   903
  by (rule poly_eqI, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   904
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   905
lemma smult_add_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   906
  "smult (a + b) p = smult a p + smult b p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   907
  by (rule poly_eqI, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   908
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
   909
lemma smult_minus_right [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   910
  "smult (a::'a::comm_ring) (- p) = - smult a p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   911
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   912
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
   913
lemma smult_minus_left [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   914
  "smult (- a::'a::comm_ring) p = - smult a p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   915
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   916
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   917
lemma smult_diff_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   918
  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   919
  by (rule poly_eqI, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   920
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   921
lemma smult_diff_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   922
  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   923
  by (rule poly_eqI, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   924
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   925
lemmas smult_distribs =
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   926
  smult_add_left smult_add_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   927
  smult_diff_left smult_diff_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   928
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   929
lemma smult_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   930
  "smult a (pCons b p) = pCons (a * b) (smult a p)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   931
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   932
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   933
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   934
  by (induct n, simp add: monom_0, simp add: monom_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   935
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   936
lemma smult_Poly: "smult c (Poly xs) = Poly (map (op * c) xs)"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
   937
  by (auto simp add: poly_eq_iff nth_default_def)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   938
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   939
lemma degree_smult_eq [simp]:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   940
  fixes a :: "'a::{comm_semiring_0,semiring_no_zero_divisors}"
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   941
  shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   942
  by (cases "a = 0", simp, simp add: degree_def)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   943
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   944
lemma smult_eq_0_iff [simp]:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   945
  fixes a :: "'a::{comm_semiring_0,semiring_no_zero_divisors}"
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   946
  shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   947
  by (simp add: poly_eq_iff)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   948
  
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   949
lemma coeffs_smult [code abstract]:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   950
  fixes p :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   951
  shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   952
  by (rule coeffs_eqI)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   953
    (auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   954
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   955
lemma smult_eq_iff:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   956
  assumes "(b :: 'a :: field) \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   957
  shows   "smult a p = smult b q \<longleftrightarrow> smult (a / b) p = q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   958
proof
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   959
  assume "smult a p = smult b q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   960
  also from assms have "smult (inverse b) \<dots> = q" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   961
  finally show "smult (a / b) p = q" by (simp add: field_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   962
qed (insert assms, auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   963
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   964
instantiation poly :: (comm_semiring_0) comm_semiring_0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   965
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   966
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   967
definition
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   968
  "p * q = fold_coeffs (\<lambda>a p. smult a q + pCons 0 p) p 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   969
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   970
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   971
  by (simp add: times_poly_def)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   972
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   973
lemma mult_pCons_left [simp]:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   974
  "pCons a p * q = smult a q + pCons 0 (p * q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   975
  by (cases "p = 0 \<and> a = 0") (auto simp add: times_poly_def)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   976
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   977
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   978
  by (induct p) (simp add: mult_poly_0_left, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   979
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   980
lemma mult_pCons_right [simp]:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   981
  "p * pCons a q = smult a p + pCons 0 (p * q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   982
  by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   983
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   984
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   985
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   986
lemma mult_smult_left [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   987
  "smult a p * q = smult a (p * q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   988
  by (induct p) (simp add: mult_poly_0, simp add: smult_add_right)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   989
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   990
lemma mult_smult_right [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   991
  "p * smult a q = smult a (p * q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   992
  by (induct q) (simp add: mult_poly_0, simp add: smult_add_right)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   993
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   994
lemma mult_poly_add_left:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   995
  fixes p q r :: "'a poly"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   996
  shows "(p + q) * r = p * r + q * r"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   997
  by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   998
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   999
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1000
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1001
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1002
  show 0: "0 * p = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1003
    by (rule mult_poly_0_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1004
  show "p * 0 = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1005
    by (rule mult_poly_0_right)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1006
  show "(p + q) * r = p * r + q * r"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1007
    by (rule mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1008
  show "(p * q) * r = p * (q * r)"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1009
    by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1010
  show "p * q = q * p"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1011
    by (induct p, simp add: mult_poly_0, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1012
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1013
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1014
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1015
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1016
lemma coeff_mult_degree_sum:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1017
  "coeff (p * q) (degree p + degree q) =
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1018
   coeff p (degree p) * coeff q (degree q)"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1019
  by (induct p, simp, simp add: coeff_eq_0)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1020
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1021
instance poly :: ("{comm_semiring_0,semiring_no_zero_divisors}") semiring_no_zero_divisors
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1022
proof
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1023
  fix p q :: "'a poly"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1024
  assume "p \<noteq> 0" and "q \<noteq> 0"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1025
  have "coeff (p * q) (degree p + degree q) =
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1026
        coeff p (degree p) * coeff q (degree q)"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1027
    by (rule coeff_mult_degree_sum)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1028
  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1029
    using \<open>p \<noteq> 0\<close> and \<open>q \<noteq> 0\<close> by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1030
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1031
  thus "p * q \<noteq> 0" by (simp add: poly_eq_iff)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1032
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1033
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
  1034
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
  1035
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1036
lemma coeff_mult:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1037
  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1038
proof (induct p arbitrary: n)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1039
  case 0 show ?case by simp
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1040
next
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1041
  case (pCons a p n) thus ?case
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  1042
    by (cases n, simp, simp add: sum_atMost_Suc_shift
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  1043
                            del: sum_atMost_Suc)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1044
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1045
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1046
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1047
apply (rule degree_le)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1048
apply (induct p)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1049
apply simp
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1050
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1051
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1052
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1053
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1054
  by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1055
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1056
instantiation poly :: (comm_semiring_1) comm_semiring_1
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1057
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1058
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1059
definition one_poly_def: "1 = pCons 1 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1060
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1061
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1062
proof
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1063
  show "1 * p = p" for p :: "'a poly"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1064
    unfolding one_poly_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1065
  show "0 \<noteq> (1::'a poly)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1066
    unfolding one_poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1067
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1068
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1069
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1070
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1071
instance poly :: ("{comm_semiring_1,semiring_1_no_zero_divisors}") semiring_1_no_zero_divisors ..
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1072
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1073
instance poly :: (comm_ring) comm_ring ..
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1074
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1075
instance poly :: (comm_ring_1) comm_ring_1 ..
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1076
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1077
instance poly :: (comm_ring_1) comm_semiring_1_cancel ..
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1078
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1079
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1080
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1081
  by (simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1082
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1083
lemma monom_eq_1 [simp]:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1084
  "monom 1 0 = 1"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1085
  by (simp add: monom_0 one_poly_def)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  1086
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  1087
lemma monom_eq_1_iff: "monom c n = 1 \<longleftrightarrow> c = 1 \<and> n = 0"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  1088
  using monom_eq_const_iff[of c n 1] by (auto simp: one_poly_def)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  1089
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  1090
lemma monom_altdef:
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  1091
  "monom c n = smult c ([:0, 1:]^n)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  1092
  by (induction n) (simp_all add: monom_0 monom_Suc one_poly_def)
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1093
  
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1094
lemma degree_1 [simp]: "degree 1 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1095
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1096
  by (rule degree_pCons_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1097
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1098
lemma coeffs_1_eq [simp, code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1099
  "coeffs 1 = [1]"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1100
  by (simp add: one_poly_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1101
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1102
lemma degree_power_le:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1103
  "degree (p ^ n) \<le> degree p * n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1104
  by (induct n) (auto intro: order_trans degree_mult_le)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1105
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1106
lemma coeff_0_power:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1107
  "coeff (p ^ n) 0 = coeff p 0 ^ n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1108
  by (induction n) (simp_all add: coeff_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1109
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1110
lemma poly_smult [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1111
  "poly (smult a p) x = a * poly p x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1112
  by (induct p, simp, simp add: algebra_simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1113
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1114
lemma poly_mult [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1115
  "poly (p * q) x = poly p x * poly q x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1116
  by (induct p, simp_all, simp add: algebra_simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1117
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1118
lemma poly_1 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1119
  "poly 1 x = 1"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1120
  by (simp add: one_poly_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1121
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1122
lemma poly_power [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1123
  fixes p :: "'a::{comm_semiring_1} poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1124
  shows "poly (p ^ n) x = poly p x ^ n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1125
  by (induct n) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1126
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  1127
lemma poly_prod: "poly (\<Prod>k\<in>A. p k) x = (\<Prod>k\<in>A. poly (p k) x)"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1128
  by (induct A rule: infinite_finite_induct) simp_all
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1129
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  1130
lemma degree_prod_sum_le: "finite S \<Longrightarrow> degree (prod f S) \<le> sum (degree o f) S"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1131
proof (induct S rule: finite_induct)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1132
  case (insert a S)
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  1133
  show ?case unfolding prod.insert[OF insert(1-2)] sum.insert[OF insert(1-2)]
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1134
    by (rule le_trans[OF degree_mult_le], insert insert, auto)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1135
qed simp
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1136
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1137
lemma coeff_0_prod_list:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1138
  "coeff (prod_list xs) 0 = prod_list (map (\<lambda>p. coeff p 0) xs)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1139
  by (induction xs) (simp_all add: coeff_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1140
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1141
lemma coeff_monom_mult: 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1142
  "coeff (monom c n * p) k = (if k < n then 0 else c * coeff p (k - n))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1143
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1144
  have "coeff (monom c n * p) k = (\<Sum>i\<le>k. (if n = i then c else 0) * coeff p (k - i))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1145
    by (simp add: coeff_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1146
  also have "\<dots> = (\<Sum>i\<le>k. (if n = i then c * coeff p (k - i) else 0))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1147
    by (intro sum.cong) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1148
  also have "\<dots> = (if k < n then 0 else c * coeff p (k - n))" by (simp add: sum.delta')
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1149
  finally show ?thesis .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1150
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1151
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1152
lemma monom_1_dvd_iff':
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1153
  "monom 1 n dvd p \<longleftrightarrow> (\<forall>k<n. coeff p k = 0)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1154
proof
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1155
  assume "monom 1 n dvd p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1156
  then obtain r where r: "p = monom 1 n * r" by (elim dvdE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1157
  thus "\<forall>k<n. coeff p k = 0" by (simp add: coeff_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1158
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1159
  assume zero: "(\<forall>k<n. coeff p k = 0)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1160
  define r where "r = Abs_poly (\<lambda>k. coeff p (k + n))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1161
  have "\<forall>\<^sub>\<infinity>k. coeff p (k + n) = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1162
    by (subst cofinite_eq_sequentially, subst eventually_sequentially_seg, 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1163
        subst cofinite_eq_sequentially [symmetric]) transfer
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1164
  hence coeff_r [simp]: "coeff r k = coeff p (k + n)" for k unfolding r_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1165
    by (subst poly.Abs_poly_inverse) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1166
  have "p = monom 1 n * r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1167
    by (intro poly_eqI, subst coeff_monom_mult) (insert zero, simp_all)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1168
  thus "monom 1 n dvd p" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1169
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1170
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1171
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1172
subsection \<open>Mapping polynomials\<close>
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1173
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1174
definition map_poly 
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1175
     :: "('a :: zero \<Rightarrow> 'b :: zero) \<Rightarrow> 'a poly \<Rightarrow> 'b poly" where
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1176
  "map_poly f p = Poly (map f (coeffs p))"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1177
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1178
lemma map_poly_0 [simp]: "map_poly f 0 = 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1179
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1180
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1181
lemma map_poly_1: "map_poly f 1 = [:f 1:]"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1182
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1183
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1184
lemma map_poly_1' [simp]: "f 1 = 1 \<Longrightarrow> map_poly f 1 = 1"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1185
  by (simp add: map_poly_def one_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1186
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1187
lemma coeff_map_poly:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1188
  assumes "f 0 = 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1189
  shows   "coeff (map_poly f p) n = f (coeff p n)"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1190
  by (auto simp: map_poly_def nth_default_def coeffs_def assms
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1191
        not_less Suc_le_eq coeff_eq_0 simp del: upt_Suc)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1192
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1193
lemma coeffs_map_poly [code abstract]: 
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1194
    "coeffs (map_poly f p) = strip_while (op = 0) (map f (coeffs p))"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1195
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1196
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1197
lemma set_coeffs_map_poly:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1198
  "(\<And>x. f x = 0 \<longleftrightarrow> x = 0) \<Longrightarrow> set (coeffs (map_poly f p)) = f ` set (coeffs p)"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1199
  by (cases "p = 0") (auto simp: coeffs_map_poly last_map last_coeffs_not_0)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1200
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1201
lemma coeffs_map_poly': 
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1202
  assumes "(\<And>x. x \<noteq> 0 \<Longrightarrow> f x \<noteq> 0)"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1203
  shows   "coeffs (map_poly f p) = map f (coeffs p)"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1204
  by (cases "p = 0") (auto simp: coeffs_map_poly last_map last_coeffs_not_0 assms 
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1205
                           intro!: strip_while_not_last split: if_splits)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1206
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1207
lemma degree_map_poly:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1208
  assumes "\<And>x. x \<noteq> 0 \<Longrightarrow> f x \<noteq> 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1209
  shows   "degree (map_poly f p) = degree p"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1210
  by (simp add: degree_eq_length_coeffs coeffs_map_poly' assms)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1211
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1212
lemma map_poly_eq_0_iff:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1213
  assumes "f 0 = 0" "\<And>x. x \<in> set (coeffs p) \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> f x \<noteq> 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1214
  shows   "map_poly f p = 0 \<longleftrightarrow> p = 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1215
proof -
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1216
  {
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1217
    fix n :: nat
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1218
    have "coeff (map_poly f p) n = f (coeff p n)" by (simp add: coeff_map_poly assms)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1219
    also have "\<dots> = 0 \<longleftrightarrow> coeff p n = 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1220
    proof (cases "n < length (coeffs p)")
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1221
      case True
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1222
      hence "coeff p n \<in> set (coeffs p)" by (auto simp: coeffs_def simp del: upt_Suc)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1223
      with assms show "f (coeff p n) = 0 \<longleftrightarrow> coeff p n = 0" by auto
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1224
    qed (auto simp: assms length_coeffs nth_default_coeffs_eq [symmetric] nth_default_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1225
    finally have "(coeff (map_poly f p) n = 0) = (coeff p n = 0)" .
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1226
  }
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1227
  thus ?thesis by (auto simp: poly_eq_iff)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1228
qed
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1229
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1230
lemma map_poly_smult:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1231
  assumes "f 0 = 0""\<And>c x. f (c * x) = f c * f x"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1232
  shows   "map_poly f (smult c p) = smult (f c) (map_poly f p)"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1233
  by (intro poly_eqI) (simp_all add: assms coeff_map_poly)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1234
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1235
lemma map_poly_pCons:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1236
  assumes "f 0 = 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1237
  shows   "map_poly f (pCons c p) = pCons (f c) (map_poly f p)"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1238
  by (intro poly_eqI) (simp_all add: assms coeff_map_poly coeff_pCons split: nat.splits)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1239
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1240
lemma map_poly_map_poly:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1241
  assumes "f 0 = 0" "g 0 = 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1242
  shows   "map_poly f (map_poly g p) = map_poly (f \<circ> g) p"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1243
  by (intro poly_eqI) (simp add: coeff_map_poly assms)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1244
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1245
lemma map_poly_id [simp]: "map_poly id p = p"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1246
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1247
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1248
lemma map_poly_id' [simp]: "map_poly (\<lambda>x. x) p = p"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1249
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1250
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1251
lemma map_poly_cong: 
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1252
  assumes "(\<And>x. x \<in> set (coeffs p) \<Longrightarrow> f x = g x)"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1253
  shows   "map_poly f p = map_poly g p"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1254
proof -
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1255
  from assms have "map f (coeffs p) = map g (coeffs p)" by (intro map_cong) simp_all
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1256
  thus ?thesis by (simp only: coeffs_eq_iff coeffs_map_poly)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1257
qed
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1258
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1259
lemma map_poly_monom: "f 0 = 0 \<Longrightarrow> map_poly f (monom c n) = monom (f c) n"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1260
  by (intro poly_eqI) (simp_all add: coeff_map_poly)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1261
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1262
lemma map_poly_idI:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1263
  assumes "\<And>x. x \<in> set (coeffs p) \<Longrightarrow> f x = x"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1264
  shows   "map_poly f p = p"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1265
  using map_poly_cong[OF assms, of _ id] by simp
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1266
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1267
lemma map_poly_idI':
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1268
  assumes "\<And>x. x \<in> set (coeffs p) \<Longrightarrow> f x = x"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1269
  shows   "p = map_poly f p"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1270
  using map_poly_cong[OF assms, of _ id] by simp
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1271
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1272
lemma smult_conv_map_poly: "smult c p = map_poly (\<lambda>x. c * x) p"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1273
  by (intro poly_eqI) (simp_all add: coeff_map_poly)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1274
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1275
64793
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1276
subsection \<open>Conversions from @{typ nat} and @{typ int} numbers\<close>
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1277
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1278
lemma of_nat_poly: "of_nat n = [:of_nat n :: 'a :: comm_semiring_1:]"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1279
proof (induction n)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1280
  case (Suc n)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1281
  hence "of_nat (Suc n) = 1 + (of_nat n :: 'a poly)" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1282
    by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1283
  also have "(of_nat n :: 'a poly) = [: of_nat n :]" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1284
    by (subst Suc) (rule refl)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1285
  also have "1 = [:1:]" by (simp add: one_poly_def)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1286
  finally show ?case by (subst (asm) add_pCons) simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1287
qed simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1288
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1289
lemma degree_of_nat [simp]: "degree (of_nat n) = 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1290
  by (simp add: of_nat_poly)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1291
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1292
lemma lead_coeff_of_nat [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1293
  "lead_coeff (of_nat n) = (of_nat n :: 'a :: {comm_semiring_1,semiring_char_0})"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1294
  by (simp add: of_nat_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1295
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1296
lemma of_int_poly: "of_int k = [:of_int k :: 'a :: comm_ring_1:]"
64793
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1297
  by (simp only: of_int_of_nat of_nat_poly) simp
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1298
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1299
lemma degree_of_int [simp]: "degree (of_int k) = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1300
  by (simp add: of_int_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1301
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1302
lemma lead_coeff_of_int [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1303
  "lead_coeff (of_int k) = (of_int k :: 'a :: {comm_ring_1,ring_char_0})"
64793
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1304
  by (simp add: of_int_poly)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1305
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1306
lemma numeral_poly: "numeral n = [:numeral n:]"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1307
  by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
64793
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1308
    
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1309
lemma degree_numeral [simp]: "degree (numeral n) = 0"
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1310
  by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1311
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1312
lemma lead_coeff_numeral [simp]: 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1313
  "lead_coeff (numeral n) = numeral n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1314
  by (simp add: numeral_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1315
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1316
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1317
subsection \<open>Lemmas about divisibility\<close>
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1318
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1319
lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1320
proof -
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1321
  assume "p dvd q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1322
  then obtain k where "q = p * k" ..
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1323
  then have "smult a q = p * smult a k" by simp
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1324
  then show "p dvd smult a q" ..
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1325
qed
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1326
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1327
lemma dvd_smult_cancel:
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1328
  fixes a :: "'a :: field"
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1329
  shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1330
  by (drule dvd_smult [where a="inverse a"]) simp
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1331
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1332
lemma dvd_smult_iff:
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1333
  fixes a :: "'a::field"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1334
  shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1335
  by (safe elim!: dvd_smult dvd_smult_cancel)
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1336
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1337
lemma smult_dvd_cancel:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1338
  "smult a p dvd q \<Longrightarrow> p dvd q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1339
proof -
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1340
  assume "smult a p dvd q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1341
  then obtain k where "q = smult a p * k" ..
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1342
  then have "q = p * smult a k" by simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1343
  then show "p dvd q" ..
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1344
qed
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1345
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1346
lemma smult_dvd:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1347
  fixes a :: "'a::field"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1348
  shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1349
  by (rule smult_dvd_cancel [where a="inverse a"]) simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1350
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1351
lemma smult_dvd_iff:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1352
  fixes a :: "'a::field"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1353
  shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1354
  by (auto elim: smult_dvd smult_dvd_cancel)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1355
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1356
lemma is_unit_smult_iff: "smult c p dvd 1 \<longleftrightarrow> c dvd 1 \<and> p dvd 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1357
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1358
  have "smult c p = [:c:] * p" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1359
  also have "\<dots> dvd 1 \<longleftrightarrow> c dvd 1 \<and> p dvd 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1360
  proof safe
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1361
    assume A: "[:c:] * p dvd 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1362
    thus "p dvd 1" by (rule dvd_mult_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1363
    from A obtain q where B: "1 = [:c:] * p * q" by (erule dvdE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1364
    have "c dvd c * (coeff p 0 * coeff q 0)" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1365
    also have "\<dots> = coeff ([:c:] * p * q) 0" by (simp add: mult.assoc coeff_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1366
    also note B [symmetric]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1367
    finally show "c dvd 1" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1368
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1369
    assume "c dvd 1" "p dvd 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1370
    from \<open>c dvd 1\<close> obtain d where "1 = c * d" by (erule dvdE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1371
    hence "1 = [:c:] * [:d:]" by (simp add: one_poly_def mult_ac)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1372
    hence "[:c:] dvd 1" by (rule dvdI)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1373
    from mult_dvd_mono[OF this \<open>p dvd 1\<close>] show "[:c:] * p dvd 1" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1374
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1375
  finally show ?thesis .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1376
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1377
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1378
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1379
subsection \<open>Polynomials form an integral domain\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1380
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1381
instance poly :: (idom) idom ..
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1382
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1383
lemma degree_mult_eq:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1384
  fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1385
  shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1386
apply (rule order_antisym [OF degree_mult_le le_degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1387
apply (simp add: coeff_mult_degree_sum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1388
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1389
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1390
lemma degree_mult_eq_0:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1391
  fixes p q:: "'a :: {comm_semiring_0,semiring_no_zero_divisors} poly"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1392
  shows "degree (p * q) = 0 \<longleftrightarrow> p = 0 \<or> q = 0 \<or> (p \<noteq> 0 \<and> q \<noteq> 0 \<and> degree p = 0 \<and> degree q = 0)"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1393
  by (auto simp add: degree_mult_eq)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1394
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1395
lemma degree_mult_right_le:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1396
  fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1397
  assumes "q \<noteq> 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1398
  shows "degree p \<le> degree (p * q)"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1399
  using assms by (cases "p = 0") (simp_all add: degree_mult_eq)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1400
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1401
lemma coeff_degree_mult:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1402
  fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1403
  shows "coeff (p * q) (degree (p * q)) =
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1404
    coeff q (degree q) * coeff p (degree p)"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1405
  by (cases "p = 0 \<or> q = 0") (auto simp add: degree_mult_eq coeff_mult_degree_sum mult_ac)
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1406
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1407
lemma dvd_imp_degree_le:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1408
  fixes p q :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1409
  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1410
  by (erule dvdE, hypsubst, subst degree_mult_eq) auto
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1411
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1412
lemma divides_degree:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1413
  assumes pq: "p dvd (q :: 'a ::{comm_semiring_1,semiring_no_zero_divisors} poly)"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1414
  shows "degree p \<le> degree q \<or> q = 0"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1415
  by (metis dvd_imp_degree_le pq)
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1416
  
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1417
lemma const_poly_dvd_iff:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1418
  fixes c :: "'a :: {comm_semiring_1,semiring_no_zero_divisors}"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1419
  shows "[:c:] dvd p \<longleftrightarrow> (\<forall>n. c dvd coeff p n)"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1420
proof (cases "c = 0 \<or> p = 0")
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1421
  case False
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1422
  show ?thesis
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1423
  proof
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1424
    assume "[:c:] dvd p"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1425
    thus "\<forall>n. c dvd coeff p n" by (auto elim!: dvdE simp: coeffs_def)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1426
  next
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1427
    assume *: "\<forall>n. c dvd coeff p n"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1428
    define mydiv where "mydiv = (\<lambda>x y :: 'a. SOME z. x = y * z)"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1429
    have mydiv: "x = y * mydiv x y" if "y dvd x" for x y
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1430
      using that unfolding mydiv_def dvd_def by (rule someI_ex)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1431
    define q where "q = Poly (map (\<lambda>a. mydiv a c) (coeffs p))"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1432
    from False * have "p = q * [:c:]"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1433
      by (intro poly_eqI) (auto simp: q_def nth_default_def not_less length_coeffs_degree
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1434
                             coeffs_nth intro!: coeff_eq_0 mydiv)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1435
    thus "[:c:] dvd p" by (simp only: dvd_triv_right)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1436
  qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1437
qed (auto intro!: poly_eqI)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1438
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1439
lemma const_poly_dvd_const_poly_iff [simp]:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1440
  "[:a::'a::{comm_semiring_1,semiring_no_zero_divisors}:] dvd [:b:] \<longleftrightarrow> a dvd b"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1441
  by (subst const_poly_dvd_iff) (auto simp: coeff_pCons split: nat.splits)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1442
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1443
lemma lead_coeff_mult:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1444
  fixes p q :: "'a :: {comm_semiring_0, semiring_no_zero_divisors} poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1445
  shows "lead_coeff (p * q) = lead_coeff p * lead_coeff q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1446
  by (cases "p=0 \<or> q=0", auto simp add:coeff_mult_degree_sum degree_mult_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1447
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1448
lemma lead_coeff_smult:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1449
  "lead_coeff (smult c p :: 'a :: {comm_semiring_0,semiring_no_zero_divisors} poly) = c * lead_coeff p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1450
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1451
  have "smult c p = [:c:] * p" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1452
  also have "lead_coeff \<dots> = c * lead_coeff p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1453
    by (subst lead_coeff_mult) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1454
  finally show ?thesis .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1455
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1456
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1457
lemma lead_coeff_1 [simp]: "lead_coeff 1 = 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1458
  by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1459
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1460
lemma lead_coeff_power: 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1461
  "lead_coeff (p ^ n :: 'a :: {comm_semiring_1,semiring_no_zero_divisors} poly) = lead_coeff p ^ n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1462
  by (induction n) (simp_all add: lead_coeff_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1463
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1464
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1465
subsection \<open>Polynomials form an ordered integral domain\<close>
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1466
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1467
definition pos_poly :: "'a::linordered_semidom poly \<Rightarrow> bool"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1468
where
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1469
  "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1470
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1471
lemma pos_poly_pCons:
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1472
  "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1473
  unfolding pos_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1474
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1475
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1476
  unfolding pos_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1477
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1478
lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1479
  apply (induct p arbitrary: q, simp)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1480
  apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1481
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1482
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1483
lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1484
  unfolding pos_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1485
  apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
56544
b60d5d119489 made mult_pos_pos a simp rule
nipkow
parents: 56383
diff changeset
  1486
  apply (simp add: degree_mult_eq coeff_mult_degree_sum)
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1487
  apply auto
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1488
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1489
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1490
lemma pos_poly_total: "(p :: 'a :: linordered_idom poly) = 0 \<or> pos_poly p \<or> pos_poly (- p)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1491
by (induct p) (auto simp add: pos_poly_pCons)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1492
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1493
lemma last_coeffs_eq_coeff_degree:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1494
  "p \<noteq> 0 \<Longrightarrow> last (coeffs p) = coeff p (degree p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1495
  by (simp add: coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1496
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1497
lemma pos_poly_coeffs [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1498
  "pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)" (is "?P \<longleftrightarrow> ?Q")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1499
proof
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1500
  assume ?Q then show ?P by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1501
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1502
  assume ?P then have *: "0 < coeff p (degree p)" by (simp add: pos_poly_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1503
  then have "p \<noteq> 0" by auto
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1504
  with * show ?Q by (simp add: last_coeffs_eq_coeff_degree)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1505
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1506
35028
108662d50512 more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents: 34973
diff changeset
  1507
instantiation poly :: (linordered_idom) linordered_idom
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1508
begin
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1509
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1510
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1511
  "x < y \<longleftrightarrow> pos_poly (y - x)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1512
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1513
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1514
  "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1515
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1516
definition
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61605
diff changeset
  1517
  "\<bar>x::'a poly\<bar> = (if x < 0 then - x else x)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1518
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1519
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1520
  "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1521
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1522
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1523
proof
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1524
  fix x y z :: "'a poly"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1525
  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1526
    unfolding less_eq_poly_def less_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1527
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1528
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1529
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1530
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1531
    done
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1532
  show "x \<le> x"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1533
    unfolding less_eq_poly_def by simp
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1534
  show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1535
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1536
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1537
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1538
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1539
    done
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1540
  show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1541
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1542
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1543
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1544
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1545
    done
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1546
  show "x \<le> y \<Longrightarrow> z + x \<le> z + y"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1547
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1548
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1549
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1550
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1551
  show "x \<le> y \<or> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1552
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1553
    using pos_poly_total [of "x - y"]
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1554
    by auto
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1555
  show "x < y \<Longrightarrow> 0 < z \<Longrightarrow> z * x < z * y"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1556
    unfolding less_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1557
    by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1558
  show "\<bar>x\<bar> = (if x < 0 then - x else x)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1559
    by (rule abs_poly_def)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1560
  show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1561
    by (rule sgn_poly_def)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1562
qed
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1563
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1564
end
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1565
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1566
text \<open>TODO: Simplification rules for comparisons\<close>
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1567
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1568
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1569
subsection \<open>Synthetic division and polynomial roots\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1570
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1571
subsubsection \<open>Synthetic division\<close>  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1572
  
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1573
text \<open>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1574
  Synthetic division is simply division by the linear polynomial @{term "x - c"}.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1575
\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1576
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1577
definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1578
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1579
  "synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1580
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1581
definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1582
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1583
  "synthetic_div p c = fst (synthetic_divmod p c)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1584
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1585
lemma synthetic_divmod_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1586
  "synthetic_divmod 0 c = (0, 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1587
  by (simp add: synthetic_divmod_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1588
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1589
lemma synthetic_divmod_pCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1590
  "synthetic_divmod (pCons a p) c = (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1591
  by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1592
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1593
lemma synthetic_div_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1594
  "synthetic_div 0 c = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1595
  unfolding synthetic_div_def by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1596
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1597
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1598
by (induct p arbitrary: a) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1599
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1600
lemma snd_synthetic_divmod:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1601
  "snd (synthetic_divmod p c) = poly p c"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1602
  by (induct p, simp, simp add: split_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1603
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1604
lemma synthetic_div_pCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1605
  "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1606
  unfolding synthetic_div_def
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1607
  by (simp add: split_def snd_synthetic_divmod)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1608
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1609
lemma synthetic_div_eq_0_iff:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1610
  "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1611
proof (induct p)
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1612
  case 0
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1613
  then show ?case by simp
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1614
next
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1615
  case (pCons a p)
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1616
  then show ?case by (cases p) simp
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1617
qed
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1618
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1619
lemma degree_synthetic_div:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1620
  "degree (synthetic_div p c) = degree p - 1"
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1621
  by (induct p) (simp_all add: synthetic_div_eq_0_iff)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1622
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1623
lemma synthetic_div_correct:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1624
  "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1625
  by (induct p) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1626
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1627
lemma synthetic_div_unique:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1628
  "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1629
apply (induct p arbitrary: q r)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1630
apply (simp, frule synthetic_div_unique_lemma, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1631
apply (case_tac q, force)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1632
done
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1633
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1634
lemma synthetic_div_correct':
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1635
  fixes c :: "'a::comm_ring_1"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1636
  shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1637
  using synthetic_div_correct [of p c]
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1638
  by (simp add: algebra_simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1639
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1640
    
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1641
subsubsection \<open>Polynomial roots\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1642
  
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1643
lemma poly_eq_0_iff_dvd:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1644
  fixes c :: "'a::{comm_ring_1}"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1645
  shows "poly p c = 0 \<longleftrightarrow> [:- c, 1:] dvd p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1646
proof
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1647
  assume "poly p c = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1648
  with synthetic_div_correct' [of c p]
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1649
  have "p = [:-c, 1:] * synthetic_div p c" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1650
  then show "[:-c, 1:] dvd p" ..
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1651
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1652
  assume "[:-c, 1:] dvd p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1653
  then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1654
  then show "poly p c = 0" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1655
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1656
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1657
lemma dvd_iff_poly_eq_0:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1658
  fixes c :: "'a::{comm_ring_1}"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1659
  shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (- c) = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1660
  by (simp add: poly_eq_0_iff_dvd)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1661
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1662
lemma poly_roots_finite:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1663
  fixes p :: "'a::{comm_ring_1,ring_no_zero_divisors} poly"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1664
  shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1665
proof (induct n \<equiv> "degree p" arbitrary: p)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1666
  case (0 p)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1667
  then obtain a where "a \<noteq> 0" and "p = [:a:]"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1668
    by (cases p, simp split: if_splits)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1669
  then show "finite {x. poly p x = 0}" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1670
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1671
  case (Suc n p)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1672
  show "finite {x. poly p x = 0}"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1673
  proof (cases "\<exists>x. poly p x = 0")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1674
    case False
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1675
    then show "finite {x. poly p x = 0}" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1676
  next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1677
    case True
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1678
    then obtain a where "poly p a = 0" ..
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1679
    then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1680
    then obtain k where k: "p = [:-a, 1:] * k" ..
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1681
    with \<open>p \<noteq> 0\<close> have "k \<noteq> 0" by auto
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1682
    with k have "degree p = Suc (degree k)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1683
      by (simp add: degree_mult_eq del: mult_pCons_left)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1684
    with \<open>Suc n = degree p\<close> have "n = degree k" by simp
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1685
    then have "finite {x. poly k x = 0}" using \<open>k \<noteq> 0\<close> by (rule Suc.hyps)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1686
    then have "finite (insert a {x. poly k x = 0})" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1687
    then show "finite {x. poly p x = 0}"
57862
8f074e6e22fc tuned proofs;
wenzelm
parents: 57512
diff changeset
  1688
      by (simp add: k Collect_disj_eq del: mult_pCons_left)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1689
  qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1690
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1691
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1692
lemma poly_eq_poly_eq_iff:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1693
  fixes p q :: "'a::{comm_ring_1,ring_no_zero_divisors,ring_char_0} poly"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1694
  shows "poly p = poly q \<longleftrightarrow> p = q" (is "?P \<longleftrightarrow> ?Q")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1695
proof
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1696
  assume ?Q then show ?P by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1697
next
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1698
  { fix p :: "'a poly"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1699
    have "poly p = poly 0 \<longleftrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1700
      apply (cases "p = 0", simp_all)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1701
      apply (drule poly_roots_finite)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1702
      apply (auto simp add: infinite_UNIV_char_0)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1703
      done
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1704
  } note this [of "p - q"]
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1705
  moreover assume ?P
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1706
  ultimately show ?Q by auto
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1707
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1708
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1709
lemma poly_all_0_iff_0:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1710
  fixes p :: "'a::{ring_char_0, comm_ring_1,ring_no_zero_divisors} poly"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1711
  shows "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1712
  by (auto simp add: poly_eq_poly_eq_iff [symmetric])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1713
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1714
    
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1715
subsubsection \<open>Order of polynomial roots\<close>
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1716
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1717
definition order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1718
where
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1719
  "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1720
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1721
lemma coeff_linear_power:
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1722
  fixes a :: "'a::comm_semiring_1"
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1723
  shows "coeff ([:a, 1:] ^ n) n = 1"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1724
apply (induct n, simp_all)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1725
apply (subst coeff_eq_0)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1726
apply (auto intro: le_less_trans degree_power_le)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1727
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1728
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1729
lemma degree_linear_power:
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1730
  fixes a :: "'a::comm_semiring_1"
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1731
  shows "degree ([:a, 1:] ^ n) = n"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1732
apply (rule order_antisym)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1733
apply (rule ord_le_eq_trans [OF degree_power_le], simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1734
apply (rule le_degree, simp add: coeff_linear_power)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1735
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1736
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1737
lemma order_1: "[:-a, 1:] ^ order a p dvd p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1738
apply (cases "p = 0", simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1739
apply (cases "order a p", simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1740
apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1741
apply (drule not_less_Least, simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1742
apply (fold order_def, simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1743
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1744
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1745
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1746
unfolding order_def
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1747
apply (rule LeastI_ex)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1748
apply (rule_tac x="degree p" in exI)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1749
apply (rule notI)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1750
apply (drule (1) dvd_imp_degree_le)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1751
apply (simp only: degree_linear_power)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1752
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1753
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1754
lemma order:
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1755
  "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1756
by (rule conjI [OF order_1 order_2])
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1757
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1758
lemma order_degree:
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1759
  assumes p: "p \<noteq> 0"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1760
  shows "order a p \<le> degree p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1761
proof -
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1762
  have "order a p = degree ([:-a, 1:] ^ order a p)"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1763
    by (simp only: degree_linear_power)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1764
  also have "\<dots> \<le> degree p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1765
    using order_1 p by (rule dvd_imp_degree_le)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1766
  finally show ?thesis .
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1767
qed
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1768
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1769
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1770
apply (cases "p = 0", simp_all)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1771
apply (rule iffI)
56383
8e7052e9fda4 Cleaned up some messy proofs
paulson <lp15@cam.ac.uk>
parents: 55642
diff changeset
  1772
apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right)
8e7052e9fda4 Cleaned up some messy proofs
paulson <lp15@cam.ac.uk>
parents: 55642
diff changeset
  1773
unfolding poly_eq_0_iff_dvd
8e7052e9fda4 Cleaned up some messy proofs
paulson <lp15@cam.ac.uk>
parents: 55642
diff changeset
  1774
apply (metis dvd_power dvd_trans order_1)
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1775
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1776
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1777
lemma order_0I: "poly p a \<noteq> 0 \<Longrightarrow> order a p = 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1778
  by (subst (asm) order_root) auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1779
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1780
lemma order_unique_lemma:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1781
  fixes p :: "'a::idom poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1782
  assumes "[:-a, 1:] ^ n dvd p" "\<not> [:-a, 1:] ^ Suc n dvd p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1783
  shows "n = order a p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1784
unfolding Polynomial.order_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1785
apply (rule Least_equality [symmetric])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1786
apply (fact assms)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1787
apply (rule classical)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1788
apply (erule notE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1789
unfolding not_less_eq_eq
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1790
using assms(1) apply (rule power_le_dvd)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1791
apply assumption
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1792
  done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1793
    
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1794
lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1795
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1796
  define i where "i = order a p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1797
  define j where "j = order a q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1798
  define t where "t = [:-a, 1:]"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1799
  have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1800
    unfolding t_def by (simp add: dvd_iff_poly_eq_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1801
  assume "p * q \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1802
  then show "order a (p * q) = i + j"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1803
    apply clarsimp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1804
    apply (drule order [where a=a and p=p, folded i_def t_def])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1805
    apply (drule order [where a=a and p=q, folded j_def t_def])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1806
    apply clarify
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1807
    apply (erule dvdE)+
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1808
    apply (rule order_unique_lemma [symmetric], fold t_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1809
    apply (simp_all add: power_add t_dvd_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1810
    done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1811
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1812
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1813
lemma order_smult:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1814
  assumes "c \<noteq> 0" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1815
  shows "order x (smult c p) = order x p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1816
proof (cases "p = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1817
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1818
  have "smult c p = [:c:] * p" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1819
  also from assms False have "order x \<dots> = order x [:c:] + order x p" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1820
    by (subst order_mult) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1821
  also from assms have "order x [:c:] = 0" by (intro order_0I) auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1822
  finally show ?thesis by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1823
qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1824
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1825
(* Next two lemmas contributed by Wenda Li *)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1826
lemma order_1_eq_0 [simp]:"order x 1 = 0" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1827
  by (metis order_root poly_1 zero_neq_one)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1828
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1829
lemma order_power_n_n: "order a ([:-a,1:]^n)=n" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1830
proof (induct n) (*might be proved more concisely using nat_less_induct*)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1831
  case 0
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1832
  thus ?case by (metis order_root poly_1 power_0 zero_neq_one)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1833
next 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1834
  case (Suc n)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1835
  have "order a ([:- a, 1:] ^ Suc n)=order a ([:- a, 1:] ^ n) + order a [:-a,1:]" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1836
    by (metis (no_types, hide_lams) One_nat_def add_Suc_right monoid_add_class.add.right_neutral 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1837
      one_neq_zero order_mult pCons_eq_0_iff power_add power_eq_0_iff power_one_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1838
  moreover have "order a [:-a,1:]=1" unfolding order_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1839
    proof (rule Least_equality,rule ccontr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1840
      assume  "\<not> \<not> [:- a, 1:] ^ Suc 1 dvd [:- a, 1:]"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1841
      hence "[:- a, 1:] ^ Suc 1 dvd [:- a, 1:]" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1842
      hence "degree ([:- a, 1:] ^ Suc 1) \<le> degree ([:- a, 1:] )" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1843
        by (rule dvd_imp_degree_le,auto) 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1844
      thus False by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1845
    next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1846
      fix y assume asm:"\<not> [:- a, 1:] ^ Suc y dvd [:- a, 1:]"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1847
      show "1 \<le> y" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1848
        proof (rule ccontr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1849
          assume "\<not> 1 \<le> y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1850
          hence "y=0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1851
          hence "[:- a, 1:] ^ Suc y dvd [:- a, 1:]" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1852
          thus False using asm by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1853
        qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1854
    qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1855
  ultimately show ?case using Suc by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1856
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1857
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1858
lemma order_0_monom [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1859
  assumes "c \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1860
  shows   "order 0 (monom c n) = n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1861
  using assms order_power_n_n[of 0 n] by (simp add: monom_altdef order_smult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1862
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1863
lemma dvd_imp_order_le:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1864
  "q \<noteq> 0 \<Longrightarrow> p dvd q \<Longrightarrow> Polynomial.order a p \<le> Polynomial.order a q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1865
  by (auto simp: order_mult elim: dvdE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1866
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1867
text\<open>Now justify the standard squarefree decomposition, i.e. f / gcd(f,f').\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1868
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1869
lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1870
apply (cases "p = 0", auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1871
apply (drule order_2 [where a=a and p=p])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1872
apply (metis not_less_eq_eq power_le_dvd)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1873
apply (erule power_le_dvd [OF order_1])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1874
done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1875
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1876
lemma order_decomp:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1877
  assumes "p \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1878
  shows "\<exists>q. p = [:- a, 1:] ^ order a p * q \<and> \<not> [:- a, 1:] dvd q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1879
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1880
  from assms have A: "[:- a, 1:] ^ order a p dvd p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1881
    and B: "\<not> [:- a, 1:] ^ Suc (order a p) dvd p" by (auto dest: order)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1882
  from A obtain q where C: "p = [:- a, 1:] ^ order a p * q" ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1883
  with B have "\<not> [:- a, 1:] ^ Suc (order a p) dvd [:- a, 1:] ^ order a p * q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1884
    by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1885
  then have "\<not> [:- a, 1:] ^ order a p * [:- a, 1:] dvd [:- a, 1:] ^ order a p * q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1886
    by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1887
  then have D: "\<not> [:- a, 1:] dvd q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1888
    using idom_class.dvd_mult_cancel_left [of "[:- a, 1:] ^ order a p" "[:- a, 1:]" q]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1889
    by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1890
  from C D show ?thesis by blast
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1891
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1892
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1893
lemma monom_1_dvd_iff:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1894
  assumes "p \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1895
  shows   "monom 1 n dvd p \<longleftrightarrow> n \<le> order 0 p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1896
  using assms order_divides[of 0 n p] by (simp add: monom_altdef)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1897
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1898
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1899
subsection \<open>Additional induction rules on polynomials\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1900
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1901
text \<open>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1902
  An induction rule for induction over the roots of a polynomial with a certain property. 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1903
  (e.g. all positive roots)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1904
\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1905
lemma poly_root_induct [case_names 0 no_roots root]:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1906
  fixes p :: "'a :: idom poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1907
  assumes "Q 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1908
  assumes "\<And>p. (\<And>a. P a \<Longrightarrow> poly p a \<noteq> 0) \<Longrightarrow> Q p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1909
  assumes "\<And>a p. P a \<Longrightarrow> Q p \<Longrightarrow> Q ([:a, -1:] * p)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1910
  shows   "Q p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1911
proof (induction "degree p" arbitrary: p rule: less_induct)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1912
  case (less p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1913
  show ?case
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1914
  proof (cases "p = 0")
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1915
    assume nz: "p \<noteq> 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1916
    show ?case
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1917
    proof (cases "\<exists>a. P a \<and> poly p a = 0")
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1918
      case False
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1919
      thus ?thesis by (intro assms(2)) blast
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1920
    next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1921
      case True
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1922
      then obtain a where a: "P a" "poly p a = 0" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1923
        by blast
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1924
      hence "-[:-a, 1:] dvd p" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1925
        by (subst minus_dvd_iff) (simp add: poly_eq_0_iff_dvd)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1926
      then obtain q where q: "p = [:a, -1:] * q" by (elim dvdE) simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1927
      with nz have q_nz: "q \<noteq> 0" by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1928
      have "degree p = Suc (degree q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1929
        by (subst q, subst degree_mult_eq) (simp_all add: q_nz)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1930
      hence "Q q" by (intro less) simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1931
      from a(1) and this have "Q ([:a, -1:] * q)" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1932
        by (rule assms(3))
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1933
      with q show ?thesis by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1934
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1935
  qed (simp add: assms(1))
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1936
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1937
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1938
lemma dropWhile_replicate_append: 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1939
  "dropWhile (op= a) (replicate n a @ ys) = dropWhile (op= a) ys"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1940
  by (induction n) simp_all
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1941
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1942
lemma Poly_append_replicate_0: "Poly (xs @ replicate n 0) = Poly xs"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1943
  by (subst coeffs_eq_iff) (simp_all add: strip_while_def dropWhile_replicate_append)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1944
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1945
text \<open>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1946
  An induction rule for simultaneous induction over two polynomials, 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1947
  prepending one coefficient in each step.
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1948
\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1949
lemma poly_induct2 [case_names 0 pCons]:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1950
  assumes "P 0 0" "\<And>a p b q. P p q \<Longrightarrow> P (pCons a p) (pCons b q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1951
  shows   "P p q"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1952
proof -
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  1953
  define n where "n = max (length (coeffs p)) (length (coeffs q))"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  1954
  define xs where "xs = coeffs p @ (replicate (n - length (coeffs p)) 0)"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  1955
  define ys where "ys = coeffs q @ (replicate (n - length (coeffs q)) 0)"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1956
  have "length xs = length ys" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1957
    by (simp add: xs_def ys_def n_def)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1958
  hence "P (Poly xs) (Poly ys)" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1959
    by (induction rule: list_induct2) (simp_all add: assms)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1960
  also have "Poly xs = p" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1961
    by (simp add: xs_def Poly_append_replicate_0)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1962
  also have "Poly ys = q" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1963
    by (simp add: ys_def Poly_append_replicate_0)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1964
  finally show ?thesis .
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1965
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1966
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1967
  
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1968
subsection \<open>Composition of polynomials\<close>
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1969
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1970
(* Several lemmas contributed by René Thiemann and Akihisa Yamada *)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1971
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1972
definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1973
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1974
  "pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1975
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1976
notation pcompose (infixl "\<circ>\<^sub>p" 71)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1977
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1978
lemma pcompose_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1979
  "pcompose 0 q = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1980
  by (simp add: pcompose_def)
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1981
  
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1982
lemma pcompose_pCons:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1983
  "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1984
  by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1985
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1986
lemma pcompose_1:
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1987
  fixes p :: "'a :: comm_semiring_1 poly"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1988
  shows "pcompose 1 p = 1"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1989
  unfolding one_poly_def by (auto simp: pcompose_pCons)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1990
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1991
lemma poly_pcompose:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1992
  "poly (pcompose p q) x = poly p (poly q x)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1993
  by (induct p) (simp_all add: pcompose_pCons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1994
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1995
lemma degree_pcompose_le:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1996
  "degree (pcompose p q) \<le> degree p * degree q"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1997
apply (induct p, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1998
apply (simp add: pcompose_pCons, clarify)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1999
apply (rule degree_add_le, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2000
apply (rule order_trans [OF degree_mult_le], simp)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2001
done
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2002
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2003
lemma pcompose_add:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2004
  fixes p q r :: "'a :: {comm_semiring_0, ab_semigroup_add} poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2005
  shows "pcompose (p + q) r = pcompose p r + pcompose q r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2006
proof (induction p q rule: poly_induct2)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2007
  case (pCons a p b q)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2008
  have "pcompose (pCons a p + pCons b q) r = 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2009
          [:a + b:] + r * pcompose p r + r * pcompose q r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2010
    by (simp_all add: pcompose_pCons pCons.IH algebra_simps)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2011
  also have "[:a + b:] = [:a:] + [:b:]" by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2012
  also have "\<dots> + r * pcompose p r + r * pcompose q r = 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2013
                 pcompose (pCons a p) r + pcompose (pCons b q) r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2014
    by (simp only: pcompose_pCons add_ac)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2015
  finally show ?case .
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2016
qed simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2017
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2018
lemma pcompose_uminus:
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2019
  fixes p r :: "'a :: comm_ring poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2020
  shows "pcompose (-p) r = -pcompose p r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2021
  by (induction p) (simp_all add: pcompose_pCons)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2022
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2023
lemma pcompose_diff:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2024
  fixes p q r :: "'a :: comm_ring poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2025
  shows "pcompose (p - q) r = pcompose p r - pcompose q r"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2026
  using pcompose_add[of p "-q"] by (simp add: pcompose_uminus)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2027
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2028
lemma pcompose_smult:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2029
  fixes p r :: "'a :: comm_semiring_0 poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2030
  shows "pcompose (smult a p) r = smult a (pcompose p r)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2031
  by (induction p) 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2032
     (simp_all add: pcompose_pCons pcompose_add smult_add_right)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2033
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2034
lemma pcompose_mult:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2035
  fixes p q r :: "'a :: comm_semiring_0 poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2036
  shows "pcompose (p * q) r = pcompose p r * pcompose q r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2037
  by (induction p arbitrary: q)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2038
     (simp_all add: pcompose_add pcompose_smult pcompose_pCons algebra_simps)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2039
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2040
lemma pcompose_assoc: 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2041
  "pcompose p (pcompose q r :: 'a :: comm_semiring_0 poly ) =
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2042
     pcompose (pcompose p q) r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2043
  by (induction p arbitrary: q) 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2044
     (simp_all add: pcompose_pCons pcompose_add pcompose_mult)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2045
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2046
lemma pcompose_idR[simp]:
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2047
  fixes p :: "'a :: comm_semiring_1 poly"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2048
  shows "pcompose p [: 0, 1 :] = p"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2049
  by (induct p; simp add: pcompose_pCons)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2050
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2051
lemma pcompose_sum: "pcompose (sum f A) p = sum (\<lambda>i. pcompose (f i) p) A"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2052
  by (cases "finite A", induction rule: finite_induct)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2053
     (simp_all add: pcompose_1 pcompose_add)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2054
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2055
lemma pcompose_prod: "pcompose (prod f A) p = prod (\<lambda>i. pcompose (f i) p) A"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2056
  by (cases "finite A", induction rule: finite_induct)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2057
     (simp_all add: pcompose_1 pcompose_mult)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2058
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  2059
lemma pcompose_const [simp]: "pcompose [:a:] q = [:a:]"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  2060
  by (subst pcompose_pCons) simp
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2061
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2062
lemma pcompose_0': "pcompose p 0 = [:coeff p 0:]"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  2063
  by (induct p) (auto simp add: pcompose_pCons)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2064
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2065
lemma degree_pcompose:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2066
  fixes p q:: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2067
  shows "degree (pcompose p q) = degree p * degree q"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2068
proof (induct p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2069
  case 0
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2070
  thus ?case by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2071
next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2072
  case (pCons a p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2073
  have "degree (q * pcompose p q) = 0 \<Longrightarrow> ?case" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2074
    proof (cases "p=0")
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2075
      case True
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2076
      thus ?thesis by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2077
    next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2078
      case False assume "degree (q * pcompose p q) = 0"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2079
      hence "degree q=0 \<or> pcompose p q=0" by (auto simp add: degree_mult_eq_0)
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2080
      moreover have "\<lbrakk>pcompose p q=0;degree q\<noteq>0\<rbrakk> \<Longrightarrow> False" using pCons.hyps(2) \<open>p\<noteq>0\<close> 
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2081
        proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2082
          assume "pcompose p q=0" "degree q\<noteq>0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2083
          hence "degree p=0" using pCons.hyps(2) by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2084
          then obtain a1 where "p=[:a1:]"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2085
            by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases)
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2086
          thus False using \<open>pcompose p q=0\<close> \<open>p\<noteq>0\<close> by auto
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2087
        qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2088
      ultimately have "degree (pCons a p) * degree q=0" by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2089
      moreover have "degree (pcompose (pCons a p) q) = 0" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2090
        proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2091
          have" 0 = max (degree [:a:]) (degree (q*pcompose p q))"
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2092
            using \<open>degree (q * pcompose p q) = 0\<close> by simp
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2093
          also have "... \<ge> degree ([:a:] + q * pcompose p q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2094
            by (rule degree_add_le_max)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2095
          finally show ?thesis by (auto simp add:pcompose_pCons)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2096
        qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2097
      ultimately show ?thesis by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2098
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2099
  moreover have "degree (q * pcompose p q)>0 \<Longrightarrow> ?case" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2100
    proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2101
      assume asm:"0 < degree (q * pcompose p q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2102
      hence "p\<noteq>0" "q\<noteq>0" "pcompose p q\<noteq>0" by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2103
      have "degree (pcompose (pCons a p) q) = degree ( q * pcompose p q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2104
        unfolding pcompose_pCons
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2105
        using degree_add_eq_right[of "[:a:]" ] asm by auto       
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2106
      thus ?thesis 
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2107
        using pCons.hyps(2) degree_mult_eq[OF \<open>q\<noteq>0\<close> \<open>pcompose p q\<noteq>0\<close>] by auto
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2108
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2109
  ultimately show ?case by blast
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2110
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2111
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2112
lemma pcompose_eq_0:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2113
  fixes p q:: "'a :: {comm_semiring_0,semiring_no_zero_divisors} poly"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2114
  assumes "pcompose p q = 0" "degree q > 0" 
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2115
  shows "p = 0"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2116
proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2117
  have "degree p=0" using assms degree_pcompose[of p q] by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2118
  then obtain a where "p=[:a:]" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2119
    by (metis degree_pCons_eq_if gr0_conv_Suc neq0_conv pCons_cases)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2120
  hence "a=0" using assms(1) by auto
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2121
  thus ?thesis using \<open>p=[:a:]\<close> by simp
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2122
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2123
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2124
lemma lead_coeff_comp:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2125
  fixes p q:: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2126
  assumes "degree q > 0" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2127
  shows "lead_coeff (pcompose p q) = lead_coeff p * lead_coeff q ^ (degree p)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2128
proof (induct p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2129
  case 0
64794
6f7391f28197 lead_coeff is more appropriate as abbreviation
haftmann
parents: 64793
diff changeset
  2130
  thus ?case by auto
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2131
next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2132
  case (pCons a p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2133
  have "degree ( q * pcompose p q) = 0 \<Longrightarrow> ?case"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2134
    proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2135
      assume "degree ( q * pcompose p q) = 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2136
      hence "pcompose p q = 0" by (metis assms degree_0 degree_mult_eq_0 neq0_conv)
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2137
      hence "p=0" using pcompose_eq_0[OF _ \<open>degree q > 0\<close>] by simp
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2138
      thus ?thesis by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2139
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2140
  moreover have "degree ( q * pcompose p q) > 0 \<Longrightarrow> ?case" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2141
    proof -
64794
6f7391f28197 lead_coeff is more appropriate as abbreviation
haftmann
parents: 64793
diff changeset
  2142
      assume "degree (q * pcompose p q) > 0"
6f7391f28197 lead_coeff is more appropriate as abbreviation
haftmann
parents: 64793
diff changeset
  2143
      then have "degree [:a:] < degree (q * pcompose p q)"
6f7391f28197 lead_coeff is more appropriate as abbreviation
haftmann
parents: 64793
diff changeset
  2144
        by simp
6f7391f28197 lead_coeff is more appropriate as abbreviation
haftmann
parents: 64793
diff changeset
  2145
      then have "lead_coeff ([:a:] + q * p \<circ>\<^sub>p q) = lead_coeff (q * p \<circ>\<^sub>p q)"
6f7391f28197 lead_coeff is more appropriate as abbreviation
haftmann
parents: 64793
diff changeset
  2146
        by (rule lead_coeff_add_le)
6f7391f28197 lead_coeff is more appropriate as abbreviation
haftmann
parents: 64793
diff changeset
  2147
      then have "lead_coeff (pcompose (pCons a p) q) = lead_coeff (q * pcompose p q)"
6f7391f28197 lead_coeff is more appropriate as abbreviation
haftmann
parents: 64793
diff changeset
  2148
        by (simp add: pcompose_pCons)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2149
      also have "... = lead_coeff q * (lead_coeff p * lead_coeff q ^ degree p)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2150
        using pCons.hyps(2) lead_coeff_mult[of q "pcompose p q"] by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2151
      also have "... = lead_coeff p * lead_coeff q ^ (degree p + 1)"
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2152
        by (auto simp: mult_ac)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2153
      finally show ?thesis by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2154
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2155
  ultimately show ?case by blast
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2156
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2157
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2158
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2159
subsection \<open>Shifting polynomials\<close>
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2160
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2161
definition poly_shift :: "nat \<Rightarrow> ('a::zero) poly \<Rightarrow> 'a poly" where
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2162
  "poly_shift n p = Abs_poly (\<lambda>i. coeff p (i + n))"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2163
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2164
lemma nth_default_drop: "nth_default x (drop n xs) m = nth_default x xs (m + n)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2165
  by (auto simp add: nth_default_def add_ac)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2166
  
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2167
lemma nth_default_take: "nth_default x (take n xs) m = (if m < n then nth_default x xs m else x)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2168
  by (auto simp add: nth_default_def add_ac)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2169
  
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2170
lemma coeff_poly_shift: "coeff (poly_shift n p) i = coeff p (i + n)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2171
proof -
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2172
  from MOST_coeff_eq_0[of p] obtain m where "\<forall>k>m. coeff p k = 0" by (auto simp: MOST_nat)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2173
  hence "\<forall>k>m. coeff p (k + n) = 0" by auto
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2174
  hence "\<forall>\<^sub>\<infinity>k. coeff p (k + n) = 0" by (auto simp: MOST_nat)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2175
  thus ?thesis by (simp add: poly_shift_def poly.Abs_poly_inverse)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2176
qed
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2177
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2178
lemma poly_shift_id [simp]: "poly_shift 0 = (\<lambda>x. x)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2179
  by (simp add: poly_eq_iff fun_eq_iff coeff_poly_shift)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2180
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2181
lemma poly_shift_0 [simp]: "poly_shift n 0 = 0"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2182
  by (simp add: poly_eq_iff coeff_poly_shift)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2183
  
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2184
lemma poly_shift_1: "poly_shift n 1 = (if n = 0 then 1 else 0)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2185
  by (simp add: poly_eq_iff coeff_poly_shift)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2186
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2187
lemma poly_shift_monom: "poly_shift n (monom c m) = (if m \<ge> n then monom c (m - n) else 0)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2188
  by (auto simp add: poly_eq_iff coeff_poly_shift)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2189
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2190
lemma coeffs_shift_poly [code abstract]: "coeffs (poly_shift n p) = drop n (coeffs p)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2191
proof (cases "p = 0")
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2192
  case False
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2193
  thus ?thesis
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2194
    by (intro coeffs_eqI)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2195
       (simp_all add: coeff_poly_shift nth_default_drop last_coeffs_not_0 nth_default_coeffs_eq)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2196
qed simp_all
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2197
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2198
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2199
subsection \<open>Truncating polynomials\<close>
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2200
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2201
definition poly_cutoff where
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2202
  "poly_cutoff n p = Abs_poly (\<lambda>k. if k < n then coeff p k else 0)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2203
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2204
lemma coeff_poly_cutoff: "coeff (poly_cutoff n p) k = (if k < n then coeff p k else 0)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2205
  unfolding poly_cutoff_def
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2206
  by (subst poly.Abs_poly_inverse) (auto simp: MOST_nat intro: exI[of _ n])
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2207
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2208
lemma poly_cutoff_0 [simp]: "poly_cutoff n 0 = 0"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2209
  by (simp add: poly_eq_iff coeff_poly_cutoff)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2210
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2211
lemma poly_cutoff_1 [simp]: "poly_cutoff n 1 = (if n = 0 then 0 else 1)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2212
  by (simp add: poly_eq_iff coeff_poly_cutoff)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2213
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2214
lemma coeffs_poly_cutoff [code abstract]: 
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2215
  "coeffs (poly_cutoff n p) = strip_while (op = 0) (take n (coeffs p))"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2216
proof (cases "strip_while (op = 0) (take n (coeffs p)) = []")
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2217
  case True
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2218
  hence "coeff (poly_cutoff n p) k = 0" for k
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2219
    unfolding coeff_poly_cutoff
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2220
    by (auto simp: nth_default_coeffs_eq [symmetric] nth_default_def set_conv_nth)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2221
  hence "poly_cutoff n p = 0" by (simp add: poly_eq_iff)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2222
  thus ?thesis by (subst True) simp_all
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2223
next
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2224
  case False
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2225
  have "no_trailing (op = 0) (strip_while (op = 0) (take n (coeffs p)))" by simp
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2226
  with False have "last (strip_while (op = 0) (take n (coeffs p))) \<noteq> 0" 
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2227
    unfolding no_trailing_unfold by auto
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2228
  thus ?thesis
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2229
    by (intro coeffs_eqI)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2230
       (simp_all add: coeff_poly_cutoff last_coeffs_not_0 nth_default_take nth_default_coeffs_eq)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2231
qed
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2232
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2233
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2234
subsection \<open>Reflecting polynomials\<close>
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2235
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2236
definition reflect_poly where
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2237
  "reflect_poly p = Poly (rev (coeffs p))"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2238
  
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2239
lemma coeffs_reflect_poly [code abstract]:
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2240
    "coeffs (reflect_poly p) = rev (dropWhile (op = 0) (coeffs p))"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2241
  unfolding reflect_poly_def by simp
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2242
  
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2243
lemma reflect_poly_0 [simp]: "reflect_poly 0 = 0"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2244
  by (simp add: reflect_poly_def)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2245
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2246
lemma reflect_poly_1 [simp]: "reflect_poly 1 = 1"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2247
  by (simp add: reflect_poly_def one_poly_def)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2248
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2249
lemma coeff_reflect_poly:
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2250
  "coeff (reflect_poly p) n = (if n > degree p then 0 else coeff p (degree p - n))"
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  2251
  by (cases "p = 0") (auto simp add: reflect_poly_def nth_default_def
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2252
                                     rev_nth degree_eq_length_coeffs coeffs_nth not_less
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2253
                                dest: le_imp_less_Suc)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2254
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2255
lemma coeff_0_reflect_poly_0_iff [simp]: "coeff (reflect_poly p) 0 = 0 \<longleftrightarrow> p = 0"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2256
  by (simp add: coeff_reflect_poly)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2257
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2258
lemma reflect_poly_at_0_eq_0_iff [simp]: "poly (reflect_poly p) 0 = 0 \<longleftrightarrow> p = 0"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2259
  by (simp add: coeff_reflect_poly poly_0_coeff_0)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2260
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2261
lemma reflect_poly_pCons':
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2262
  "p \<noteq> 0 \<Longrightarrow> reflect_poly (pCons c p) = reflect_poly p + monom c (Suc (degree p))"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2263
  by (intro poly_eqI)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2264
     (auto simp: coeff_reflect_poly coeff_pCons not_less Suc_diff_le split: nat.split)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2265
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2266
lemma reflect_poly_const [simp]: "reflect_poly [:a:] = [:a:]"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2267
  by (cases "a = 0") (simp_all add: reflect_poly_def)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2268
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2269
lemma poly_reflect_poly_nz:
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2270
  "(x :: 'a :: field) \<noteq> 0 \<Longrightarrow> poly (reflect_poly p) x = x ^ degree p * poly p (inverse x)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2271
  by (induction rule: pCons_induct) (simp_all add: field_simps reflect_poly_pCons' poly_monom)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2272
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2273
lemma coeff_0_reflect_poly [simp]: "coeff (reflect_poly p) 0 = lead_coeff p"
64794
6f7391f28197 lead_coeff is more appropriate as abbreviation
haftmann
parents: 64793
diff changeset
  2274
  by (simp add: coeff_reflect_poly)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2275
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2276
lemma poly_reflect_poly_0 [simp]: "poly (reflect_poly p) 0 = lead_coeff p"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2277
  by (simp add: poly_0_coeff_0)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2278
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2279
lemma reflect_poly_reflect_poly [simp]: "coeff p 0 \<noteq> 0 \<Longrightarrow> reflect_poly (reflect_poly p) = p"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2280
  by (cases p rule: pCons_cases) (simp add: reflect_poly_def )
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2281
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2282
lemma degree_reflect_poly_le: "degree (reflect_poly p) \<le> degree p"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2283
  by (simp add: degree_eq_length_coeffs coeffs_reflect_poly length_dropWhile_le diff_le_mono)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2284
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2285
lemma reflect_poly_pCons:
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2286
  "a \<noteq> 0 \<Longrightarrow> reflect_poly (pCons a p) = Poly (rev (a # coeffs p))"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2287
  by (subst coeffs_eq_iff) (simp add: coeffs_reflect_poly)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2288
  
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2289
lemma degree_reflect_poly_eq [simp]: "coeff p 0 \<noteq> 0 \<Longrightarrow> degree (reflect_poly p) = degree p"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2290
  by (cases p rule: pCons_cases) (simp add: reflect_poly_pCons degree_eq_length_coeffs)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2291
  
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2292
(* TODO: does this work with zero divisors as well? Probably not. *)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2293
lemma reflect_poly_mult:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2294
  "reflect_poly (p * q) = 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2295
     reflect_poly p * reflect_poly (q :: _ :: {comm_semiring_0,semiring_no_zero_divisors} poly)"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2296
proof (cases "p = 0 \<or> q = 0")
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2297
  case False
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2298
  hence [simp]: "p \<noteq> 0" "q \<noteq> 0" by auto
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2299
  show ?thesis
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2300
  proof (rule poly_eqI)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2301
    fix i :: nat
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2302
    show "coeff (reflect_poly (p * q)) i = coeff (reflect_poly p * reflect_poly q) i"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2303
    proof (cases "i \<le> degree (p * q)")
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2304
      case True
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  2305
      define A where "A = {..i} \<inter> {i - degree q..degree p}"
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  2306
      define B where "B = {..degree p} \<inter> {degree p - i..degree (p*q) - i}"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2307
      let ?f = "\<lambda>j. degree p - j"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2308
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2309
      from True have "coeff (reflect_poly (p * q)) i = coeff (p * q) (degree (p * q) - i)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2310
        by (simp add: coeff_reflect_poly)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2311
      also have "\<dots> = (\<Sum>j\<le>degree (p * q) - i. coeff p j * coeff q (degree (p * q) - i - j))"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2312
        unfolding coeff_mult by simp
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2313
      also have "\<dots> = (\<Sum>j\<in>B. coeff p j * coeff q (degree (p * q) - i - j))"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2314
        by (intro sum.mono_neutral_right) (auto simp: B_def degree_mult_eq not_le coeff_eq_0)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2315
      also from True have "\<dots> = (\<Sum>j\<in>A. coeff p (degree p - j) * coeff q (degree q - (i - j)))"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2316
        by (intro sum.reindex_bij_witness[of _ ?f ?f])
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2317
           (auto simp: A_def B_def degree_mult_eq add_ac)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2318
      also have "\<dots> = (\<Sum>j\<le>i. if j \<in> {i - degree q..degree p} then
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2319
                 coeff p (degree p - j) * coeff q (degree q - (i - j)) else 0)"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2320
        by (subst sum.inter_restrict [symmetric]) (simp_all add: A_def)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2321
       also have "\<dots> = coeff (reflect_poly p * reflect_poly q) i"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2322
          by (fastforce simp: coeff_mult coeff_reflect_poly intro!: sum.cong)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2323
       finally show ?thesis .
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2324
    qed (auto simp: coeff_mult coeff_reflect_poly coeff_eq_0 degree_mult_eq intro!: sum.neutral)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2325
  qed
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2326
qed auto
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2327
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2328
lemma reflect_poly_smult: 
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2329
  "reflect_poly (Polynomial.smult (c::'a::{comm_semiring_0,semiring_no_zero_divisors}) p) = 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2330
     Polynomial.smult c (reflect_poly p)"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2331
  using reflect_poly_mult[of "[:c:]" p] by simp
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2332
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2333
lemma reflect_poly_power:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2334
    "reflect_poly (p ^ n :: 'a :: {comm_semiring_1,semiring_no_zero_divisors} poly) = 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2335
       reflect_poly p ^ n"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2336
  by (induction n) (simp_all add: reflect_poly_mult)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2337
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2338
lemma reflect_poly_prod:
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2339
  "reflect_poly (prod (f :: _ \<Rightarrow> _ :: {comm_semiring_0,semiring_no_zero_divisors} poly) A) = 
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2340
     prod (\<lambda>x. reflect_poly (f x)) A"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2341
  by (cases "finite A", induction rule: finite_induct) (simp_all add: reflect_poly_mult)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2342
63882
018998c00003 renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents: 63649
diff changeset
  2343
lemma reflect_poly_prod_list:
018998c00003 renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents: 63649
diff changeset
  2344
  "reflect_poly (prod_list (xs :: _ :: {comm_semiring_0,semiring_no_zero_divisors} poly list)) = 
018998c00003 renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents: 63649
diff changeset
  2345
     prod_list (map reflect_poly xs)"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2346
  by (induction xs) (simp_all add: reflect_poly_mult)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2347
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2348
lemma reflect_poly_Poly_nz: 
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2349
  "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0 \<Longrightarrow> reflect_poly (Poly xs) = Poly (rev xs)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2350
  unfolding reflect_poly_def coeffs_Poly by simp
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2351
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2352
lemmas reflect_poly_simps = 
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2353
  reflect_poly_0 reflect_poly_1 reflect_poly_const reflect_poly_smult reflect_poly_mult
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2354
  reflect_poly_power reflect_poly_prod reflect_poly_prod_list
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2355
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2356
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2357
subsection \<open>Derivatives\<close>
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2358
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2359
function pderiv :: "('a :: {comm_semiring_1,semiring_no_zero_divisors}) poly \<Rightarrow> 'a poly"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2360
where
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
  2361
  "pderiv (pCons a p) = (if p = 0 then 0 else p + pCons 0 (pderiv p))"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2362
  by (auto intro: pCons_cases)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2363
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2364
termination pderiv
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2365
  by (relation "measure degree") simp_all
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2366
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
  2367
declare pderiv.simps[simp del]
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
  2368
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2369
lemma pderiv_0 [simp]:
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2370
  "pderiv 0 = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2371
  using pderiv.simps [of 0 0] by simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2372
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2373
lemma pderiv_pCons:
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2374
  "pderiv (pCons a p) = p + pCons 0 (pderiv p)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2375
  by (simp add: pderiv.simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2376
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2377
lemma pderiv_1 [simp]: "pderiv 1 = 0" 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2378
  unfolding one_poly_def by (simp add: pderiv_pCons)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2379
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2380
lemma pderiv_of_nat  [simp]: "pderiv (of_nat n) = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2381
  and pderiv_numeral [simp]: "pderiv (numeral m) = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2382
  by (simp_all add: of_nat_poly numeral_poly pderiv_pCons)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2383
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2384
lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2385
  by (induct p arbitrary: n) 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2386
     (auto simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2387
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2388
fun pderiv_coeffs_code 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2389
      :: "('a :: {comm_semiring_1,semiring_no_zero_divisors}) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2390
  "pderiv_coeffs_code f (x # xs) = cCons (f * x) (pderiv_coeffs_code (f+1) xs)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2391
| "pderiv_coeffs_code f [] = []"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2392
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2393
definition pderiv_coeffs :: 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2394
    "'a :: {comm_semiring_1,semiring_no_zero_divisors} list \<Rightarrow> 'a list" where
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2395
  "pderiv_coeffs xs = pderiv_coeffs_code 1 (tl xs)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2396
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2397
(* Efficient code for pderiv contributed by René Thiemann and Akihisa Yamada *)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2398
lemma pderiv_coeffs_code: 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2399
  "nth_default 0 (pderiv_coeffs_code f xs) n = (f + of_nat n) * (nth_default 0 xs n)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2400
proof (induct xs arbitrary: f n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2401
  case (Cons x xs f n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2402
  show ?case 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2403
  proof (cases n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2404
    case 0
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2405
    thus ?thesis by (cases "pderiv_coeffs_code (f + 1) xs = [] \<and> f * x = 0", auto simp: cCons_def)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2406
  next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2407
    case (Suc m) note n = this
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2408
    show ?thesis 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2409
    proof (cases "pderiv_coeffs_code (f + 1) xs = [] \<and> f * x = 0")
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2410
      case False
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2411
      hence "nth_default 0 (pderiv_coeffs_code f (x # xs)) n = 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2412
               nth_default 0 (pderiv_coeffs_code (f + 1) xs) m" 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2413
        by (auto simp: cCons_def n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2414
      also have "\<dots> = (f + of_nat n) * (nth_default 0 xs m)" 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2415
        unfolding Cons by (simp add: n add_ac)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2416
      finally show ?thesis by (simp add: n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2417
    next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2418
      case True
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2419
      {
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2420
        fix g 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2421
        have "pderiv_coeffs_code g xs = [] \<Longrightarrow> g + of_nat m = 0 \<or> nth_default 0 xs m = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2422
        proof (induct xs arbitrary: g m)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2423
          case (Cons x xs g)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2424
          from Cons(2) have empty: "pderiv_coeffs_code (g + 1) xs = []"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2425
                            and g: "(g = 0 \<or> x = 0)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2426
            by (auto simp: cCons_def split: if_splits)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2427
          note IH = Cons(1)[OF empty]
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2428
          from IH[of m] IH[of "m - 1"] g
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2429
          show ?case by (cases m, auto simp: field_simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2430
        qed simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2431
      } note empty = this
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2432
      from True have "nth_default 0 (pderiv_coeffs_code f (x # xs)) n = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2433
        by (auto simp: cCons_def n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2434
      moreover have "(f + of_nat n) * nth_default 0 (x # xs) n = 0" using True
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2435
        by (simp add: n, insert empty[of "f+1"], auto simp: field_simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2436
      ultimately show ?thesis by simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2437
    qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2438
  qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2439
qed simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2440
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2441
lemma map_upt_Suc: "map f [0 ..< Suc n] = f 0 # map (\<lambda> i. f (Suc i)) [0 ..< n]"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2442
  by (induct n arbitrary: f, auto)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2443
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2444
lemma coeffs_pderiv_code [code abstract]:
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2445
  "coeffs (pderiv p) = pderiv_coeffs (coeffs p)" unfolding pderiv_coeffs_def
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2446
proof (rule coeffs_eqI, unfold pderiv_coeffs_code coeff_pderiv, goal_cases)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2447
  case (1 n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2448
  have id: "coeff p (Suc n) = nth_default 0 (map (\<lambda>i. coeff p (Suc i)) [0..<degree p]) n"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2449
    by (cases "n < degree p", auto simp: nth_default_def coeff_eq_0)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2450
  show ?case unfolding coeffs_def map_upt_Suc by (auto simp: id)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2451
next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2452
  case 2
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2453
  obtain n xs where id: "tl (coeffs p) = xs" "(1 :: 'a) = n" by auto
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2454
  from 2 show ?case
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2455
    unfolding id by (induct xs arbitrary: n, auto simp: cCons_def)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2456
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2457
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2458
context
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2459
  assumes "SORT_CONSTRAINT('a::{comm_semiring_1,semiring_no_zero_divisors, semiring_char_0})"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2460
begin
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2461
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2462
lemma pderiv_eq_0_iff: 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2463
  "pderiv (p :: 'a poly) = 0 \<longleftrightarrow> degree p = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2464
  apply (rule iffI)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2465
  apply (cases p, simp)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2466
  apply (simp add: poly_eq_iff coeff_pderiv del: of_nat_Suc)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2467
  apply (simp add: poly_eq_iff coeff_pderiv coeff_eq_0)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2468
  done
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2469
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2470
lemma degree_pderiv: "degree (pderiv (p :: 'a poly)) = degree p - 1"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2471
  apply (rule order_antisym [OF degree_le])
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2472
  apply (simp add: coeff_pderiv coeff_eq_0)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2473
  apply (cases "degree p", simp)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2474
  apply (rule le_degree)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2475
  apply (simp add: coeff_pderiv del: of_nat_Suc)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2476
  apply (metis degree_0 leading_coeff_0_iff nat.distinct(1))
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2477
  done
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2478
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2479
lemma not_dvd_pderiv: 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2480
  assumes "degree (p :: 'a poly) \<noteq> 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2481
  shows "\<not> p dvd pderiv p"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2482
proof
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2483
  assume dvd: "p dvd pderiv p"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2484
  then obtain q where p: "pderiv p = p * q" unfolding dvd_def by auto
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2485
  from dvd have le: "degree p \<le> degree (pderiv p)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2486
    by (simp add: assms dvd_imp_degree_le pderiv_eq_0_iff)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2487
  from this[unfolded degree_pderiv] assms show False by auto
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2488
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2489
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2490
lemma dvd_pderiv_iff [simp]: "(p :: 'a poly) dvd pderiv p \<longleftrightarrow> degree p = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2491
  using not_dvd_pderiv[of p] by (auto simp: pderiv_eq_0_iff [symmetric])
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2492
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2493
end
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2494
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2495
lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2496
by (simp add: pderiv_pCons)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2497
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2498
lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2499
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2500
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2501
lemma pderiv_minus: "pderiv (- p :: 'a :: idom poly) = - pderiv p"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2502
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2503
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2504
lemma pderiv_diff: "pderiv ((p :: _ :: idom poly) - q) = pderiv p - pderiv q"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2505
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2506
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2507
lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2508
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2509
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2510
lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2511
by (induct p) (auto simp: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2512
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2513
lemma pderiv_power_Suc:
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2514
  "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2515
apply (induct n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2516
apply simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2517
apply (subst power_Suc)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2518
apply (subst pderiv_mult)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2519
apply (erule ssubst)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2520
apply (simp only: of_nat_Suc smult_add_left smult_1_left)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2521
apply (simp add: algebra_simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2522
done
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2523
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2524
lemma pderiv_prod: "pderiv (prod f (as)) = 
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2525
  (\<Sum>a \<in> as. prod f (as - {a}) * pderiv (f a))"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2526
proof (induct as rule: infinite_finite_induct)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2527
  case (insert a as)
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2528
  hence id: "prod f (insert a as) = f a * prod f as" 
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2529
    "\<And> g. sum g (insert a as) = g a + sum g as"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2530
    "insert a as - {a} = as"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2531
    by auto
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2532
  {
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2533
    fix b
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2534
    assume "b \<in> as"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2535
    hence id2: "insert a as - {b} = insert a (as - {b})" using \<open>a \<notin> as\<close> by auto
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2536
    have "prod f (insert a as - {b}) = f a * prod f (as - {b})"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2537
      unfolding id2
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2538
      by (subst prod.insert, insert insert, auto)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2539
  } note id2 = this
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2540
  show ?case
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2541
    unfolding id pderiv_mult insert(3) sum_distrib_left
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2542
    by (auto simp add: ac_simps id2 intro!: sum.cong)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2543
qed auto
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2544
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2545
lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2546
by (rule DERIV_cong, rule DERIV_pow, simp)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2547
declare DERIV_pow2 [simp] DERIV_pow [simp]
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2548
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2549
lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2550
by (rule DERIV_cong, rule DERIV_add, auto)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2551
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2552
lemma poly_DERIV [simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2553
  by (induct p, auto intro!: derivative_eq_intros simp add: pderiv_pCons)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2554
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2555
lemma continuous_on_poly [continuous_intros]: 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2556
  fixes p :: "'a :: {real_normed_field} poly"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2557
  assumes "continuous_on A f"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2558
  shows   "continuous_on A (\<lambda>x. poly p (f x))"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2559
proof -
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2560
  have "continuous_on A (\<lambda>x. (\<Sum>i\<le>degree p. (f x) ^ i * coeff p i))" 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2561
    by (intro continuous_intros assms)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2562
  also have "\<dots> = (\<lambda>x. poly p (f x))" by (intro ext) (simp add: poly_altdef mult_ac)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2563
  finally show ?thesis .
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2564
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2565
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2566
text\<open>Consequences of the derivative theorem above\<close>
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2567
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2568
lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (at x::real filter)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2569
apply (simp add: real_differentiable_def)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2570
apply (blast intro: poly_DERIV)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2571
done
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2572
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2573
lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2574
by (rule poly_DERIV [THEN DERIV_isCont])
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2575
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2576
lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2577
      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2578
using IVT_objl [of "poly p" a 0 b]
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2579
by (auto simp add: order_le_less)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2580
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2581
lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2582
      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2583
by (insert poly_IVT_pos [where p = "- p" ]) simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2584
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2585
lemma poly_IVT:
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2586
  fixes p::"real poly"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2587
  assumes "a<b" and "poly p a * poly p b < 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2588
  shows "\<exists>x>a. x < b \<and> poly p x = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2589
by (metis assms(1) assms(2) less_not_sym mult_less_0_iff poly_IVT_neg poly_IVT_pos)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2590
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2591
lemma poly_MVT: "(a::real) < b ==>
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2592
     \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2593
using MVT [of a b "poly p"]
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2594
apply auto
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2595
apply (rule_tac x = z in exI)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2596
apply (auto simp add: mult_left_cancel poly_DERIV [THEN DERIV_unique])
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2597
done
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2598
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2599
lemma poly_MVT':
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2600
  assumes "{min a b..max a b} \<subseteq> A"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2601
  shows   "\<exists>x\<in>A. poly p b - poly p a = (b - a) * poly (pderiv p) (x::real)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2602
proof (cases a b rule: linorder_cases)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2603
  case less
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2604
  from poly_MVT[OF less, of p] guess x by (elim exE conjE)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2605
  thus ?thesis by (intro bexI[of _ x]) (auto intro!: subsetD[OF assms])
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2606
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2607
next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2608
  case greater
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2609
  from poly_MVT[OF greater, of p] guess x by (elim exE conjE)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2610
  thus ?thesis by (intro bexI[of _ x]) (auto simp: algebra_simps intro!: subsetD[OF assms])
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2611
qed (insert assms, auto)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2612
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2613
lemma poly_pinfty_gt_lc:
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2614
  fixes p :: "real poly"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2615
  assumes "lead_coeff p > 0" 
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2616
  shows "\<exists> n. \<forall> x \<ge> n. poly p x \<ge> lead_coeff p"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2617
  using assms
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2618
proof (induct p)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2619
  case 0
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2620
  then show ?case by auto
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2621
next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2622
  case (pCons a p)
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2623
  from this(1) consider "a \<noteq> 0" "p = 0" | "p \<noteq> 0" by auto
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2624
  then show ?case
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2625
  proof cases
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2626
    case 1
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2627
    then show ?thesis by auto
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2628
  next
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2629
    case 2
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2630
    with pCons obtain n1 where gte_lcoeff: "\<forall>x\<ge>n1. lead_coeff p \<le> poly p x"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2631
      by auto
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2632
    from pCons(3) \<open>p \<noteq> 0\<close> have gt_0: "lead_coeff p > 0" by auto
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2633
    define n where "n = max n1 (1 + \<bar>a\<bar> / lead_coeff p)"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2634
    have "lead_coeff (pCons a p) \<le> poly (pCons a p) x" if "n \<le> x" for x
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2635
    proof -
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2636
      from gte_lcoeff that have "lead_coeff p \<le> poly p x"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2637
        by (auto simp: n_def)
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2638
      with gt_0 have "\<bar>a\<bar> / lead_coeff p \<ge> \<bar>a\<bar> / poly p x" and "poly p x > 0"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2639
        by (auto intro: frac_le)
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2640
      with \<open>n\<le>x\<close>[unfolded n_def] have "x \<ge> 1 + \<bar>a\<bar> / poly p x"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2641
        by auto
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2642
      with \<open>lead_coeff p \<le> poly p x\<close> \<open>poly p x > 0\<close> \<open>p \<noteq> 0\<close>
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2643
      show "lead_coeff (pCons a p) \<le> poly (pCons a p) x"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2644
        by (auto simp: field_simps)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2645
    qed
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2646
    then show ?thesis by blast
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2647
  qed
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2648
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2649
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2650
lemma lemma_order_pderiv1:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2651
  "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2652
    smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2653
apply (simp only: pderiv_mult pderiv_power_Suc)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2654
apply (simp del: power_Suc of_nat_Suc add: pderiv_pCons)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2655
done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2656
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2657
lemma lemma_order_pderiv:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2658
  fixes p :: "'a :: field_char_0 poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2659
  assumes n: "0 < n" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2660
      and pd: "pderiv p \<noteq> 0" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2661
      and pe: "p = [:- a, 1:] ^ n * q" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2662
      and nd: "~ [:- a, 1:] dvd q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2663
    shows "n = Suc (order a (pderiv p))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2664
using n 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2665
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2666
  have "pderiv ([:- a, 1:] ^ n * q) \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2667
    using assms by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2668
  obtain n' where "n = Suc n'" "0 < Suc n'" "pderiv ([:- a, 1:] ^ Suc n' * q) \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2669
    using assms by (cases n) auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2670
  have *: "!!k l. k dvd k * pderiv q + smult (of_nat (Suc n')) l \<Longrightarrow> k dvd l"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2671
    by (auto simp del: of_nat_Suc simp: dvd_add_right_iff dvd_smult_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2672
  have "n' = order a (pderiv ([:- a, 1:] ^ Suc n' * q))" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2673
  proof (rule order_unique_lemma)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2674
    show "[:- a, 1:] ^ n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2675
      apply (subst lemma_order_pderiv1)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2676
      apply (rule dvd_add)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2677
      apply (metis dvdI dvd_mult2 power_Suc2)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2678
      apply (metis dvd_smult dvd_triv_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2679
      done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2680
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2681
    show "\<not> [:- a, 1:] ^ Suc n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2682
     apply (subst lemma_order_pderiv1)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2683
     by (metis * nd dvd_mult_cancel_right power_not_zero pCons_eq_0_iff power_Suc zero_neq_one)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2684
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2685
  then show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2686
    by (metis \<open>n = Suc n'\<close> pe)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2687
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2688
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2689
lemma order_pderiv:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2690
  "\<lbrakk>pderiv p \<noteq> 0; order a (p :: 'a :: field_char_0 poly) \<noteq> 0\<rbrakk> \<Longrightarrow>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2691
     (order a p = Suc (order a (pderiv p)))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2692
apply (case_tac "p = 0", simp)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2693
apply (drule_tac a = a and p = p in order_decomp)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2694
using neq0_conv
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2695
apply (blast intro: lemma_order_pderiv)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2696
done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2697
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2698
lemma poly_squarefree_decomp_order:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2699
  assumes "pderiv (p :: 'a :: field_char_0 poly) \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2700
  and p: "p = q * d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2701
  and p': "pderiv p = e * d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2702
  and d: "d = r * p + s * pderiv p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2703
  shows "order a q = (if order a p = 0 then 0 else 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2704
proof (rule classical)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2705
  assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2706
  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2707
  with p have "order a p = order a q + order a d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2708
    by (simp add: order_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2709
  with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2710
  have "order a (pderiv p) = order a e + order a d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2711
    using \<open>pderiv p \<noteq> 0\<close> \<open>pderiv p = e * d\<close> by (simp add: order_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2712
  have "order a p = Suc (order a (pderiv p))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2713
    using \<open>pderiv p \<noteq> 0\<close> \<open>order a p \<noteq> 0\<close> by (rule order_pderiv)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2714
  have "d \<noteq> 0" using \<open>p \<noteq> 0\<close> \<open>p = q * d\<close> by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2715
  have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2716
    apply (simp add: d)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2717
    apply (rule dvd_add)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2718
    apply (rule dvd_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2719
    apply (simp add: order_divides \<open>p \<noteq> 0\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2720
           \<open>order a p = Suc (order a (pderiv p))\<close>)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2721
    apply (rule dvd_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2722
    apply (simp add: order_divides)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2723
    done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2724
  then have "order a (pderiv p) \<le> order a d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2725
    using \<open>d \<noteq> 0\<close> by (simp add: order_divides)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2726
  show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2727
    using \<open>order a p = order a q + order a d\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2728
    using \<open>order a (pderiv p) = order a e + order a d\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2729
    using \<open>order a p = Suc (order a (pderiv p))\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2730
    using \<open>order a (pderiv p) \<le> order a d\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2731
    by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2732
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2733
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2734
lemma poly_squarefree_decomp_order2: 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2735
     "\<lbrakk>pderiv p \<noteq> (0 :: 'a :: field_char_0 poly);
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2736
       p = q * d;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2737
       pderiv p = e * d;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2738
       d = r * p + s * pderiv p
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2739
      \<rbrakk> \<Longrightarrow> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2740
by (blast intro: poly_squarefree_decomp_order)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2741
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2742
lemma order_pderiv2: 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2743
  "\<lbrakk>pderiv p \<noteq> 0; order a (p :: 'a :: field_char_0 poly) \<noteq> 0\<rbrakk>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2744
      \<Longrightarrow> (order a (pderiv p) = n) = (order a p = Suc n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2745
by (auto dest: order_pderiv)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2746
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2747
definition rsquarefree :: "'a::idom poly \<Rightarrow> bool"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2748
  where "rsquarefree p \<longleftrightarrow> p \<noteq> 0 \<and> (\<forall>a. order a p = 0 \<or> order a p = 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2749
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2750
lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h :: 'a :: {semidom,semiring_char_0}:]"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2751
  by (cases p) (auto simp: pderiv_eq_0_iff split: if_splits)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2752
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2753
lemma rsquarefree_roots:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2754
  fixes p :: "'a :: field_char_0 poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2755
  shows "rsquarefree p = (\<forall>a. \<not>(poly p a = 0 \<and> poly (pderiv p) a = 0))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2756
apply (simp add: rsquarefree_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2757
apply (case_tac "p = 0", simp, simp)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2758
apply (case_tac "pderiv p = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2759
apply simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2760
apply (drule pderiv_iszero, clarsimp)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2761
apply (metis coeff_0 coeff_pCons_0 degree_pCons_0 le0 le_antisym order_degree)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2762
apply (force simp add: order_root order_pderiv2)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2763
  done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2764
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2765
lemma poly_squarefree_decomp:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2766
  assumes "pderiv (p :: 'a :: field_char_0 poly) \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2767
    and "p = q * d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2768
    and "pderiv p = e * d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2769
    and "d = r * p + s * pderiv p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2770
  shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2771
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2772
  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2773
  with \<open>p = q * d\<close> have "q \<noteq> 0" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2774
  have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2775
    using assms by (rule poly_squarefree_decomp_order2)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2776
  with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2777
    by (simp add: rsquarefree_def order_root)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2778
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2779
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2780
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2781
subsection \<open>Algebraic numbers\<close>
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2782
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2783
text \<open>
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2784
  Algebraic numbers can be defined in two equivalent ways: all real numbers that are 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2785
  roots of rational polynomials or of integer polynomials. The Algebraic-Numbers AFP entry 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2786
  uses the rational definition, but we need the integer definition.
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2787
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2788
  The equivalence is obvious since any rational polynomial can be multiplied with the 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2789
  LCM of its coefficients, yielding an integer polynomial with the same roots.
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2790
\<close>
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2791
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2792
definition algebraic :: "'a :: field_char_0 \<Rightarrow> bool" where
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2793
  "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<int>) \<and> p \<noteq> 0 \<and> poly p x = 0)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2794
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2795
lemma algebraicI:
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2796
  assumes "\<And>i. coeff p i \<in> \<int>" "p \<noteq> 0" "poly p x = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2797
  shows   "algebraic x"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2798
  using assms unfolding algebraic_def by blast
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2799
  
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2800
lemma algebraicE:
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2801
  assumes "algebraic x"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2802
  obtains p where "\<And>i. coeff p i \<in> \<int>" "p \<noteq> 0" "poly p x = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2803
  using assms unfolding algebraic_def by blast
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2804
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2805
lemma algebraic_altdef:
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2806
  fixes p :: "'a :: field_char_0 poly"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2807
  shows "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2808
proof safe
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2809
  fix p assume rat: "\<forall>i. coeff p i \<in> \<rat>" and root: "poly p x = 0" and nz: "p \<noteq> 0"
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2810
  define cs where "cs = coeffs p"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2811
  from rat have "\<forall>c\<in>range (coeff p). \<exists>c'. c = of_rat c'" unfolding Rats_def by blast
63060
293ede07b775 some uses of 'obtain' with structure statement;
wenzelm
parents: 63040
diff changeset
  2812
  then obtain f where f: "coeff p i = of_rat (f (coeff p i))" for i
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2813
    by (subst (asm) bchoice_iff) blast
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2814
  define cs' where "cs' = map (quotient_of \<circ> f) (coeffs p)"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2815
  define d where "d = Lcm (set (map snd cs'))"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2816
  define p' where "p' = smult (of_int d) p"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2817
  
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2818
  have "\<forall>n. coeff p' n \<in> \<int>"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2819
  proof
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2820
    fix n :: nat
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2821
    show "coeff p' n \<in> \<int>"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2822
    proof (cases "n \<le> degree p")
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2823
      case True
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2824
      define c where "c = coeff p n"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2825
      define a where "a = fst (quotient_of (f (coeff p n)))"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2826
      define b where "b = snd (quotient_of (f (coeff p n)))"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2827
      have b_pos: "b > 0" unfolding b_def using quotient_of_denom_pos' by simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2828
      have "coeff p' n = of_int d * coeff p n" by (simp add: p'_def)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2829
      also have "coeff p n = of_rat (of_int a / of_int b)" unfolding a_def b_def
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2830
        by (subst quotient_of_div [of "f (coeff p n)", symmetric])
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2831
           (simp_all add: f [symmetric])
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2832
      also have "of_int d * \<dots> = of_rat (of_int (a*d) / of_int b)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2833
        by (simp add: of_rat_mult of_rat_divide)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2834
      also from nz True have "b \<in> snd ` set cs'" unfolding cs'_def
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2835
        by (force simp: o_def b_def coeffs_def simp del: upt_Suc)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2836
      hence "b dvd (a * d)" unfolding d_def by simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2837
      hence "of_int (a * d) / of_int b \<in> (\<int> :: rat set)"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2838
        by (rule of_int_divide_in_Ints)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2839
      hence "of_rat (of_int (a * d) / of_int b) \<in> \<int>" by (elim Ints_cases) auto
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2840
      finally show ?thesis .
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2841
    qed (auto simp: p'_def not_le coeff_eq_0)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2842
  qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2843
  
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2844
  moreover have "set (map snd cs') \<subseteq> {0<..}"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2845
    unfolding cs'_def using quotient_of_denom_pos' by (auto simp: coeffs_def simp del: upt_Suc) 
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2846
  hence "d \<noteq> 0" unfolding d_def by (induction cs') simp_all
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2847
  with nz have "p' \<noteq> 0" by (simp add: p'_def)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2848
  moreover from root have "poly p' x = 0" by (simp add: p'_def)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2849
  ultimately show "algebraic x" unfolding algebraic_def by blast
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2850
next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2851
  assume "algebraic x"
63060
293ede07b775 some uses of 'obtain' with structure statement;
wenzelm
parents: 63040
diff changeset
  2852
  then obtain p where p: "coeff p i \<in> \<int>" "poly p x = 0" "p \<noteq> 0" for i
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2853
    by (force simp: algebraic_def)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2854
  moreover have "coeff p i \<in> \<int> \<Longrightarrow> coeff p i \<in> \<rat>" for i by (elim Ints_cases) simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2855
  ultimately show  "(\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0)" by auto
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2856
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2857
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2858
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2859
subsection \<open>Content and primitive part of a polynomial\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2860
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2861
definition content :: "('a :: semiring_gcd poly) \<Rightarrow> 'a" where
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2862
  "content p = gcd_list (coeffs p)"
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2863
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2864
lemma content_eq_fold_coeffs [code]:
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2865
  "content p = fold_coeffs gcd p 0"
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2866
  by (simp add: content_def Gcd_fin.set_eq_fold fold_coeffs_def foldr_fold fun_eq_iff ac_simps)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2867
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2868
lemma content_0 [simp]: "content 0 = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2869
  by (simp add: content_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2870
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2871
lemma content_1 [simp]: "content 1 = 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2872
  by (simp add: content_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2873
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2874
lemma content_const [simp]: "content [:c:] = normalize c"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2875
  by (simp add: content_def cCons_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2876
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2877
lemma const_poly_dvd_iff_dvd_content:
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2878
  fixes c :: "'a :: semiring_gcd"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2879
  shows "[:c:] dvd p \<longleftrightarrow> c dvd content p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2880
proof (cases "p = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2881
  case [simp]: False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2882
  have "[:c:] dvd p \<longleftrightarrow> (\<forall>n. c dvd coeff p n)" by (rule const_poly_dvd_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2883
  also have "\<dots> \<longleftrightarrow> (\<forall>a\<in>set (coeffs p). c dvd a)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2884
  proof safe
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2885
    fix n :: nat assume "\<forall>a\<in>set (coeffs p). c dvd a"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2886
    thus "c dvd coeff p n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2887
      by (cases "n \<le> degree p") (auto simp: coeff_eq_0 coeffs_def split: if_splits)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2888
  qed (auto simp: coeffs_def simp del: upt_Suc split: if_splits)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2889
  also have "\<dots> \<longleftrightarrow> c dvd content p"
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2890
    by (simp add: content_def dvd_Gcd_fin_iff dvd_mult_unit_iff)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2891
  finally show ?thesis .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2892
qed simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2893
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2894
lemma content_dvd [simp]: "[:content p:] dvd p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2895
  by (subst const_poly_dvd_iff_dvd_content) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2896
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2897
lemma content_dvd_coeff [simp]: "content p dvd coeff p n"
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2898
proof (cases "p = 0")
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2899
  case True
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2900
  then show ?thesis
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2901
    by simp
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2902
next
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2903
  case False
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2904
  then show ?thesis
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2905
    by (cases "n \<le> degree p")
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2906
      (auto simp add: content_def not_le coeff_eq_0 coeff_in_coeffs intro: Gcd_fin_dvd)
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2907
qed
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2908
  
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2909
lemma content_dvd_coeffs: "c \<in> set (coeffs p) \<Longrightarrow> content p dvd c"
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2910
  by (simp add: content_def Gcd_fin_dvd)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2911
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2912
lemma normalize_content [simp]: "normalize (content p) = content p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2913
  by (simp add: content_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2914
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2915
lemma is_unit_content_iff [simp]: "is_unit (content p) \<longleftrightarrow> content p = 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2916
proof
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2917
  assume "is_unit (content p)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2918
  hence "normalize (content p) = 1" by (simp add: is_unit_normalize del: normalize_content)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2919
  thus "content p = 1" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2920
qed auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2921
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2922
lemma content_smult [simp]: "content (smult c p) = normalize c * content p"
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2923
  by (simp add: content_def coeffs_smult Gcd_fin_mult)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2924
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2925
lemma content_eq_zero_iff [simp]: "content p = 0 \<longleftrightarrow> p = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2926
  by (auto simp: content_def simp: poly_eq_iff coeffs_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2927
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2928
definition primitive_part :: "'a :: semiring_gcd poly \<Rightarrow> 'a poly" where
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2929
  "primitive_part p = map_poly (\<lambda>x. x div content p) p"
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2930
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2931
lemma primitive_part_0 [simp]: "primitive_part 0 = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2932
  by (simp add: primitive_part_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2933
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2934
lemma content_times_primitive_part [simp]:
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2935
  fixes p :: "'a :: semiring_gcd poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2936
  shows "smult (content p) (primitive_part p) = p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2937
proof (cases "p = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2938
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2939
  thus ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2940
  unfolding primitive_part_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2941
  by (auto simp: smult_conv_map_poly map_poly_map_poly o_def content_dvd_coeffs 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2942
           intro: map_poly_idI)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2943
qed simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2944
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2945
lemma primitive_part_eq_0_iff [simp]: "primitive_part p = 0 \<longleftrightarrow> p = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2946
proof (cases "p = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2947
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2948
  hence "primitive_part p = map_poly (\<lambda>x. x div content p) p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2949
    by (simp add:  primitive_part_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2950
  also from False have "\<dots> = 0 \<longleftrightarrow> p = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2951
    by (intro map_poly_eq_0_iff) (auto simp: dvd_div_eq_0_iff content_dvd_coeffs)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2952
  finally show ?thesis using False by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2953
qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2954
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2955
lemma content_primitive_part [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2956
  assumes "p \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2957
  shows   "content (primitive_part p) = 1"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2958
proof -
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2959
  have "p = smult (content p) (primitive_part p)" by simp
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2960
  also have "content \<dots> = content (primitive_part p) * content p" 
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2961
    by (simp del: content_times_primitive_part add: ac_simps)
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2962
  finally have "1 * content p = content (primitive_part p) * content p"
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2963
    by simp
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2964
  then have "1 * content p div content p = content (primitive_part p) * content p div content p"
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2965
    by simp
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2966
  with assms show ?thesis
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2967
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2968
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2969
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2970
lemma content_decompose:
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
  2971
  fixes p :: "'a :: semiring_gcd poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2972
  obtains p' where "p = smult (content p) p'" "content p' = 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2973
proof (cases "p = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2974
  case True
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2975
  thus ?thesis by (intro that[of 1]) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2976
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2977
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2978
  from content_dvd[of p] obtain r where r: "p = [:content p:] * r" by (erule dvdE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2979
  have "content p * 1 = content p * content r" by (subst r) simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2980
  with False have "content r = 1" by (subst (asm) mult_left_cancel) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2981
  with r show ?thesis by (intro that[of r]) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2982
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2983
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2984
lemma content_dvd_contentI [intro]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2985
  "p dvd q \<Longrightarrow> content p dvd content q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2986
  using const_poly_dvd_iff_dvd_content content_dvd dvd_trans by blast
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2987
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2988
lemma primitive_part_const_poly [simp]: "primitive_part [:x:] = [:unit_factor x:]"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2989
  by (simp add: primitive_part_def map_poly_pCons)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2990
 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2991
lemma primitive_part_prim: "content p = 1 \<Longrightarrow> primitive_part p = p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2992
  by (auto simp: primitive_part_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2993
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2994
lemma degree_primitive_part [simp]: "degree (primitive_part p) = degree p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2995
proof (cases "p = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2996
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2997
  have "p = smult (content p) (primitive_part p)" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2998
  also from False have "degree \<dots> = degree (primitive_part p)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2999
    by (subst degree_smult_eq) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3000
  finally show ?thesis ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3001
qed simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3002
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3003
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3004
subsection \<open>Division of polynomials\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3005
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3006
subsubsection \<open>Division in general\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3007
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3008
instantiation poly :: (idom_divide) idom_divide
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3009
begin
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3010
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3011
fun divide_poly_main :: "'a \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3012
  \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a poly" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3013
  "divide_poly_main lc q r d dr (Suc n) = (let cr = coeff r dr; a = cr div lc; mon = monom a n in 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3014
     if False \<or> a * lc = cr then (* False \<or> is only because of problem in function-package *)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3015
     divide_poly_main 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3016
       lc 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3017
       (q + mon) 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3018
       (r - mon * d) 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3019
       d (dr - 1) n else 0)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3020
| "divide_poly_main lc q r d dr 0 = q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3021
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3022
definition divide_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3023
  "divide_poly f g = (if g = 0 then 0 else
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3024
     divide_poly_main (coeff g (degree g)) 0 f g (degree f) (1 + length (coeffs f) - length (coeffs g)))" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3025
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3026
lemma divide_poly_main:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3027
  assumes d: "d \<noteq> 0" "lc = coeff d (degree d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3028
    and *: "degree (d * r) \<le> dr" "divide_poly_main lc q (d * r) d dr n = q'" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3029
    "n = 1 + dr - degree d \<or> dr = 0 \<and> n = 0 \<and> d * r = 0" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3030
  shows "q' = q + r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3031
  using *
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3032
proof (induct n arbitrary: q r dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3033
  case (Suc n q r dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3034
  let ?rr = "d * r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3035
  let ?a = "coeff ?rr dr"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3036
  let ?qq = "?a div lc"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3037
  define b where [simp]: "b = monom ?qq n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3038
  let ?rrr =  "d * (r - b)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3039
  let ?qqq = "q + b"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3040
  note res = Suc(3)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3041
  have dr: "dr = n + degree d" using Suc(4) by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3042
  have lc: "lc \<noteq> 0" using d by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3043
  have "coeff (b * d) dr = coeff b n * coeff d (degree d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3044
  proof (cases "?qq = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3045
    case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3046
    hence n: "n = degree b" by (simp add: degree_monom_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3047
    show ?thesis unfolding n dr by (simp add: coeff_mult_degree_sum)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3048
  qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3049
  also have "\<dots> = lc * coeff b n" unfolding d by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3050
  finally have c2: "coeff (b * d) dr = lc * coeff b n" .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3051
  have rrr: "?rrr = ?rr - b * d" by (simp add: field_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3052
  have c1: "coeff (d * r) dr = lc * coeff r n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3053
  proof (cases "degree r = n")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3054
    case True
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3055
    thus ?thesis using Suc(2) unfolding dr using coeff_mult_degree_sum[of d r] d by (auto simp: ac_simps)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3056
  next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3057
    case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3058
    have "degree r \<le> n" using dr Suc(2) by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3059
      (metis add.commute add_le_cancel_left d(1) degree_0 degree_mult_eq diff_is_0_eq diff_zero le_cases)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3060
    with False have r_n: "degree r < n" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3061
    hence right: "lc * coeff r n = 0" by (simp add: coeff_eq_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3062
    have "coeff (d * r) dr = coeff (d * r) (degree d + n)" unfolding dr by (simp add: ac_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3063
    also have "\<dots> = 0" using r_n
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3064
      by (metis False Suc.prems(1) add.commute add_left_imp_eq coeff_degree_mult coeff_eq_0 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3065
        coeff_mult_degree_sum degree_mult_le dr le_eq_less_or_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3066
    finally show ?thesis unfolding right .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3067
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3068
  have c0: "coeff ?rrr dr = 0" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3069
    and id: "lc * (coeff (d * r) dr div lc) = coeff (d * r) dr" unfolding rrr coeff_diff c2
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3070
    unfolding b_def coeff_monom coeff_smult c1 using lc by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3071
  from res[unfolded divide_poly_main.simps[of lc q] Let_def] id
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3072
  have res: "divide_poly_main lc ?qqq ?rrr d (dr - 1) n = q'" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3073
    by (simp del: divide_poly_main.simps add: field_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3074
  note IH = Suc(1)[OF _ res] 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3075
  have dr: "dr = n + degree d" using Suc(4) by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3076
  have deg_rr: "degree ?rr \<le> dr" using Suc(2) by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3077
  have deg_bd: "degree (b * d) \<le> dr"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3078
    unfolding dr b_def by (rule order.trans[OF degree_mult_le], auto simp: degree_monom_le)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3079
  have "degree ?rrr \<le> dr" unfolding rrr by (rule degree_diff_le[OF deg_rr deg_bd])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3080
  with c0 have deg_rrr: "degree ?rrr \<le> (dr - 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3081
    by (rule coeff_0_degree_minus_1)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3082
  have "n = 1 + (dr - 1) - degree d \<or> dr - 1 = 0 \<and> n = 0 \<and> ?rrr = 0"  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3083
  proof (cases dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3084
    case 0
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3085
    with Suc(4) have 0: "dr = 0" "n = 0" "degree d = 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3086
    with deg_rrr have "degree ?rrr = 0" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3087
    from degree_eq_zeroE[OF this] obtain a where rrr: "?rrr = [:a:]" by metis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3088
    show ?thesis unfolding 0 using c0 unfolding rrr 0 by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3089
  qed (insert Suc(4), auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3090
  note IH = IH[OF deg_rrr this]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3091
  show ?case using IH by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3092
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3093
  case (0 q r dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3094
  show ?case 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3095
  proof (cases "r = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3096
    case True
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3097
    thus ?thesis using 0 by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3098
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3099
    case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3100
    have "degree (d * r) = degree d + degree r" using d False 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3101
      by (subst degree_mult_eq, auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3102
    thus ?thesis using 0 d by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3103
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3104
qed 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3105
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3106
lemma divide_poly_main_0: "divide_poly_main 0 0 r d dr n = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3107
proof (induct n arbitrary: r d dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3108
  case (Suc n r d dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3109
  show ?case unfolding divide_poly_main.simps[of _ _ r] Let_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3110
    by (simp add: Suc del: divide_poly_main.simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3111
qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3112
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3113
lemma divide_poly:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3114
  assumes g: "g \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3115
  shows "(f * g) div g = (f :: 'a poly)" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3116
proof - 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3117
  have "divide_poly_main (coeff g (degree g)) 0 (g * f) g (degree (g * f)) (1 + length (coeffs (g * f)) - length (coeffs  g)) 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3118
    = (f * g) div g" unfolding divide_poly_def Let_def by (simp add: ac_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3119
  note main = divide_poly_main[OF g refl le_refl this]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3120
  {
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3121
    fix f :: "'a poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3122
    assume "f \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3123
    hence "length (coeffs f) = Suc (degree f)" unfolding degree_eq_length_coeffs by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3124
  } note len = this
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3125
  have "(f * g) div g = 0 + f"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3126
  proof (rule main, goal_cases)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3127
    case 1
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3128
    show ?case
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3129
    proof (cases "f = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3130
      case True
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3131
      with g show ?thesis by (auto simp: degree_eq_length_coeffs)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3132
    next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3133
      case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3134
      with g have fg: "g * f \<noteq> 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3135
      show ?thesis unfolding len[OF fg] len[OF g] by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3136
    qed
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3137
  qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3138
  thus ?thesis by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3139
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3140
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3141
lemma divide_poly_0: "f div 0 = (0 :: 'a poly)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3142
  by (simp add: divide_poly_def Let_def divide_poly_main_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3143
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3144
instance
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3145
  by standard (auto simp: divide_poly divide_poly_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3146
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3147
end
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3148
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3149
instance poly :: (idom_divide) algebraic_semidom ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3150
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3151
lemma div_const_poly_conv_map_poly: 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3152
  assumes "[:c:] dvd p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3153
  shows   "p div [:c:] = map_poly (\<lambda>x. x div c) p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3154
proof (cases "c = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3155
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3156
  from assms obtain q where p: "p = [:c:] * q" by (erule dvdE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3157
  moreover {
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3158
    have "smult c q = [:c:] * q" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3159
    also have "\<dots> div [:c:] = q" by (rule nonzero_mult_div_cancel_left) (insert False, auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3160
    finally have "smult c q div [:c:] = q" .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3161
  }
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3162
  ultimately show ?thesis by (intro poly_eqI) (auto simp: coeff_map_poly False)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3163
qed (auto intro!: poly_eqI simp: coeff_map_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3164
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3165
lemma is_unit_monom_0:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3166
  fixes a :: "'a::field"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3167
  assumes "a \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3168
  shows "is_unit (monom a 0)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3169
proof
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3170
  from assms show "1 = monom a 0 * monom (inverse a) 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3171
    by (simp add: mult_monom)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3172
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3173
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3174
lemma is_unit_triv:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3175
  fixes a :: "'a::field"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3176
  assumes "a \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3177
  shows "is_unit [:a:]"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3178
  using assms by (simp add: is_unit_monom_0 monom_0 [symmetric])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3179
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3180
lemma is_unit_iff_degree:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3181
  assumes "p \<noteq> (0 :: _ :: field poly)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3182
  shows "is_unit p \<longleftrightarrow> degree p = 0" (is "?P \<longleftrightarrow> ?Q")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3183
proof
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3184
  assume ?Q
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3185
  then obtain a where "p = [:a:]" by (rule degree_eq_zeroE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3186
  with assms show ?P by (simp add: is_unit_triv)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3187
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3188
  assume ?P
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3189
  then obtain q where "q \<noteq> 0" "p * q = 1" ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3190
  then have "degree (p * q) = degree 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3191
    by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3192
  with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "degree p + degree q = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3193
    by (simp add: degree_mult_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3194
  then show ?Q by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3195
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3196
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3197
lemma is_unit_pCons_iff:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3198
  "is_unit (pCons (a::_::field) p) \<longleftrightarrow> p = 0 \<and> a \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3199
  by (cases "p = 0") (auto simp add: is_unit_triv is_unit_iff_degree)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3200
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3201
lemma is_unit_monom_trival:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3202
  fixes p :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3203
  assumes "is_unit p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3204
  shows "monom (coeff p (degree p)) 0 = p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3205
  using assms by (cases p) (simp_all add: monom_0 is_unit_pCons_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3206
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3207
lemma is_unit_const_poly_iff: 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3208
  "[:c :: 'a :: {comm_semiring_1,semiring_no_zero_divisors}:] dvd 1 \<longleftrightarrow> c dvd 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3209
  by (auto simp: one_poly_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3210
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3211
lemma is_unit_polyE:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3212
  fixes p :: "'a :: {comm_semiring_1,semiring_no_zero_divisors} poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3213
  assumes "p dvd 1" obtains c where "p = [:c:]" "c dvd 1"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3214
proof -
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3215
  from assms obtain q where "1 = p * q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3216
    by (rule dvdE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3217
  then have "p \<noteq> 0" and "q \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3218
    by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3219
  from \<open>1 = p * q\<close> have "degree 1 = degree (p * q)"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3220
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3221
  also from \<open>p \<noteq> 0\<close> and \<open>q \<noteq> 0\<close> have "\<dots> = degree p + degree q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3222
    by (simp add: degree_mult_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3223
  finally have "degree p = 0" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3224
  with degree_eq_zeroE obtain c where c: "p = [:c:]" .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3225
  moreover with \<open>p dvd 1\<close> have "c dvd 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3226
    by (simp add: is_unit_const_poly_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3227
  ultimately show thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3228
    by (rule that)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3229
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3230
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3231
lemma is_unit_polyE':
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3232
  assumes "is_unit (p::_::field poly)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3233
  obtains a where "p = monom a 0" and "a \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3234
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3235
  obtain a q where "p = pCons a q" by (cases p)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3236
  with assms have "p = [:a:]" and "a \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3237
    by (simp_all add: is_unit_pCons_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3238
  with that show thesis by (simp add: monom_0)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3239
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3240
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3241
lemma is_unit_poly_iff:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3242
  fixes p :: "'a :: {comm_semiring_1,semiring_no_zero_divisors} poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3243
  shows "p dvd 1 \<longleftrightarrow> (\<exists>c. p = [:c:] \<and> c dvd 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3244
  by (auto elim: is_unit_polyE simp add: is_unit_const_poly_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3245
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3246
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3247
subsubsection \<open>Pseudo-Division\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3248
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3249
text\<open>This part is by René Thiemann and Akihisa Yamada.\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3250
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3251
fun pseudo_divmod_main :: "'a :: comm_ring_1  \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3252
  \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a poly \<times> 'a poly" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3253
  "pseudo_divmod_main lc q r d dr (Suc n) = (let
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3254
     rr = smult lc r;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3255
     qq = coeff r dr;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3256
     rrr = rr - monom qq n * d;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3257
     qqq = smult lc q + monom qq n
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3258
     in pseudo_divmod_main lc qqq rrr d (dr - 1) n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3259
| "pseudo_divmod_main lc q r d dr 0 = (q,r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3260
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3261
definition pseudo_divmod :: "'a :: comm_ring_1 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3262
  "pseudo_divmod p q \<equiv> if q = 0 then (0,p) else
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3263
     pseudo_divmod_main (coeff q (degree q)) 0 p q (degree p) (1 + length (coeffs p) - length (coeffs q))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3264
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3265
lemma pseudo_divmod_main: assumes d: "d \<noteq> 0" "lc = coeff d (degree d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3266
  and *: "degree r \<le> dr" "pseudo_divmod_main lc q r d dr n = (q',r')" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3267
    "n = 1 + dr - degree d \<or> dr = 0 \<and> n = 0 \<and> r = 0" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3268
  shows "(r' = 0 \<or> degree r' < degree d) \<and> smult (lc^n) (d * q + r) = d * q' + r'"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3269
  using *
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3270
proof (induct n arbitrary: q r dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3271
  case (Suc n q r dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3272
  let ?rr = "smult lc r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3273
  let ?qq = "coeff r dr"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3274
  define b where [simp]: "b = monom ?qq n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3275
  let ?rrr = "?rr - b * d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3276
  let ?qqq = "smult lc q + b"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3277
  note res = Suc(3)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3278
  from res[unfolded pseudo_divmod_main.simps[of lc q] Let_def] 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3279
  have res: "pseudo_divmod_main lc ?qqq ?rrr d (dr - 1) n = (q',r')" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3280
    by (simp del: pseudo_divmod_main.simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3281
  have dr: "dr = n + degree d" using Suc(4) by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3282
  have "coeff (b * d) dr = coeff b n * coeff d (degree d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3283
  proof (cases "?qq = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3284
    case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3285
    hence n: "n = degree b" by (simp add: degree_monom_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3286
    show ?thesis unfolding n dr by (simp add: coeff_mult_degree_sum)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3287
  qed auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3288
  also have "\<dots> = lc * coeff b n" unfolding d by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3289
  finally have "coeff (b * d) dr = lc * coeff b n" .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3290
  moreover have "coeff ?rr dr = lc * coeff r dr" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3291
  ultimately have c0: "coeff ?rrr dr = 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3292
  have dr: "dr = n + degree d" using Suc(4) by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3293
  have deg_rr: "degree ?rr \<le> dr" using Suc(2) 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3294
    using degree_smult_le dual_order.trans by blast 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3295
  have deg_bd: "degree (b * d) \<le> dr"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3296
    unfolding dr
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3297
    by(rule order.trans[OF degree_mult_le], auto simp: degree_monom_le)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3298
  have "degree ?rrr \<le> dr"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3299
    using degree_diff_le[OF deg_rr deg_bd] by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3300
  with c0 have deg_rrr: "degree ?rrr \<le> (dr - 1)" by (rule coeff_0_degree_minus_1)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3301
  have "n = 1 + (dr - 1) - degree d \<or> dr - 1 = 0 \<and> n = 0 \<and> ?rrr = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3302
  proof (cases dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3303
    case 0
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3304
    with Suc(4) have 0: "dr = 0" "n = 0" "degree d = 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3305
    with deg_rrr have "degree ?rrr = 0" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3306
    hence "\<exists> a. ?rrr = [: a :]" by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3307
    from this obtain a where rrr: "?rrr = [:a:]" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3308
    show ?thesis unfolding 0 using c0 unfolding rrr 0 by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3309
  qed (insert Suc(4), auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3310
  note IH = Suc(1)[OF deg_rrr res this]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3311
  show ?case
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3312
  proof (intro conjI)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3313
    show "r' = 0 \<or> degree r' < degree d" using IH by blast
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3314
    show "smult (lc ^ Suc n) (d * q + r) = d * q' + r'"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3315
      unfolding IH[THEN conjunct2,symmetric]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3316
      by (simp add: field_simps smult_add_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3317
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3318
qed auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3319
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3320
lemma pseudo_divmod:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3321
  assumes g: "g \<noteq> 0" and *: "pseudo_divmod f g = (q,r)" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3322
  shows "smult (coeff g (degree g) ^ (Suc (degree f) - degree g)) f = g * q + r" (is ?A)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3323
    and "r = 0 \<or> degree r < degree g" (is ?B)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3324
proof -
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3325
  from *[unfolded pseudo_divmod_def Let_def]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3326
  have "pseudo_divmod_main (coeff g (degree g)) 0 f g (degree f) (1 + length (coeffs f) - length (coeffs g)) = (q, r)" by (auto simp: g)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3327
  note main = pseudo_divmod_main[OF _ _ _ this, OF g refl le_refl]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3328
  have "1 + length (coeffs f) - length (coeffs g) = 1 + degree f - degree g \<or>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3329
    degree f = 0 \<and> 1 + length (coeffs f) - length (coeffs g) = 0 \<and> f = 0" using g 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3330
    by (cases "f = 0"; cases "coeffs g", auto simp: degree_eq_length_coeffs)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3331
  note main = main[OF this]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3332
  from main show "r = 0 \<or> degree r < degree g" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3333
  show "smult (coeff g (degree g) ^ (Suc (degree f) - degree g)) f = g * q + r" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3334
    by (subst main[THEN conjunct2, symmetric], simp add: degree_eq_length_coeffs,
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3335
    insert g, cases "f = 0"; cases "coeffs g", auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3336
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3337
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3338
definition "pseudo_mod_main lc r d dr n = snd (pseudo_divmod_main lc 0 r d dr n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3339
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3340
lemma snd_pseudo_divmod_main:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3341
  "snd (pseudo_divmod_main lc q r d dr n) = snd (pseudo_divmod_main lc q' r d dr n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3342
by (induct n arbitrary: q q' lc r d dr; simp add: Let_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3343
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3344
definition pseudo_mod 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3345
    :: "'a :: {comm_ring_1,semiring_1_no_zero_divisors} poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3346
  "pseudo_mod f g = snd (pseudo_divmod f g)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3347
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3348
lemma pseudo_mod:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3349
  fixes f g
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3350
  defines "r \<equiv> pseudo_mod f g"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3351
  assumes g: "g \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3352
  shows "\<exists> a q. a \<noteq> 0 \<and> smult a f = g * q + r" "r = 0 \<or> degree r < degree g"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3353
proof - 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3354
  let ?cg = "coeff g (degree g)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3355
  let ?cge = "?cg ^ (Suc (degree f) - degree g)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3356
  define a where "a = ?cge"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3357
  obtain q where pdm: "pseudo_divmod f g = (q,r)" using r_def[unfolded pseudo_mod_def]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3358
    by (cases "pseudo_divmod f g", auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3359
  from pseudo_divmod[OF g pdm] have id: "smult a f = g * q + r" and "r = 0 \<or> degree r < degree g" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3360
    unfolding a_def by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3361
  show "r = 0 \<or> degree r < degree g" by fact
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3362
  from g have "a \<noteq> 0" unfolding a_def by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3363
  thus "\<exists> a q. a \<noteq> 0 \<and> smult a f = g * q + r" using id by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3364
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3365
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3366
lemma fst_pseudo_divmod_main_as_divide_poly_main:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3367
  assumes d: "d \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3368
  defines lc: "lc \<equiv> coeff d (degree d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3369
  shows "fst (pseudo_divmod_main lc q r d dr n) = divide_poly_main lc (smult (lc^n) q) (smult (lc^n) r) d dr n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3370
proof(induct n arbitrary: q r dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3371
  case 0 then show ?case by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3372
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3373
  case (Suc n)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3374
    note lc0 = leading_coeff_neq_0[OF d, folded lc]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3375
    then have "pseudo_divmod_main lc q r d dr (Suc n) =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3376
    pseudo_divmod_main lc (smult lc q + monom (coeff r dr) n)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3377
      (smult lc r - monom (coeff r dr) n * d) d (dr - 1) n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3378
    by (simp add: Let_def ac_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3379
    also have "fst ... = divide_poly_main lc
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3380
     (smult (lc^n) (smult lc q + monom (coeff r dr) n))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3381
     (smult (lc^n) (smult lc r - monom (coeff r dr) n * d))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3382
     d (dr - 1) n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3383
      unfolding Suc[unfolded divide_poly_main.simps Let_def]..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3384
    also have "... = divide_poly_main lc (smult (lc ^ Suc n) q)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3385
        (smult (lc ^ Suc n) r) d dr (Suc n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3386
      unfolding smult_monom smult_distribs mult_smult_left[symmetric]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3387
      using lc0 by (simp add: Let_def ac_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3388
    finally show ?case.
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3389
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3390
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3391
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3392
subsubsection \<open>Division in polynomials over fields\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3393
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3394
lemma pseudo_divmod_field:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3395
  assumes g: "(g::'a::field poly) \<noteq> 0" and *: "pseudo_divmod f g = (q,r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3396
  defines "c \<equiv> coeff g (degree g) ^ (Suc (degree f) - degree g)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3397
  shows "f = g * smult (1/c) q + smult (1/c) r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3398
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3399
  from leading_coeff_neq_0[OF g] have c0: "c \<noteq> 0" unfolding c_def by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3400
  from pseudo_divmod(1)[OF g *, folded c_def]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3401
  have "smult c f = g * q + r" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3402
  also have "smult (1/c) ... = g * smult (1/c) q + smult (1/c) r" by (simp add: smult_add_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3403
  finally show ?thesis using c0 by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3404
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3405
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3406
lemma divide_poly_main_field:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3407
  assumes d: "(d::'a::field poly) \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3408
  defines lc: "lc \<equiv> coeff d (degree d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3409
  shows "divide_poly_main lc q r d dr n = fst (pseudo_divmod_main lc (smult ((1/lc)^n) q) (smult ((1/lc)^n) r) d dr n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3410
  unfolding lc
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3411
  by(subst fst_pseudo_divmod_main_as_divide_poly_main, auto simp: d power_one_over)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3412
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3413
lemma divide_poly_field:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3414
  fixes f g :: "'a :: field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3415
  defines "f' \<equiv> smult ((1 / coeff g (degree g)) ^ (Suc (degree f) - degree g)) f"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3416
  shows "(f::'a::field poly) div g = fst (pseudo_divmod f' g)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3417
proof (cases "g = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3418
  case True show ?thesis 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3419
    unfolding divide_poly_def pseudo_divmod_def Let_def f'_def True by (simp add: divide_poly_main_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3420
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3421
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3422
    from leading_coeff_neq_0[OF False] have "degree f' = degree f" unfolding f'_def by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3423
    then show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3424
      using length_coeffs_degree[of f'] length_coeffs_degree[of f]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3425
      unfolding divide_poly_def pseudo_divmod_def Let_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3426
                divide_poly_main_field[OF False]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3427
                length_coeffs_degree[OF False] 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3428
                f'_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3429
      by force
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3430
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3431
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3432
instantiation poly :: ("{semidom_divide_unit_factor, idom_divide}") normalization_semidom
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3433
begin
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3434
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3435
definition unit_factor_poly :: "'a poly \<Rightarrow> 'a poly"
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3436
  where "unit_factor_poly p = [:unit_factor (lead_coeff p):]"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3437
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3438
definition normalize_poly :: "'a poly \<Rightarrow> 'a poly"
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3439
  where "normalize p = p div [:unit_factor (lead_coeff p):]"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3440
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3441
instance proof
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3442
  fix p :: "'a poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3443
  show "unit_factor p * normalize p = p"
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3444
  proof (cases "p = 0")
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3445
    case True
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3446
    then show ?thesis
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3447
      by (simp add: unit_factor_poly_def normalize_poly_def)
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3448
  next
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3449
    case False
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3450
    then have "lead_coeff p \<noteq> 0"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3451
      by simp
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3452
    then have *: "unit_factor (lead_coeff p) \<noteq> 0"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3453
      using unit_factor_is_unit [of "lead_coeff p"] by auto
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3454
    then have "unit_factor (lead_coeff p) dvd 1"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3455
      by (auto intro: unit_factor_is_unit)
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3456
    then have **: "unit_factor (lead_coeff p) dvd c" for c
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3457
      by (rule dvd_trans) simp
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3458
    have ***: "unit_factor (lead_coeff p) * (c div unit_factor (lead_coeff p)) = c" for c
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3459
    proof -
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3460
      from ** obtain b where "c = unit_factor (lead_coeff p) * b" ..
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3461
      then show ?thesis
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3462
        using False * by simp
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3463
    qed
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3464
    have "p div [:unit_factor (lead_coeff p):] =
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3465
      map_poly (\<lambda>c. c div unit_factor (lead_coeff p)) p"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3466
      by (simp add: const_poly_dvd_iff div_const_poly_conv_map_poly **)
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3467
    then show ?thesis
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3468
      by (simp add: normalize_poly_def unit_factor_poly_def
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3469
        smult_conv_map_poly map_poly_map_poly o_def ***)
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3470
  qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3471
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3472
  fix p :: "'a poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3473
  assume "is_unit p"
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3474
  then obtain c where p: "p = [:c:]" "c dvd 1"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3475
    by (auto simp: is_unit_poly_iff)
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3476
  then show "unit_factor p = p"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3477
    by (simp add: unit_factor_poly_def monom_0 is_unit_unit_factor)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3478
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3479
  fix p :: "'a poly" assume "p \<noteq> 0"
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3480
  then show "is_unit (unit_factor p)"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3481
    by (simp add: unit_factor_poly_def monom_0 is_unit_poly_iff unit_factor_is_unit)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3482
qed (simp_all add: normalize_poly_def unit_factor_poly_def monom_0 lead_coeff_mult unit_factor_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3483
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3484
end
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3485
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3486
lemma normalize_poly_eq_map_poly:
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3487
  "normalize p = map_poly (\<lambda>x. x div unit_factor (lead_coeff p)) p"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3488
proof -
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3489
  have "[:unit_factor (lead_coeff p):] dvd p"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3490
    by (metis unit_factor_poly_def unit_factor_self)
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3491
  then show ?thesis
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3492
    by (simp add: normalize_poly_def div_const_poly_conv_map_poly)
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3493
qed
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3494
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3495
lemma coeff_normalize [simp]:
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3496
  "coeff (normalize p) n = coeff p n div unit_factor (lead_coeff p)"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3497
  by (simp add: normalize_poly_eq_map_poly coeff_map_poly)
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3498
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3499
class field_unit_factor = field + unit_factor +
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3500
  assumes unit_factor_field [simp]: "unit_factor = id"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3501
begin
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3502
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3503
subclass semidom_divide_unit_factor
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3504
proof
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3505
  fix a
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3506
  assume "a \<noteq> 0"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3507
  then have "1 = a * inverse a"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3508
    by simp
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3509
  then have "a dvd 1" ..
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3510
  then show "unit_factor a dvd 1"
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3511
    by simp
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3512
qed simp_all
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3513
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3514
end
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3515
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3516
lemma unit_factor_pCons:
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3517
  "unit_factor (pCons a p) = (if p = 0 then [:unit_factor a:] else unit_factor p)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3518
  by (simp add: unit_factor_poly_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3519
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3520
lemma normalize_monom [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3521
  "normalize (monom a n) = monom (normalize a) n"
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3522
  by (cases "a = 0") (simp_all add: map_poly_monom normalize_poly_eq_map_poly degree_monom_eq)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3523
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3524
lemma unit_factor_monom [simp]:
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3525
  "unit_factor (monom a n) = [:unit_factor a:]"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3526
  by (cases "a = 0") (simp_all add: unit_factor_poly_def degree_monom_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3527
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3528
lemma normalize_const_poly: "normalize [:c:] = [:normalize c:]"
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3529
  by (simp add: normalize_poly_eq_map_poly map_poly_pCons)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3530
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3531
lemma normalize_smult: "normalize (smult c p) = smult (normalize c) (normalize p)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3532
proof -
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3533
  have "smult c p = [:c:] * p" by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3534
  also have "normalize \<dots> = smult (normalize c) (normalize p)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3535
    by (subst normalize_mult) (simp add: normalize_const_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3536
  finally show ?thesis .
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3537
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3538
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3539
lemma smult_content_normalize_primitive_part [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3540
  "smult (content p) (normalize (primitive_part p)) = normalize p"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3541
proof -
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3542
  have "smult (content p) (normalize (primitive_part p)) = 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3543
          normalize ([:content p:] * primitive_part p)" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3544
    by (subst normalize_mult) (simp_all add: normalize_const_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3545
  also have "[:content p:] * primitive_part p = p" by simp
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  3546
  finally show ?thesis .
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  3547
qed
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  3548
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3549
inductive eucl_rel_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly \<Rightarrow> bool"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3550
  where eucl_rel_poly_by0: "eucl_rel_poly x 0 (0, x)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3551
  | eucl_rel_poly_dividesI: "y \<noteq> 0 \<Longrightarrow> x = q * y \<Longrightarrow> eucl_rel_poly x y (q, 0)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3552
  | eucl_rel_poly_remainderI: "y \<noteq> 0 \<Longrightarrow> degree r < degree y
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3553
      \<Longrightarrow> x = q * y + r \<Longrightarrow> eucl_rel_poly x y (q, r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3554
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3555
lemma eucl_rel_poly_iff:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3556
  "eucl_rel_poly x y (q, r) \<longleftrightarrow>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3557
    x = q * y + r \<and>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3558
      (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3559
  by (auto elim: eucl_rel_poly.cases
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3560
    intro: eucl_rel_poly_by0 eucl_rel_poly_dividesI eucl_rel_poly_remainderI)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3561
  
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3562
lemma eucl_rel_poly_0:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3563
  "eucl_rel_poly 0 y (0, 0)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3564
  unfolding eucl_rel_poly_iff by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3565
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3566
lemma eucl_rel_poly_by_0:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3567
  "eucl_rel_poly x 0 (0, x)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3568
  unfolding eucl_rel_poly_iff by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3569
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3570
lemma eucl_rel_poly_pCons:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3571
  assumes rel: "eucl_rel_poly x y (q, r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3572
  assumes y: "y \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3573
  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3574
  shows "eucl_rel_poly (pCons a x) y (pCons b q, pCons a r - smult b y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3575
    (is "eucl_rel_poly ?x y (?q, ?r)")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3576
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3577
  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3578
    using assms unfolding eucl_rel_poly_iff by simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3579
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3580
  have 1: "?x = ?q * y + ?r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3581
    using b x by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3582
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3583
  have 2: "?r = 0 \<or> degree ?r < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3584
  proof (rule eq_zero_or_degree_less)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3585
    show "degree ?r \<le> degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3586
    proof (rule degree_diff_le)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3587
      show "degree (pCons a r) \<le> degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3588
        using r by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3589
      show "degree (smult b y) \<le> degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3590
        by (rule degree_smult_le)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3591
    qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3592
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3593
    show "coeff ?r (degree y) = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3594
      using \<open>y \<noteq> 0\<close> unfolding b by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3595
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3596
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3597
  from 1 2 show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3598
    unfolding eucl_rel_poly_iff
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3599
    using \<open>y \<noteq> 0\<close> by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3600
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3601
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3602
lemma eucl_rel_poly_exists: "\<exists>q r. eucl_rel_poly x y (q, r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3603
apply (cases "y = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3604
apply (fast intro!: eucl_rel_poly_by_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3605
apply (induct x)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3606
apply (fast intro!: eucl_rel_poly_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3607
apply (fast intro!: eucl_rel_poly_pCons)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3608
done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3609
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3610
lemma eucl_rel_poly_unique:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3611
  assumes 1: "eucl_rel_poly x y (q1, r1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3612
  assumes 2: "eucl_rel_poly x y (q2, r2)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3613
  shows "q1 = q2 \<and> r1 = r2"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3614
proof (cases "y = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3615
  assume "y = 0" with assms show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3616
    by (simp add: eucl_rel_poly_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3617
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3618
  assume [simp]: "y \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3619
  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3620
    unfolding eucl_rel_poly_iff by simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3621
  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3622
    unfolding eucl_rel_poly_iff by simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3623
  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3624
    by (simp add: algebra_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3625
  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3626
    by (auto intro: degree_diff_less)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3627
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3628
  show "q1 = q2 \<and> r1 = r2"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3629
  proof (rule ccontr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3630
    assume "\<not> (q1 = q2 \<and> r1 = r2)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3631
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3632
    with r3 have "degree (r2 - r1) < degree y" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3633
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3634
    also have "\<dots> = degree ((q1 - q2) * y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3635
      using \<open>q1 \<noteq> q2\<close> by (simp add: degree_mult_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3636
    also have "\<dots> = degree (r2 - r1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3637
      using q3 by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3638
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3639
    then show "False" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3640
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3641
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3642
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3643
lemma eucl_rel_poly_0_iff: "eucl_rel_poly 0 y (q, r) \<longleftrightarrow> q = 0 \<and> r = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3644
by (auto dest: eucl_rel_poly_unique intro: eucl_rel_poly_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3645
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3646
lemma eucl_rel_poly_by_0_iff: "eucl_rel_poly x 0 (q, r) \<longleftrightarrow> q = 0 \<and> r = x"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3647
by (auto dest: eucl_rel_poly_unique intro: eucl_rel_poly_by_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3648
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3649
lemmas eucl_rel_poly_unique_div = eucl_rel_poly_unique [THEN conjunct1]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3650
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3651
lemmas eucl_rel_poly_unique_mod = eucl_rel_poly_unique [THEN conjunct2]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3652
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3653
instantiation poly :: (field) semidom_modulo
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3654
begin
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3655
 
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3656
definition modulo_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3657
  where mod_poly_def: "f mod g = (if g = 0 then f
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3658
    else pseudo_mod (smult ((1 / lead_coeff g) ^ (Suc (degree f) - degree g)) f) g)"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3659
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3660
instance proof
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3661
  fix x y :: "'a poly"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3662
  show "x div y * y + x mod y = x"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3663
  proof (cases "y = 0")
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3664
    case True then show ?thesis
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3665
      by (simp add: divide_poly_0 mod_poly_def)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3666
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3667
    case False
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3668
    then have "pseudo_divmod (smult ((1 / lead_coeff y) ^ (Suc (degree x) - degree y)) x) y =
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3669
      (x div y, x mod y)"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3670
      by (simp add: divide_poly_field mod_poly_def pseudo_mod_def)
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3671
    from pseudo_divmod [OF False this]
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3672
    show ?thesis using False
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3673
      by (simp add: power_mult_distrib [symmetric] ac_simps)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3674
  qed
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3675
qed
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3676
  
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3677
end
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3678
    
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3679
lemma eucl_rel_poly: "eucl_rel_poly x y (x div y, x mod y)"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3680
unfolding eucl_rel_poly_iff proof
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3681
  show "x = x div y * y + x mod y"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3682
    by (simp add: div_mult_mod_eq)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3683
  show "if y = 0 then x div y = 0 else x mod y = 0 \<or> degree (x mod y) < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3684
  proof (cases "y = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3685
    case True then show ?thesis by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3686
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3687
    case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3688
      with pseudo_mod[OF this] show ?thesis unfolding mod_poly_def by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3689
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3690
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3691
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3692
lemma div_poly_eq:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3693
  "eucl_rel_poly (x::'a::field poly) y (q, r) \<Longrightarrow> x div y = q"
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3694
  by(rule eucl_rel_poly_unique_div [OF eucl_rel_poly])
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3695
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3696
lemma mod_poly_eq:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3697
  "eucl_rel_poly (x::'a::field poly) y (q, r) \<Longrightarrow> x mod y = r"
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3698
  by (rule eucl_rel_poly_unique_mod [OF eucl_rel_poly])
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3699
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3700
instance poly :: (field) ring_div
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  3701
proof
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3702
  fix x y z :: "'a poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3703
  assume "y \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3704
  hence "eucl_rel_poly (x + z * y) y (z + x div y, x mod y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3705
    using eucl_rel_poly [of x y]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3706
    by (simp add: eucl_rel_poly_iff distrib_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3707
  thus "(x + z * y) div y = z + x div y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3708
    by (rule div_poly_eq)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  3709
next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3710
  fix x y z :: "'a poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3711
  assume "x \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3712
  show "(x * y) div (x * z) = y div z"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3713
  proof (cases "y \<noteq> 0 \<and> z \<noteq> 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3714
    have "\<And>x::'a poly. eucl_rel_poly x 0 (0, x)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3715
      by (rule eucl_rel_poly_by_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3716
    then have [simp]: "\<And>x::'a poly. x div 0 = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3717
      by (rule div_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3718
    have "\<And>x::'a poly. eucl_rel_poly 0 x (0, 0)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3719
      by (rule eucl_rel_poly_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3720
    then have [simp]: "\<And>x::'a poly. 0 div x = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3721
      by (rule div_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3722
    case False then show ?thesis by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3723
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3724
    case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3725
    with \<open>x \<noteq> 0\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3726
    have "\<And>q r. eucl_rel_poly y z (q, r) \<Longrightarrow> eucl_rel_poly (x * y) (x * z) (q, x * r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3727
      by (auto simp add: eucl_rel_poly_iff algebra_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3728
        (rule classical, simp add: degree_mult_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3729
    moreover from eucl_rel_poly have "eucl_rel_poly y z (y div z, y mod z)" .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3730
    ultimately have "eucl_rel_poly (x * y) (x * z) (y div z, x * (y mod z))" .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3731
    then show ?thesis by (simp add: div_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3732
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3733
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3734
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3735
lemma div_pCons_eq:
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3736
  "pCons a p div q = (if q = 0 then 0
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3737
     else pCons (coeff (pCons a (p mod q)) (degree q) / lead_coeff q)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3738
       (p div q))"
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3739
  using eucl_rel_poly_pCons [OF eucl_rel_poly _ refl, of q a p]
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3740
  by (auto intro: div_poly_eq)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3741
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3742
lemma mod_pCons_eq:
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3743
  "pCons a p mod q = (if q = 0 then pCons a p
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3744
     else pCons a (p mod q) - smult (coeff (pCons a (p mod q)) (degree q) / lead_coeff q)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3745
       q)"
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3746
  using eucl_rel_poly_pCons [OF eucl_rel_poly _ refl, of q a p]
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3747
  by (auto intro: mod_poly_eq)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3748
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3749
lemma div_mod_fold_coeffs:
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3750
  "(p div q, p mod q) = (if q = 0 then (0, p)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3751
    else fold_coeffs (\<lambda>a (s, r).
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3752
      let b = coeff (pCons a r) (degree q) / coeff q (degree q)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3753
      in (pCons b s, pCons a r - smult b q)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3754
   ) p (0, 0))"
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3755
  by (rule sym, induct p) (auto simp add: div_pCons_eq mod_pCons_eq Let_def)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3756
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3757
lemma degree_mod_less:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3758
  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3759
  using eucl_rel_poly [of x y]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3760
  unfolding eucl_rel_poly_iff by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3761
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3762
lemma degree_mod_less': "b \<noteq> 0 \<Longrightarrow> a mod b \<noteq> 0 \<Longrightarrow> degree (a mod b) < degree b"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3763
  using degree_mod_less[of b a] by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3764
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3765
lemma div_poly_less: "degree (x::'a::field poly) < degree y \<Longrightarrow> x div y = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3766
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3767
  assume "degree x < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3768
  hence "eucl_rel_poly x y (0, x)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3769
    by (simp add: eucl_rel_poly_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3770
  thus "x div y = 0" by (rule div_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3771
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3772
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3773
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3774
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3775
  assume "degree x < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3776
  hence "eucl_rel_poly x y (0, x)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3777
    by (simp add: eucl_rel_poly_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3778
  thus "x mod y = x" by (rule mod_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3779
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3780
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3781
lemma eucl_rel_poly_smult_left:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3782
  "eucl_rel_poly x y (q, r)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3783
    \<Longrightarrow> eucl_rel_poly (smult a x) y (smult a q, smult a r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3784
  unfolding eucl_rel_poly_iff by (simp add: smult_add_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3785
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3786
lemma div_smult_left: "(smult (a::'a::field) x) div y = smult a (x div y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3787
  by (rule div_poly_eq, rule eucl_rel_poly_smult_left, rule eucl_rel_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3788
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3789
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3790
  by (rule mod_poly_eq, rule eucl_rel_poly_smult_left, rule eucl_rel_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3791
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3792
lemma poly_div_minus_left [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3793
  fixes x y :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3794
  shows "(- x) div y = - (x div y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3795
  using div_smult_left [of "- 1::'a"] by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3796
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3797
lemma poly_mod_minus_left [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3798
  fixes x y :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3799
  shows "(- x) mod y = - (x mod y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3800
  using mod_smult_left [of "- 1::'a"] by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3801
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3802
lemma eucl_rel_poly_add_left:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3803
  assumes "eucl_rel_poly x y (q, r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3804
  assumes "eucl_rel_poly x' y (q', r')"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3805
  shows "eucl_rel_poly (x + x') y (q + q', r + r')"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3806
  using assms unfolding eucl_rel_poly_iff
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3807
  by (auto simp add: algebra_simps degree_add_less)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3808
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3809
lemma poly_div_add_left:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3810
  fixes x y z :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3811
  shows "(x + y) div z = x div z + y div z"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3812
  using eucl_rel_poly_add_left [OF eucl_rel_poly eucl_rel_poly]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3813
  by (rule div_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3814
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3815
lemma poly_mod_add_left:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3816
  fixes x y z :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3817
  shows "(x + y) mod z = x mod z + y mod z"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3818
  using eucl_rel_poly_add_left [OF eucl_rel_poly eucl_rel_poly]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3819
  by (rule mod_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3820
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3821
lemma poly_div_diff_left:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3822
  fixes x y z :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3823
  shows "(x - y) div z = x div z - y div z"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3824
  by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3825
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3826
lemma poly_mod_diff_left:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3827
  fixes x y z :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3828
  shows "(x - y) mod z = x mod z - y mod z"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3829
  by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3830
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3831
lemma eucl_rel_poly_smult_right:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3832
  "a \<noteq> 0 \<Longrightarrow> eucl_rel_poly x y (q, r)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3833
    \<Longrightarrow> eucl_rel_poly x (smult a y) (smult (inverse a) q, r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3834
  unfolding eucl_rel_poly_iff by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3835
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3836
lemma div_smult_right:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3837
  "(a::'a::field) \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3838
  by (rule div_poly_eq, erule eucl_rel_poly_smult_right, rule eucl_rel_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3839
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3840
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3841
  by (rule mod_poly_eq, erule eucl_rel_poly_smult_right, rule eucl_rel_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3842
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3843
lemma poly_div_minus_right [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3844
  fixes x y :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3845
  shows "x div (- y) = - (x div y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3846
  using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3847
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3848
lemma poly_mod_minus_right [simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3849
  fixes x y :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3850
  shows "x mod (- y) = x mod y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3851
  using mod_smult_right [of "- 1::'a"] by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3852
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3853
lemma eucl_rel_poly_mult:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3854
  "eucl_rel_poly x y (q, r) \<Longrightarrow> eucl_rel_poly q z (q', r')
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3855
    \<Longrightarrow> eucl_rel_poly x (y * z) (q', y * r' + r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3856
apply (cases "z = 0", simp add: eucl_rel_poly_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3857
apply (cases "y = 0", simp add: eucl_rel_poly_by_0_iff eucl_rel_poly_0_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3858
apply (cases "r = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3859
apply (cases "r' = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3860
apply (simp add: eucl_rel_poly_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3861
apply (simp add: eucl_rel_poly_iff field_simps degree_mult_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3862
apply (cases "r' = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3863
apply (simp add: eucl_rel_poly_iff degree_mult_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3864
apply (simp add: eucl_rel_poly_iff field_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3865
apply (simp add: degree_mult_eq degree_add_less)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3866
done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3867
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3868
lemma poly_div_mult_right:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3869
  fixes x y z :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3870
  shows "x div (y * z) = (x div y) div z"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3871
  by (rule div_poly_eq, rule eucl_rel_poly_mult, (rule eucl_rel_poly)+)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3872
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3873
lemma poly_mod_mult_right:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3874
  fixes x y z :: "'a::field poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3875
  shows "x mod (y * z) = y * (x div y mod z) + x mod y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3876
  by (rule mod_poly_eq, rule eucl_rel_poly_mult, (rule eucl_rel_poly)+)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3877
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3878
lemma mod_pCons:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3879
  fixes a and x
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3880
  assumes y: "y \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3881
  defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3882
  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3883
unfolding b
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3884
apply (rule mod_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3885
apply (rule eucl_rel_poly_pCons [OF eucl_rel_poly y refl])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3886
done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3887
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3888
    
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3889
subsubsection \<open>List-based versions for fast implementation\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3890
(* Subsection by:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3891
      Sebastiaan Joosten
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3892
      René Thiemann
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3893
      Akihisa Yamada
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3894
    *)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3895
fun minus_poly_rev_list :: "'a :: group_add list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3896
  "minus_poly_rev_list (x # xs) (y # ys) = (x - y) # (minus_poly_rev_list xs ys)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3897
| "minus_poly_rev_list xs [] = xs"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3898
| "minus_poly_rev_list [] (y # ys) = []"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3899
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3900
fun pseudo_divmod_main_list :: "'a::comm_ring_1 \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3901
  \<Rightarrow> nat \<Rightarrow> 'a list \<times> 'a list" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3902
  "pseudo_divmod_main_list lc q r d (Suc n) = (let
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3903
     rr = map (op * lc) r;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3904
     a = hd r;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3905
     qqq = cCons a (map (op * lc) q);
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3906
     rrr = tl (if a = 0 then rr else minus_poly_rev_list rr (map (op * a) d))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3907
     in pseudo_divmod_main_list lc qqq rrr d n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3908
| "pseudo_divmod_main_list lc q r d 0 = (q,r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3909
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3910
fun pseudo_mod_main_list :: "'a::comm_ring_1 \<Rightarrow> 'a list \<Rightarrow> 'a list 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3911
  \<Rightarrow> nat \<Rightarrow> 'a list" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3912
  "pseudo_mod_main_list lc r d (Suc n) = (let
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3913
     rr = map (op * lc) r;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3914
     a = hd r;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3915
     rrr = tl (if a = 0 then rr else minus_poly_rev_list rr (map (op * a) d))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3916
     in pseudo_mod_main_list lc rrr d n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3917
| "pseudo_mod_main_list lc r d 0 = r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3918
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3919
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3920
fun divmod_poly_one_main_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3921
  \<Rightarrow> nat \<Rightarrow> 'a list \<times> 'a list" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3922
  "divmod_poly_one_main_list q r d (Suc n) = (let
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3923
     a = hd r;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3924
     qqq = cCons a q;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3925
     rr = tl (if a = 0 then r else minus_poly_rev_list r (map (op * a) d))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3926
     in divmod_poly_one_main_list qqq rr d n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3927
| "divmod_poly_one_main_list q r d 0 = (q,r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3928
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3929
fun mod_poly_one_main_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3930
  \<Rightarrow> nat \<Rightarrow> 'a list" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3931
  "mod_poly_one_main_list r d (Suc n) = (let
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3932
     a = hd r;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3933
     rr = tl (if a = 0 then r else minus_poly_rev_list r (map (op * a) d))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3934
     in mod_poly_one_main_list rr d n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3935
| "mod_poly_one_main_list r d 0 = r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3936
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3937
definition pseudo_divmod_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list \<times> 'a list" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3938
  "pseudo_divmod_list p q =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3939
  (if q = [] then ([],p) else
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3940
 (let rq = rev q;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3941
     (qu,re) = pseudo_divmod_main_list (hd rq) [] (rev p) rq (1 + length p - length q) in 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3942
   (qu,rev re)))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3943
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3944
definition pseudo_mod_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3945
  "pseudo_mod_list p q =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3946
  (if q = [] then p else
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3947
 (let rq = rev q;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3948
     re = pseudo_mod_main_list (hd rq) (rev p) rq (1 + length p - length q) in 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3949
   (rev re)))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3950
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3951
lemma minus_zero_does_nothing:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3952
"(minus_poly_rev_list x (map (op * 0) y)) = (x :: 'a :: ring list)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3953
  by(induct x y rule: minus_poly_rev_list.induct, auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3954
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3955
lemma length_minus_poly_rev_list[simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3956
 "length (minus_poly_rev_list xs ys) = length xs"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3957
  by(induct xs ys rule: minus_poly_rev_list.induct, auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3958
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3959
lemma if_0_minus_poly_rev_list:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3960
  "(if a = 0 then x else minus_poly_rev_list x (map (op * a) y))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3961
      = minus_poly_rev_list x (map (op * (a :: 'a :: ring)) y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3962
  by(cases "a=0",simp_all add:minus_zero_does_nothing)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3963
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3964
lemma Poly_append:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3965
  "Poly ((a::'a::comm_semiring_1 list) @ b) = Poly a + monom 1 (length a) * Poly b"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3966
  by (induct a,auto simp: monom_0 monom_Suc)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3967
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3968
lemma minus_poly_rev_list: "length p \<ge> length (q :: 'a :: comm_ring_1 list) \<Longrightarrow>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3969
  Poly (rev (minus_poly_rev_list (rev p) (rev q)))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3970
  = Poly p - monom 1 (length p - length q) * Poly q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3971
proof (induct "rev p" "rev q" arbitrary: p q rule: minus_poly_rev_list.induct)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3972
  case (1 x xs y ys) 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3973
  have "length (rev q) \<le> length (rev p)" using 1 by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3974
  from this[folded 1(2,3)] have ys_xs:"length ys \<le> length xs" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3975
  hence a:"Poly (rev (minus_poly_rev_list xs ys)) =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3976
           Poly (rev xs) - monom 1 (length xs - length ys) * Poly (rev ys)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3977
    by(subst "1.hyps"(1)[of "rev xs" "rev ys", unfolded rev_rev_ident length_rev],auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3978
  have "Poly p - monom 1 (length p - length q) * Poly q
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3979
      = Poly (rev (rev p)) - monom 1 (length (rev (rev p)) - length (rev (rev q))) * Poly (rev (rev q))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3980
    by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3981
  also have "\<dots> = Poly (rev (x # xs)) - monom 1 (length (x # xs) - length (y # ys)) * Poly (rev (y # ys))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3982
    unfolding 1(2,3) by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3983
  also have "\<dots> = Poly (rev xs) + monom x (length xs) -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3984
  (monom 1 (length xs - length ys) * Poly (rev ys) + monom y (length xs))" using ys_xs
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3985
    by (simp add:Poly_append distrib_left mult_monom smult_monom)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3986
  also have "\<dots> = Poly (rev (minus_poly_rev_list xs ys)) + monom (x - y) (length xs)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3987
    unfolding a diff_monom[symmetric] by(simp)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3988
  finally show ?case
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3989
    unfolding 1(2,3)[symmetric] by (simp add: smult_monom Poly_append)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3990
qed auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3991
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3992
lemma smult_monom_mult: "smult a (monom b n * f) = monom (a * b) n * f"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3993
  using smult_monom [of a _ n] by (metis mult_smult_left)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3994
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3995
lemma head_minus_poly_rev_list:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3996
  "length d \<le> length r \<Longrightarrow> d\<noteq>[] \<Longrightarrow>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3997
  hd (minus_poly_rev_list (map (op * (last d :: 'a :: comm_ring)) r) (map (op * (hd r)) (rev d))) = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3998
proof(induct r)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3999
  case (Cons a rs)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4000
  thus ?case by(cases "rev d", simp_all add:ac_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4001
qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4002
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4003
lemma Poly_map: "Poly (map (op * a) p) = smult a (Poly p)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4004
proof (induct p)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4005
  case(Cons x xs) thus ?case by (cases "Poly xs = 0",auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4006
qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4007
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4008
lemma last_coeff_is_hd: "xs \<noteq> [] \<Longrightarrow> coeff (Poly xs) (length xs - 1) = hd (rev xs)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4009
  by (simp_all add: hd_conv_nth rev_nth nth_default_nth nth_append)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4010
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4011
lemma pseudo_divmod_main_list_invar :
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4012
  assumes leading_nonzero:"last d \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4013
  and lc:"last d = lc"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4014
  and dNonempty:"d \<noteq> []"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4015
  and "pseudo_divmod_main_list lc q (rev r) (rev d) n = (q',rev r')"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4016
  and "n = (1 + length r - length d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4017
  shows 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4018
  "pseudo_divmod_main lc (monom 1 n * Poly q) (Poly r) (Poly d) (length r - 1) n = 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4019
  (Poly q', Poly r')"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4020
using assms(4-)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4021
proof(induct "n" arbitrary: r q)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4022
case (Suc n r q)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4023
  have ifCond: "\<not> Suc (length r) \<le> length d" using Suc.prems by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4024
  have rNonempty:"r \<noteq> []"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4025
    using ifCond dNonempty using Suc_leI length_greater_0_conv list.size(3) by fastforce
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4026
  let ?a = "(hd (rev r))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4027
  let ?rr = "map (op * lc) (rev r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4028
  let ?rrr = "rev (tl (minus_poly_rev_list ?rr (map (op * ?a) (rev d))))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4029
  let ?qq = "cCons ?a (map (op * lc) q)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4030
  have n: "n = (1 + length r - length d - 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4031
    using ifCond Suc(3) by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4032
  have rr_val:"(length ?rrr) = (length r - 1)" using ifCond by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4033
  hence rr_smaller: "(1 + length r - length d - 1) = (1 + length ?rrr - length d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4034
    using rNonempty ifCond unfolding One_nat_def by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4035
  from ifCond have id: "Suc (length r) - length d = Suc (length r - length d)" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4036
  have "pseudo_divmod_main_list lc ?qq (rev ?rrr) (rev d) (1 + length r - length d - 1) = (q', rev r')"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4037
    using Suc.prems ifCond by (simp add:Let_def if_0_minus_poly_rev_list id)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4038
  hence v:"pseudo_divmod_main_list lc ?qq (rev ?rrr) (rev d) n = (q', rev r')"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4039
    using n by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4040
  have sucrr:"Suc (length r) - length d = Suc (length r - length d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4041
    using Suc_diff_le ifCond not_less_eq_eq by blast
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4042
  have n_ok : "n = 1 + (length ?rrr) - length d" using Suc(3) rNonempty by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4043
  have cong: "\<And> x1 x2 x3 x4 y1 y2 y3 y4. x1 = y1 \<Longrightarrow> x2 = y2 \<Longrightarrow> x3 = y3 \<Longrightarrow> x4 = y4 \<Longrightarrow>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4044
    pseudo_divmod_main lc x1 x2 x3 x4 n = pseudo_divmod_main lc y1 y2 y3 y4 n" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4045
  have hd_rev:"coeff (Poly r) (length r - Suc 0) = hd (rev r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4046
    using last_coeff_is_hd[OF rNonempty] by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4047
  show ?case unfolding Suc.hyps(1)[OF v n_ok, symmetric] pseudo_divmod_main.simps Let_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4048
  proof (rule cong[OF _ _ refl], goal_cases)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4049
    case 1 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4050
    show ?case unfolding monom_Suc hd_rev[symmetric]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4051
      by (simp add: smult_monom Poly_map)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4052
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4053
    case 2 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4054
    show ?case 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4055
    proof (subst Poly_on_rev_starting_with_0, goal_cases)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4056
      show "hd (minus_poly_rev_list (map (op * lc) (rev r)) (map (op * (hd (rev r))) (rev d))) = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4057
        by (fold lc, subst head_minus_poly_rev_list, insert ifCond dNonempty,auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4058
      from ifCond have "length d \<le> length r" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4059
      then show "smult lc (Poly r) - monom (coeff (Poly r) (length r - 1)) n * Poly d =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4060
        Poly (rev (minus_poly_rev_list (map (op * lc) (rev r)) (map (op * (hd (rev r))) (rev d))))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4061
        by (fold rev_map) (auto simp add: n smult_monom_mult Poly_map hd_rev [symmetric]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4062
          minus_poly_rev_list)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4063
    qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4064
  qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4065
qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4066
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4067
lemma pseudo_divmod_impl[code]: "pseudo_divmod (f::'a::comm_ring_1 poly) g =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4068
  map_prod poly_of_list poly_of_list (pseudo_divmod_list (coeffs f) (coeffs g))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4069
proof (cases "g=0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4070
case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4071
  hence coeffs_g_nonempty:"(coeffs g) \<noteq> []" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4072
  hence lastcoeffs:"last (coeffs g) = coeff g (degree g)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4073
    by (simp add: hd_rev last_coeffs_eq_coeff_degree not_0_coeffs_not_Nil)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4074
  obtain q r where qr: "pseudo_divmod_main_list
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4075
            (last (coeffs g)) (rev [])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4076
            (rev (coeffs f)) (rev (coeffs g))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4077
            (1 + length (coeffs f) -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4078
             length (coeffs g)) = (q,rev (rev r))"  by force
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4079
  then have qr': "pseudo_divmod_main_list
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4080
            (hd (rev (coeffs g))) []
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4081
            (rev (coeffs f)) (rev (coeffs g))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4082
            (1 + length (coeffs f) -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4083
             length (coeffs g)) = (q,r)" using hd_rev[OF coeffs_g_nonempty] by(auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4084
  from False have cg: "(coeffs g = []) = False" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4085
  have last_non0:"last (coeffs g) \<noteq> 0" using False by (simp add:last_coeffs_not_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4086
  show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4087
    unfolding pseudo_divmod_def pseudo_divmod_list_def Let_def qr' map_prod_def split cg if_False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4088
    pseudo_divmod_main_list_invar[OF last_non0 _ _ qr,unfolded lastcoeffs,simplified,symmetric,OF False]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4089
    poly_of_list_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4090
    using False by (auto simp: degree_eq_length_coeffs)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4091
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4092
  case True
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4093
  show ?thesis unfolding True unfolding pseudo_divmod_def pseudo_divmod_list_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4094
  by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4095
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4096
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4097
lemma pseudo_mod_main_list: "snd (pseudo_divmod_main_list l q 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4098
  xs ys n) = pseudo_mod_main_list l xs ys n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4099
  by (induct n arbitrary: l q xs ys, auto simp: Let_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4100
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4101
lemma pseudo_mod_impl[code]: "pseudo_mod f g =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4102
  poly_of_list (pseudo_mod_list (coeffs f) (coeffs g))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4103
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4104
  have snd_case: "\<And> f g p. snd ((\<lambda> (x,y). (f x, g y)) p) = g (snd p)" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4105
    by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4106
  show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4107
  unfolding pseudo_mod_def pseudo_divmod_impl pseudo_divmod_list_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4108
    pseudo_mod_list_def Let_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4109
  by (simp add: snd_case pseudo_mod_main_list)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4110
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4111
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4112
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4113
(* *************** *)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4114
subsubsection \<open>Improved Code-Equations for Polynomial (Pseudo) Division\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4115
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4116
lemma pdivmod_pdivmodrel: "eucl_rel_poly p q (r, s) \<longleftrightarrow> (p div q, p mod q) = (r, s)"
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4117
  by (metis eucl_rel_poly eucl_rel_poly_unique)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4118
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4119
lemma pdivmod_via_pseudo_divmod: "(f div g, f mod g) = (if g = 0 then (0,f) 
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4120
     else let 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4121
       ilc = inverse (coeff g (degree g));       
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4122
       h = smult ilc g;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4123
       (q,r) = pseudo_divmod f h
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4124
     in (smult ilc q, r))" (is "?l = ?r")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4125
proof (cases "g = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4126
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4127
  define lc where "lc = inverse (coeff g (degree g))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4128
  define h where "h = smult lc g"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4129
  from False have h1: "coeff h (degree h) = 1" and lc: "lc \<noteq> 0" unfolding h_def lc_def by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4130
  hence h0: "h \<noteq> 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4131
  obtain q r where p: "pseudo_divmod f h = (q,r)" by force
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4132
  from False have id: "?r = (smult lc q, r)" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4133
    unfolding Let_def h_def[symmetric] lc_def[symmetric] p by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4134
  from pseudo_divmod[OF h0 p, unfolded h1] 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4135
  have f: "f = h * q + r" and r: "r = 0 \<or> degree r < degree h" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4136
  have "eucl_rel_poly f h (q, r)" unfolding eucl_rel_poly_iff using f r h0 by auto
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4137
  hence "(f div h, f mod h) = (q,r)" by (simp add: pdivmod_pdivmodrel)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4138
  hence "(f div g, f mod g) = (smult lc q, r)" 
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4139
    unfolding h_def div_smult_right[OF lc] mod_smult_right[OF lc]
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4140
    using lc by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4141
  with id show ?thesis by auto
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4142
qed simp
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4143
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4144
lemma pdivmod_via_pseudo_divmod_list: "(f div g, f mod g) = (let 
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4145
  cg = coeffs g
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4146
  in if cg = [] then (0,f)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4147
     else let 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4148
       cf = coeffs f;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4149
       ilc = inverse (last cg);       
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4150
       ch = map (op * ilc) cg;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4151
       (q,r) = pseudo_divmod_main_list 1 [] (rev cf) (rev ch) (1 + length cf - length cg)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4152
     in (poly_of_list (map (op * ilc) q), poly_of_list (rev r)))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4153
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4154
  note d = pdivmod_via_pseudo_divmod
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4155
      pseudo_divmod_impl pseudo_divmod_list_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4156
  show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4157
  proof (cases "g = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4158
    case True thus ?thesis unfolding d by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4159
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4160
    case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4161
    define ilc where "ilc = inverse (coeff g (degree g))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4162
    from False have ilc: "ilc \<noteq> 0" unfolding ilc_def by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4163
    with False have id: "(g = 0) = False" "(coeffs g = []) = False" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4164
      "last (coeffs g) = coeff g (degree g)" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4165
      "(coeffs (smult ilc g) = []) = False"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4166
      by (auto simp: last_coeffs_eq_coeff_degree) 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4167
    have id2: "hd (rev (coeffs (smult ilc g))) = 1"      
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4168
      by (subst hd_rev, insert id ilc, auto simp: coeffs_smult, subst last_map, auto simp: id ilc_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4169
    have id3: "length (coeffs (smult ilc g)) = length (coeffs g)" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4170
      "rev (coeffs (smult ilc g)) = rev (map (op * ilc) (coeffs g))" unfolding coeffs_smult using ilc by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4171
    obtain q r where pair: "pseudo_divmod_main_list 1 [] (rev (coeffs f)) (rev (map (op * ilc) (coeffs g)))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4172
           (1 + length (coeffs f) - length (coeffs g)) = (q,r)" by force
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4173
    show ?thesis unfolding d Let_def id if_False ilc_def[symmetric] map_prod_def[symmetric] id2 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4174
      unfolding id3 pair map_prod_def split by (auto simp: Poly_map)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4175
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4176
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4177
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4178
lemma pseudo_divmod_main_list_1: "pseudo_divmod_main_list 1 = divmod_poly_one_main_list"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4179
proof (intro ext, goal_cases)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4180
  case (1 q r d n)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4181
  {
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4182
    fix xs :: "'a list"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4183
    have "map (op * 1) xs = xs" by (induct xs, auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4184
  } note [simp] = this
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4185
  show ?case by (induct n arbitrary: q r d, auto simp: Let_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4186
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4187
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4188
fun divide_poly_main_list :: "'a::idom_divide \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4189
  \<Rightarrow> nat \<Rightarrow> 'a list" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4190
  "divide_poly_main_list lc q r d (Suc n) = (let
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4191
     cr = hd r
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4192
     in if cr = 0 then divide_poly_main_list lc (cCons cr q) (tl r) d n else let 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4193
     a = cr div lc;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4194
     qq = cCons a q;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4195
     rr = minus_poly_rev_list r (map (op * a) d)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4196
     in if hd rr = 0 then divide_poly_main_list lc qq (tl rr) d n else [])"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4197
| "divide_poly_main_list lc q r d 0 = q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4198
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4199
lemma divide_poly_main_list_simp[simp]: "divide_poly_main_list lc q r d (Suc n) = (let
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4200
     cr = hd r;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4201
     a = cr div lc;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4202
     qq = cCons a q;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4203
     rr = minus_poly_rev_list r (map (op * a) d)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4204
     in if hd rr = 0 then divide_poly_main_list lc qq (tl rr) d n else [])"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4205
  by (simp add: Let_def minus_zero_does_nothing)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4206
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4207
declare divide_poly_main_list.simps(1)[simp del]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4208
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4209
definition divide_poly_list :: "'a::idom_divide poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4210
  "divide_poly_list f g =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4211
    (let cg = coeffs g
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4212
     in if cg = [] then g
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4213
        else let cf = coeffs f; cgr = rev cg
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4214
          in poly_of_list (divide_poly_main_list (hd cgr) [] (rev cf) cgr (1 + length cf - length cg)))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4215
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4216
lemmas pdivmod_via_divmod_list = pdivmod_via_pseudo_divmod_list[unfolded pseudo_divmod_main_list_1]
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4217
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4218
lemma mod_poly_one_main_list: "snd (divmod_poly_one_main_list q r d n) = mod_poly_one_main_list r d n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4219
  by  (induct n arbitrary: q r d, auto simp: Let_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4220
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4221
lemma mod_poly_code[code]: "f mod g =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4222
    (let cg = coeffs g
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4223
     in if cg = [] then f
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4224
        else let cf = coeffs f; ilc = inverse (last cg); ch = map (op * ilc) cg;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4225
                 r = mod_poly_one_main_list (rev cf) (rev ch) (1 + length cf - length cg)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4226
             in poly_of_list (rev r))" (is "?l = ?r")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4227
proof -
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4228
  have "snd (f div g, f mod g) = ?r" unfolding pdivmod_via_divmod_list Let_def
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4229
     mod_poly_one_main_list [symmetric, of _ _ _ Nil] by (auto split: prod.splits)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4230
  then show ?thesis
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4231
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4232
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4233
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4234
definition div_field_poly_impl :: "'a :: field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4235
  "div_field_poly_impl f g = (
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4236
    let cg = coeffs g
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4237
      in if cg = [] then 0
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4238
        else let cf = coeffs f; ilc = inverse (last cg); ch = map (op * ilc) cg;
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4239
                 q = fst (divmod_poly_one_main_list [] (rev cf) (rev ch) (1 + length cf - length cg))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4240
             in poly_of_list ((map (op * ilc) q)))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4241
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4242
text \<open>We do not declare the following lemma as code equation, since then polynomial division 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4243
  on non-fields will no longer be executable. However, a code-unfold is possible, since 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4244
  \<open>div_field_poly_impl\<close> is a bit more efficient than the generic polynomial division.\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4245
lemma div_field_poly_impl[code_unfold]: "op div = div_field_poly_impl"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4246
proof (intro ext)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4247
  fix f g :: "'a poly"
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4248
  have "fst (f div g, f mod g) = div_field_poly_impl f g" unfolding 
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4249
    div_field_poly_impl_def pdivmod_via_divmod_list Let_def by (auto split: prod.splits)
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4250
  then show "f div g =  div_field_poly_impl f g"
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4251
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4252
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4253
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4254
lemma divide_poly_main_list:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4255
  assumes lc0: "lc \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4256
  and lc:"last d = lc"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4257
  and d:"d \<noteq> []"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4258
  and "n = (1 + length r - length d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4259
  shows 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4260
  "Poly (divide_poly_main_list lc q (rev r) (rev d) n) =
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4261
  divide_poly_main lc (monom 1 n * Poly q) (Poly r) (Poly d) (length r - 1) n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4262
using assms(4-)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4263
proof(induct "n" arbitrary: r q)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4264
case (Suc n r q)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4265
  have ifCond: "\<not> Suc (length r) \<le> length d" using Suc.prems by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4266
  have r: "r \<noteq> []"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4267
    using ifCond d using Suc_leI length_greater_0_conv list.size(3) by fastforce
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4268
  then obtain rr lcr where r: "r = rr @ [lcr]" by (cases r rule: rev_cases, auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4269
  from d lc obtain dd where d: "d = dd @ [lc]" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4270
    by (cases d rule: rev_cases, auto)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4271
  from Suc(2) ifCond have n: "n = 1 + length rr - length d" by (auto simp: r)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4272
  from ifCond have len: "length dd \<le> length rr" by (simp add: r d)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4273
  show ?case
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4274
  proof (cases "lcr div lc * lc = lcr")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4275
    case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4276
    thus ?thesis unfolding Suc(2)[symmetric] using r d
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4277
      by (auto simp add: Let_def nth_default_append)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4278
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4279
    case True
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4280
    hence id:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4281
    "?thesis = (Poly (divide_poly_main_list lc (cCons (lcr div lc) q)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4282
         (rev (rev (minus_poly_rev_list (rev rr) (rev (map (op * (lcr div lc)) dd))))) (rev d) n) = 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4283
      divide_poly_main lc
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4284
           (monom 1 (Suc n) * Poly q + monom (lcr div lc) n)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4285
           (Poly r - monom (lcr div lc) n * Poly d)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4286
           (Poly d) (length rr - 1) n)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4287
           using r d 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4288
      by (cases r rule: rev_cases; cases "d" rule: rev_cases; 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4289
        auto simp add: Let_def rev_map nth_default_append)      
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4290
    have cong: "\<And> x1 x2 x3 x4 y1 y2 y3 y4. x1 = y1 \<Longrightarrow> x2 = y2 \<Longrightarrow> x3 = y3 \<Longrightarrow> x4 = y4 \<Longrightarrow>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4291
      divide_poly_main lc x1 x2 x3 x4 n = divide_poly_main lc y1 y2 y3 y4 n" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4292
    show ?thesis unfolding id 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4293
    proof (subst Suc(1), simp add: n,
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4294
      subst minus_poly_rev_list, force simp: len, rule cong[OF _ _ refl], goal_cases)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4295
      case 2 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4296
      have "monom lcr (length rr) = monom (lcr div lc) (length rr - length dd) * monom lc (length dd)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4297
        by (simp add: mult_monom len True)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4298
      thus ?case unfolding r d Poly_append n ring_distribs
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4299
        by (auto simp: Poly_map smult_monom smult_monom_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4300
    qed (auto simp: len monom_Suc smult_monom)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4301
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4302
qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4303
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4304
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4305
lemma divide_poly_list[code]: "f div g = divide_poly_list f g" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4306
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4307
  note d = divide_poly_def divide_poly_list_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4308
  show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4309
  proof (cases "g = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4310
    case True
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4311
    show ?thesis unfolding d True by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4312
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4313
    case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4314
    then obtain cg lcg where cg: "coeffs g = cg @ [lcg]" by (cases "coeffs g" rule: rev_cases, auto)    
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4315
    with False have id: "(g = 0) = False" "(cg @ [lcg] = []) = False" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4316
    from cg False have lcg: "coeff g (degree g) = lcg" 
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4317
      using last_coeffs_eq_coeff_degree last_snoc by force
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4318
    with False have lcg0: "lcg \<noteq> 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4319
    from cg have ltp: "Poly (cg @ [lcg]) = g"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4320
     using Poly_coeffs [of g] by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4321
    show ?thesis unfolding d cg Let_def id if_False poly_of_list_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4322
      by (subst divide_poly_main_list, insert False cg lcg0, auto simp: lcg ltp,
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4323
      simp add: degree_eq_length_coeffs)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4324
  qed
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  4325
qed
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  4326
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  4327
no_notation cCons (infixr "##" 65)
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  4328
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  4329
end