src/HOL/Computational_Algebra/Polynomial.thy
author haftmann
Wed, 22 Aug 2018 13:33:50 +0000
changeset 68790 851a9d9746c6
parent 68534 914e1bc7369a
child 69022 e2858770997a
permissions -rw-r--r--
prefer constructive primitive_part over implicit content_decompose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65435
378175f44328 tuned headers;
wenzelm
parents: 65417
diff changeset
     1
(*  Title:      HOL/Computational_Algebra/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
65417
fc41a5650fb1 session containing computational algebra
haftmann
parents: 65390
diff changeset
    11
imports
68534
914e1bc7369a Now based on Complex_Main, not HOL.Deriv
paulson <lp15@cam.ac.uk>
parents: 68532
diff changeset
    12
  Complex_Main
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 65811
diff changeset
    13
  "HOL-Library.More_List"
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 65811
diff changeset
    14
  "HOL-Library.Infinite_Set"
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
    15
  Factorial_Ring
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    16
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    17
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
    18
subsection \<open>Auxiliary: operations for lists (later) representing coefficients\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    19
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    20
definition cCons :: "'a::zero \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixr "##" 65)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    21
  where "x ## xs = (if xs = [] \<and> x = 0 then [] else x # xs)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    22
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    23
lemma cCons_0_Nil_eq [simp]: "0 ## [] = []"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    24
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    25
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    26
lemma cCons_Cons_eq [simp]: "x ## y # ys = x # y # ys"
52380
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
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    29
lemma cCons_append_Cons_eq [simp]: "x ## xs @ y # ys = x # xs @ y # ys"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    30
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    31
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    32
lemma cCons_not_0_eq [simp]: "x \<noteq> 0 \<Longrightarrow> x ## xs = x # xs"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    33
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    34
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    35
lemma strip_while_not_0_Cons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    36
  "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
    37
proof (cases "x = 0")
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    38
  case False
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    39
  then show ?thesis by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    40
next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    41
  case True
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    42
  show ?thesis
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    43
  proof (induct xs rule: rev_induct)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    44
    case Nil
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    45
    with True show ?case by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    46
  next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    47
    case (snoc y ys)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    48
    then show ?case
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    49
      by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    50
  qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    51
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    52
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    53
lemma tl_cCons [simp]: "tl (x ## xs) = xs"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    54
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    55
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    56
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61260
diff changeset
    57
subsection \<open>Definition of type \<open>poly\<close>\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    58
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 60686
diff changeset
    59
typedef (overloaded) 'a poly = "{f :: nat \<Rightarrow> 'a::zero. \<forall>\<^sub>\<infinity> n. f n = 0}"
63433
wenzelm
parents: 63317
diff changeset
    60
  morphisms coeff Abs_poly
wenzelm
parents: 63317
diff changeset
    61
  by (auto intro!: ALL_MOST)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    62
59487
adaa430fc0f7 default abstypes and default abstract equations make technical (no_code) annotation superfluous
haftmann
parents: 58881
diff changeset
    63
setup_lifting type_definition_poly
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    64
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    65
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
    66
  by (simp add: coeff_inject [symmetric] fun_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    67
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    68
lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    69
  by (simp add: poly_eq_iff)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    70
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    71
lemma MOST_coeff_eq_0: "\<forall>\<^sub>\<infinity> n. coeff p n = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    72
  using coeff [of p] by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    73
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    74
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
    75
subsection \<open>Degree of a polynomial\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    76
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    77
definition degree :: "'a::zero poly \<Rightarrow> nat"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
    78
  where "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    79
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    80
lemma coeff_eq_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    81
  assumes "degree p < n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    82
  shows "coeff p n = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    83
proof -
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    84
  have "\<exists>n. \<forall>i>n. coeff p i = 0"
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    85
    using MOST_coeff_eq_0 by (simp add: MOST_nat)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    86
  then have "\<forall>i>degree p. coeff p i = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    87
    unfolding degree_def by (rule LeastI_ex)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    88
  with assms show ?thesis by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    89
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    90
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    91
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    92
  by (erule contrapos_np, rule coeff_eq_0, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    93
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    94
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    95
  unfolding degree_def by (erule Least_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    96
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    97
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
    98
  unfolding degree_def by (drule not_less_Least, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    99
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   100
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   101
subsection \<open>The zero polynomial\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   102
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   103
instantiation poly :: (zero) zero
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   104
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   105
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   106
lift_definition zero_poly :: "'a poly"
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   107
  is "\<lambda>_. 0"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   108
  by (rule MOST_I) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   109
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   110
instance ..
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   111
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   112
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   113
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   114
lemma coeff_0 [simp]: "coeff 0 n = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   115
  by transfer rule
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   116
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   117
lemma degree_0 [simp]: "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
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   125
  from \<open>p \<noteq> 0\<close> obtain n where "coeff p n \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   126
    by (auto simp add: poly_eq_iff)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   127
  then have "n \<le> degree p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   128
    by (rule le_degree)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   129
  with \<open>coeff p n \<noteq> 0\<close> and \<open>degree p = 0\<close> show "coeff p (degree p) \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   130
    by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   131
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   132
  case (Suc n)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   133
  from \<open>degree p = Suc n\<close> have "n < degree p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   134
    by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   135
  then have "\<exists>i>n. coeff p i \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   136
    by (rule less_degree_imp)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   137
  then obtain i where "n < i" and "coeff p i \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   138
    by blast
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   139
  from \<open>degree p = Suc n\<close> and \<open>n < i\<close> have "degree p \<le> i"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   140
    by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   141
  also from \<open>coeff p i \<noteq> 0\<close> have "i \<le> degree p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   142
    by (rule le_degree)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   143
  finally have "degree p = i" .
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   144
  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
   145
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   146
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   147
lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   148
  by (cases "p = 0") (simp_all add: leading_coeff_neq_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   149
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   150
lemma eq_zero_or_degree_less:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   151
  assumes "degree p \<le> n" and "coeff p n = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   152
  shows "p = 0 \<or> degree p < n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   153
proof (cases n)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   154
  case 0
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   155
  with \<open>degree p \<le> n\<close> and \<open>coeff p n = 0\<close> have "coeff p (degree p) = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   156
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   157
  then have "p = 0" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   158
  then show ?thesis ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   159
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   160
  case (Suc m)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   161
  from \<open>degree p \<le> n\<close> have "\<forall>i>n. coeff p i = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   162
    by (simp add: coeff_eq_0)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   163
  with \<open>coeff p n = 0\<close> have "\<forall>i\<ge>n. coeff p i = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   164
    by (simp add: le_less)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   165
  with \<open>n = Suc m\<close> have "\<forall>i>m. coeff p i = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   166
    by (simp add: less_eq_Suc_le)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   167
  then have "degree p \<le> m"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   168
    by (rule degree_le)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   169
  with \<open>n = Suc m\<close> have "degree p < n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   170
    by (simp add: less_Suc_eq_le)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   171
  then show ?thesis ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   172
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   173
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   174
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
   175
  using eq_zero_or_degree_less by fastforce
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   176
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   177
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   178
subsection \<open>List-style constructor for polynomials\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   179
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   180
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
   181
  is "\<lambda>a p. case_nat a (coeff p)"
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   182
  by (rule MOST_SucD) (simp add: MOST_coeff_eq_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   183
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   184
lemmas coeff_pCons = pCons.rep_eq
29455
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   185
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   186
lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   187
  by transfer simp
29455
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   188
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   189
lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   190
  by (simp add: coeff_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   191
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   192
lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   193
  by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   194
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   195
lemma degree_pCons_eq: "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
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   200
lemma degree_pCons_0: "degree (pCons a 0) = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   201
  apply (rule order_antisym [OF _ le0])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   202
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   203
  done
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   204
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   205
lemma degree_pCons_eq_if [simp]: "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   206
  apply (cases "p = 0", simp_all)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   207
  apply (rule order_antisym [OF _ le0])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   208
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   209
  apply (rule order_antisym [OF degree_pCons_le])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   210
  apply (rule le_degree, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   211
  done
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   212
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   213
lemma pCons_0_0 [simp]: "pCons 0 0 = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   214
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   215
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   216
lemma pCons_eq_iff [simp]: "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   217
proof safe
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   218
  assume "pCons a p = pCons b q"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   219
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   220
    by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   221
  then show "a = b"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   222
    by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   223
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   224
  assume "pCons a p = pCons b q"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   225
  then have "coeff (pCons a p) (Suc n) = coeff (pCons b q) (Suc n)" for n
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   226
    by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   227
  then show "p = q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   228
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   229
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   230
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   231
lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   232
  using pCons_eq_iff [of a p 0 0] by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   233
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   234
lemma pCons_cases [cases type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   235
  obtains (pCons) a q where "p = pCons a q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   236
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   237
  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
   238
    by transfer
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   239
      (simp_all add: MOST_inj[where f=Suc and P="\<lambda>n. p n = 0" for p] fun_eq_iff Abs_poly_inverse
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   240
        split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   241
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   242
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   243
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   244
  assumes zero: "P 0"
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   245
  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
   246
  shows "P p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   247
proof (induct p rule: measure_induct_rule [where f=degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   248
  case (less p)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   249
  obtain a q where "p = pCons a q" by (rule pCons_cases)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   250
  have "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   251
  proof (cases "q = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   252
    case True
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   253
    then show "P q" by (simp add: zero)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   254
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   255
    case False
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   256
    then have "degree (pCons a q) = Suc (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   257
      by (rule degree_pCons_eq)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   258
    with \<open>p = pCons a q\<close> have "degree q < degree p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   259
      by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   260
    then show "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   261
      by (rule less.hyps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   262
  qed
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   263
  have "P (pCons a q)"
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   264
  proof (cases "a \<noteq> 0 \<or> q \<noteq> 0")
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   265
    case True
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   266
    with \<open>P q\<close> show ?thesis by (auto intro: pCons)
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   267
  next
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   268
    case False
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   269
    with zero show ?thesis by simp
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   270
  qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   271
  with \<open>p = pCons a q\<close> show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   272
    by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   273
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   274
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   275
lemma degree_eq_zeroE:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   276
  fixes p :: "'a::zero poly"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   277
  assumes "degree p = 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   278
  obtains a where "p = pCons a 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   279
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   280
  obtain a q where p: "p = pCons a q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   281
    by (cases p)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   282
  with assms have "q = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   283
    by (cases "q = 0") simp_all
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   284
  with p have "p = pCons a 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   285
    by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   286
  then show thesis ..
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   287
qed
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   288
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   289
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   290
subsection \<open>Quickcheck generator for polynomials\<close>
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   291
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   292
quickcheck_generator poly constructors: "0 :: _ poly", pCons
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   293
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62352
diff changeset
   294
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   295
subsection \<open>List-style syntax for polynomials\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   296
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   297
syntax "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   298
translations
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   299
  "[:x, xs:]" \<rightleftharpoons> "CONST pCons x [:xs:]"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   300
  "[:x:]" \<rightleftharpoons> "CONST pCons x 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   301
  "[:x:]" \<leftharpoondown> "CONST pCons x (_constrain 0 t)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   302
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   303
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   304
subsection \<open>Representation of polynomials by lists of coefficients\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   305
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   306
primrec Poly :: "'a::zero list \<Rightarrow> 'a poly"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   307
  where
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   308
    [code_post]: "Poly [] = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   309
  | [code_post]: "Poly (a # as) = pCons a (Poly as)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   310
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   311
lemma Poly_replicate_0 [simp]: "Poly (replicate n 0) = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   312
  by (induct n) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   313
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   314
lemma Poly_eq_0: "Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   315
  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
   316
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   317
lemma Poly_append_replicate_zero [simp]: "Poly (as @ replicate n 0) = Poly as"
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   318
  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
   319
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   320
lemma Poly_snoc_zero [simp]: "Poly (as @ [0]) = Poly as"
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   321
  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
   322
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   323
lemma Poly_cCons_eq_pCons_Poly [simp]: "Poly (a ## p) = pCons a (Poly p)"
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   324
  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
   325
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   326
lemma Poly_on_rev_starting_with_0 [simp]: "hd as = 0 \<Longrightarrow> Poly (rev (tl as)) = Poly (rev as)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   327
  by (cases as) simp_all
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   328
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   329
lemma degree_Poly: "degree (Poly xs) \<le> length xs"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   330
  by (induct xs) simp_all
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   331
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   332
lemma coeff_Poly_eq [simp]: "coeff (Poly xs) = nth_default 0 xs"
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   333
  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
   334
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   335
definition coeffs :: "'a poly \<Rightarrow> 'a::zero list"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   336
  where "coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   337
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   338
lemma coeffs_eq_Nil [simp]: "coeffs p = [] \<longleftrightarrow> p = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   339
  by (simp add: coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   340
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   341
lemma not_0_coeffs_not_Nil: "p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   342
  by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   343
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   344
lemma coeffs_0_eq_Nil [simp]: "coeffs 0 = []"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   345
  by simp
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   346
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   347
lemma coeffs_pCons_eq_cCons [simp]: "coeffs (pCons a p) = a ## coeffs p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   348
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   349
  have *: "\<forall>m\<in>set ms. m > 0 \<Longrightarrow> map (case_nat x f) ms = map f (map (\<lambda>n. n - 1) ms)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   350
    for ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   351
    by (induct ms) (auto split: nat.split)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   352
  show ?thesis
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   353
    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
   354
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   355
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   356
lemma length_coeffs: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = degree p + 1"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   357
  by (simp add: coeffs_def)
64860
4d56170d97b3 generalized definition
haftmann
parents: 64849
diff changeset
   358
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   359
lemma coeffs_nth: "p \<noteq> 0 \<Longrightarrow> n \<le> degree p \<Longrightarrow> coeffs p ! n = coeff p n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   360
  by (auto simp: coeffs_def simp del: upt_Suc)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   361
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   362
lemma coeff_in_coeffs: "p \<noteq> 0 \<Longrightarrow> n \<le> degree p \<Longrightarrow> coeff p n \<in> set (coeffs p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   363
  using coeffs_nth [of p n, symmetric] by (simp add: length_coeffs)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   364
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   365
lemma not_0_cCons_eq [simp]: "p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   366
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   367
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   368
lemma Poly_coeffs [simp, code abstype]: "Poly (coeffs p) = p"
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   369
  by (induct p) auto
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   370
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   371
lemma coeffs_Poly [simp]: "coeffs (Poly as) = strip_while (HOL.eq 0) as"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   372
proof (induct as)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   373
  case Nil
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   374
  then show ?case by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   375
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   376
  case (Cons a as)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   377
  from replicate_length_same [of as 0] have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   378
    by (auto dest: sym [of _ as])
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   379
  with Cons show ?case by auto
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   380
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   381
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   382
lemma no_trailing_coeffs [simp]:
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   383
  "no_trailing (HOL.eq 0) (coeffs p)"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   384
  by (induct p)  auto
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   385
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   386
lemma strip_while_coeffs [simp]:
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   387
  "strip_while (HOL.eq 0) (coeffs p) = coeffs p"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   388
  by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   389
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   390
lemma coeffs_eq_iff: "p = q \<longleftrightarrow> coeffs p = coeffs q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   391
  (is "?P \<longleftrightarrow> ?Q")
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   392
proof
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   393
  assume ?P
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   394
  then show ?Q by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   395
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   396
  assume ?Q
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   397
  then have "Poly (coeffs p) = Poly (coeffs q)" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   398
  then show ?P by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   399
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   400
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   401
lemma nth_default_coeffs_eq: "nth_default 0 (coeffs p) = coeff p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   402
  by (simp add: fun_eq_iff coeff_Poly_eq [symmetric])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   403
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   404
lemma [code]: "coeff p = nth_default 0 (coeffs p)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   405
  by (simp add: nth_default_coeffs_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   406
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   407
lemma coeffs_eqI:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   408
  assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n"
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   409
  assumes zero: "no_trailing (HOL.eq 0) xs"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   410
  shows "coeffs p = xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   411
proof -
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   412
  from coeff have "p = Poly xs"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   413
    by (simp add: poly_eq_iff)
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   414
  with zero show ?thesis by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   415
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   416
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   417
lemma degree_eq_length_coeffs [code]: "degree p = length (coeffs p) - 1"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   418
  by (simp add: coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   419
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   420
lemma length_coeffs_degree: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   421
  by (induct p) (auto simp: cCons_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   422
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   423
lemma [code abstract]: "coeffs 0 = []"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   424
  by (fact coeffs_0_eq_Nil)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   425
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   426
lemma [code abstract]: "coeffs (pCons a p) = a ## coeffs p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   427
  by (fact coeffs_pCons_eq_cCons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   428
65811
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   429
lemma set_coeffs_subset_singleton_0_iff [simp]:
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   430
  "set (coeffs p) \<subseteq> {0} \<longleftrightarrow> p = 0"
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   431
  by (auto simp add: coeffs_def intro: classical)
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   432
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   433
lemma set_coeffs_not_only_0 [simp]:
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   434
  "set (coeffs p) \<noteq> {0}"
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   435
  by (auto simp add: set_eq_subset)
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   436
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   437
lemma forall_coeffs_conv:
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   438
  "(\<forall>n. P (coeff p n)) \<longleftrightarrow> (\<forall>c \<in> set (coeffs p). P c)" if "P 0"
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   439
  using that by (auto simp add: coeffs_def)
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   440
    (metis atLeastLessThan_iff coeff_eq_0 not_less_iff_gr_or_eq zero_le)
2653f1cd8775 more lemmas
haftmann
parents: 65577
diff changeset
   441
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   442
instantiation poly :: ("{zero, equal}") equal
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   443
begin
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   444
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   445
definition [code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   446
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   447
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   448
  by standard (simp add: equal equal_poly_def coeffs_eq_iff)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   449
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   450
end
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   451
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   452
lemma [code nbe]: "HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   453
  by (fact equal_refl)
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   454
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   455
definition is_zero :: "'a::zero poly \<Rightarrow> bool"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   456
  where [code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   457
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   458
lemma is_zero_null [code_abbrev]: "is_zero p \<longleftrightarrow> p = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   459
  by (simp add: is_zero_def null_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   460
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   461
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   462
subsubsection \<open>Reconstructing the polynomial from the list\<close>
63145
703edebd1d92 isabelle update_cartouches -c -t;
wenzelm
parents: 63060
diff changeset
   463
  \<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
   464
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   465
definition poly_of_list :: "'a::comm_monoid_add list \<Rightarrow> 'a poly"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   466
  where [simp]: "poly_of_list = Poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   467
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   468
lemma poly_of_list_impl [code abstract]: "coeffs (poly_of_list as) = strip_while (HOL.eq 0) as"
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   469
  by simp
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
   470
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   471
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   472
subsection \<open>Fold combinator for polynomials\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   473
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   474
definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   475
  where "fold_coeffs f p = foldr f (coeffs p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   476
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   477
lemma fold_coeffs_0_eq [simp]: "fold_coeffs f 0 = id"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   478
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   479
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   480
lemma fold_coeffs_pCons_eq [simp]: "f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   481
  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
   482
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   483
lemma fold_coeffs_pCons_0_0_eq [simp]: "fold_coeffs f (pCons 0 0) = id"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   484
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   485
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   486
lemma fold_coeffs_pCons_coeff_not_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   487
  "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
   488
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   489
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   490
lemma fold_coeffs_pCons_not_0_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   491
  "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
   492
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   493
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   494
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   495
subsection \<open>Canonical morphism on polynomials -- evaluation\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   496
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   497
definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   498
  where "poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" \<comment> \<open>The Horner Schema\<close>
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   499
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   500
lemma poly_0 [simp]: "poly 0 x = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   501
  by (simp add: poly_def)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   502
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   503
lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   504
  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
   505
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   506
lemma poly_altdef: "poly p x = (\<Sum>i\<le>degree p. coeff p i * x ^ i)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   507
  for x :: "'a::{comm_semiring_0,semiring_1}"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   508
proof (induction p rule: pCons_induct)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   509
  case 0
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   510
  then show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   511
    by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   512
next
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   513
  case (pCons a p)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   514
  show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   515
  proof (cases "p = 0")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   516
    case True
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   517
    then show ?thesis by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   518
  next
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   519
    case False
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   520
    let ?p' = "pCons a p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   521
    note poly_pCons[of a p x]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   522
    also note pCons.IH
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   523
    also have "a + x * (\<Sum>i\<le>degree p. coeff p i * x ^ i) =
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   524
        coeff ?p' 0 * x^0 + (\<Sum>i\<le>degree p. coeff ?p' (Suc i) * x^Suc i)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   525
      by (simp add: field_simps sum_distrib_left coeff_pCons)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   526
    also note sum_atMost_Suc_shift[symmetric]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   527
    also note degree_pCons_eq[OF \<open>p \<noteq> 0\<close>, of a, symmetric]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   528
    finally show ?thesis .
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   529
  qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   530
qed
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   531
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   532
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
   533
  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
   534
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   535
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   536
subsection \<open>Monomials\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   537
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   538
lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   539
  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
   540
  by (simp add: MOST_iff_cofinite)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   541
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   542
lemma coeff_monom [simp]: "coeff (monom a m) n = (if m = n then a else 0)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   543
  by transfer rule
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   544
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   545
lemma monom_0: "monom a 0 = pCons a 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   546
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   547
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   548
lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   549
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   550
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   551
lemma monom_eq_0 [simp]: "monom 0 n = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   552
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   553
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   554
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   555
  by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   556
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   557
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
   558
  by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   559
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   560
lemma degree_monom_le: "degree (monom a n) \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   561
  by (rule degree_le, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   562
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   563
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   564
  apply (rule order_antisym [OF degree_monom_le])
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   565
  apply (rule le_degree)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   566
  apply simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   567
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   568
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   569
lemma coeffs_monom [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   570
  "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   571
  by (induct n) (simp_all add: monom_0 monom_Suc)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   572
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   573
lemma fold_coeffs_monom [simp]: "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   574
  by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   575
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   576
lemma poly_monom: "poly (monom a n) x = a * x ^ n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   577
  for a x :: "'a::comm_semiring_1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   578
  by (cases "a = 0", simp_all) (induct n, simp_all add: mult.left_commute poly_def)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   579
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   580
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
   581
  by (auto simp: poly_eq_iff)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   582
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
   583
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
   584
  using monom_eq_iff'[of c n d 0] by (simp add: monom_0)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   585
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   586
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   587
subsection \<open>Leading coefficient\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   588
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   589
abbreviation lead_coeff:: "'a::zero poly \<Rightarrow> 'a"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   590
  where "lead_coeff p \<equiv> coeff p (degree p)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   591
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   592
lemma lead_coeff_pCons[simp]:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   593
  "p \<noteq> 0 \<Longrightarrow> lead_coeff (pCons a p) = lead_coeff p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   594
  "p = 0 \<Longrightarrow> lead_coeff (pCons a p) = a"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   595
  by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   596
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   597
lemma lead_coeff_monom [simp]: "lead_coeff (monom c n) = c"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   598
  by (cases "c = 0") (simp_all add: degree_monom_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   599
66799
haftmann
parents: 66550
diff changeset
   600
lemma last_coeffs_eq_coeff_degree:
haftmann
parents: 66550
diff changeset
   601
  "last (coeffs p) = lead_coeff p" if "p \<noteq> 0"
haftmann
parents: 66550
diff changeset
   602
  using that by (simp add: coeffs_def)
haftmann
parents: 66550
diff changeset
   603
  
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   604
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   605
subsection \<open>Addition and subtraction\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   606
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   607
instantiation poly :: (comm_monoid_add) comm_monoid_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   608
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   609
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   610
lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   611
  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
   612
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   613
  fix q p :: "'a poly"
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   614
  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
   615
    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
   616
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   617
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   618
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
   619
  by (simp add: plus_poly.rep_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   620
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   621
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   622
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   623
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   624
  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
   625
    by (simp add: poly_eq_iff add.assoc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   626
  show "p + q = q + p"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57482
diff changeset
   627
    by (simp add: poly_eq_iff add.commute)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   628
  show "0 + p = p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   629
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   630
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   631
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   632
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   633
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   634
instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   635
begin
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   636
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   637
lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   638
  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
   639
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   640
  fix q p :: "'a poly"
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   641
  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
   642
    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
   643
qed
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   644
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   645
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
   646
  by (simp add: minus_poly.rep_eq)
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   647
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   648
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   649
proof
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   650
  fix p q r :: "'a poly"
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   651
  show "p + q - p = q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   652
    by (simp add: poly_eq_iff)
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   653
  show "p - q - r = p - (q + r)"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   654
    by (simp add: poly_eq_iff diff_diff_eq)
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   655
qed
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   656
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   657
end
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   658
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   659
instantiation poly :: (ab_group_add) ab_group_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   660
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   661
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   662
lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   663
  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
   664
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   665
  fix p :: "'a poly"
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   666
  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
   667
    using MOST_coeff_eq_0 by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   668
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   669
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   670
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   671
  by (simp add: uminus_poly.rep_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   672
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   673
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   674
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   675
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   676
  show "- p + p = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   677
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   678
  show "p - q = p + - q"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 52380
diff changeset
   679
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   680
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   681
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   682
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   683
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   684
lemma add_pCons [simp]: "pCons a p + pCons b q = pCons (a + b) (p + q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   685
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   686
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   687
lemma minus_pCons [simp]: "- pCons a p = pCons (- a) (- p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   688
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   689
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   690
lemma diff_pCons [simp]: "pCons a p - pCons b q = pCons (a - b) (p - q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   691
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   692
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   693
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   694
  by (rule degree_le) (auto simp add: coeff_eq_0)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   695
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   696
lemma degree_add_le: "degree p \<le> n \<Longrightarrow> degree q \<le> n \<Longrightarrow> degree (p + q) \<le> n"
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   697
  by (auto intro: order_trans degree_add_le_max)
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   698
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   699
lemma degree_add_less: "degree p < n \<Longrightarrow> degree q < n \<Longrightarrow> degree (p + q) < n"
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   700
  by (auto intro: le_less_trans degree_add_le_max)
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   701
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   702
lemma degree_add_eq_right: "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   703
  apply (cases "q = 0")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   704
   apply simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   705
  apply (rule order_antisym)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   706
   apply (simp add: degree_add_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   707
  apply (rule le_degree)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   708
  apply (simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   709
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   710
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   711
lemma degree_add_eq_left: "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   712
  using degree_add_eq_right [of q p] by (simp add: add.commute)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   713
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   714
lemma degree_minus [simp]: "degree (- p) = degree p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   715
  by (simp add: degree_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   716
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   717
lemma lead_coeff_add_le: "degree p < degree q \<Longrightarrow> lead_coeff (p + q) = lead_coeff q"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   718
  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
   719
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   720
lemma lead_coeff_minus: "lead_coeff (- p) = - lead_coeff p"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   721
  by (metis coeff_minus degree_minus)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   722
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   723
lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   724
  for p q :: "'a::ab_group_add poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   725
  using degree_add_le [where p=p and q="-q"] by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   726
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   727
lemma degree_diff_le: "degree p \<le> n \<Longrightarrow> degree q \<le> n \<Longrightarrow> degree (p - q) \<le> n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   728
  for p q :: "'a::ab_group_add poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   729
  using degree_add_le [of p n "- q"] by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   730
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   731
lemma degree_diff_less: "degree p < n \<Longrightarrow> degree q < n \<Longrightarrow> degree (p - q) < n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   732
  for p q :: "'a::ab_group_add poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   733
  using degree_add_less [of p n "- q"] by simp
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   734
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   735
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   736
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   737
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   738
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   739
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   740
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   741
lemma minus_monom: "- monom a n = monom (- a) n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   742
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   743
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   744
lemma coeff_sum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   745
  by (induct A rule: infinite_finite_induct) simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   746
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   747
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
   748
  by (rule poly_eqI) (simp add: coeff_sum)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   749
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   750
fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   751
  where
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   752
    "plus_coeffs xs [] = xs"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   753
  | "plus_coeffs [] ys = ys"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   754
  | "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   755
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   756
lemma coeffs_plus_eq_plus_coeffs [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   757
  "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   758
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   759
  have *: "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   760
    for xs ys :: "'a list" and n
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   761
  proof (induct xs ys arbitrary: n rule: plus_coeffs.induct)
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   762
    case (3 x xs y ys n)
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   763
    then show ?case
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   764
      by (cases n) (auto simp add: cCons_def)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   765
  qed simp_all
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   766
  have **: "no_trailing (HOL.eq 0) (plus_coeffs xs ys)"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   767
    if "no_trailing (HOL.eq 0) xs" and "no_trailing (HOL.eq 0) ys"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   768
    for xs ys :: "'a list"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   769
    using that by (induct xs ys rule: plus_coeffs.induct) (simp_all add: cCons_def)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   770
  show ?thesis
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   771
    by (rule coeffs_eqI) (auto simp add: * nth_default_coeffs_eq intro: **)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   772
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   773
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   774
lemma coeffs_uminus [code abstract]:
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   775
  "coeffs (- p) = map uminus (coeffs p)"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   776
proof -
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   777
  have eq_0: "HOL.eq 0 \<circ> uminus = HOL.eq (0::'a)"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   778
    by (simp add: fun_eq_iff)
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   779
  show ?thesis
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   780
    by (rule coeffs_eqI) (simp_all add: nth_default_map_eq nth_default_coeffs_eq no_trailing_map eq_0)
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   781
qed
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   782
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   783
lemma [code]: "p - q = p + - q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   784
  for p q :: "'a::ab_group_add poly"
59557
ebd8ecacfba6 establish unique preferred fact names
haftmann
parents: 59487
diff changeset
   785
  by (fact diff_conv_add_uminus)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   786
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   787
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   788
  apply (induct p arbitrary: q)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   789
   apply simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   790
  apply (case_tac q, simp, simp add: algebra_simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   791
  done
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   792
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   793
lemma poly_minus [simp]: "poly (- p) x = - poly p x"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   794
  for x :: "'a::comm_ring"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   795
  by (induct p) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   796
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   797
lemma poly_diff [simp]: "poly (p - q) x = poly p x - poly q x"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   798
  for x :: "'a::comm_ring"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 52380
diff changeset
   799
  using poly_add [of p "- q" x] by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   800
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
   801
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
   802
  by (induct A rule: infinite_finite_induct) simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   803
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   804
lemma degree_sum_le: "finite S \<Longrightarrow> (\<And>p. p \<in> S \<Longrightarrow> degree (f p) \<le> n) \<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
   805
proof (induct S rule: finite_induct)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   806
  case empty
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   807
  then show ?case by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   808
next
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   809
  case (insert p S)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   810
  then have "degree (sum f S) \<le> n" "degree (f p) \<le> n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   811
    by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   812
  then show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   813
    unfolding sum.insert[OF insert(1-2)] by (metis degree_add_le)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   814
qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   815
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   816
lemma poly_as_sum_of_monoms':
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   817
  assumes "degree p \<le> n"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   818
  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
   819
proof -
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   820
  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
   821
    by auto
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   822
  from assms show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   823
    by (simp add: poly_eq_iff coeff_sum coeff_eq_0 sum.If_cases eq
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   824
        if_distrib[where f="\<lambda>x. x * a" for a])
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   825
qed
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   826
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   827
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
   828
  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
   829
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   830
lemma Poly_snoc: "Poly (xs @ [x]) = Poly xs + monom x (length xs)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   831
  by (induct xs) (simp_all add: monom_0 monom_Suc)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   832
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   833
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   834
subsection \<open>Multiplication by a constant, polynomial multiplication and the unit polynomial\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   835
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   836
lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   837
  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
   838
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   839
  fix a :: 'a and p :: "'a poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   840
  show "\<forall>\<^sub>\<infinity> i. a * coeff p i = 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
   841
    using MOST_coeff_eq_0[of p] by eventually_elim simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   842
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   843
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   844
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   845
  by (simp add: smult.rep_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   846
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   847
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   848
  by (rule degree_le) (simp add: coeff_eq_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   849
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   850
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   851
  by (rule poly_eqI) (simp add: mult.assoc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   852
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   853
lemma smult_0_right [simp]: "smult a 0 = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   854
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   855
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   856
lemma smult_0_left [simp]: "smult 0 p = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   857
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   858
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   859
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   860
  by (rule poly_eqI) simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   861
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   862
lemma smult_add_right: "smult a (p + q) = smult a p + smult a q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   863
  by (rule poly_eqI) (simp add: algebra_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   864
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   865
lemma smult_add_left: "smult (a + b) p = smult a p + smult b p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   866
  by (rule poly_eqI) (simp add: algebra_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   867
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   868
lemma smult_minus_right [simp]: "smult a (- p) = - smult a p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   869
  for a :: "'a::comm_ring"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   870
  by (rule poly_eqI) simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   871
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   872
lemma smult_minus_left [simp]: "smult (- a) p = - smult a p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   873
  for a :: "'a::comm_ring"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   874
  by (rule poly_eqI) simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   875
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   876
lemma smult_diff_right: "smult a (p - q) = smult a p - smult a q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   877
  for a :: "'a::comm_ring"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   878
  by (rule poly_eqI) (simp add: algebra_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   879
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   880
lemma smult_diff_left: "smult (a - b) p = smult a p - smult b p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   881
  for a b :: "'a::comm_ring"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   882
  by (rule poly_eqI) (simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   883
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   884
lemmas smult_distribs =
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   885
  smult_add_left smult_add_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   886
  smult_diff_left smult_diff_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   887
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   888
lemma smult_pCons [simp]: "smult a (pCons b p) = pCons (a * b) (smult a p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   889
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   890
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   891
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   892
  by (induct n) (simp_all add: monom_0 monom_Suc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   893
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
   894
lemma smult_Poly: "smult c (Poly xs) = Poly (map (( * ) c) xs)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   895
  by (auto simp: poly_eq_iff nth_default_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   896
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   897
lemma degree_smult_eq [simp]: "degree (smult a p) = (if a = 0 then 0 else degree p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   898
  for a :: "'a::{comm_semiring_0,semiring_no_zero_divisors}"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   899
  by (cases "a = 0") (simp_all add: degree_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   900
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   901
lemma smult_eq_0_iff [simp]: "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   902
  for a :: "'a::{comm_semiring_0,semiring_no_zero_divisors}"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   903
  by (simp add: poly_eq_iff)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   904
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   905
lemma coeffs_smult [code abstract]:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   906
  "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   907
  for p :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   908
proof -
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   909
  have eq_0: "HOL.eq 0 \<circ> times a = HOL.eq (0::'a)" if "a \<noteq> 0"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   910
    using that by (simp add: fun_eq_iff)
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   911
  show ?thesis
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   912
    by (rule coeffs_eqI) (auto simp add: no_trailing_map nth_default_map_eq nth_default_coeffs_eq eq_0)
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
   913
qed  
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   914
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   915
lemma smult_eq_iff:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   916
  fixes b :: "'a :: field"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   917
  assumes "b \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   918
  shows "smult a p = smult b q \<longleftrightarrow> smult (a / b) p = q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   919
    (is "?lhs \<longleftrightarrow> ?rhs")
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   920
proof
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   921
  assume ?lhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   922
  also from assms have "smult (inverse b) \<dots> = q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   923
    by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   924
  finally show ?rhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   925
    by (simp add: field_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   926
next
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   927
  assume ?rhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   928
  with assms show ?lhs by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   929
qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
   930
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   931
instantiation poly :: (comm_semiring_0) comm_semiring_0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   932
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   933
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   934
definition "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
   935
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   936
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   937
  by (simp add: times_poly_def)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   938
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   939
lemma mult_pCons_left [simp]: "pCons a p * q = smult a q + pCons 0 (p * q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   940
  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
   941
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   942
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   943
  by (induct p) (simp_all add: mult_poly_0_left)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   944
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   945
lemma mult_pCons_right [simp]: "p * pCons a q = smult a p + pCons 0 (p * q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   946
  by (induct p) (simp_all add: mult_poly_0_left algebra_simps)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   947
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   948
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   949
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   950
lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   951
  by (induct p) (simp_all add: mult_poly_0 smult_add_right)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   952
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   953
lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   954
  by (induct q) (simp_all add: mult_poly_0 smult_add_right)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   955
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   956
lemma mult_poly_add_left: "(p + q) * r = p * r + q * r"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   957
  for p q r :: "'a poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   958
  by (induct r) (simp_all add: mult_poly_0 smult_distribs algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   959
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   960
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   961
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   962
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   963
  show 0: "0 * p = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   964
    by (rule mult_poly_0_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   965
  show "p * 0 = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   966
    by (rule mult_poly_0_right)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   967
  show "(p + q) * r = p * r + q * r"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   968
    by (rule mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   969
  show "(p * q) * r = p * (q * r)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   970
    by (induct p) (simp_all add: mult_poly_0 mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   971
  show "p * q = q * p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   972
    by (induct p) (simp_all add: mult_poly_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   973
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   974
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   975
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   976
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   977
lemma coeff_mult_degree_sum:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   978
  "coeff (p * q) (degree p + degree q) = coeff p (degree p) * coeff q (degree q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   979
  by (induct p) (simp_all add: coeff_eq_0)
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   980
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   981
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
   982
proof
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   983
  fix p q :: "'a poly"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   984
  assume "p \<noteq> 0" and "q \<noteq> 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   985
  have "coeff (p * q) (degree p + degree q) = coeff p (degree p) * coeff q (degree q)"
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   986
    by (rule coeff_mult_degree_sum)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   987
  also from \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   988
    by simp
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   989
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   990
  then show "p * q \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   991
    by (simp add: poly_eq_iff)
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   992
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
   993
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   994
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   995
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   996
lemma coeff_mult: "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   997
proof (induct p arbitrary: n)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   998
  case 0
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
   999
  show ?case by simp
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1000
next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1001
  case (pCons a p n)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1002
  then show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1003
    by (cases n) (simp_all add: sum_atMost_Suc_shift del: sum_atMost_Suc)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1004
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1005
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
  1006
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1007
  apply (rule degree_le)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1008
  apply (induct p)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1009
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1010
  apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1011
  done
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1012
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1013
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1014
  by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1015
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1016
instantiation poly :: (comm_semiring_1) comm_semiring_1
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1017
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1018
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1019
lift_definition one_poly :: "'a poly"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1020
  is "\<lambda>n. of_bool (n = 0)"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1021
  by (rule MOST_SucD) simp
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1022
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1023
lemma coeff_1 [simp]:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1024
  "coeff 1 n = of_bool (n = 0)"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1025
  by (simp add: one_poly.rep_eq)
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1026
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1027
lemma one_pCons:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1028
  "1 = [:1:]"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1029
  by (simp add: poly_eq_iff coeff_pCons split: nat.splits)
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1030
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1031
lemma pCons_one:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1032
  "[:1:] = 1"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1033
  by (simp add: one_pCons)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1034
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1035
instance
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1036
  by standard (simp_all add: one_pCons)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1037
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1038
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1039
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1040
lemma poly_1 [simp]:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1041
  "poly 1 x = 1"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1042
  by (simp add: one_pCons)
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1043
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1044
lemma one_poly_eq_simps [simp]:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1045
  "1 = [:1:] \<longleftrightarrow> True"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1046
  "[:1:] = 1 \<longleftrightarrow> True"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1047
  by (simp_all add: one_pCons)
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1048
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1049
lemma degree_1 [simp]:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1050
  "degree 1 = 0"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1051
  by (simp add: one_pCons)
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1052
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1053
lemma coeffs_1_eq [simp, code abstract]:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1054
  "coeffs 1 = [1]"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1055
  by (simp add: one_pCons)
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1056
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1057
lemma smult_one [simp]:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1058
  "smult c 1 = [:c:]"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1059
  by (simp add: one_pCons)
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1060
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1061
lemma monom_eq_1 [simp]:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1062
  "monom 1 0 = 1"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1063
  by (simp add: monom_0 one_pCons)
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1064
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1065
lemma monom_eq_1_iff:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1066
  "monom c n = 1 \<longleftrightarrow> c = 1 \<and> n = 0"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1067
  using monom_eq_const_iff [of c n 1] by auto
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1068
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1069
lemma monom_altdef:
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1070
  "monom c n = smult c ([:0, 1:] ^ n)"
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1071
  by (induct n) (simp_all add: monom_0 monom_Suc)  
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1072
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1073
instance poly :: ("{comm_semiring_1,semiring_1_no_zero_divisors}") semiring_1_no_zero_divisors ..
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1074
instance poly :: (comm_ring) comm_ring ..
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1075
instance poly :: (comm_ring_1) comm_ring_1 ..
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1076
instance poly :: (comm_ring_1) comm_semiring_1_cancel ..
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1077
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1078
lemma degree_power_le: "degree (p ^ n) \<le> degree p * n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1079
  by (induct n) (auto intro: order_trans degree_mult_le)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1080
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1081
lemma coeff_0_power: "coeff (p ^ n) 0 = coeff p 0 ^ n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1082
  by (induct n) (simp_all add: coeff_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1083
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1084
lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1085
  by (induct p) (simp_all add: algebra_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1086
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1087
lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1088
  by (induct p) (simp_all add: algebra_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1089
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1090
lemma poly_power [simp]: "poly (p ^ n) x = poly p x ^ n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1091
  for p :: "'a::comm_semiring_1 poly"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1092
  by (induct n) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1093
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  1094
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
  1095
  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
  1096
67091
1393c2340eec more symbols;
wenzelm
parents: 66806
diff changeset
  1097
lemma degree_prod_sum_le: "finite S \<Longrightarrow> degree (prod f S) \<le> sum (degree \<circ> f) S"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1098
proof (induct S rule: finite_induct)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1099
  case empty
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1100
  then show ?case by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1101
next
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1102
  case (insert a S)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1103
  show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1104
    unfolding prod.insert[OF insert(1-2)] sum.insert[OF insert(1-2)]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1105
    by (rule le_trans[OF degree_mult_le]) (use insert in auto)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1106
qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1107
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1108
lemma coeff_0_prod_list: "coeff (prod_list xs) 0 = prod_list (map (\<lambda>p. coeff p 0) xs)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1109
  by (induct xs) (simp_all add: coeff_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1110
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1111
lemma coeff_monom_mult: "coeff (monom c n * p) k = (if k < n then 0 else c * coeff p (k - n))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1112
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1113
  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
  1114
    by (simp add: coeff_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1115
  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
  1116
    by (intro sum.cong) simp_all
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1117
  also have "\<dots> = (if k < n then 0 else c * coeff p (k - n))"
66799
haftmann
parents: 66550
diff changeset
  1118
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1119
  finally show ?thesis .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1120
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1121
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1122
lemma monom_1_dvd_iff': "monom 1 n dvd p \<longleftrightarrow> (\<forall>k<n. coeff p k = 0)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1123
proof
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1124
  assume "monom 1 n dvd p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1125
  then obtain r where "p = monom 1 n * r"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1126
    by (rule dvdE)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1127
  then show "\<forall>k<n. coeff p k = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1128
    by (simp add: coeff_mult)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1129
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1130
  assume zero: "(\<forall>k<n. coeff p k = 0)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1131
  define r where "r = Abs_poly (\<lambda>k. coeff p (k + n))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1132
  have "\<forall>\<^sub>\<infinity>k. coeff p (k + n) = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1133
    by (subst cofinite_eq_sequentially, subst eventually_sequentially_seg,
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1134
        subst cofinite_eq_sequentially [symmetric]) transfer
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1135
  then have coeff_r [simp]: "coeff r k = coeff p (k + n)" for k
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1136
    unfolding r_def by (subst poly.Abs_poly_inverse) simp_all
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1137
  have "p = monom 1 n * r"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1138
    by (rule poly_eqI, subst coeff_monom_mult) (simp_all add: zero)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1139
  then show "monom 1 n dvd p" by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1140
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1141
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1142
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1143
subsection \<open>Mapping polynomials\<close>
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1144
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1145
definition map_poly :: "('a :: zero \<Rightarrow> 'b :: zero) \<Rightarrow> 'a poly \<Rightarrow> 'b poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1146
  where "map_poly f p = Poly (map f (coeffs p))"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1147
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1148
lemma map_poly_0 [simp]: "map_poly f 0 = 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1149
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1150
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1151
lemma map_poly_1: "map_poly f 1 = [:f 1:]"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1152
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1153
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1154
lemma map_poly_1' [simp]: "f 1 = 1 \<Longrightarrow> map_poly f 1 = 1"
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1155
  by (simp add: map_poly_def one_pCons)
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1156
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1157
lemma coeff_map_poly:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1158
  assumes "f 0 = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1159
  shows "coeff (map_poly f p) n = f (coeff p n)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1160
  by (auto simp: assms map_poly_def nth_default_def coeffs_def not_less Suc_le_eq coeff_eq_0
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1161
      simp del: upt_Suc)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1162
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1163
lemma coeffs_map_poly [code abstract]:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  1164
  "coeffs (map_poly f p) = strip_while ((=) 0) (map f (coeffs p))"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1165
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1166
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1167
lemma coeffs_map_poly':
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1168
  assumes "\<And>x. x \<noteq> 0 \<Longrightarrow> f x \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1169
  shows "coeffs (map_poly f p) = map f (coeffs p)"
66799
haftmann
parents: 66550
diff changeset
  1170
  using assms
haftmann
parents: 66550
diff changeset
  1171
  by (auto simp add: coeffs_map_poly strip_while_idem_iff
haftmann
parents: 66550
diff changeset
  1172
    last_coeffs_eq_coeff_degree no_trailing_unfold last_map)
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  1173
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  1174
lemma set_coeffs_map_poly:
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  1175
  "(\<And>x. f x = 0 \<longleftrightarrow> x = 0) \<Longrightarrow> set (coeffs (map_poly f p)) = f ` set (coeffs p)"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  1176
  by (simp add: coeffs_map_poly')
64591
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 degree_map_poly:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1179
  assumes "\<And>x. x \<noteq> 0 \<Longrightarrow> f x \<noteq> 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1180
  shows "degree (map_poly f p) = degree p"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1181
  by (simp add: degree_eq_length_coeffs coeffs_map_poly' assms)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1182
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1183
lemma map_poly_eq_0_iff:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1184
  assumes "f 0 = 0" "\<And>x. x \<in> set (coeffs p) \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> f x \<noteq> 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1185
  shows "map_poly f p = 0 \<longleftrightarrow> p = 0"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1186
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1187
  have "(coeff (map_poly f p) n = 0) = (coeff p n = 0)" for n
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1188
  proof -
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1189
    have "coeff (map_poly f p) n = f (coeff p n)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1190
      by (simp add: coeff_map_poly assms)
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1191
    also have "\<dots> = 0 \<longleftrightarrow> coeff p n = 0"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1192
    proof (cases "n < length (coeffs p)")
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1193
      case True
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1194
      then have "coeff p n \<in> set (coeffs p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1195
        by (auto simp: coeffs_def simp del: upt_Suc)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1196
      with assms show "f (coeff p n) = 0 \<longleftrightarrow> coeff p n = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1197
        by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1198
    next
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1199
      case False
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1200
      then show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1201
        by (auto simp: assms length_coeffs nth_default_coeffs_eq [symmetric] nth_default_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1202
    qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1203
    finally show ?thesis .
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1204
  qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1205
  then show ?thesis by (auto simp: poly_eq_iff)
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1206
qed
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1207
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1208
lemma map_poly_smult:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1209
  assumes "f 0 = 0""\<And>c x. f (c * x) = f c * f x"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1210
  shows "map_poly f (smult c p) = smult (f c) (map_poly f p)"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1211
  by (intro poly_eqI) (simp_all add: assms coeff_map_poly)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1212
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1213
lemma map_poly_pCons:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1214
  assumes "f 0 = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1215
  shows "map_poly f (pCons c p) = pCons (f c) (map_poly f p)"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1216
  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
  1217
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1218
lemma map_poly_map_poly:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1219
  assumes "f 0 = 0" "g 0 = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1220
  shows "map_poly f (map_poly g p) = map_poly (f \<circ> g) p"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1221
  by (intro poly_eqI) (simp add: coeff_map_poly assms)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1222
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1223
lemma map_poly_id [simp]: "map_poly id p = p"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1224
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1225
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1226
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
  1227
  by (simp add: map_poly_def)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1228
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1229
lemma map_poly_cong:
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1230
  assumes "(\<And>x. x \<in> set (coeffs p) \<Longrightarrow> f x = g x)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1231
  shows "map_poly f p = map_poly g p"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1232
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1233
  from assms have "map f (coeffs p) = map g (coeffs p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1234
    by (intro map_cong) simp_all
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1235
  then show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1236
    by (simp only: coeffs_eq_iff coeffs_map_poly)
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1237
qed
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1238
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1239
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
  1240
  by (intro poly_eqI) (simp_all add: coeff_map_poly)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1241
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1242
lemma map_poly_idI:
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1243
  assumes "\<And>x. x \<in> set (coeffs p) \<Longrightarrow> f x = x"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1244
  shows "map_poly f p = p"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1245
  using map_poly_cong[OF assms, of _ id] by simp
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1246
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1247
lemma map_poly_idI':
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1248
  assumes "\<And>x. x \<in> set (coeffs p) \<Longrightarrow> f x = x"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1249
  shows "p = map_poly f p"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1250
  using map_poly_cong[OF assms, of _ id] by simp
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1251
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1252
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
  1253
  by (intro poly_eqI) (simp_all add: coeff_map_poly)
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1254
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1255
65484
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1256
subsection \<open>Conversions\<close>
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1257
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1258
lemma of_nat_poly:
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1259
  "of_nat n = [:of_nat n:]"
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1260
  by (induct n) (simp_all add: one_pCons)
65484
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1261
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1262
lemma of_nat_monom:
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1263
  "of_nat n = monom (of_nat n) 0"
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1264
  by (simp add: of_nat_poly monom_0)
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1265
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1266
lemma degree_of_nat [simp]:
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1267
  "degree (of_nat n) = 0"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1268
  by (simp add: of_nat_poly)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1269
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1270
lemma lead_coeff_of_nat [simp]:
65484
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1271
  "lead_coeff (of_nat n) = of_nat n"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1272
  by (simp add: of_nat_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1273
65484
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1274
lemma of_int_poly:
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1275
  "of_int k = [:of_int k:]"
64793
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1276
  by (simp only: of_int_of_nat of_nat_poly) simp
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1277
65484
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1278
lemma of_int_monom:
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1279
  "of_int k = monom (of_int k) 0"
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1280
  by (simp add: of_int_poly monom_0)
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1281
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1282
lemma degree_of_int [simp]:
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1283
  "degree (of_int k) = 0"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1284
  by (simp add: of_int_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1285
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1286
lemma lead_coeff_of_int [simp]:
65484
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1287
  "lead_coeff (of_int k) = of_int k"
64793
3df00fb1ce0b more lemmas;
haftmann
parents: 64635
diff changeset
  1288
  by (simp add: of_int_poly)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1289
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1290
lemma numeral_poly: "numeral n = [:numeral n:]"
65484
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1291
proof -
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1292
  have "numeral n = of_nat (numeral n)"
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1293
    by simp
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1294
  also have "\<dots> = [:of_nat (numeral n):]"
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1295
    by (simp add: of_nat_poly)
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1296
  finally show ?thesis
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1297
    by simp
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1298
qed
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1299
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1300
lemma numeral_monom:
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1301
  "numeral n = monom (numeral n) 0"
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1302
  by (simp add: numeral_poly monom_0)
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1303
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1304
lemma degree_numeral [simp]:
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1305
  "degree (numeral n) = 0"
751f9ed8e940 more rules concerning of_nat, of_int, numeral
haftmann
parents: 65435
diff changeset
  1306
  by (simp add: numeral_poly)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1307
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1308
lemma lead_coeff_numeral [simp]:
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1309
  "lead_coeff (numeral n) = numeral n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1310
  by (simp add: numeral_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1311
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1312
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1313
subsection \<open>Lemmas about divisibility\<close>
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1314
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1315
lemma dvd_smult:
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1316
  assumes "p dvd q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1317
  shows "p dvd smult a q"
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1318
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1319
  from assms obtain k where "q = p * k" ..
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1320
  then have "smult a q = p * smult a k" by simp
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1321
  then show "p dvd smult a q" ..
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1322
qed
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1323
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1324
lemma dvd_smult_cancel: "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1325
  for a :: "'a::field"
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1326
  by (drule dvd_smult [where a="inverse a"]) simp
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1327
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1328
lemma dvd_smult_iff: "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1329
  for a :: "'a::field"
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1330
  by (safe elim!: dvd_smult dvd_smult_cancel)
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1331
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1332
lemma smult_dvd_cancel:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1333
  assumes "smult a p dvd q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1334
  shows "p dvd q"
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1335
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1336
  from assms obtain k where "q = smult a p * k" ..
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1337
  then have "q = p * smult a k" by simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1338
  then show "p dvd q" ..
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1339
qed
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1340
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1341
lemma smult_dvd: "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1342
  for a :: "'a::field"
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1343
  by (rule smult_dvd_cancel [where a="inverse a"]) simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1344
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1345
lemma smult_dvd_iff: "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1346
  for a :: "'a::field"
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1347
  by (auto elim: smult_dvd smult_dvd_cancel)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1348
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1349
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
  1350
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1351
  have "smult c p = [:c:] * p" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1352
  also have "\<dots> dvd 1 \<longleftrightarrow> c dvd 1 \<and> p dvd 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1353
  proof safe
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1354
    assume *: "[:c:] * p dvd 1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1355
    then show "p dvd 1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1356
      by (rule dvd_mult_right)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1357
    from * obtain q where q: "1 = [:c:] * p * q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1358
      by (rule dvdE)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1359
    have "c dvd c * (coeff p 0 * coeff q 0)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1360
      by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1361
    also have "\<dots> = coeff ([:c:] * p * q) 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1362
      by (simp add: mult.assoc coeff_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1363
    also note q [symmetric]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1364
    finally have "c dvd coeff 1 0" .
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1365
    then show "c dvd 1" by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1366
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1367
    assume "c dvd 1" "p dvd 1"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1368
    from this(1) obtain d where "1 = c * d"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1369
      by (rule dvdE)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1370
    then have "1 = [:c:] * [:d:]"
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  1371
      by (simp add: one_pCons ac_simps)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1372
    then have "[:c:] dvd 1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1373
      by (rule dvdI)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1374
    from mult_dvd_mono[OF this \<open>p dvd 1\<close>] show "[:c:] * p dvd 1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1375
      by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1376
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1377
  finally show ?thesis .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1378
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1379
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1380
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1381
subsection \<open>Polynomials form an integral domain\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1382
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1383
instance poly :: (idom) idom ..
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1384
65577
32d4117ad6e8 instance for polynomial rings with characteristic zero
haftmann
parents: 65486
diff changeset
  1385
instance poly :: ("{ring_char_0, comm_ring_1}") ring_char_0
32d4117ad6e8 instance for polynomial rings with characteristic zero
haftmann
parents: 65486
diff changeset
  1386
  by standard (auto simp add: of_nat_poly intro: injI)
32d4117ad6e8 instance for polynomial rings with characteristic zero
haftmann
parents: 65486
diff changeset
  1387
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1388
lemma degree_mult_eq: "p \<noteq> 0 \<Longrightarrow> q \<noteq> 0 \<Longrightarrow> degree (p * q) = degree p + degree q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1389
  for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1390
  by (rule order_antisym [OF degree_mult_le le_degree]) (simp add: coeff_mult_degree_sum)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1391
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1392
lemma degree_mult_eq_0:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1393
  "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)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1394
  for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1395
  by (auto simp: degree_mult_eq)
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  1396
66550
e5d82cf3c387 Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents: 66453
diff changeset
  1397
lemma degree_power_eq: "p \<noteq> 0 \<Longrightarrow> degree ((p :: 'a :: idom poly) ^ n) = n * degree p"
e5d82cf3c387 Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents: 66453
diff changeset
  1398
  by (induction n) (simp_all add: degree_mult_eq)
e5d82cf3c387 Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents: 66453
diff changeset
  1399
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1400
lemma degree_mult_right_le:
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1401
  fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1402
  assumes "q \<noteq> 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1403
  shows "degree p \<le> degree (p * q)"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1404
  using assms by (cases "p = 0") (simp_all add: degree_mult_eq)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1405
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1406
lemma coeff_degree_mult: "coeff (p * q) (degree (p * q)) = coeff q (degree q) * coeff p (degree p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1407
  for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1408
  by (cases "p = 0 \<or> q = 0") (auto simp: degree_mult_eq coeff_mult_degree_sum mult_ac)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1409
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1410
lemma dvd_imp_degree_le: "p dvd q \<Longrightarrow> q \<noteq> 0 \<Longrightarrow> degree p \<le> degree q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1411
  for p 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
  1412
  by (erule dvdE, hypsubst, subst degree_mult_eq) auto
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1413
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1414
lemma divides_degree:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1415
  fixes p q :: "'a ::{comm_semiring_1,semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1416
  assumes "p dvd q"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1417
  shows "degree p \<le> degree q \<or> q = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1418
  by (metis dvd_imp_degree_le assms)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1419
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1420
lemma const_poly_dvd_iff:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1421
  fixes c :: "'a::{comm_semiring_1,semiring_no_zero_divisors}"
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1422
  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
  1423
proof (cases "c = 0 \<or> p = 0")
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1424
  case True
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1425
  then show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1426
    by (auto intro!: poly_eqI)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1427
next
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1428
  case False
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1429
  show ?thesis
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1430
  proof
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1431
    assume "[:c:] dvd p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1432
    then show "\<forall>n. c dvd coeff p n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1433
      by (auto elim!: dvdE simp: coeffs_def)
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1434
  next
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1435
    assume *: "\<forall>n. c dvd coeff p n"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1436
    define mydiv where "mydiv x y = (SOME z. x = y * z)" for x y :: 'a
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1437
    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
  1438
      using that unfolding mydiv_def dvd_def by (rule someI_ex)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1439
    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
  1440
    from False * have "p = q * [:c:]"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1441
      by (intro poly_eqI)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1442
        (auto simp: q_def nth_default_def not_less length_coeffs_degree coeffs_nth
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1443
          intro!: coeff_eq_0 mydiv)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1444
    then show "[:c:] dvd p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1445
      by (simp only: dvd_triv_right)
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1446
  qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1447
qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1448
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1449
lemma const_poly_dvd_const_poly_iff [simp]: "[:a:] dvd [:b:] \<longleftrightarrow> a dvd b"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1450
  for a b :: "'a::{comm_semiring_1,semiring_no_zero_divisors}"
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1451
  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
  1452
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1453
lemma lead_coeff_mult: "lead_coeff (p * q) = lead_coeff p * lead_coeff q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1454
  for p q :: "'a::{comm_semiring_0, semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1455
  by (cases "p = 0 \<or> q = 0") (auto simp: coeff_mult_degree_sum degree_mult_eq)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1456
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1457
lemma lead_coeff_smult: "lead_coeff (smult c p) = c * lead_coeff p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1458
  for p :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1459
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1460
  have "smult c p = [:c:] * p" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1461
  also have "lead_coeff \<dots> = c * lead_coeff p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1462
    by (subst lead_coeff_mult) simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1463
  finally show ?thesis .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1464
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1465
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1466
lemma lead_coeff_1 [simp]: "lead_coeff 1 = 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1467
  by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1468
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1469
lemma lead_coeff_power: "lead_coeff (p ^ n) = lead_coeff p ^ n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1470
  for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1471
  by (induct n) (simp_all add: lead_coeff_mult)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1472
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1473
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1474
subsection \<open>Polynomials form an ordered integral domain\<close>
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1475
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  1476
definition pos_poly :: "'a::linordered_semidom poly \<Rightarrow> bool"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1477
  where "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1478
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1479
lemma pos_poly_pCons: "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1480
  by (simp add: pos_poly_def)
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1481
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1482
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1483
  by (simp add: pos_poly_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1484
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1485
lemma pos_poly_add: "pos_poly p \<Longrightarrow> pos_poly q \<Longrightarrow> pos_poly (p + q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1486
  apply (induct p arbitrary: q)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1487
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1488
  apply (case_tac q)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1489
  apply (force simp add: pos_poly_pCons add_pos_pos)
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1490
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1491
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1492
lemma pos_poly_mult: "pos_poly p \<Longrightarrow> pos_poly q \<Longrightarrow> pos_poly (p * q)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1493
  unfolding pos_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1494
  apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1495
   apply (simp add: degree_mult_eq coeff_mult_degree_sum)
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1496
  apply auto
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1497
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1498
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1499
lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1500
  for p :: "'a::linordered_idom poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1501
  by (induct p) (auto simp: pos_poly_pCons)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1502
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1503
lemma pos_poly_coeffs [code]: "pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1504
  (is "?lhs \<longleftrightarrow> ?rhs")
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1505
proof
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1506
  assume ?rhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1507
  then show ?lhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1508
    by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1509
next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1510
  assume ?lhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1511
  then have *: "0 < coeff p (degree p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1512
    by (simp add: pos_poly_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1513
  then have "p \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1514
    by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1515
  with * show ?rhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1516
    by (simp add: last_coeffs_eq_coeff_degree)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1517
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1518
35028
108662d50512 more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents: 34973
diff changeset
  1519
instantiation poly :: (linordered_idom) linordered_idom
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1520
begin
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1521
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1522
definition "x < y \<longleftrightarrow> pos_poly (y - x)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1523
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1524
definition "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1525
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1526
definition "\<bar>x::'a poly\<bar> = (if x < 0 then - x else x)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1527
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1528
definition "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
  1529
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1530
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1531
proof
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1532
  fix x y z :: "'a poly"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1533
  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1534
    unfolding less_eq_poly_def less_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1535
    apply safe
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1536
     apply simp
29878
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
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> x"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1541
    by (simp add: less_eq_poly_def)
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1542
  show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1543
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1544
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1545
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1546
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1547
    done
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1548
  show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1549
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1550
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1551
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1552
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1553
    done
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1554
  show "x \<le> y \<Longrightarrow> z + x \<le> z + y"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1555
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1556
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1557
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1558
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1559
  show "x \<le> y \<or> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1560
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1561
    using pos_poly_total [of "x - y"]
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1562
    by auto
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1563
  show "x < y \<Longrightarrow> 0 < z \<Longrightarrow> z * x < z * y"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1564
    by (simp add: less_poly_def right_diff_distrib [symmetric] pos_poly_mult)
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1565
  show "\<bar>x\<bar> = (if x < 0 then - x else x)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1566
    by (rule abs_poly_def)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1567
  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
  1568
    by (rule sgn_poly_def)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1569
qed
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1570
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1571
end
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1572
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1573
text \<open>TODO: Simplification rules for comparisons\<close>
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1574
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1575
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1576
subsection \<open>Synthetic division and polynomial roots\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1577
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1578
subsubsection \<open>Synthetic division\<close>
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1579
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1580
text \<open>Synthetic division is simply division by the linear polynomial @{term "x - c"}.\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1581
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1582
definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1583
  where "synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1584
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1585
definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1586
  where "synthetic_div p c = fst (synthetic_divmod p c)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1587
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1588
lemma synthetic_divmod_0 [simp]: "synthetic_divmod 0 c = (0, 0)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1589
  by (simp add: synthetic_divmod_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1590
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1591
lemma synthetic_divmod_pCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1592
  "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
  1593
  by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1594
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1595
lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1596
  by (simp add: synthetic_div_def)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1597
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1598
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1599
  by (induct p arbitrary: a) simp_all
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1600
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1601
lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1602
  by (induct p) (simp_all add: split_def)
52380
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)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1606
  by (simp add: synthetic_div_def split_def snd_synthetic_divmod)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1607
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1608
lemma synthetic_div_eq_0_iff: "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1609
proof (induct p)
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1610
  case 0
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1611
  then show ?case by simp
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1612
next
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1613
  case (pCons a p)
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1614
  then show ?case by (cases p) simp
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1615
qed
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1616
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1617
lemma degree_synthetic_div: "degree (synthetic_div p c) = degree p - 1"
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  1618
  by (induct p) (simp_all add: synthetic_div_eq_0_iff)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1619
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1620
lemma synthetic_div_correct:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1621
  "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
  1622
  by (induct p) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1623
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1624
lemma synthetic_div_unique: "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1625
  apply (induct p arbitrary: q r)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1626
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1627
   apply (frule synthetic_div_unique_lemma)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1628
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1629
  apply (case_tac q, force)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1630
  done
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1631
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1632
lemma synthetic_div_correct': "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1633
  for c :: "'a::comm_ring_1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1634
  using synthetic_div_correct [of p c] by (simp add: algebra_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1635
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1636
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1637
subsubsection \<open>Polynomial roots\<close>
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1638
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1639
lemma poly_eq_0_iff_dvd: "poly p c = 0 \<longleftrightarrow> [:- c, 1:] dvd p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1640
  (is "?lhs \<longleftrightarrow> ?rhs")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1641
  for c :: "'a::comm_ring_1"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1642
proof
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1643
  assume ?lhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1644
  with synthetic_div_correct' [of c p] have "p = [:-c, 1:] * synthetic_div p c" by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1645
  then show ?rhs ..
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1646
next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1647
  assume ?rhs
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1648
  then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1649
  then show ?lhs by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1650
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1651
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1652
lemma dvd_iff_poly_eq_0: "[:c, 1:] dvd p \<longleftrightarrow> poly p (- c) = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1653
  for c :: "'a::comm_ring_1"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1654
  by (simp add: poly_eq_0_iff_dvd)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1655
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1656
lemma poly_roots_finite: "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1657
  for p :: "'a::{comm_ring_1,ring_no_zero_divisors} poly"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1658
proof (induct n \<equiv> "degree p" arbitrary: p)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1659
  case 0
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1660
  then obtain a where "a \<noteq> 0" and "p = [:a:]"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1661
    by (cases p) (simp split: if_splits)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1662
  then show "finite {x. poly p x = 0}"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1663
    by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1664
next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1665
  case (Suc n)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1666
  show "finite {x. poly p x = 0}"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1667
  proof (cases "\<exists>x. poly p x = 0")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1668
    case False
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 True
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1672
    then obtain a where "poly p a = 0" ..
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1673
    then have "[:-a, 1:] dvd p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1674
      by (simp only: poly_eq_0_iff_dvd)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1675
    then obtain k where k: "p = [:-a, 1:] * k" ..
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1676
    with \<open>p \<noteq> 0\<close> have "k \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1677
      by auto
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1678
    with k have "degree p = Suc (degree k)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1679
      by (simp add: degree_mult_eq del: mult_pCons_left)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1680
    with \<open>Suc n = degree p\<close> have "n = degree k"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1681
      by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1682
    from this \<open>k \<noteq> 0\<close> have "finite {x. poly k x = 0}"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1683
      by (rule Suc.hyps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1684
    then have "finite (insert a {x. poly k x = 0})"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1685
      by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1686
    then show "finite {x. poly p x = 0}"
57862
8f074e6e22fc tuned proofs;
wenzelm
parents: 57512
diff changeset
  1687
      by (simp add: k Collect_disj_eq del: mult_pCons_left)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1688
  qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1689
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1690
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1691
lemma poly_eq_poly_eq_iff: "poly p = poly q \<longleftrightarrow> p = q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1692
  (is "?lhs \<longleftrightarrow> ?rhs")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1693
  for 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
proof
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1695
  assume ?rhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1696
  then show ?lhs by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1697
next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1698
  assume ?lhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1699
  have "poly p = poly 0 \<longleftrightarrow> p = 0" for p :: "'a poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1700
    apply (cases "p = 0")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1701
     apply simp_all
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1702
    apply (drule poly_roots_finite)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1703
    apply (auto simp add: infinite_UNIV_char_0)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1704
    done
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1705
  from \<open>?lhs\<close> and this [of "p - q"] show ?rhs
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1706
    by auto
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1707
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1708
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1709
lemma poly_all_0_iff_0: "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1710
  for 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
  by (auto simp add: poly_eq_poly_eq_iff [symmetric])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1712
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1713
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1714
subsubsection \<open>Order of polynomial roots\<close>
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1715
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1716
definition order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1717
  where "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1718
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1719
lemma coeff_linear_power: "coeff ([:a, 1:] ^ n) n = 1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1720
  for a :: "'a::comm_semiring_1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1721
  apply (induct n)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1722
   apply simp_all
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1723
  apply (subst coeff_eq_0)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1724
   apply (auto intro: le_less_trans degree_power_le)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1725
  done
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1726
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1727
lemma degree_linear_power: "degree ([:a, 1:] ^ n) = n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1728
  for a :: "'a::comm_semiring_1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1729
  apply (rule order_antisym)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1730
   apply (rule ord_le_eq_trans [OF degree_power_le])
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1731
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1732
  apply (rule le_degree)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1733
  apply (simp add: coeff_linear_power)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1734
  done
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1735
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1736
lemma order_1: "[:-a, 1:] ^ order a p dvd p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1737
  apply (cases "p = 0")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1738
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1739
  apply (cases "order a p")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1740
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1741
  apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1742
   apply (drule not_less_Least)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1743
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1744
  apply (fold order_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1745
  apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1746
  done
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1747
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1748
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1749
  unfolding order_def
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1750
  apply (rule LeastI_ex)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1751
  apply (rule_tac x="degree p" in exI)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1752
  apply (rule notI)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1753
  apply (drule (1) dvd_imp_degree_le)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1754
  apply (simp only: degree_linear_power)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1755
  done
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1756
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1757
lemma order: "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1758
  by (rule conjI [OF order_1 order_2])
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1759
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1760
lemma order_degree:
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1761
  assumes p: "p \<noteq> 0"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1762
  shows "order a p \<le> degree p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1763
proof -
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1764
  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
  1765
    by (simp only: degree_linear_power)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1766
  also from order_1 p have "\<dots> \<le> degree p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1767
    by (rule dvd_imp_degree_le)
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1768
  finally show ?thesis .
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1769
qed
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1770
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1771
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1772
  apply (cases "p = 0")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1773
   apply simp_all
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1774
  apply (rule iffI)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1775
   apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1776
  unfolding poly_eq_0_iff_dvd
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1777
  apply (metis dvd_power dvd_trans order_1)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1778
  done
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1779
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1780
lemma order_0I: "poly p a \<noteq> 0 \<Longrightarrow> order a p = 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1781
  by (subst (asm) order_root) auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1782
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1783
lemma order_unique_lemma:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1784
  fixes p :: "'a::idom poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1785
  assumes "[:-a, 1:] ^ n dvd p" "\<not> [:-a, 1:] ^ Suc n dvd p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1786
  shows "n = order a p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1787
  unfolding Polynomial.order_def
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1788
  apply (rule Least_equality [symmetric])
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1789
   apply (fact assms)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1790
  apply (rule classical)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1791
  apply (erule notE)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1792
  unfolding not_less_eq_eq
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1793
  using assms(1)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1794
  apply (rule power_le_dvd)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1795
  apply assumption
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1796
  done
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1797
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1798
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
  1799
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1800
  define i where "i = order a p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1801
  define j where "j = order a q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1802
  define t where "t = [:-a, 1:]"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1803
  have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1804
    by (simp add: t_def dvd_iff_poly_eq_0)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1805
  assume "p * q \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1806
  then show "order a (p * q) = i + j"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1807
    apply clarsimp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1808
    apply (drule order [where a=a and p=p, folded i_def t_def])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1809
    apply (drule order [where a=a and p=q, folded j_def t_def])
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1810
    apply clarify
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1811
    apply (erule dvdE)+
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1812
    apply (rule order_unique_lemma [symmetric], fold t_def)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1813
     apply (simp_all add: power_add t_dvd_iff)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1814
    done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1815
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1816
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1817
lemma order_smult:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1818
  assumes "c \<noteq> 0"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1819
  shows "order x (smult c p) = order x p"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1820
proof (cases "p = 0")
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1821
  case True
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1822
  then show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1823
    by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1824
next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1825
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1826
  have "smult c p = [:c:] * p" by simp
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1827
  also from assms False have "order x \<dots> = order x [:c:] + order x p"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1828
    by (subst order_mult) simp_all
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1829
  also have "order x [:c:] = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1830
    by (rule order_0I) (use assms in auto)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1831
  finally show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1832
    by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1833
qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1834
68532
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  1835
(* Next three lemmas contributed by Wenda Li *)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1836
lemma order_1_eq_0 [simp]:"order x 1 = 0"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1837
  by (metis order_root poly_1 zero_neq_one)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1838
68532
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  1839
lemma order_uminus[simp]: "order x (-p) = order x p"
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  1840
  by (metis neg_equal_0_iff_equal order_smult smult_1_left smult_minus_left) 
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  1841
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1842
lemma order_power_n_n: "order a ([:-a,1:]^n)=n"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1843
proof (induct n) (*might be proved more concisely using nat_less_induct*)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1844
  case 0
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1845
  then show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1846
    by (metis order_root poly_1 power_0 zero_neq_one)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1847
next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1848
  case (Suc n)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1849
  have "order a ([:- a, 1:] ^ Suc n) = order a ([:- a, 1:] ^ n) + order a [:-a,1:]"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1850
    by (metis (no_types, hide_lams) One_nat_def add_Suc_right monoid_add_class.add.right_neutral
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1851
      one_neq_zero order_mult pCons_eq_0_iff power_add power_eq_0_iff power_one_right)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1852
  moreover have "order a [:-a,1:] = 1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1853
    unfolding order_def
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1854
  proof (rule Least_equality, rule notI)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1855
    assume "[:- a, 1:] ^ Suc 1 dvd [:- a, 1:]"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1856
    then have "degree ([:- a, 1:] ^ Suc 1) \<le> degree ([:- a, 1:])"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1857
      by (rule dvd_imp_degree_le) auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1858
    then show False
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1859
      by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1860
  next
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1861
    fix y
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1862
    assume *: "\<not> [:- a, 1:] ^ Suc y dvd [:- a, 1:]"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1863
    show "1 \<le> y"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1864
    proof (rule ccontr)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1865
      assume "\<not> 1 \<le> y"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1866
      then have "y = 0" by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1867
      then have "[:- a, 1:] ^ Suc y dvd [:- a, 1:]" by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1868
      with * show False by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1869
    qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1870
  qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1871
  ultimately show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1872
    using Suc by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1873
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1874
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1875
lemma order_0_monom [simp]: "c \<noteq> 0 \<Longrightarrow> order 0 (monom c n) = n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1876
  using order_power_n_n[of 0 n] by (simp add: monom_altdef order_smult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1877
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1878
lemma dvd_imp_order_le: "q \<noteq> 0 \<Longrightarrow> p dvd q \<Longrightarrow> Polynomial.order a p \<le> Polynomial.order a q"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1879
  by (auto simp: order_mult elim: dvdE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1880
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1881
text \<open>Now justify the standard squarefree decomposition, i.e. \<open>f / gcd f f'\<close>.\<close>
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1882
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1883
lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1884
  apply (cases "p = 0")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1885
  apply auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1886
   apply (drule order_2 [where a=a and p=p])
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1887
   apply (metis not_less_eq_eq power_le_dvd)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1888
  apply (erule power_le_dvd [OF order_1])
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1889
  done
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1890
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1891
lemma order_decomp:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1892
  assumes "p \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1893
  shows "\<exists>q. p = [:- a, 1:] ^ order a p * q \<and> \<not> [:- a, 1:] dvd q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1894
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1895
  from assms have *: "[:- a, 1:] ^ order a p dvd p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1896
    and **: "\<not> [:- a, 1:] ^ Suc (order a p) dvd p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1897
    by (auto dest: order)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1898
  from * obtain q where q: "p = [:- a, 1:] ^ order a p * q" ..
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1899
  with ** have "\<not> [:- a, 1:] ^ Suc (order a p) dvd [:- a, 1:] ^ order a p * q"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1900
    by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1901
  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
  1902
    by simp
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1903
  with idom_class.dvd_mult_cancel_left [of "[:- a, 1:] ^ order a p" "[:- a, 1:]" q]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1904
  have "\<not> [:- a, 1:] dvd q" by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1905
  with q show ?thesis by blast
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1906
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1907
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1908
lemma monom_1_dvd_iff: "p \<noteq> 0 \<Longrightarrow> monom 1 n dvd p \<longleftrightarrow> n \<le> order 0 p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1909
  using order_divides[of 0 n p] by (simp add: monom_altdef)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  1910
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1911
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1912
subsection \<open>Additional induction rules on polynomials\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1913
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1914
text \<open>
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1915
  An induction rule for induction over the roots of a polynomial with a certain property.
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1916
  (e.g. all positive roots)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1917
\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1918
lemma poly_root_induct [case_names 0 no_roots root]:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1919
  fixes p :: "'a :: idom poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1920
  assumes "Q 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1921
    and "\<And>p. (\<And>a. P a \<Longrightarrow> poly p a \<noteq> 0) \<Longrightarrow> Q p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1922
    and "\<And>a p. P a \<Longrightarrow> Q p \<Longrightarrow> Q ([:a, -1:] * p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1923
  shows "Q p"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1924
proof (induction "degree p" arbitrary: p rule: less_induct)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1925
  case (less p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1926
  show ?case
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1927
  proof (cases "p = 0")
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1928
    case True
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1929
    with assms(1) show ?thesis by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1930
  next
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1931
    case False
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1932
    show ?thesis
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1933
    proof (cases "\<exists>a. P a \<and> poly p a = 0")
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1934
      case False
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1935
      then show ?thesis by (intro assms(2)) blast
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1936
    next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1937
      case True
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1938
      then obtain a where a: "P a" "poly p a = 0"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1939
        by blast
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1940
      then have "-[:-a, 1:] dvd p"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1941
        by (subst minus_dvd_iff) (simp add: poly_eq_0_iff_dvd)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1942
      then obtain q where q: "p = [:a, -1:] * q" by (elim dvdE) simp
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1943
      with False have "q \<noteq> 0" by auto
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1944
      have "degree p = Suc (degree q)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1945
        by (subst q, subst degree_mult_eq) (simp_all add: \<open>q \<noteq> 0\<close>)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1946
      then have "Q q" by (intro less) simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1947
      with a(1) have "Q ([:a, -1:] * q)"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1948
        by (rule assms(3))
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1949
      with q show ?thesis by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1950
    qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1951
  qed
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1952
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1953
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1954
lemma dropWhile_replicate_append:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  1955
  "dropWhile ((=) a) (replicate n a @ ys) = dropWhile ((=) a) ys"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1956
  by (induct n) simp_all
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1957
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1958
lemma Poly_append_replicate_0: "Poly (xs @ replicate n 0) = Poly xs"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1959
  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
  1960
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1961
text \<open>
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1962
  An induction rule for simultaneous induction over two polynomials,
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1963
  prepending one coefficient in each step.
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1964
\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1965
lemma poly_induct2 [case_names 0 pCons]:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1966
  assumes "P 0 0" "\<And>a p b q. P p q \<Longrightarrow> P (pCons a p) (pCons b q)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1967
  shows "P p q"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1968
proof -
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  1969
  define n where "n = max (length (coeffs p)) (length (coeffs q))"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  1970
  define xs where "xs = coeffs p @ (replicate (n - length (coeffs p)) 0)"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  1971
  define ys where "ys = coeffs q @ (replicate (n - length (coeffs q)) 0)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1972
  have "length xs = length ys"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1973
    by (simp add: xs_def ys_def n_def)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1974
  then have "P (Poly xs) (Poly ys)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1975
    by (induct rule: list_induct2) (simp_all add: assms)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1976
  also have "Poly xs = p"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1977
    by (simp add: xs_def Poly_append_replicate_0)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1978
  also have "Poly ys = q"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1979
    by (simp add: ys_def Poly_append_replicate_0)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1980
  finally show ?thesis .
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1981
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1982
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1983
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1984
subsection \<open>Composition of polynomials\<close>
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
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
(* 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
  1987
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1988
definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1989
  where "pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1990
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1991
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
  1992
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1993
lemma pcompose_0 [simp]: "pcompose 0 q = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1994
  by (simp add: pcompose_def)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1995
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1996
lemma pcompose_pCons: "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1997
  by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1998
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  1999
lemma pcompose_1: "pcompose 1 p = 1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2000
  for p :: "'a::comm_semiring_1 poly"
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  2001
  by (auto simp: one_pCons pcompose_pCons)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2002
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2003
lemma poly_pcompose: "poly (pcompose p q) x = poly p (poly q x)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2004
  by (induct p) (simp_all add: pcompose_pCons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2005
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2006
lemma degree_pcompose_le: "degree (pcompose p q) \<le> degree p * degree q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2007
  apply (induct p)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2008
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2009
  apply (simp add: pcompose_pCons)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2010
  apply clarify
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2011
  apply (rule degree_add_le)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2012
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2013
  apply (rule order_trans [OF degree_mult_le])
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2014
  apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2015
  done
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2016
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2017
lemma pcompose_add: "pcompose (p + q) r = pcompose p r + pcompose q r"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2018
  for p q r :: "'a::{comm_semiring_0, ab_semigroup_add} poly"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2019
proof (induction p q rule: poly_induct2)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2020
  case 0
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2021
  then show ?case by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2022
next
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2023
  case (pCons a p b q)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2024
  have "pcompose (pCons a p + pCons b q) r = [:a + b:] + r * pcompose p r + r * pcompose q r"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2025
    by (simp_all add: pcompose_pCons pCons.IH algebra_simps)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2026
  also have "[:a + b:] = [:a:] + [:b:]" by simp
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2027
  also have "\<dots> + r * pcompose p r + r * pcompose q r =
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2028
    pcompose (pCons a p) r + pcompose (pCons b q) r"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2029
    by (simp only: pcompose_pCons add_ac)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2030
  finally show ?case .
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2031
qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2032
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2033
lemma pcompose_uminus: "pcompose (-p) r = -pcompose p r"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2034
  for p r :: "'a::comm_ring poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2035
  by (induct p) (simp_all add: pcompose_pCons)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2036
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2037
lemma pcompose_diff: "pcompose (p - q) r = pcompose p r - pcompose q r"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2038
  for p q r :: "'a::comm_ring poly"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2039
  using pcompose_add[of p "-q"] by (simp add: pcompose_uminus)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2040
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2041
lemma pcompose_smult: "pcompose (smult a p) r = smult a (pcompose p r)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2042
  for p r :: "'a::comm_semiring_0 poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2043
  by (induct p) (simp_all add: pcompose_pCons pcompose_add smult_add_right)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2044
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2045
lemma pcompose_mult: "pcompose (p * q) r = pcompose p r * pcompose q r"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2046
  for p q r :: "'a::comm_semiring_0 poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2047
  by (induct p arbitrary: q) (simp_all add: pcompose_add pcompose_smult pcompose_pCons algebra_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2048
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2049
lemma pcompose_assoc: "pcompose p (pcompose q r) = pcompose (pcompose p q) r"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2050
  for p q r :: "'a::comm_semiring_0 poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2051
  by (induct p arbitrary: q) (simp_all add: pcompose_pCons pcompose_add pcompose_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2052
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2053
lemma pcompose_idR[simp]: "pcompose p [: 0, 1 :] = p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2054
  for p :: "'a::comm_semiring_1 poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2055
  by (induct p) (simp_all add: pcompose_pCons)
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2056
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2057
lemma pcompose_sum: "pcompose (sum f A) p = sum (\<lambda>i. pcompose (f i) p) A"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2058
  by (induct A rule: infinite_finite_induct) (simp_all add: pcompose_1 pcompose_add)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2059
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2060
lemma pcompose_prod: "pcompose (prod f A) p = prod (\<lambda>i. pcompose (f i) p) A"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2061
  by (induct A rule: infinite_finite_induct) (simp_all add: pcompose_1 pcompose_mult)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2062
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  2063
lemma pcompose_const [simp]: "pcompose [:a:] q = [:a:]"
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  2064
  by (subst pcompose_pCons) simp
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2065
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2066
lemma pcompose_0': "pcompose p 0 = [:coeff p 0:]"
64591
240a39af9ec4 restructured matter on polynomials and normalized fractions
haftmann
parents: 64272
diff changeset
  2067
  by (induct p) (auto simp add: pcompose_pCons)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2068
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2069
lemma degree_pcompose: "degree (pcompose p q) = degree p * degree q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2070
  for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2071
proof (induct p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2072
  case 0
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2073
  then show ?case by auto
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2074
next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2075
  case (pCons a p)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2076
  consider "degree (q * pcompose p q) = 0" | "degree (q * pcompose p q) > 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2077
    by blast
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2078
  then show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2079
  proof cases
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2080
    case prems: 1
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2081
    show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2082
    proof (cases "p = 0")
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2083
      case True
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2084
      then show ?thesis by auto
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2085
    next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2086
      case False
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2087
      from prems have "degree q = 0 \<or> pcompose p q = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2088
        by (auto simp add: degree_mult_eq_0)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2089
      moreover have False if "pcompose p q = 0" "degree q \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2090
      proof -
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2091
        from pCons.hyps(2) that have "degree p = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2092
          by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2093
        then obtain a1 where "p = [:a1:]"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2094
          by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2095
        with \<open>pcompose p q = 0\<close> \<open>p \<noteq> 0\<close> show False
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2096
          by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2097
      qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2098
      ultimately have "degree (pCons a p) * degree q = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2099
        by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2100
      moreover have "degree (pcompose (pCons a p) q) = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2101
      proof -
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2102
        from prems have "0 = max (degree [:a:]) (degree (q * pcompose p q))"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2103
          by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2104
        also have "\<dots> \<ge> degree ([:a:] + q * pcompose p q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2105
          by (rule degree_add_le_max)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2106
        finally show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2107
          by (auto simp add: pcompose_pCons)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2108
      qed
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2109
      ultimately show ?thesis by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2110
    qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2111
  next
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2112
    case prems: 2
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2113
    then have "p \<noteq> 0" "q \<noteq> 0" "pcompose p q \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2114
      by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2115
    from prems degree_add_eq_right [of "[:a:]"]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2116
    have "degree (pcompose (pCons a p) q) = degree (q * pcompose p q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2117
      by (auto simp: pcompose_pCons)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2118
    with pCons.hyps(2) degree_mult_eq[OF \<open>q\<noteq>0\<close> \<open>pcompose p q\<noteq>0\<close>] show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2119
      by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2120
  qed
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2121
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2122
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2123
lemma pcompose_eq_0:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2124
  fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2125
  assumes "pcompose p q = 0" "degree q > 0"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2126
  shows "p = 0"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2127
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2128
  from assms degree_pcompose [of p q] have "degree p = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2129
    by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2130
  then obtain a where "p = [:a:]"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2131
    by (metis degree_pCons_eq_if gr0_conv_Suc neq0_conv pCons_cases)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2132
  with assms(1) have "a = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2133
    by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2134
  with \<open>p = [:a:]\<close> show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2135
    by simp
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2136
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2137
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2138
lemma lead_coeff_comp:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2139
  fixes p q :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2140
  assumes "degree q > 0"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2141
  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
  2142
proof (induct p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2143
  case 0
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2144
  then show ?case by auto
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2145
next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2146
  case (pCons a p)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2147
  consider "degree (q * pcompose p q) = 0" | "degree (q * pcompose p q) > 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2148
    by blast
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2149
  then show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2150
  proof cases
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2151
    case prems: 1
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2152
    then have "pcompose p q = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2153
      by (metis assms degree_0 degree_mult_eq_0 neq0_conv)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2154
    with pcompose_eq_0[OF _ \<open>degree q > 0\<close>] have "p = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2155
      by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2156
    then show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2157
      by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2158
  next
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2159
    case prems: 2
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2160
    then have "degree [:a:] < degree (q * pcompose p q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2161
      by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2162
    then have "lead_coeff ([:a:] + q * p \<circ>\<^sub>p q) = lead_coeff (q * p \<circ>\<^sub>p q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2163
      by (rule lead_coeff_add_le)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2164
    then have "lead_coeff (pcompose (pCons a p) q) = lead_coeff (q * pcompose p q)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2165
      by (simp add: pcompose_pCons)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2166
    also have "\<dots> = lead_coeff q * (lead_coeff p * lead_coeff q ^ degree p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2167
      using pCons.hyps(2) lead_coeff_mult[of q "pcompose p q"] by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2168
    also have "\<dots> = lead_coeff p * lead_coeff q ^ (degree p + 1)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2169
      by (auto simp: mult_ac)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2170
    finally show ?thesis by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2171
  qed
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2172
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2173
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2174
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2175
subsection \<open>Shifting polynomials\<close>
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2176
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2177
definition poly_shift :: "nat \<Rightarrow> 'a::zero poly \<Rightarrow> 'a poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2178
  where "poly_shift n p = Abs_poly (\<lambda>i. coeff p (i + n))"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2179
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2180
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
  2181
  by (auto simp add: nth_default_def add_ac)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2182
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2183
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
  2184
  by (auto simp add: nth_default_def add_ac)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2185
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2186
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
  2187
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2188
  from MOST_coeff_eq_0[of p] obtain m where "\<forall>k>m. coeff p k = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2189
    by (auto simp: MOST_nat)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2190
  then have "\<forall>k>m. coeff p (k + n) = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2191
    by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2192
  then have "\<forall>\<^sub>\<infinity>k. coeff p (k + n) = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2193
    by (auto simp: MOST_nat)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2194
  then show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2195
    by (simp add: poly_shift_def poly.Abs_poly_inverse)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2196
qed
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
lemma poly_shift_id [simp]: "poly_shift 0 = (\<lambda>x. x)"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2199
  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
  2200
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2201
lemma poly_shift_0 [simp]: "poly_shift n 0 = 0"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2202
  by (simp add: poly_eq_iff coeff_poly_shift)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2203
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2204
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
  2205
  by (simp add: poly_eq_iff coeff_poly_shift)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2206
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2207
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
  2208
  by (auto simp add: poly_eq_iff coeff_poly_shift)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2209
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  2210
lemma coeffs_shift_poly [code abstract]:
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  2211
  "coeffs (poly_shift n p) = drop n (coeffs p)"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2212
proof (cases "p = 0")
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2213
  case True
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2214
  then show ?thesis by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2215
next
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2216
  case False
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2217
  then show ?thesis
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2218
    by (intro coeffs_eqI)
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  2219
      (simp_all add: coeff_poly_shift nth_default_drop nth_default_coeffs_eq)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2220
qed
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2221
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2222
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2223
subsection \<open>Truncating polynomials\<close>
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2224
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2225
definition poly_cutoff
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2226
  where "poly_cutoff n p = Abs_poly (\<lambda>k. if k < n then coeff p k else 0)"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2227
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2228
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
  2229
  unfolding poly_cutoff_def
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2230
  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
  2231
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2232
lemma poly_cutoff_0 [simp]: "poly_cutoff n 0 = 0"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2233
  by (simp add: poly_eq_iff coeff_poly_cutoff)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2234
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2235
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
  2236
  by (simp add: poly_eq_iff coeff_poly_cutoff)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2237
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2238
lemma coeffs_poly_cutoff [code abstract]:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  2239
  "coeffs (poly_cutoff n p) = strip_while ((=) 0) (take n (coeffs p))"
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  2240
proof (cases "strip_while ((=) 0) (take n (coeffs p)) = []")
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2241
  case True
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2242
  then have "coeff (poly_cutoff n p) k = 0" for k
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2243
    unfolding coeff_poly_cutoff
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2244
    by (auto simp: nth_default_coeffs_eq [symmetric] nth_default_def set_conv_nth)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2245
  then have "poly_cutoff n p = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2246
    by (simp add: poly_eq_iff)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2247
  then show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2248
    by (subst True) simp_all
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2249
next
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2250
  case False
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  2251
  have "no_trailing ((=) 0) (strip_while ((=) 0) (take n (coeffs p)))"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2252
    by simp
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  2253
  with False have "last (strip_while ((=) 0) (take n (coeffs p))) \<noteq> 0"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2254
    unfolding no_trailing_unfold by auto
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2255
  then show ?thesis
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2256
    by (intro coeffs_eqI)
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  2257
      (simp_all add: coeff_poly_cutoff nth_default_take nth_default_coeffs_eq)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2258
qed
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2259
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
subsection \<open>Reflecting polynomials\<close>
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2262
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2263
definition reflect_poly :: "'a::zero poly \<Rightarrow> 'a poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2264
  where "reflect_poly p = Poly (rev (coeffs p))"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2265
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2266
lemma coeffs_reflect_poly [code abstract]:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  2267
  "coeffs (reflect_poly p) = rev (dropWhile ((=) 0) (coeffs p))"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2268
  by (simp add: reflect_poly_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2269
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2270
lemma reflect_poly_0 [simp]: "reflect_poly 0 = 0"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2271
  by (simp add: reflect_poly_def)
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 reflect_poly_1 [simp]: "reflect_poly 1 = 1"
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  2274
  by (simp add: reflect_poly_def one_pCons)
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 coeff_reflect_poly:
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2277
  "coeff (reflect_poly p) n = (if n > degree p then 0 else coeff p (degree p - n))"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2278
  by (cases "p = 0")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2279
    (auto simp add: reflect_poly_def nth_default_def
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2280
      rev_nth degree_eq_length_coeffs coeffs_nth not_less
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2281
      dest: le_imp_less_Suc)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2282
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2283
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
  2284
  by (simp add: coeff_reflect_poly)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2285
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2286
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
  2287
  by (simp add: coeff_reflect_poly poly_0_coeff_0)
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 reflect_poly_pCons':
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2290
  "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
  2291
  by (intro poly_eqI)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2292
    (auto simp: coeff_reflect_poly coeff_pCons not_less Suc_diff_le split: nat.split)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2293
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2294
lemma reflect_poly_const [simp]: "reflect_poly [:a:] = [:a:]"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2295
  by (cases "a = 0") (simp_all add: reflect_poly_def)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2296
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2297
lemma poly_reflect_poly_nz:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2298
  "x \<noteq> 0 \<Longrightarrow> poly (reflect_poly p) x = x ^ degree p * poly p (inverse x)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2299
  for x :: "'a::field"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2300
  by (induct rule: pCons_induct) (simp_all add: field_simps reflect_poly_pCons' poly_monom)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2301
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2302
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
  2303
  by (simp add: coeff_reflect_poly)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2304
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2305
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
  2306
  by (simp add: poly_0_coeff_0)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2307
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2308
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
  2309
  by (cases p rule: pCons_cases) (simp add: reflect_poly_def )
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2310
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2311
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
  2312
  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
  2313
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2314
lemma reflect_poly_pCons: "a \<noteq> 0 \<Longrightarrow> reflect_poly (pCons a p) = Poly (rev (a # coeffs p))"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2315
  by (subst coeffs_eq_iff) (simp add: coeffs_reflect_poly)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2316
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2317
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
  2318
  by (cases p rule: pCons_cases) (simp add: reflect_poly_pCons degree_eq_length_coeffs)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2319
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2320
(* TODO: does this work with zero divisors as well? Probably not. *)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2321
lemma reflect_poly_mult: "reflect_poly (p * q) = reflect_poly p * reflect_poly q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2322
  for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2323
proof (cases "p = 0 \<or> q = 0")
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2324
  case False
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2325
  then have [simp]: "p \<noteq> 0" "q \<noteq> 0" by auto
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2326
  show ?thesis
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2327
  proof (rule poly_eqI)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2328
    show "coeff (reflect_poly (p * q)) i = coeff (reflect_poly p * reflect_poly q) i" for i
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2329
    proof (cases "i \<le> degree (p * q)")
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2330
      case True
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  2331
      define A where "A = {..i} \<inter> {i - degree q..degree p}"
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  2332
      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
  2333
      let ?f = "\<lambda>j. degree p - j"
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2334
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2335
      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
  2336
        by (simp add: coeff_reflect_poly)
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2337
      also have "\<dots> = (\<Sum>j\<le>degree (p * q) - i. coeff p j * coeff q (degree (p * q) - i - j))"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2338
        by (simp add: coeff_mult)
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2339
      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
  2340
        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
  2341
      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
  2342
        by (intro sum.reindex_bij_witness[of _ ?f ?f])
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2343
          (auto simp: A_def B_def degree_mult_eq add_ac)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2344
      also have "\<dots> =
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2345
        (\<Sum>j\<le>i.
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2346
          if j \<in> {i - degree q..degree p}
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2347
          then coeff p (degree p - j) * coeff q (degree q - (i - j))
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2348
          else 0)"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2349
        by (subst sum.inter_restrict [symmetric]) (simp_all add: A_def)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2350
      also have "\<dots> = coeff (reflect_poly p * reflect_poly q) i"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2351
        by (fastforce simp: coeff_mult coeff_reflect_poly intro!: sum.cong)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2352
      finally show ?thesis .
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2353
    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
  2354
  qed
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2355
qed auto
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2356
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2357
lemma reflect_poly_smult: "reflect_poly (smult c p) = smult c (reflect_poly p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2358
  for p :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2359
  using reflect_poly_mult[of "[:c:]" p] by simp
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2360
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2361
lemma reflect_poly_power: "reflect_poly (p ^ n) = reflect_poly p ^ n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2362
  for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2363
  by (induct n) (simp_all add: reflect_poly_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2364
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2365
lemma reflect_poly_prod: "reflect_poly (prod f A) = prod (\<lambda>x. reflect_poly (f x)) A"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2366
  for f :: "_ \<Rightarrow> _::{comm_semiring_0,semiring_no_zero_divisors} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2367
  by (induct A rule: infinite_finite_induct) (simp_all add: reflect_poly_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2368
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2369
lemma reflect_poly_prod_list: "reflect_poly (prod_list xs) = prod_list (map reflect_poly xs)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2370
  for xs :: "_::{comm_semiring_0,semiring_no_zero_divisors} poly list"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2371
  by (induct xs) (simp_all add: reflect_poly_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2372
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  2373
lemma reflect_poly_Poly_nz:
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  2374
  "no_trailing (HOL.eq 0) xs \<Longrightarrow> reflect_poly (Poly xs) = Poly (rev xs)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2375
  by (simp add: reflect_poly_def)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2376
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2377
lemmas reflect_poly_simps =
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2378
  reflect_poly_0 reflect_poly_1 reflect_poly_const reflect_poly_smult reflect_poly_mult
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  2379
  reflect_poly_power reflect_poly_prod reflect_poly_prod_list
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2380
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  2381
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2382
subsection \<open>Derivatives\<close>
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2383
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2384
function pderiv :: "('a :: {comm_semiring_1,semiring_no_zero_divisors}) poly \<Rightarrow> 'a poly"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2385
  where "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
  2386
  by (auto intro: pCons_cases)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2387
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2388
termination pderiv
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2389
  by (relation "measure degree") simp_all
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2390
63027
8de0ebee3f1c several updates on polynomial long division and pseudo division
Rene Thiemann <rene.thiemann@uibk.ac.at>
parents: 62422
diff changeset
  2391
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
  2392
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2393
lemma pderiv_0 [simp]: "pderiv 0 = 0"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2394
  using pderiv.simps [of 0 0] by simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2395
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2396
lemma pderiv_pCons: "pderiv (pCons a p) = p + pCons 0 (pderiv p)"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2397
  by (simp add: pderiv.simps)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2398
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2399
lemma pderiv_1 [simp]: "pderiv 1 = 0"
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  2400
  by (simp add: one_pCons pderiv_pCons)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2401
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2402
lemma pderiv_of_nat [simp]: "pderiv (of_nat n) = 0"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2403
  and pderiv_numeral [simp]: "pderiv (numeral m) = 0"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2404
  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
  2405
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2406
lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2407
  by (induct p arbitrary: n)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2408
    (auto simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2409
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2410
fun pderiv_coeffs_code :: "'a::{comm_semiring_1,semiring_no_zero_divisors} \<Rightarrow> 'a list \<Rightarrow> 'a list"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2411
  where
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2412
    "pderiv_coeffs_code f (x # xs) = cCons (f * x) (pderiv_coeffs_code (f+1) xs)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2413
  | "pderiv_coeffs_code f [] = []"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2414
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2415
definition pderiv_coeffs :: "'a::{comm_semiring_1,semiring_no_zero_divisors} list \<Rightarrow> 'a list"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2416
  where "pderiv_coeffs xs = pderiv_coeffs_code 1 (tl xs)"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2417
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2418
(* Efficient code for pderiv contributed by René Thiemann and Akihisa Yamada *)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2419
lemma pderiv_coeffs_code:
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2420
  "nth_default 0 (pderiv_coeffs_code f xs) n = (f + of_nat n) * nth_default 0 xs n"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2421
proof (induct xs arbitrary: f n)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2422
  case Nil
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2423
  then show ?case by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2424
next
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2425
  case (Cons x xs)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2426
  show ?case
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2427
  proof (cases n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2428
    case 0
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2429
    then show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2430
      by (cases "pderiv_coeffs_code (f + 1) xs = [] \<and> f * x = 0") (auto simp: cCons_def)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2431
  next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2432
    case n: (Suc m)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2433
    show ?thesis
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2434
    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
  2435
      case False
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2436
      then have "nth_default 0 (pderiv_coeffs_code f (x # xs)) n =
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2437
          nth_default 0 (pderiv_coeffs_code (f + 1) xs) m"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2438
        by (auto simp: cCons_def n)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2439
      also have "\<dots> = (f + of_nat n) * nth_default 0 xs m"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2440
        by (simp add: Cons n add_ac)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2441
      finally show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2442
        by (simp add: n)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2443
    next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2444
      case True
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2445
      have empty: "pderiv_coeffs_code g xs = [] \<Longrightarrow> g + of_nat m = 0 \<or> nth_default 0 xs m = 0" for g
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2446
      proof (induct xs arbitrary: g m)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2447
        case Nil
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2448
        then show ?case by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2449
      next
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2450
        case (Cons x xs)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2451
        from Cons(2) have empty: "pderiv_coeffs_code (g + 1) xs = []" and g: "g = 0 \<or> x = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2452
          by (auto simp: cCons_def split: if_splits)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2453
        note IH = Cons(1)[OF empty]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2454
        from IH[of m] IH[of "m - 1"] g show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2455
          by (cases m) (auto simp: field_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2456
      qed
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2457
      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
  2458
        by (auto simp: cCons_def n)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2459
      moreover from True have "(f + of_nat n) * nth_default 0 (x # xs) n = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2460
        by (simp add: n) (use empty[of "f+1"] in \<open>auto simp: field_simps\<close>)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2461
      ultimately show ?thesis by simp
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2462
    qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2463
  qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2464
qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2465
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2466
lemma coeffs_pderiv_code [code abstract]: "coeffs (pderiv p) = pderiv_coeffs (coeffs p)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2467
  unfolding pderiv_coeffs_def
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2468
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
  2469
  case (1 n)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2470
  have id: "coeff p (Suc n) = nth_default 0 (map (\<lambda>i. coeff p (Suc i)) [0..<degree p]) n"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2471
    by (cases "n < degree p") (auto simp: nth_default_def coeff_eq_0)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2472
  show ?case
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2473
    unfolding coeffs_def map_upt_Suc by (auto simp: id)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2474
next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2475
  case 2
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2476
  obtain n :: 'a and xs where defs: "tl (coeffs p) = xs" "1 = n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2477
    by simp
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2478
  from 2 show ?case
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2479
    unfolding defs by (induct xs arbitrary: n) (auto simp: cCons_def)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2480
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2481
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2482
lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2483
  for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors,semiring_char_0} poly"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2484
  apply (rule iffI)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2485
   apply (cases p)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2486
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2487
   apply (simp add: poly_eq_iff coeff_pderiv del: of_nat_Suc)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2488
  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
  2489
  done
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2490
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2491
lemma degree_pderiv: "degree (pderiv p) = degree p - 1"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2492
  for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors,semiring_char_0} poly"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2493
  apply (rule order_antisym [OF degree_le])
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2494
   apply (simp add: coeff_pderiv coeff_eq_0)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2495
  apply (cases "degree p")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2496
   apply simp
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2497
  apply (rule le_degree)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2498
  apply (simp add: coeff_pderiv del: of_nat_Suc)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2499
  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
  2500
  done
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2501
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2502
lemma not_dvd_pderiv:
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2503
  fixes p :: "'a::{comm_semiring_1,semiring_no_zero_divisors,semiring_char_0} poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2504
  assumes "degree p \<noteq> 0"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2505
  shows "\<not> p dvd pderiv p"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2506
proof
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2507
  assume dvd: "p dvd pderiv p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2508
  then obtain q where p: "pderiv p = p * q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2509
    unfolding dvd_def by auto
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2510
  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
  2511
    by (simp add: assms dvd_imp_degree_le pderiv_eq_0_iff)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2512
  from assms and this [unfolded degree_pderiv]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2513
    show False by auto
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2514
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2515
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2516
lemma dvd_pderiv_iff [simp]: "p dvd pderiv p \<longleftrightarrow> degree p = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2517
  for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors,semiring_char_0} poly"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2518
  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
  2519
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2520
lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2521
  by (simp add: pderiv_pCons)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2522
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2523
lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2524
  by (rule poly_eqI) (simp add: coeff_pderiv algebra_simps)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2525
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2526
lemma pderiv_minus: "pderiv (- p :: 'a :: idom poly) = - pderiv p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2527
  by (rule poly_eqI) (simp add: coeff_pderiv algebra_simps)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2528
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63433
diff changeset
  2529
lemma pderiv_diff: "pderiv ((p :: _ :: idom poly) - q) = pderiv p - pderiv q"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2530
  by (rule poly_eqI) (simp add: coeff_pderiv algebra_simps)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2531
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2532
lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2533
  by (rule poly_eqI) (simp add: coeff_pderiv algebra_simps)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2534
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2535
lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2536
  by (induct p) (auto simp: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2537
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2538
lemma pderiv_power_Suc: "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2539
  apply (induct n)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2540
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2541
  apply (subst power_Suc)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2542
  apply (subst pderiv_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2543
  apply (erule ssubst)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2544
  apply (simp only: of_nat_Suc smult_add_left smult_1_left)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2545
  apply (simp add: algebra_simps)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2546
  done
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2547
66550
e5d82cf3c387 Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents: 66453
diff changeset
  2548
lemma pderiv_pcompose: "pderiv (pcompose p q) = pcompose (pderiv p) q * pderiv q"
e5d82cf3c387 Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents: 66453
diff changeset
  2549
  by (induction p rule: pCons_induct)
e5d82cf3c387 Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents: 66453
diff changeset
  2550
     (auto simp: pcompose_pCons pderiv_add pderiv_mult pderiv_pCons pcompose_add algebra_simps)
e5d82cf3c387 Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents: 66453
diff changeset
  2551
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2552
lemma pderiv_prod: "pderiv (prod f (as)) = (\<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
  2553
proof (induct as rule: infinite_finite_induct)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2554
  case (insert a as)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2555
  then have id: "prod f (insert a as) = f a * prod f as"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2556
    "\<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
  2557
    "insert a as - {a} = as"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2558
    by auto
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2559
  have "prod f (insert a as - {b}) = f a * prod f (as - {b})" if "b \<in> as" for b
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2560
  proof -
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2561
    from \<open>a \<notin> as\<close> that have *: "insert a as - {b} = insert a (as - {b})"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2562
      by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2563
    show ?thesis
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2564
      unfolding * by (subst prod.insert) (use insert in auto)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2565
  qed
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2566
  then show ?case
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63950
diff changeset
  2567
    unfolding id pderiv_mult insert(3) sum_distrib_left
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2568
    by (auto simp add: ac_simps intro!: sum.cong)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2569
qed auto
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2570
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2571
lemma DERIV_pow2: "DERIV (\<lambda>x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2572
  by (rule DERIV_cong, rule DERIV_pow) simp
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2573
declare DERIV_pow2 [simp] DERIV_pow [simp]
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2574
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2575
lemma DERIV_add_const: "DERIV f x :> D \<Longrightarrow> DERIV (\<lambda>x. a + f x :: 'a::real_normed_field) x :> D"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2576
  by (rule DERIV_cong, rule DERIV_add) auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2577
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2578
lemma poly_DERIV [simp]: "DERIV (\<lambda>x. poly p x) x :> poly (pderiv p) x"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2579
  by (induct p) (auto intro!: derivative_eq_intros simp add: pderiv_pCons)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2580
68532
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2581
lemma poly_isCont[simp]: 
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2582
  fixes x::"'a::real_normed_field"
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2583
  shows "isCont (\<lambda>x. poly p x) x"
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2584
by (rule poly_DERIV [THEN DERIV_isCont])
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2585
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2586
lemma tendsto_poly [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. poly p (f x)) \<longlongrightarrow> poly p a) F"
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2587
  for f :: "_ \<Rightarrow> 'a::real_normed_field"  
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2588
  by (rule isCont_tendsto_compose [OF poly_isCont])
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2589
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2590
lemma continuous_within_poly: "continuous (at z within s) (poly p)"
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2591
  for z :: "'a::{real_normed_field}"
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2592
  by (simp add: continuous_within tendsto_poly)  
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2593
    
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2594
lemma continuous_poly [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. poly p (f x))"
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2595
  for f :: "_ \<Rightarrow> 'a::real_normed_field"
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2596
  unfolding continuous_def by (rule tendsto_poly)
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2597
      
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2598
lemma continuous_on_poly [continuous_intros]:
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2599
  fixes p :: "'a :: {real_normed_field} poly"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2600
  assumes "continuous_on A f"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2601
  shows "continuous_on A (\<lambda>x. poly p (f x))"
68532
f8b98d31ad45 Incorporating new/strengthened proofs from Library and AFP entries
paulson <lp15@cam.ac.uk>
parents: 67399
diff changeset
  2602
  by (metis DERIV_continuous_on assms continuous_on_compose2 poly_DERIV subset_UNIV)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2603
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2604
text \<open>Consequences of the derivative theorem above.\<close>
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2605
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2606
lemma poly_differentiable[simp]: "(\<lambda>x. poly p x) differentiable (at x)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2607
  for x :: real
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2608
  by (simp add: real_differentiable_def) (blast intro: poly_DERIV)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2609
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2610
lemma poly_IVT_pos: "a < b \<Longrightarrow> poly p a < 0 \<Longrightarrow> 0 < poly p b \<Longrightarrow> \<exists>x. a < x \<and> x < b \<and> poly p x = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2611
  for a b :: real
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2612
  using IVT_objl [of "poly p" a 0 b] by (auto simp add: order_le_less)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2613
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2614
lemma poly_IVT_neg: "a < b \<Longrightarrow> 0 < poly p a \<Longrightarrow> poly p b < 0 \<Longrightarrow> \<exists>x. a < x \<and> x < b \<and> poly p x = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2615
  for a b :: real
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2616
  using poly_IVT_pos [where p = "- p"] by simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2617
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2618
lemma poly_IVT: "a < b \<Longrightarrow> poly p a * poly p b < 0 \<Longrightarrow> \<exists>x>a. x < b \<and> poly p x = 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2619
  for p :: "real poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2620
  by (metis less_not_sym mult_less_0_iff poly_IVT_neg poly_IVT_pos)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2621
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2622
lemma poly_MVT: "a < b \<Longrightarrow> \<exists>x. a < x \<and> x < b \<and> poly p b - poly p a = (b - a) * poly (pderiv p) x"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2623
  for a b :: real
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2624
  using MVT [of a b "poly p"]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2625
  apply auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2626
  apply (rule_tac x = z in exI)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2627
  apply (auto simp add: mult_left_cancel poly_DERIV [THEN DERIV_unique])
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2628
  done
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2629
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2630
lemma poly_MVT':
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2631
  fixes a b :: real
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2632
  assumes "{min a b..max a b} \<subseteq> A"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2633
  shows "\<exists>x\<in>A. poly p b - poly p a = (b - a) * poly (pderiv p) x"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2634
proof (cases a b rule: linorder_cases)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2635
  case less
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2636
  from poly_MVT[OF less, of p] guess x by (elim exE conjE)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2637
  then show ?thesis by (intro bexI[of _ x]) (auto intro!: subsetD[OF assms])
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2638
next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2639
  case greater
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2640
  from poly_MVT[OF greater, of p] guess x by (elim exE conjE)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2641
  then show ?thesis by (intro bexI[of _ x]) (auto simp: algebra_simps intro!: subsetD[OF assms])
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2642
qed (use assms in auto)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2643
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2644
lemma poly_pinfty_gt_lc:
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2645
  fixes p :: "real poly"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2646
  assumes "lead_coeff p > 0"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2647
  shows "\<exists>n. \<forall> x \<ge> n. poly p x \<ge> lead_coeff p"
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2648
  using assms
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2649
proof (induct p)
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2650
  case 0
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2651
  then show ?case by auto
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2652
next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2653
  case (pCons a p)
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2654
  from this(1) consider "a \<noteq> 0" "p = 0" | "p \<noteq> 0" by auto
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2655
  then show ?case
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2656
  proof cases
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2657
    case 1
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2658
    then show ?thesis by auto
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2659
  next
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2660
    case 2
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2661
    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
  2662
      by auto
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2663
    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
  2664
    define n where "n = max n1 (1 + \<bar>a\<bar> / lead_coeff p)"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2665
    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
  2666
    proof -
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2667
      from gte_lcoeff that have "lead_coeff p \<le> poly p x"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2668
        by (auto simp: n_def)
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2669
      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
  2670
        by (auto intro: frac_le)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2671
      with \<open>n \<le> x\<close>[unfolded n_def] have "x \<ge> 1 + \<bar>a\<bar> / poly p x"
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2672
        by auto
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2673
      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
  2674
      show "lead_coeff (pCons a p) \<le> poly (pCons a p) x"
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2675
        by (auto simp: field_simps)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2676
    qed
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2677
    then show ?thesis by blast
e690d6f2185b tuned proofs;
wenzelm
parents: 63498
diff changeset
  2678
  qed
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2679
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2680
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2681
lemma lemma_order_pderiv1:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2682
  "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2683
    smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2684
  by (simp only: pderiv_mult pderiv_power_Suc) (simp del: power_Suc of_nat_Suc add: pderiv_pCons)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2685
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2686
lemma lemma_order_pderiv:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2687
  fixes p :: "'a :: field_char_0 poly"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2688
  assumes n: "0 < n"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2689
    and pd: "pderiv p \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2690
    and pe: "p = [:- a, 1:] ^ n * q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2691
    and nd: "\<not> [:- a, 1:] dvd q"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2692
  shows "n = Suc (order a (pderiv p))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2693
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2694
  from assms have "pderiv ([:- a, 1:] ^ n * q) \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2695
    by auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2696
  from assms obtain n' where "n = Suc n'" "0 < Suc n'" "pderiv ([:- a, 1:] ^ Suc n' * q) \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2697
    by (cases n) auto
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2698
  have *: "k dvd k * pderiv q + smult (of_nat (Suc n')) l \<Longrightarrow> k dvd l" for k l
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2699
    by (auto simp del: of_nat_Suc simp: dvd_add_right_iff dvd_smult_iff)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2700
  have "n' = order a (pderiv ([:- a, 1:] ^ Suc n' * q))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2701
  proof (rule order_unique_lemma)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2702
    show "[:- a, 1:] ^ n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2703
      apply (subst lemma_order_pderiv1)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2704
      apply (rule dvd_add)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2705
       apply (metis dvdI dvd_mult2 power_Suc2)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2706
      apply (metis dvd_smult dvd_triv_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2707
      done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2708
    show "\<not> [:- a, 1:] ^ Suc n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2709
      apply (subst lemma_order_pderiv1)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2710
      apply (metis * nd dvd_mult_cancel_right power_not_zero pCons_eq_0_iff power_Suc zero_neq_one)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2711
      done
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2712
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2713
  then show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2714
    by (metis \<open>n = Suc n'\<close> pe)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2715
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2716
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2717
lemma order_pderiv: "pderiv p \<noteq> 0 \<Longrightarrow> order a p \<noteq> 0 \<Longrightarrow> order a p = Suc (order a (pderiv p))"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2718
  for p :: "'a::field_char_0 poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2719
  apply (cases "p = 0")
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2720
   apply simp
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2721
  apply (drule_tac a = a and p = p in order_decomp)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2722
  using neq0_conv
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2723
  apply (blast intro: lemma_order_pderiv)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2724
  done
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2725
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2726
lemma poly_squarefree_decomp_order:
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2727
  fixes p :: "'a::field_char_0 poly"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2728
  assumes "pderiv p \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2729
    and p: "p = q * d"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2730
    and p': "pderiv p = e * d"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2731
    and d: "d = r * p + s * pderiv p"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2732
  shows "order a q = (if order a p = 0 then 0 else 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2733
proof (rule classical)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2734
  assume 1: "\<not> ?thesis"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2735
  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2736
  with p have "order a p = order a q + order a d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2737
    by (simp add: order_mult)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2738
  with 1 have "order a p \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2739
    by (auto split: if_splits)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2740
  from \<open>pderiv p \<noteq> 0\<close> \<open>pderiv p = e * d\<close> have "order a (pderiv p) = order a e + order a d"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2741
    by (simp add: order_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2742
  from \<open>pderiv p \<noteq> 0\<close> \<open>order a p \<noteq> 0\<close> have "order a p = Suc (order a (pderiv p))"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2743
    by (rule order_pderiv)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2744
  from \<open>p \<noteq> 0\<close> \<open>p = q * d\<close> have "d \<noteq> 0"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2745
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2746
  have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2747
    apply (simp add: d)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2748
    apply (rule dvd_add)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2749
     apply (rule dvd_mult)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2750
     apply (simp add: order_divides \<open>p \<noteq> 0\<close> \<open>order a p = Suc (order a (pderiv p))\<close>)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2751
    apply (rule dvd_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2752
    apply (simp add: order_divides)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2753
    done
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2754
  with \<open>d \<noteq> 0\<close> have "order a (pderiv p) \<le> order a d"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2755
    by (simp add: order_divides)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2756
  show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2757
    using \<open>order a p = order a q + order a d\<close>
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2758
      and \<open>order a (pderiv p) = order a e + order a d\<close>
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2759
      and \<open>order a p = Suc (order a (pderiv p))\<close>
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2760
      and \<open>order a (pderiv p) \<le> order a d\<close>
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2761
    by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2762
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2763
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2764
lemma poly_squarefree_decomp_order2:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2765
  "pderiv p \<noteq> 0 \<Longrightarrow> p = q * d \<Longrightarrow> pderiv p = e * d \<Longrightarrow>
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2766
    d = r * p + s * pderiv p \<Longrightarrow> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2767
  for p :: "'a::field_char_0 poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2768
  by (blast intro: poly_squarefree_decomp_order)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2769
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2770
lemma order_pderiv2:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2771
  "pderiv p \<noteq> 0 \<Longrightarrow> order a p \<noteq> 0 \<Longrightarrow> order a (pderiv p) = n \<longleftrightarrow> order a p = Suc n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2772
  for p :: "'a::field_char_0 poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2773
  by (auto dest: order_pderiv)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2774
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2775
definition rsquarefree :: "'a::idom poly \<Rightarrow> bool"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2776
  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
  2777
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2778
lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2779
  for p :: "'a::{semidom,semiring_char_0} poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2780
  by (cases p) (auto simp: pderiv_eq_0_iff split: if_splits)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2781
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2782
lemma rsquarefree_roots: "rsquarefree p \<longleftrightarrow> (\<forall>a. \<not> (poly p a = 0 \<and> poly (pderiv p) a = 0))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2783
  for p :: "'a::field_char_0 poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2784
  apply (simp add: rsquarefree_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2785
  apply (case_tac "p = 0")
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2786
   apply simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2787
  apply simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2788
  apply (case_tac "pderiv p = 0")
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2789
   apply simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2790
   apply (drule pderiv_iszero, clarsimp)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2791
   apply (metis coeff_0 coeff_pCons_0 degree_pCons_0 le0 le_antisym order_degree)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2792
  apply (force simp add: order_root order_pderiv2)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2793
  done
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2794
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2795
lemma poly_squarefree_decomp:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2796
  fixes p :: "'a::field_char_0 poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2797
  assumes "pderiv p \<noteq> 0"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2798
    and "p = q * d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2799
    and "pderiv p = e * d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2800
    and "d = r * p + s * pderiv p"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2801
  shows "rsquarefree q \<and> (\<forall>a. poly q a = 0 \<longleftrightarrow> poly p a = 0)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2802
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2803
  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2804
  with \<open>p = q * d\<close> have "q \<noteq> 0" by simp
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2805
  from assms have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2806
    by (rule poly_squarefree_decomp_order2)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2807
  with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2808
    by (simp add: rsquarefree_def order_root)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2809
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2810
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2811
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2812
subsection \<open>Algebraic numbers\<close>
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2813
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2814
text \<open>
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2815
  Algebraic numbers can be defined in two equivalent ways: all real numbers that are
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2816
  roots of rational polynomials or of integer polynomials. The Algebraic-Numbers AFP entry
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2817
  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
  2818
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2819
  The equivalence is obvious since any rational polynomial can be multiplied with the
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2820
  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
  2821
\<close>
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2822
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2823
definition algebraic :: "'a :: field_char_0 \<Rightarrow> bool"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2824
  where "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<int>) \<and> p \<noteq> 0 \<and> poly p x = 0)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2825
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2826
lemma algebraicI: "(\<And>i. coeff p i \<in> \<int>) \<Longrightarrow> p \<noteq> 0 \<Longrightarrow> poly p x = 0 \<Longrightarrow> algebraic x"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2827
  unfolding algebraic_def by blast
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2828
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2829
lemma algebraicE:
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2830
  assumes "algebraic x"
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2831
  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
  2832
  using assms unfolding algebraic_def by blast
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2833
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2834
lemma algebraic_altdef: "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2835
  for p :: "'a::field_char_0 poly"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2836
proof safe
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2837
  fix p
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2838
  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
  2839
  define cs where "cs = coeffs p"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2840
  from rat have "\<forall>c\<in>range (coeff p). \<exists>c'. c = of_rat c'"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2841
    unfolding Rats_def by blast
63060
293ede07b775 some uses of 'obtain' with structure statement;
wenzelm
parents: 63040
diff changeset
  2842
  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
  2843
    by (subst (asm) bchoice_iff) blast
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2844
  define cs' where "cs' = map (quotient_of \<circ> f) (coeffs p)"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2845
  define d where "d = Lcm (set (map snd cs'))"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 63035
diff changeset
  2846
  define p' where "p' = smult (of_int d) p"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2847
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2848
  have "coeff p' n \<in> \<int>" for n
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2849
  proof (cases "n \<le> degree p")
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2850
    case True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2851
    define c where "c = coeff p n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2852
    define a where "a = fst (quotient_of (f (coeff p n)))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2853
    define b where "b = snd (quotient_of (f (coeff p n)))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2854
    have b_pos: "b > 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2855
      unfolding b_def using quotient_of_denom_pos' by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2856
    have "coeff p' n = of_int d * coeff p n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2857
      by (simp add: p'_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2858
    also have "coeff p n = of_rat (of_int a / of_int b)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2859
      unfolding a_def b_def
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2860
      by (subst quotient_of_div [of "f (coeff p n)", symmetric]) (simp_all add: f [symmetric])
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2861
    also have "of_int d * \<dots> = of_rat (of_int (a*d) / of_int b)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2862
      by (simp add: of_rat_mult of_rat_divide)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2863
    also from nz True have "b \<in> snd ` set cs'"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2864
      by (force simp: cs'_def o_def b_def coeffs_def simp del: upt_Suc)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2865
    then have "b dvd (a * d)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2866
      by (simp add: d_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2867
    then have "of_int (a * d) / of_int b \<in> (\<int> :: rat set)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2868
      by (rule of_int_divide_in_Ints)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2869
    then have "of_rat (of_int (a * d) / of_int b) \<in> \<int>" by (elim Ints_cases) auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2870
    finally show ?thesis .
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2871
  next
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2872
    case False
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2873
    then show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2874
      by (auto simp: p'_def not_le coeff_eq_0)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2875
  qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2876
  moreover have "set (map snd cs') \<subseteq> {0<..}"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2877
    unfolding cs'_def using quotient_of_denom_pos' by (auto simp: coeffs_def simp del: upt_Suc)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2878
  then have "d \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2879
    unfolding d_def by (induct cs') simp_all
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2880
  with nz have "p' \<noteq> 0" by (simp add: p'_def)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2881
  moreover from root have "poly p' x = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2882
    by (simp add: p'_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2883
  ultimately show "algebraic x"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2884
    unfolding algebraic_def by blast
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2885
next
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2886
  assume "algebraic x"
63060
293ede07b775 some uses of 'obtain' with structure statement;
wenzelm
parents: 63040
diff changeset
  2887
  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
  2888
    by (force simp: algebraic_def)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2889
  moreover have "coeff p i \<in> \<int> \<Longrightarrow> coeff p i \<in> \<rat>" for i
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2890
    by (elim Ints_cases) simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2891
  ultimately show "\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0" by auto
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2892
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2893
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  2894
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2895
subsection \<open>Division of polynomials\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2896
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2897
subsubsection \<open>Division in general\<close>
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2898
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2899
instantiation poly :: (idom_divide) idom_divide
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2900
begin
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2901
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2902
fun divide_poly_main :: "'a \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2903
  where
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2904
    "divide_poly_main lc q r d dr (Suc n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2905
      (let cr = coeff r dr; a = cr div lc; mon = monom a n in
67369
7360fe6bb423 prefer formal comments;
wenzelm
parents: 67091
diff changeset
  2906
        if False \<or> a * lc = cr then \<comment> \<open>\<open>False \<or>\<close> is only because of problem in function-package\<close>
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2907
          divide_poly_main
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2908
            lc
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2909
            (q + mon)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2910
            (r - mon * d)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2911
            d (dr - 1) n else 0)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2912
  | "divide_poly_main lc q r d dr 0 = q"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2913
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2914
definition divide_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2915
  where "divide_poly f g =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2916
    (if g = 0 then 0
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2917
     else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2918
      divide_poly_main (coeff g (degree g)) 0 f g (degree f)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2919
        (1 + length (coeffs f) - length (coeffs g)))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2920
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2921
lemma divide_poly_main:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2922
  assumes d: "d \<noteq> 0" "lc = coeff d (degree d)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2923
    and "degree (d * r) \<le> dr" "divide_poly_main lc q (d * r) d dr n = q'"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2924
    and "n = 1 + dr - degree d \<or> dr = 0 \<and> n = 0 \<and> d * r = 0"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2925
  shows "q' = q + r"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2926
  using assms(3-)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2927
proof (induct n arbitrary: q r dr)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2928
  case (Suc n)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2929
  let ?rr = "d * r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2930
  let ?a = "coeff ?rr dr"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2931
  let ?qq = "?a div lc"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2932
  define b where [simp]: "b = monom ?qq n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2933
  let ?rrr =  "d * (r - b)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2934
  let ?qqq = "q + b"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2935
  note res = Suc(3)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2936
  from Suc(4) have dr: "dr = n + degree d" by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2937
  from d have lc: "lc \<noteq> 0" by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2938
  have "coeff (b * d) dr = coeff b n * coeff d (degree d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2939
  proof (cases "?qq = 0")
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2940
    case True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2941
    then show ?thesis by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2942
  next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2943
    case False
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2944
    then have n: "n = degree b"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2945
      by (simp add: degree_monom_eq)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2946
    show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2947
      unfolding n dr by (simp add: coeff_mult_degree_sum)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2948
  qed
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2949
  also have "\<dots> = lc * coeff b n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2950
    by (simp add: d)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2951
  finally have c2: "coeff (b * d) dr = lc * coeff b n" .
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2952
  have rrr: "?rrr = ?rr - b * d"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2953
    by (simp add: field_simps)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2954
  have c1: "coeff (d * r) dr = lc * coeff r n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2955
  proof (cases "degree r = n")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2956
    case True
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2957
    with Suc(2) show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2958
      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
  2959
  next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2960
    case False
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2961
    from dr Suc(2) have "degree r \<le> n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2962
      by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2963
        (metis add.commute add_le_cancel_left d(1) degree_0 degree_mult_eq
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2964
          diff_is_0_eq diff_zero le_cases)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2965
    with False have r_n: "degree r < n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2966
      by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2967
    then have right: "lc * coeff r n = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2968
      by (simp add: coeff_eq_0)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2969
    have "coeff (d * r) dr = coeff (d * r) (degree d + n)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2970
      by (simp add: dr ac_simps)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2971
    also from r_n have "\<dots> = 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2972
      by (metis False Suc.prems(1) add.commute add_left_imp_eq coeff_degree_mult coeff_eq_0
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2973
        coeff_mult_degree_sum degree_mult_le dr le_eq_less_or_eq)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2974
    finally show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2975
      by (simp only: right)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2976
  qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2977
  have c0: "coeff ?rrr dr = 0"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2978
    and id: "lc * (coeff (d * r) dr div lc) = coeff (d * r) dr"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2979
    unfolding rrr coeff_diff c2
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2980
    unfolding b_def coeff_monom coeff_smult c1 using lc by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2981
  from res[unfolded divide_poly_main.simps[of lc q] Let_def] id
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2982
  have res: "divide_poly_main lc ?qqq ?rrr d (dr - 1) n = q'"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2983
    by (simp del: divide_poly_main.simps add: field_simps)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2984
  note IH = Suc(1)[OF _ res]
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2985
  from Suc(4) have dr: "dr = n + degree d" by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2986
  from Suc(2) have deg_rr: "degree ?rr \<le> dr" by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2987
  have deg_bd: "degree (b * d) \<le> dr"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2988
    unfolding dr b_def by (rule order.trans[OF degree_mult_le]) (auto simp: degree_monom_le)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2989
  have "degree ?rrr \<le> dr"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2990
    unfolding rrr by (rule degree_diff_le[OF deg_rr deg_bd])
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2991
  with c0 have deg_rrr: "degree ?rrr \<le> (dr - 1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2992
    by (rule coeff_0_degree_minus_1)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  2993
  have "n = 1 + (dr - 1) - degree d \<or> dr - 1 = 0 \<and> n = 0 \<and> ?rrr = 0"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2994
  proof (cases dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  2995
    case 0
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2996
    with Suc(4) have 0: "dr = 0" "n = 0" "degree d = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2997
      by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2998
    with deg_rrr have "degree ?rrr = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  2999
      by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3000
    from degree_eq_zeroE[OF this] obtain a where rrr: "?rrr = [:a:]"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3001
      by metis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3002
    show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3003
      unfolding 0 using c0 unfolding rrr 0 by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3004
  next
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3005
    case _: Suc
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3006
    with Suc(4) show ?thesis by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3007
  qed
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3008
  from IH[OF deg_rrr this] show ?case
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3009
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3010
next
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3011
  case 0
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3012
  show ?case
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3013
  proof (cases "r = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3014
    case True
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3015
    with 0 show ?thesis by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3016
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3017
    case False
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3018
    from d False have "degree (d * r) = degree d + degree r"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3019
      by (subst degree_mult_eq) auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3020
    with 0 d show ?thesis by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3021
  qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3022
qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3023
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3024
lemma divide_poly_main_0: "divide_poly_main 0 0 r d dr n = 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3025
proof (induct n arbitrary: r d dr)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3026
  case 0
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3027
  then show ?case by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3028
next
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3029
  case Suc
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3030
  show ?case
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3031
    unfolding divide_poly_main.simps[of _ _ r] Let_def
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3032
    by (simp add: Suc del: divide_poly_main.simps)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3033
qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3034
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3035
lemma divide_poly:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3036
  assumes g: "g \<noteq> 0"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3037
  shows "(f * g) div g = (f :: 'a poly)"
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3038
proof -
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3039
  have len: "length (coeffs f) = Suc (degree f)" if "f \<noteq> 0" for f :: "'a poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3040
    using that unfolding degree_eq_length_coeffs by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3041
  have "divide_poly_main (coeff g (degree g)) 0 (g * f) g (degree (g * f))
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3042
    (1 + length (coeffs (g * f)) - length (coeffs  g)) = (f * g) div g"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3043
    by (simp add: divide_poly_def Let_def ac_simps)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3044
  note main = divide_poly_main[OF g refl le_refl this]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3045
  have "(f * g) div g = 0 + f"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3046
  proof (rule main, goal_cases)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3047
    case 1
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3048
    show ?case
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3049
    proof (cases "f = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3050
      case True
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3051
      with g show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3052
        by (auto simp: degree_eq_length_coeffs)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3053
    next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3054
      case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3055
      with g have fg: "g * f \<noteq> 0" by auto
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3056
      show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3057
        unfolding len[OF fg] len[OF g] by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3058
    qed
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3059
  qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3060
  then show ?thesis by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3061
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3062
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3063
lemma divide_poly_0: "f div 0 = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3064
  for f :: "'a poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3065
  by (simp add: divide_poly_def Let_def divide_poly_main_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3066
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3067
instance
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3068
  by standard (auto simp: divide_poly divide_poly_0)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3069
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3070
end
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3071
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3072
instance poly :: (idom_divide) algebraic_semidom ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3073
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3074
lemma div_const_poly_conv_map_poly:
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3075
  assumes "[:c:] dvd p"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3076
  shows "p div [:c:] = map_poly (\<lambda>x. x div c) p"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3077
proof (cases "c = 0")
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3078
  case True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3079
  then show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3080
    by (auto intro!: poly_eqI simp: coeff_map_poly)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3081
next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3082
  case False
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3083
  from assms obtain q where p: "p = [:c:] * q" by (rule dvdE)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3084
  moreover {
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3085
    have "smult c q = [:c:] * q"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3086
      by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3087
    also have "\<dots> div [:c:] = q"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3088
      by (rule nonzero_mult_div_cancel_left) (use False in auto)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3089
    finally have "smult c q div [:c:] = q" .
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3090
  }
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3091
  ultimately show ?thesis by (intro poly_eqI) (auto simp: coeff_map_poly False)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3092
qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3093
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3094
lemma is_unit_monom_0:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3095
  fixes a :: "'a::field"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3096
  assumes "a \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3097
  shows "is_unit (monom a 0)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3098
proof
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3099
  from assms show "1 = monom a 0 * monom (inverse a) 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3100
    by (simp add: mult_monom)
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3101
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3102
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3103
lemma is_unit_triv: "a \<noteq> 0 \<Longrightarrow> is_unit [:a:]"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3104
  for a :: "'a::field"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3105
  by (simp add: is_unit_monom_0 monom_0 [symmetric])
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3106
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3107
lemma is_unit_iff_degree:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3108
  fixes p :: "'a::field poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3109
  assumes "p \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3110
  shows "is_unit p \<longleftrightarrow> degree p = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3111
    (is "?lhs \<longleftrightarrow> ?rhs")
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3112
proof
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3113
  assume ?rhs
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3114
  then obtain a where "p = [:a:]"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3115
    by (rule degree_eq_zeroE)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3116
  with assms show ?lhs
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3117
    by (simp add: is_unit_triv)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3118
next
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3119
  assume ?lhs
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3120
  then obtain q where "q \<noteq> 0" "p * q = 1" ..
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3121
  then have "degree (p * q) = degree 1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3122
    by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3123
  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
  3124
    by (simp add: degree_mult_eq)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3125
  then show ?rhs by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3126
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3127
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3128
lemma is_unit_pCons_iff: "is_unit (pCons a p) \<longleftrightarrow> p = 0 \<and> a \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3129
  for p :: "'a::field poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3130
  by (cases "p = 0") (auto simp: is_unit_triv is_unit_iff_degree)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3131
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3132
lemma is_unit_monom_trival: "is_unit p \<Longrightarrow> monom (coeff p (degree p)) 0 = p"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3133
  for p :: "'a::field poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3134
  by (cases p) (simp_all add: monom_0 is_unit_pCons_iff)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3135
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3136
lemma is_unit_const_poly_iff: "[:c:] dvd 1 \<longleftrightarrow> c dvd 1"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3137
  for c :: "'a::{comm_semiring_1,semiring_no_zero_divisors}"
65486
d801126a14cb more systematic treatment of polynomial 1
haftmann
parents: 65484
diff changeset
  3138
  by (auto simp: one_pCons)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3139
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3140
lemma is_unit_polyE:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3141
  fixes p :: "'a :: {comm_semiring_1,semiring_no_zero_divisors} poly"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3142
  assumes "p dvd 1"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3143
  obtains c where "p = [:c:]" "c dvd 1"
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3144
proof -
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3145
  from assms obtain q where "1 = p * q"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3146
    by (rule dvdE)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3147
  then have "p \<noteq> 0" and "q \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3148
    by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3149
  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
  3150
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3151
  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
  3152
    by (simp add: degree_mult_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3153
  finally have "degree p = 0" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3154
  with degree_eq_zeroE obtain c where c: "p = [:c:]" .
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3155
  with \<open>p dvd 1\<close> have "c dvd 1"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3156
    by (simp add: is_unit_const_poly_iff)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3157
  with c show thesis ..
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3158
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3159
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3160
lemma is_unit_polyE':
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3161
  fixes p :: "'a::field poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3162
  assumes "is_unit p"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3163
  obtains a where "p = monom a 0" and "a \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3164
proof -
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3165
  obtain a q where "p = pCons a q"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3166
    by (cases p)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3167
  with assms have "p = [:a:]" and "a \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3168
    by (simp_all add: is_unit_pCons_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3169
  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
  3170
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3171
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3172
lemma is_unit_poly_iff: "p dvd 1 \<longleftrightarrow> (\<exists>c. p = [:c:] \<and> c dvd 1)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3173
  for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3174
  by (auto elim: is_unit_polyE simp add: is_unit_const_poly_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3175
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3176
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3177
subsubsection \<open>Pseudo-Division\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3178
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3179
text \<open>This part is by René Thiemann and Akihisa Yamada.\<close>
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3180
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3181
fun pseudo_divmod_main ::
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3182
  "'a :: comm_ring_1  \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a poly \<times> 'a poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3183
  where
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3184
    "pseudo_divmod_main lc q r d dr (Suc n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3185
      (let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3186
        rr = smult lc r;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3187
        qq = coeff r dr;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3188
        rrr = rr - monom qq n * d;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3189
        qqq = smult lc q + monom qq n
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3190
       in pseudo_divmod_main lc qqq rrr d (dr - 1) n)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3191
  | "pseudo_divmod_main lc q r d dr 0 = (q,r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3192
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3193
definition pseudo_divmod :: "'a :: comm_ring_1 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3194
  where "pseudo_divmod p q \<equiv>
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3195
    if q = 0 then (0, p)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3196
    else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3197
      pseudo_divmod_main (coeff q (degree q)) 0 p q (degree p)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3198
        (1 + length (coeffs p) - length (coeffs q))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3199
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3200
lemma pseudo_divmod_main:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3201
  assumes d: "d \<noteq> 0" "lc = coeff d (degree d)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3202
    and "degree r \<le> dr" "pseudo_divmod_main lc q r d dr n = (q',r')"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3203
    and "n = 1 + dr - degree d \<or> dr = 0 \<and> n = 0 \<and> r = 0"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3204
  shows "(r' = 0 \<or> degree r' < degree d) \<and> smult (lc^n) (d * q + r) = d * q' + r'"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3205
  using assms(3-)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3206
proof (induct n arbitrary: q r dr)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3207
  case 0
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3208
  then show ?case by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3209
next
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3210
  case (Suc n)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3211
  let ?rr = "smult lc r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3212
  let ?qq = "coeff r dr"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3213
  define b where [simp]: "b = monom ?qq n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3214
  let ?rrr = "?rr - b * d"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3215
  let ?qqq = "smult lc q + b"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3216
  note res = Suc(3)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3217
  from res[unfolded pseudo_divmod_main.simps[of lc q] Let_def]
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3218
  have res: "pseudo_divmod_main lc ?qqq ?rrr d (dr - 1) n = (q',r')"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3219
    by (simp del: pseudo_divmod_main.simps)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3220
  from Suc(4) have dr: "dr = n + degree d" by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3221
  have "coeff (b * d) dr = coeff b n * coeff d (degree d)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3222
  proof (cases "?qq = 0")
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3223
    case True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3224
    then show ?thesis by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3225
  next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3226
    case False
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3227
    then have n: "n = degree b"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3228
      by (simp add: degree_monom_eq)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3229
    show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3230
      unfolding n dr by (simp add: coeff_mult_degree_sum)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3231
  qed
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3232
  also have "\<dots> = lc * coeff b n" by (simp add: d)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3233
  finally have "coeff (b * d) dr = lc * coeff b n" .
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3234
  moreover have "coeff ?rr dr = lc * coeff r dr"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3235
    by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3236
  ultimately have c0: "coeff ?rrr dr = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3237
    by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3238
  from Suc(4) have dr: "dr = n + degree d" by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3239
  have deg_rr: "degree ?rr \<le> dr"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3240
    using Suc(2) degree_smult_le dual_order.trans by blast
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3241
  have deg_bd: "degree (b * d) \<le> dr"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3242
    unfolding dr by (rule order.trans[OF degree_mult_le]) (auto simp: degree_monom_le)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3243
  have "degree ?rrr \<le> dr"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3244
    using degree_diff_le[OF deg_rr deg_bd] by auto
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3245
  with c0 have deg_rrr: "degree ?rrr \<le> (dr - 1)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3246
    by (rule coeff_0_degree_minus_1)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3247
  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
  3248
  proof (cases dr)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3249
    case 0
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3250
    with Suc(4) have 0: "dr = 0" "n = 0" "degree d = 0" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3251
    with deg_rrr have "degree ?rrr = 0" by simp
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3252
    then have "\<exists>a. ?rrr = [:a:]"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3253
      by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3254
    from this obtain a where rrr: "?rrr = [:a:]"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3255
      by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3256
    show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3257
      unfolding 0 using c0 unfolding rrr 0 by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3258
  next
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3259
    case _: Suc
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3260
    with Suc(4) show ?thesis by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3261
  qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3262
  note IH = Suc(1)[OF deg_rrr res this]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3263
  show ?case
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3264
  proof (intro conjI)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3265
    from IH show "r' = 0 \<or> degree r' < degree d"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3266
      by blast
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3267
    show "smult (lc ^ Suc n) (d * q + r) = d * q' + r'"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3268
      unfolding IH[THEN conjunct2,symmetric]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3269
      by (simp add: field_simps smult_add_right)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3270
  qed
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3271
qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3272
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3273
lemma pseudo_divmod:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3274
  assumes g: "g \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3275
    and *: "pseudo_divmod f g = (q,r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3276
  shows "smult (coeff g (degree g) ^ (Suc (degree f) - degree g)) f = g * q + r"  (is ?A)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3277
    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
  3278
proof -
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3279
  from *[unfolded pseudo_divmod_def Let_def]
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3280
  have "pseudo_divmod_main (coeff g (degree g)) 0 f g (degree f)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3281
      (1 + length (coeffs f) - length (coeffs g)) = (q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3282
    by (auto simp: g)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3283
  note main = pseudo_divmod_main[OF _ _ _ this, OF g refl le_refl]
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3284
  from g have "1 + length (coeffs f) - length (coeffs g) = 1 + degree f - degree g \<or>
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3285
    degree f = 0 \<and> 1 + length (coeffs f) - length (coeffs g) = 0 \<and> f = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3286
    by (cases "f = 0"; cases "coeffs g") (auto simp: degree_eq_length_coeffs)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3287
  note main' = main[OF this]
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3288
  then show "r = 0 \<or> degree r < degree g" by auto
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3289
  show "smult (coeff g (degree g) ^ (Suc (degree f) - degree g)) f = g * q + r"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3290
    by (subst main'[THEN conjunct2, symmetric], simp add: degree_eq_length_coeffs,
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3291
        cases "f = 0"; cases "coeffs g", use g in auto)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3292
qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3293
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3294
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
  3295
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3296
lemma snd_pseudo_divmod_main:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3297
  "snd (pseudo_divmod_main lc q r d dr n) = snd (pseudo_divmod_main lc q' r d dr n)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3298
  by (induct n arbitrary: q q' lc r d dr) (simp_all add: Let_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3299
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3300
definition pseudo_mod :: "'a::{comm_ring_1,semiring_1_no_zero_divisors} poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3301
  where "pseudo_mod f g = snd (pseudo_divmod f g)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3302
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3303
lemma pseudo_mod:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3304
  fixes f g :: "'a::{comm_ring_1,semiring_1_no_zero_divisors} poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3305
  defines "r \<equiv> pseudo_mod f g"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3306
  assumes g: "g \<noteq> 0"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3307
  shows "\<exists>a q. a \<noteq> 0 \<and> smult a f = g * q + r" "r = 0 \<or> degree r < degree g"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3308
proof -
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3309
  let ?cg = "coeff g (degree g)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3310
  let ?cge = "?cg ^ (Suc (degree f) - degree g)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3311
  define a where "a = ?cge"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3312
  from r_def[unfolded pseudo_mod_def] obtain q where pdm: "pseudo_divmod f g = (q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3313
    by (cases "pseudo_divmod f g") auto
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3314
  from pseudo_divmod[OF g pdm] have id: "smult a f = g * q + r" and "r = 0 \<or> degree r < degree g"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3315
    by (auto simp: a_def)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3316
  show "r = 0 \<or> degree r < degree g" by fact
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3317
  from g have "a \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3318
    by (auto simp: a_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3319
  with id show "\<exists>a q. a \<noteq> 0 \<and> smult a f = g * q + r"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3320
    by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3321
qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3322
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3323
lemma fst_pseudo_divmod_main_as_divide_poly_main:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3324
  assumes d: "d \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3325
  defines lc: "lc \<equiv> coeff d (degree d)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3326
  shows "fst (pseudo_divmod_main lc q r d dr n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3327
    divide_poly_main lc (smult (lc^n) q) (smult (lc^n) r) d dr n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3328
proof (induct n arbitrary: q r dr)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3329
  case 0
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3330
  then show ?case by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3331
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3332
  case (Suc n)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3333
  note lc0 = leading_coeff_neq_0[OF d, folded lc]
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3334
  then have "pseudo_divmod_main lc q r d dr (Suc n) =
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3335
    pseudo_divmod_main lc (smult lc q + monom (coeff r dr) n)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3336
      (smult lc r - monom (coeff r dr) n * d) d (dr - 1) n"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3337
    by (simp add: Let_def ac_simps)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3338
  also have "fst \<dots> = divide_poly_main lc
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3339
     (smult (lc^n) (smult lc q + monom (coeff r dr) n))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3340
     (smult (lc^n) (smult lc r - monom (coeff r dr) n * d))
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3341
     d (dr - 1) n"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3342
    by (simp only: Suc[unfolded divide_poly_main.simps Let_def])
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3343
  also have "\<dots> = divide_poly_main lc (smult (lc ^ Suc n) q) (smult (lc ^ Suc n) r) d dr (Suc n)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3344
    unfolding smult_monom smult_distribs mult_smult_left[symmetric]
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3345
    using lc0 by (simp add: Let_def ac_simps)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3346
  finally show ?case .
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3347
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3348
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3349
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3350
subsubsection \<open>Division in polynomials over fields\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3351
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3352
lemma pseudo_divmod_field:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3353
  fixes g :: "'a::field poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3354
  assumes g: "g \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3355
    and *: "pseudo_divmod f g = (q,r)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3356
  defines "c \<equiv> coeff g (degree g) ^ (Suc (degree f) - degree g)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3357
  shows "f = g * smult (1/c) q + smult (1/c) r"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3358
proof -
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3359
  from leading_coeff_neq_0[OF g] have c0: "c \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3360
    by (auto simp: c_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3361
  from pseudo_divmod(1)[OF g *, folded c_def] have "smult c f = g * q + r"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3362
    by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3363
  also have "smult (1 / c) \<dots> = g * smult (1 / c) q + smult (1 / c) r"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3364
    by (simp add: smult_add_right)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3365
  finally show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3366
    using c0 by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3367
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3368
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3369
lemma divide_poly_main_field:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3370
  fixes d :: "'a::field poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3371
  assumes d: "d \<noteq> 0"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3372
  defines lc: "lc \<equiv> coeff d (degree d)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3373
  shows "divide_poly_main lc q r d dr n =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3374
    fst (pseudo_divmod_main lc (smult ((1 / lc)^n) q) (smult ((1 / lc)^n) r) d dr n)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3375
  unfolding lc by (subst fst_pseudo_divmod_main_as_divide_poly_main) (auto simp: d power_one_over)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3376
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3377
lemma divide_poly_field:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3378
  fixes f g :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3379
  defines "f' \<equiv> smult ((1 / coeff g (degree g)) ^ (Suc (degree f) - degree g)) f"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3380
  shows "f div g = fst (pseudo_divmod f' g)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3381
proof (cases "g = 0")
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3382
  case True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3383
  show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3384
    unfolding divide_poly_def pseudo_divmod_def Let_def f'_def True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3385
    by (simp add: divide_poly_main_0)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3386
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3387
  case False
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3388
  from leading_coeff_neq_0[OF False] have "degree f' = degree f"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3389
    by (auto simp: f'_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3390
  then show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3391
    using length_coeffs_degree[of f'] length_coeffs_degree[of f]
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3392
    unfolding divide_poly_def pseudo_divmod_def Let_def
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3393
      divide_poly_main_field[OF False]
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3394
      length_coeffs_degree[OF False]
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3395
      f'_def
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3396
    by force
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3397
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3398
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3399
instantiation poly :: ("{semidom_divide_unit_factor,idom_divide}") normalization_semidom
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3400
begin
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3401
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3402
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
  3403
  where "unit_factor_poly p = [:unit_factor (lead_coeff p):]"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3404
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3405
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
  3406
  where "normalize p = p div [:unit_factor (lead_coeff p):]"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3407
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3408
instance
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3409
proof
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3410
  fix p :: "'a poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3411
  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
  3412
  proof (cases "p = 0")
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3413
    case True
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3414
    then show ?thesis
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3415
      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
  3416
  next
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3417
    case False
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3418
    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
  3419
      by simp
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3420
    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
  3421
      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
  3422
    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
  3423
      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
  3424
    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
  3425
      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
  3426
    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
  3427
    proof -
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3428
      from ** obtain b where "c = unit_factor (lead_coeff p) * b" ..
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3429
      with False * show ?thesis by simp
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3430
    qed
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3431
    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
  3432
      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
  3433
      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
  3434
    then show ?thesis
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3435
      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
  3436
        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
  3437
  qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3438
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3439
  fix p :: "'a poly"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3440
  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
  3441
  then obtain c where p: "p = [:c:]" "c dvd 1"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3442
    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
  3443
  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
  3444
    by (simp add: unit_factor_poly_def monom_0 is_unit_unit_factor)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3445
next
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3446
  fix p :: "'a poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3447
  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
  3448
  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
  3449
    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
  3450
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
  3451
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3452
end
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3453
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3454
lemma normalize_poly_eq_map_poly: "normalize p = map_poly (\<lambda>x. x div unit_factor (lead_coeff p)) p"
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3455
proof -
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3456
  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
  3457
    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
  3458
  then show ?thesis
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3459
    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
  3460
qed
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3461
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3462
lemma coeff_normalize [simp]:
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3463
  "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
  3464
  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
  3465
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3466
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
  3467
  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
  3468
begin
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3469
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3470
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
  3471
proof
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3472
  fix a
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3473
  assume "a \<noteq> 0"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3474
  then have "1 = a * inverse a" by simp
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3475
  then have "a dvd 1" ..
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3476
  then show "unit_factor a dvd 1" by simp
64848
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3477
qed simp_all
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3478
c50db2128048 slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
haftmann
parents: 64811
diff changeset
  3479
end
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3480
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3481
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
  3482
  "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
  3483
  by (simp add: unit_factor_poly_def)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3484
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3485
lemma normalize_monom [simp]: "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
  3486
  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
  3487
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3488
lemma unit_factor_monom [simp]: "unit_factor (monom a n) = [:unit_factor a:]"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3489
  by (cases "a = 0") (simp_all add: unit_factor_poly_def degree_monom_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3490
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3491
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
  3492
  by (simp add: normalize_poly_eq_map_poly map_poly_pCons)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3493
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3494
lemma normalize_smult: "normalize (smult c p) = smult (normalize c) (normalize p)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3495
proof -
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3496
  have "smult c p = [:c:] * p" by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3497
  also have "normalize \<dots> = smult (normalize c) (normalize p)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3498
    by (subst normalize_mult) (simp add: normalize_const_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3499
  finally show ?thesis .
62352
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3500
qed
35a9e1cbb5b3 separated potentially conflicting type class instance into separate theory
haftmann
parents: 62351
diff changeset
  3501
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3502
inductive eucl_rel_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly \<Rightarrow> bool"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3503
  where
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3504
    eucl_rel_poly_by0: "eucl_rel_poly x 0 (0, x)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3505
  | eucl_rel_poly_dividesI: "y \<noteq> 0 \<Longrightarrow> x = q * y \<Longrightarrow> eucl_rel_poly x y (q, 0)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3506
  | eucl_rel_poly_remainderI:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3507
      "y \<noteq> 0 \<Longrightarrow> degree r < degree y \<Longrightarrow> x = q * y + r \<Longrightarrow> eucl_rel_poly x y (q, r)"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3508
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3509
lemma eucl_rel_poly_iff:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3510
  "eucl_rel_poly x y (q, r) \<longleftrightarrow>
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3511
    x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3512
  by (auto elim: eucl_rel_poly.cases
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3513
      intro: eucl_rel_poly_by0 eucl_rel_poly_dividesI eucl_rel_poly_remainderI)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3514
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3515
lemma eucl_rel_poly_0: "eucl_rel_poly 0 y (0, 0)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3516
  by (simp add: eucl_rel_poly_iff)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3517
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3518
lemma eucl_rel_poly_by_0: "eucl_rel_poly x 0 (0, x)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3519
  by (simp add: eucl_rel_poly_iff)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3520
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3521
lemma eucl_rel_poly_pCons:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3522
  assumes rel: "eucl_rel_poly x y (q, r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3523
  assumes y: "y \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3524
  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3525
  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
  3526
    (is "eucl_rel_poly ?x y (?q, ?r)")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3527
proof -
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3528
  from assms have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3529
    by (simp_all add: eucl_rel_poly_iff)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3530
  from b x have "?x = ?q * y + ?r" by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3531
  moreover
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3532
  have "?r = 0 \<or> degree ?r < degree y"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3533
  proof (rule eq_zero_or_degree_less)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3534
    show "degree ?r \<le> degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3535
    proof (rule degree_diff_le)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3536
      from r show "degree (pCons a r) \<le> degree y"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3537
        by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3538
      show "degree (smult b y) \<le> degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3539
        by (rule degree_smult_le)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3540
    qed
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3541
    from \<open>y \<noteq> 0\<close> show "coeff ?r (degree y) = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3542
      by (simp add: b)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3543
  qed
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3544
  ultimately show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3545
    unfolding eucl_rel_poly_iff using \<open>y \<noteq> 0\<close> by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3546
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3547
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3548
lemma eucl_rel_poly_exists: "\<exists>q r. eucl_rel_poly x y (q, r)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3549
  apply (cases "y = 0")
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3550
   apply (fast intro!: eucl_rel_poly_by_0)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3551
  apply (induct x)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3552
   apply (fast intro!: eucl_rel_poly_0)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3553
  apply (fast intro!: eucl_rel_poly_pCons)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3554
  done
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3555
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3556
lemma eucl_rel_poly_unique:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3557
  assumes 1: "eucl_rel_poly x y (q1, r1)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3558
  assumes 2: "eucl_rel_poly x y (q2, r2)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3559
  shows "q1 = q2 \<and> r1 = r2"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3560
proof (cases "y = 0")
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3561
  assume "y = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3562
  with assms show ?thesis
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3563
    by (simp add: eucl_rel_poly_iff)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3564
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3565
  assume [simp]: "y \<noteq> 0"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3566
  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
  3567
    unfolding eucl_rel_poly_iff by simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3568
  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
  3569
    unfolding eucl_rel_poly_iff by simp_all
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3570
  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3571
    by (simp add: algebra_simps)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3572
  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3573
    by (auto intro: degree_diff_less)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3574
  show "q1 = q2 \<and> r1 = r2"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3575
  proof (rule classical)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3576
    assume "\<not> ?thesis"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3577
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3578
    with r3 have "degree (r2 - r1) < degree y" by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3579
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3580
    also from \<open>q1 \<noteq> q2\<close> have "\<dots> = degree ((q1 - q2) * y)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3581
      by (simp add: degree_mult_eq)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3582
    also from q3 have "\<dots> = degree (r2 - r1)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3583
      by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3584
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3585
    then show ?thesis by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3586
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3587
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3588
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3589
lemma eucl_rel_poly_0_iff: "eucl_rel_poly 0 y (q, r) \<longleftrightarrow> q = 0 \<and> r = 0"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3590
  by (auto dest: eucl_rel_poly_unique intro: eucl_rel_poly_0)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3591
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3592
lemma eucl_rel_poly_by_0_iff: "eucl_rel_poly x 0 (q, r) \<longleftrightarrow> q = 0 \<and> r = x"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3593
  by (auto dest: eucl_rel_poly_unique intro: eucl_rel_poly_by_0)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3594
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3595
lemmas eucl_rel_poly_unique_div = eucl_rel_poly_unique [THEN conjunct1]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3596
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3597
lemmas eucl_rel_poly_unique_mod = eucl_rel_poly_unique [THEN conjunct2]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3598
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3599
instantiation poly :: (field) semidom_modulo
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3600
begin
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3601
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3602
definition modulo_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3603
  where mod_poly_def: "f mod g =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3604
    (if g = 0 then f else pseudo_mod (smult ((1 / lead_coeff g) ^ (Suc (degree f) - degree g)) f) g)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3605
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3606
instance
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3607
proof
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3608
  fix x y :: "'a poly"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3609
  show "x div y * y + x mod y = x"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3610
  proof (cases "y = 0")
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3611
    case True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3612
    then show ?thesis
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3613
      by (simp add: divide_poly_0 mod_poly_def)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3614
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3615
    case False
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3616
    then have "pseudo_divmod (smult ((1 / lead_coeff y) ^ (Suc (degree x) - degree y)) x) y =
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3617
        (x div y, x mod y)"
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3618
      by (simp add: divide_poly_field mod_poly_def pseudo_mod_def)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3619
    with False pseudo_divmod [OF False this] show ?thesis
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3620
      by (simp add: power_mult_distrib [symmetric] ac_simps)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3621
  qed
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3622
qed
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3623
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3624
end
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3625
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3626
lemma eucl_rel_poly: "eucl_rel_poly x y (x div y, x mod y)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3627
  unfolding eucl_rel_poly_iff
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3628
proof
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3629
  show "x = x div y * y + x mod y"
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3630
    by (simp add: div_mult_mod_eq)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3631
  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
  3632
  proof (cases "y = 0")
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3633
    case True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3634
    then show ?thesis by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3635
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3636
    case False
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3637
    with pseudo_mod[OF this] show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3638
      by (simp add: mod_poly_def)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3639
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3640
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3641
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3642
lemma div_poly_eq: "eucl_rel_poly x y (q, r) \<Longrightarrow> x div y = q"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3643
  for x :: "'a::field poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3644
  by (rule eucl_rel_poly_unique_div [OF eucl_rel_poly])
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3645
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3646
lemma mod_poly_eq: "eucl_rel_poly x y (q, r) \<Longrightarrow> x mod y = r"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3647
  for x :: "'a::field poly"
64861
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3648
  by (rule eucl_rel_poly_unique_mod [OF eucl_rel_poly])
9e8de30fd859 separate instance for semidom_modulo
haftmann
parents: 64860
diff changeset
  3649
66806
a4e82b58d833 abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
haftmann
parents: 66805
diff changeset
  3650
instance poly :: (field) idom_modulo ..
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3651
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3652
lemma div_pCons_eq:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3653
  "pCons a p div q =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3654
    (if q = 0 then 0
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3655
     else pCons (coeff (pCons a (p mod q)) (degree q) / lead_coeff q) (p div q))"
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3656
  using eucl_rel_poly_pCons [OF eucl_rel_poly _ refl, of q a p]
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3657
  by (auto intro: div_poly_eq)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3658
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3659
lemma mod_pCons_eq:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3660
  "pCons a p mod q =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3661
    (if q = 0 then pCons a p
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3662
     else pCons a (p mod q) - smult (coeff (pCons a (p mod q)) (degree q) / lead_coeff q) q)"
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3663
  using eucl_rel_poly_pCons [OF eucl_rel_poly _ refl, of q a p]
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3664
  by (auto intro: mod_poly_eq)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3665
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  3666
lemma div_mod_fold_coeffs:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3667
  "(p div q, p mod q) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3668
    (if q = 0 then (0, p)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3669
     else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3670
      fold_coeffs
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3671
        (\<lambda>a (s, r).
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3672
          let b = coeff (pCons a r) (degree q) / coeff q (degree q)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3673
          in (pCons b s, pCons a r - smult b q)) p (0, 0))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3674
  by (rule sym, induct p) (auto simp: div_pCons_eq mod_pCons_eq Let_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3675
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3676
lemma degree_mod_less: "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3677
  using eucl_rel_poly [of x y] unfolding eucl_rel_poly_iff by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3678
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3679
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
  3680
  using degree_mod_less[of b a] by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3681
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3682
lemma div_poly_less:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3683
  fixes x :: "'a::field poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3684
  assumes "degree x < degree y"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3685
  shows "x div y = 0"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3686
proof -
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3687
  from assms have "eucl_rel_poly x y (0, x)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3688
    by (simp add: eucl_rel_poly_iff)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3689
  then show "x div y = 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3690
    by (rule div_poly_eq)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3691
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3692
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3693
lemma mod_poly_less:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3694
  assumes "degree x < degree y"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3695
  shows "x mod y = x"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3696
proof -
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3697
  from assms have "eucl_rel_poly x y (0, x)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3698
    by (simp add: eucl_rel_poly_iff)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3699
  then show "x mod y = x"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3700
    by (rule mod_poly_eq)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3701
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3702
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3703
lemma eucl_rel_poly_smult_left:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3704
  "eucl_rel_poly x y (q, r) \<Longrightarrow> eucl_rel_poly (smult a x) y (smult a q, smult a r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3705
  by (simp add: eucl_rel_poly_iff smult_add_right)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3706
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3707
lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3708
  for x y :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3709
  by (rule div_poly_eq, rule eucl_rel_poly_smult_left, rule eucl_rel_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3710
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3711
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3712
  by (rule mod_poly_eq, rule eucl_rel_poly_smult_left, rule eucl_rel_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3713
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3714
lemma poly_div_minus_left [simp]: "(- x) div y = - (x div y)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3715
  for x y :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3716
  using div_smult_left [of "- 1::'a"] by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3717
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3718
lemma poly_mod_minus_left [simp]: "(- x) mod y = - (x mod y)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3719
  for x y :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3720
  using mod_smult_left [of "- 1::'a"] by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3721
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3722
lemma eucl_rel_poly_add_left:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3723
  assumes "eucl_rel_poly x y (q, r)"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3724
  assumes "eucl_rel_poly x' y (q', r')"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3725
  shows "eucl_rel_poly (x + x') y (q + q', r + r')"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3726
  using assms unfolding eucl_rel_poly_iff
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3727
  by (auto simp: algebra_simps degree_add_less)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3728
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3729
lemma poly_div_add_left: "(x + y) div z = x div z + y div z"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3730
  for x y z :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3731
  using eucl_rel_poly_add_left [OF eucl_rel_poly eucl_rel_poly]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3732
  by (rule div_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3733
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3734
lemma poly_mod_add_left: "(x + y) mod z = x mod z + y mod z"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3735
  for x y z :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3736
  using eucl_rel_poly_add_left [OF eucl_rel_poly eucl_rel_poly]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3737
  by (rule mod_poly_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3738
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3739
lemma poly_div_diff_left: "(x - y) div z = x div z - y div z"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3740
  for x y z :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3741
  by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3742
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3743
lemma poly_mod_diff_left: "(x - y) mod z = x mod z - y mod z"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3744
  for x y z :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3745
  by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3746
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3747
lemma eucl_rel_poly_smult_right:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3748
  "a \<noteq> 0 \<Longrightarrow> eucl_rel_poly x y (q, r) \<Longrightarrow> eucl_rel_poly x (smult a y) (smult (inverse a) q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3749
  by (simp add: eucl_rel_poly_iff)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3750
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3751
lemma div_smult_right: "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3752
  for x y :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3753
  by (rule div_poly_eq, erule eucl_rel_poly_smult_right, rule eucl_rel_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3754
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3755
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3756
  by (rule mod_poly_eq, erule eucl_rel_poly_smult_right, rule eucl_rel_poly)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3757
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3758
lemma poly_div_minus_right [simp]: "x div (- y) = - (x div y)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3759
  for x y :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3760
  using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3761
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3762
lemma poly_mod_minus_right [simp]: "x mod (- y) = x mod y"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3763
  for x y :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3764
  using mod_smult_right [of "- 1::'a"] by simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3765
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3766
lemma eucl_rel_poly_mult:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3767
  "eucl_rel_poly x y (q, r) \<Longrightarrow> eucl_rel_poly q z (q', r') \<Longrightarrow>
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3768
    eucl_rel_poly x (y * z) (q', y * r' + r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3769
  apply (cases "z = 0", simp add: eucl_rel_poly_iff)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3770
  apply (cases "y = 0", simp add: eucl_rel_poly_by_0_iff eucl_rel_poly_0_iff)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3771
  apply (cases "r = 0")
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3772
   apply (cases "r' = 0")
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3773
    apply (simp add: eucl_rel_poly_iff)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3774
   apply (simp add: eucl_rel_poly_iff field_simps degree_mult_eq)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3775
  apply (cases "r' = 0")
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3776
   apply (simp add: eucl_rel_poly_iff degree_mult_eq)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3777
  apply (simp add: eucl_rel_poly_iff field_simps)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3778
  apply (simp add: degree_mult_eq degree_add_less)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3779
  done
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3780
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3781
lemma poly_div_mult_right: "x div (y * z) = (x div y) div z"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3782
  for x y z :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3783
  by (rule div_poly_eq, rule eucl_rel_poly_mult, (rule eucl_rel_poly)+)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3784
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3785
lemma poly_mod_mult_right: "x mod (y * z) = y * (x div y mod z) + x mod y"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3786
  for x y z :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3787
  by (rule mod_poly_eq, rule eucl_rel_poly_mult, (rule eucl_rel_poly)+)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3788
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3789
lemma mod_pCons:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3790
  fixes a :: "'a::field"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3791
    and x y :: "'a::field poly"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3792
  assumes y: "y \<noteq> 0"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3793
  defines "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3794
  shows "(pCons a x) mod y = pCons a (x mod y) - smult b y"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3795
  unfolding b_def
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3796
  by (rule mod_poly_eq, rule eucl_rel_poly_pCons [OF eucl_rel_poly y refl])
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3797
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3798
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3799
subsubsection \<open>List-based versions for fast implementation\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3800
(* Subsection by:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3801
      Sebastiaan Joosten
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3802
      René Thiemann
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3803
      Akihisa Yamada
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3804
    *)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3805
fun minus_poly_rev_list :: "'a :: group_add list \<Rightarrow> 'a list \<Rightarrow> 'a list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3806
  where
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3807
    "minus_poly_rev_list (x # xs) (y # ys) = (x - y) # (minus_poly_rev_list xs ys)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3808
  | "minus_poly_rev_list xs [] = xs"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3809
  | "minus_poly_rev_list [] (y # ys) = []"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3810
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3811
fun pseudo_divmod_main_list ::
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3812
  "'a::comm_ring_1 \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list \<times> 'a list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3813
  where
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3814
    "pseudo_divmod_main_list lc q r d (Suc n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3815
      (let
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3816
        rr = map (( * ) lc) r;
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3817
        a = hd r;
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3818
        qqq = cCons a (map (( * ) lc) q);
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3819
        rrr = tl (if a = 0 then rr else minus_poly_rev_list rr (map (( * ) a) d))
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3820
       in pseudo_divmod_main_list lc qqq rrr d n)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3821
  | "pseudo_divmod_main_list lc q r d 0 = (q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3822
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3823
fun pseudo_mod_main_list :: "'a::comm_ring_1 \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3824
  where
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3825
    "pseudo_mod_main_list lc r d (Suc n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3826
      (let
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3827
        rr = map (( * ) lc) r;
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3828
        a = hd r;
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3829
        rrr = tl (if a = 0 then rr else minus_poly_rev_list rr (map (( * ) a) d))
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3830
       in pseudo_mod_main_list lc rrr d n)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3831
  | "pseudo_mod_main_list lc r d 0 = r"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3832
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3833
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3834
fun divmod_poly_one_main_list ::
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3835
    "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list \<times> 'a list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3836
  where
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3837
    "divmod_poly_one_main_list q r d (Suc n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3838
      (let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3839
        a = hd r;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3840
        qqq = cCons a q;
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3841
        rr = tl (if a = 0 then r else minus_poly_rev_list r (map (( * ) a) d))
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3842
       in divmod_poly_one_main_list qqq rr d n)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3843
  | "divmod_poly_one_main_list q r d 0 = (q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3844
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3845
fun mod_poly_one_main_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3846
  where
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3847
    "mod_poly_one_main_list r d (Suc n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3848
      (let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3849
        a = hd r;
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3850
        rr = tl (if a = 0 then r else minus_poly_rev_list r (map (( * ) a) d))
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3851
       in mod_poly_one_main_list rr d n)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3852
  | "mod_poly_one_main_list r d 0 = r"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3853
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3854
definition pseudo_divmod_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list \<times> 'a list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3855
  where "pseudo_divmod_list p q =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3856
    (if q = [] then ([], p)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3857
     else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3858
      (let rq = rev q;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3859
        (qu,re) = pseudo_divmod_main_list (hd rq) [] (rev p) rq (1 + length p - length q)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3860
       in (qu, rev re)))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3861
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3862
definition pseudo_mod_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3863
  where "pseudo_mod_list p q =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3864
    (if q = [] then p
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3865
     else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3866
      (let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3867
        rq = rev q;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3868
        re = pseudo_mod_main_list (hd rq) (rev p) rq (1 + length p - length q)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3869
       in rev re))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3870
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3871
lemma minus_zero_does_nothing: "minus_poly_rev_list x (map (( * ) 0) y) = x"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3872
  for x :: "'a::ring list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3873
  by (induct x y rule: minus_poly_rev_list.induct) auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3874
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3875
lemma length_minus_poly_rev_list [simp]: "length (minus_poly_rev_list xs ys) = length xs"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3876
  by (induct xs ys rule: minus_poly_rev_list.induct) auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3877
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3878
lemma if_0_minus_poly_rev_list:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3879
  "(if a = 0 then x else minus_poly_rev_list x (map (( * ) a) y)) =
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3880
    minus_poly_rev_list x (map (( * ) a) y)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3881
  for a :: "'a::ring"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3882
  by(cases "a = 0") (simp_all add: minus_zero_does_nothing)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3883
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3884
lemma Poly_append: "Poly (a @ b) = Poly a + monom 1 (length a) * Poly b"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3885
  for a :: "'a::comm_semiring_1 list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3886
  by (induct a) (auto simp: monom_0 monom_Suc)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3887
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3888
lemma minus_poly_rev_list: "length p \<ge> length q \<Longrightarrow>
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3889
  Poly (rev (minus_poly_rev_list (rev p) (rev q))) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3890
    Poly p - monom 1 (length p - length q) * Poly q"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3891
  for p q :: "'a :: comm_ring_1 list"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3892
proof (induct "rev p" "rev q" arbitrary: p q rule: minus_poly_rev_list.induct)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3893
  case (1 x xs y ys)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3894
  then have "length (rev q) \<le> length (rev p)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3895
    by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3896
  from this[folded 1(2,3)] have ys_xs: "length ys \<le> length xs"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3897
    by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3898
  then have *: "Poly (rev (minus_poly_rev_list xs ys)) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3899
      Poly (rev xs) - monom 1 (length xs - length ys) * Poly (rev ys)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3900
    by (subst "1.hyps"(1)[of "rev xs" "rev ys", unfolded rev_rev_ident length_rev]) auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3901
  have "Poly p - monom 1 (length p - length q) * Poly q =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3902
    Poly (rev (rev p)) - monom 1 (length (rev (rev p)) - length (rev (rev q))) * Poly (rev (rev q))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3903
    by simp
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3904
  also have "\<dots> =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3905
      Poly (rev (x # xs)) - monom 1 (length (x # xs) - length (y # ys)) * Poly (rev (y # ys))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3906
    unfolding 1(2,3) by simp
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3907
  also from ys_xs have "\<dots> =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3908
    Poly (rev xs) + monom x (length xs) -
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3909
      (monom 1 (length xs - length ys) * Poly (rev ys) + monom y (length xs))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3910
    by (simp add: Poly_append distrib_left mult_monom smult_monom)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3911
  also have "\<dots> = Poly (rev (minus_poly_rev_list xs ys)) + monom (x - y) (length xs)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3912
    unfolding * diff_monom[symmetric] by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3913
  finally show ?case
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3914
    by (simp add: 1(2,3)[symmetric] smult_monom Poly_append)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3915
qed auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3916
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3917
lemma smult_monom_mult: "smult a (monom b n * f) = monom (a * b) n * f"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3918
  using smult_monom [of a _ n] by (metis mult_smult_left)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3919
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3920
lemma head_minus_poly_rev_list:
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3921
  "length d \<le> length r \<Longrightarrow> d \<noteq> [] \<Longrightarrow>
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3922
    hd (minus_poly_rev_list (map (( * ) (last d)) r) (map (( * ) (hd r)) (rev d))) = 0"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3923
  for d r :: "'a::comm_ring list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3924
proof (induct r)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3925
  case Nil
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3926
  then show ?case by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3927
next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3928
  case (Cons a rs)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3929
  then show ?case by (cases "rev d") (simp_all add: ac_simps)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3930
qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3931
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3932
lemma Poly_map: "Poly (map (( * ) a) p) = smult a (Poly p)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3933
proof (induct p)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3934
  case Nil
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3935
  then show ?case by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3936
next
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3937
  case (Cons x xs)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3938
  then show ?case by (cases "Poly xs = 0") auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3939
qed
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3940
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3941
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
  3942
  by (simp_all add: hd_conv_nth rev_nth nth_default_nth nth_append)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3943
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3944
lemma pseudo_divmod_main_list_invar:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3945
  assumes leading_nonzero: "last d \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3946
    and lc: "last d = lc"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3947
    and "d \<noteq> []"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3948
    and "pseudo_divmod_main_list lc q (rev r) (rev d) n = (q', rev r')"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3949
    and "n = 1 + length r - length d"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3950
  shows "pseudo_divmod_main lc (monom 1 n * Poly q) (Poly r) (Poly d) (length r - 1) n =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3951
    (Poly q', Poly r')"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3952
  using assms(4-)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3953
proof (induct n arbitrary: r q)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3954
  case (Suc n)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3955
  from Suc.prems have *: "\<not> Suc (length r) \<le> length d"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3956
    by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3957
  with \<open>d \<noteq> []\<close> have "r \<noteq> []"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3958
    using Suc_leI length_greater_0_conv list.size(3) by fastforce
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3959
  let ?a = "(hd (rev r))"
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3960
  let ?rr = "map (( * ) lc) (rev r)"
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3961
  let ?rrr = "rev (tl (minus_poly_rev_list ?rr (map (( * ) ?a) (rev d))))"
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3962
  let ?qq = "cCons ?a (map (( * ) lc) q)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3963
  from * Suc(3) have n: "n = (1 + length r - length d - 1)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3964
    by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3965
  from * have rr_val:"(length ?rrr) = (length r - 1)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3966
    by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3967
  with \<open>r \<noteq> []\<close> * have rr_smaller: "(1 + length r - length d - 1) = (1 + length ?rrr - length d)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3968
    by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3969
  from * have id: "Suc (length r) - length d = Suc (length r - length d)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3970
    by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3971
  from Suc.prems *
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3972
  have "pseudo_divmod_main_list lc ?qq (rev ?rrr) (rev d) (1 + length r - length d - 1) = (q', rev r')"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3973
    by (simp add: Let_def if_0_minus_poly_rev_list id)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3974
  with n have v: "pseudo_divmod_main_list lc ?qq (rev ?rrr) (rev d) n = (q', rev r')"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3975
    by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3976
  from * have sucrr:"Suc (length r) - length d = Suc (length r - length d)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3977
    using Suc_diff_le not_less_eq_eq by blast
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3978
  from Suc(3) \<open>r \<noteq> []\<close> have n_ok : "n = 1 + (length ?rrr) - length d"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3979
    by simp
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3980
  have cong: "\<And>x1 x2 x3 x4 y1 y2 y3 y4. x1 = y1 \<Longrightarrow> x2 = y2 \<Longrightarrow> x3 = y3 \<Longrightarrow> x4 = y4 \<Longrightarrow>
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3981
      pseudo_divmod_main lc x1 x2 x3 x4 n = pseudo_divmod_main lc y1 y2 y3 y4 n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3982
    by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3983
  have hd_rev: "coeff (Poly r) (length r - Suc 0) = hd (rev r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3984
    using last_coeff_is_hd[OF \<open>r \<noteq> []\<close>] by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3985
  show ?case
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3986
    unfolding Suc.hyps(1)[OF v n_ok, symmetric] pseudo_divmod_main.simps Let_def
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3987
  proof (rule cong[OF _ _ refl], goal_cases)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3988
    case 1
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3989
    show ?case
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3990
      by (simp add: monom_Suc hd_rev[symmetric] smult_monom Poly_map)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3991
  next
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3992
    case 2
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  3993
    show ?case
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3994
    proof (subst Poly_on_rev_starting_with_0, goal_cases)
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  3995
      show "hd (minus_poly_rev_list (map (( * ) lc) (rev r)) (map (( * ) (hd (rev r))) (rev d))) = 0"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3996
        by (fold lc, subst head_minus_poly_rev_list, insert * \<open>d \<noteq> []\<close>, auto)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3997
      from * have "length d \<le> length r"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  3998
        by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  3999
      then show "smult lc (Poly r) - monom (coeff (Poly r) (length r - 1)) n * Poly d =
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4000
          Poly (rev (minus_poly_rev_list (map (( * ) lc) (rev r)) (map (( * ) (hd (rev r))) (rev d))))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4001
        by (fold rev_map) (auto simp add: n smult_monom_mult Poly_map hd_rev [symmetric]
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4002
            minus_poly_rev_list)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4003
    qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4004
  qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4005
qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4006
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4007
lemma pseudo_divmod_impl [code]:
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4008
  "pseudo_divmod f g = map_prod poly_of_list poly_of_list (pseudo_divmod_list (coeffs f) (coeffs g))"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4009
    for f g :: "'a::comm_ring_1 poly"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4010
proof (cases "g = 0")
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4011
  case False
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4012
  then have "last (coeffs g) \<noteq> 0"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4013
    and "last (coeffs g) = lead_coeff g"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4014
    and "coeffs g \<noteq> []"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4015
    by (simp_all add: last_coeffs_eq_coeff_degree)
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4016
  moreover obtain q r where qr: "pseudo_divmod_main_list
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4017
    (last (coeffs g)) (rev [])
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4018
    (rev (coeffs f)) (rev (coeffs g))
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4019
    (1 + length (coeffs f) -
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4020
    length (coeffs g)) = (q, rev (rev r))"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4021
    by force
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4022
  ultimately have "(Poly q, Poly (rev r)) = pseudo_divmod_main (lead_coeff g) 0 f g
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4023
    (length (coeffs f) - Suc 0) (Suc (length (coeffs f)) - length (coeffs g))"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4024
    by (subst pseudo_divmod_main_list_invar [symmetric]) auto
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4025
  moreover have "pseudo_divmod_main_list
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4026
    (hd (rev (coeffs g))) []
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4027
    (rev (coeffs f)) (rev (coeffs g))
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4028
    (1 + length (coeffs f) -
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4029
    length (coeffs g)) = (q, r)"
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4030
    using qr hd_rev [OF \<open>coeffs g \<noteq> []\<close>] by simp
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4031
  ultimately show ?thesis
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4032
    by (auto simp: degree_eq_length_coeffs pseudo_divmod_def pseudo_divmod_list_def Let_def)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4033
next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4034
  case True
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4035
  then show ?thesis
65390
83586780598b more concise criterion
haftmann
parents: 65366
diff changeset
  4036
    by (auto simp add: pseudo_divmod_def pseudo_divmod_list_def)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4037
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4038
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4039
lemma pseudo_mod_main_list:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4040
  "snd (pseudo_divmod_main_list l q xs ys n) = pseudo_mod_main_list l xs ys n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4041
  by (induct n arbitrary: l q xs ys) (auto simp: Let_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4042
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4043
lemma pseudo_mod_impl[code]: "pseudo_mod f g = poly_of_list (pseudo_mod_list (coeffs f) (coeffs g))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4044
proof -
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4045
  have snd_case: "\<And>f g p. snd ((\<lambda>(x,y). (f x, g y)) p) = g (snd p)"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4046
    by auto
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4047
  show ?thesis
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4048
    unfolding pseudo_mod_def pseudo_divmod_impl pseudo_divmod_list_def
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4049
      pseudo_mod_list_def Let_def
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4050
    by (simp add: snd_case pseudo_mod_main_list)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4051
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4052
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4053
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4054
subsubsection \<open>Improved Code-Equations for Polynomial (Pseudo) Division\<close>
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4055
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4056
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
  4057
  by (metis eucl_rel_poly eucl_rel_poly_unique)
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4058
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4059
lemma pdivmod_via_pseudo_divmod:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4060
  "(f div g, f mod g) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4061
    (if g = 0 then (0, f)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4062
     else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4063
      let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4064
        ilc = inverse (coeff g (degree g));
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4065
        h = smult ilc g;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4066
        (q,r) = pseudo_divmod f h
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4067
      in (smult ilc q, r))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4068
  (is "?l = ?r")
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4069
proof (cases "g = 0")
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4070
  case True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4071
  then show ?thesis by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4072
next
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4073
  case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4074
  define lc where "lc = inverse (coeff g (degree g))"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4075
  define h where "h = smult lc g"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4076
  from False have h1: "coeff h (degree h) = 1" and lc: "lc \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4077
    by (auto simp: h_def lc_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4078
  then have h0: "h \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4079
    by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4080
  obtain q r where p: "pseudo_divmod f h = (q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4081
    by force
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4082
  from False have id: "?r = (smult lc q, r)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4083
    by (auto simp: Let_def h_def[symmetric] lc_def[symmetric] p)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4084
  from pseudo_divmod[OF h0 p, unfolded h1]
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4085
  have f: "f = h * q + r" and r: "r = 0 \<or> degree r < degree h"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4086
    by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4087
  from f r h0 have "eucl_rel_poly f h (q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4088
    by (auto simp: eucl_rel_poly_iff)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4089
  then have "(f div h, f mod h) = (q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4090
    by (simp add: pdivmod_pdivmodrel)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4091
  with lc have "(f div g, f mod g) = (smult lc q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4092
    by (auto simp: h_def div_smult_right[OF lc] mod_smult_right[OF lc])
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4093
  with id show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4094
    by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4095
qed
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4096
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4097
lemma pdivmod_via_pseudo_divmod_list:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4098
  "(f div g, f mod g) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4099
    (let cg = coeffs g in
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4100
      if cg = [] then (0, f)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4101
      else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4102
        let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4103
          cf = coeffs f;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4104
          ilc = inverse (last cg);
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4105
          ch = map (( * ) ilc) cg;
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4106
          (q, r) = pseudo_divmod_main_list 1 [] (rev cf) (rev ch) (1 + length cf - length cg)
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4107
        in (poly_of_list (map (( * ) ilc) q), poly_of_list (rev r)))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4108
proof -
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4109
  note d = pdivmod_via_pseudo_divmod pseudo_divmod_impl pseudo_divmod_list_def
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4110
  show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4111
  proof (cases "g = 0")
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4112
    case True
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4113
    with d show ?thesis by auto
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4114
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4115
    case False
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4116
    define ilc where "ilc = inverse (coeff g (degree g))"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4117
    from False have ilc: "ilc \<noteq> 0"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4118
      by (auto simp: ilc_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4119
    with False have id: "g = 0 \<longleftrightarrow> False" "coeffs g = [] \<longleftrightarrow> False"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4120
      "last (coeffs g) = coeff g (degree g)"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4121
      "coeffs (smult ilc g) = [] \<longleftrightarrow> False"
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4122
      by (auto simp: last_coeffs_eq_coeff_degree)
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4123
    have id2: "hd (rev (coeffs (smult ilc g))) = 1"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4124
      by (subst hd_rev, insert id ilc, auto simp: coeffs_smult, subst last_map, auto simp: id ilc_def)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4125
    have id3: "length (coeffs (smult ilc g)) = length (coeffs g)"
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4126
      "rev (coeffs (smult ilc g)) = rev (map (( * ) ilc) (coeffs g))"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4127
      unfolding coeffs_smult using ilc by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4128
    obtain q r where pair:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4129
      "pseudo_divmod_main_list 1 [] (rev (coeffs f)) (rev (map (( * ) ilc) (coeffs g)))
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4130
        (1 + length (coeffs f) - length (coeffs g)) = (q, r)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4131
      by force
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4132
    show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4133
      unfolding d Let_def id if_False ilc_def[symmetric] map_prod_def[symmetric] id2
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4134
      unfolding id3 pair map_prod_def split
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4135
      by (auto simp: Poly_map)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4136
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4137
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4138
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4139
lemma pseudo_divmod_main_list_1: "pseudo_divmod_main_list 1 = divmod_poly_one_main_list"
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4140
proof (intro ext, goal_cases)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4141
  case (1 q r d n)
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4142
  have *: "map (( * ) 1) xs = xs" for xs :: "'a list"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4143
    by (induct xs) auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4144
  show ?case
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4145
    by (induct n arbitrary: q r d) (auto simp: * Let_def)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4146
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4147
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4148
fun divide_poly_main_list :: "'a::idom_divide \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4149
  where
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4150
    "divide_poly_main_list lc q r d (Suc n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4151
      (let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4152
        cr = hd r
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4153
        in if cr = 0 then divide_poly_main_list lc (cCons cr q) (tl r) d n else let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4154
        a = cr div lc;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4155
        qq = cCons a q;
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4156
        rr = minus_poly_rev_list r (map (( * ) a) d)
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4157
       in if hd rr = 0 then divide_poly_main_list lc qq (tl rr) d n else [])"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4158
  | "divide_poly_main_list lc q r d 0 = q"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4159
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4160
lemma divide_poly_main_list_simp [simp]:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4161
  "divide_poly_main_list lc q r d (Suc n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4162
    (let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4163
      cr = hd r;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4164
      a = cr div lc;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4165
      qq = cCons a q;
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4166
      rr = minus_poly_rev_list r (map (( * ) a) d)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4167
     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
  4168
  by (simp add: Let_def minus_zero_does_nothing)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4169
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4170
declare divide_poly_main_list.simps(1)[simp del]
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4171
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4172
definition divide_poly_list :: "'a::idom_divide poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4173
  where "divide_poly_list f g =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4174
    (let cg = coeffs g in
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4175
      if cg = [] then g
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4176
      else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4177
        let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4178
          cf = coeffs f;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4179
          cgr = rev cg
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4180
        in poly_of_list (divide_poly_main_list (hd cgr) [] (rev cf) cgr (1 + length cf - length cg)))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4181
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4182
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
  4183
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4184
lemma mod_poly_one_main_list: "snd (divmod_poly_one_main_list q r d n) = mod_poly_one_main_list r d n"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4185
  by (induct n arbitrary: q r d) (auto simp: Let_def)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4186
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4187
lemma mod_poly_code [code]:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4188
  "f mod g =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4189
    (let cg = coeffs g in
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4190
      if cg = [] then f
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4191
      else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4192
        let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4193
          cf = coeffs f;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4194
          ilc = inverse (last cg);
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4195
          ch = map (( * ) ilc) cg;
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4196
          r = mod_poly_one_main_list (rev cf) (rev ch) (1 + length cf - length cg)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4197
        in poly_of_list (rev r))"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4198
  (is "_ = ?rhs")
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4199
proof -
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4200
  have "snd (f div g, f mod g) = ?rhs"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4201
    unfolding pdivmod_via_divmod_list Let_def mod_poly_one_main_list [symmetric, of _ _ _ Nil]
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4202
    by (auto split: prod.splits)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4203
  then show ?thesis by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4204
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4205
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4206
definition div_field_poly_impl :: "'a :: field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4207
  where "div_field_poly_impl f g =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4208
    (let cg = coeffs g in
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4209
      if cg = [] then 0
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4210
      else
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4211
        let
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4212
          cf = coeffs f;
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4213
          ilc = inverse (last cg);
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4214
          ch = map (( * ) ilc) cg;
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4215
          q = fst (divmod_poly_one_main_list [] (rev cf) (rev ch) (1 + length cf - length cg))
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4216
        in poly_of_list ((map (( * ) ilc) q)))"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4217
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4218
text \<open>We do not declare the following lemma as code equation, since then polynomial division
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4219
  on non-fields will no longer be executable. However, a code-unfold is possible, since
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4220
  \<open>div_field_poly_impl\<close> is a bit more efficient than the generic polynomial division.\<close>
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4221
lemma div_field_poly_impl[code_unfold]: "(div) = div_field_poly_impl"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4222
proof (intro ext)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4223
  fix f g :: "'a poly"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4224
  have "fst (f div g, f mod g) = div_field_poly_impl f g"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4225
    unfolding div_field_poly_impl_def pdivmod_via_divmod_list Let_def
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4226
    by (auto split: prod.splits)
64811
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4227
  then show "f div g =  div_field_poly_impl f g"
5477d6b1222f obsolete
haftmann
parents: 64795
diff changeset
  4228
    by simp
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4229
qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4230
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4231
lemma divide_poly_main_list:
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4232
  assumes lc0: "lc \<noteq> 0"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4233
    and lc: "last d = lc"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4234
    and d: "d \<noteq> []"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4235
    and "n = (1 + length r - length d)"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4236
  shows "Poly (divide_poly_main_list lc q (rev r) (rev d) n) =
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4237
    divide_poly_main lc (monom 1 n * Poly q) (Poly r) (Poly d) (length r - 1) n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4238
  using assms(4-)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4239
proof (induct "n" arbitrary: r q)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4240
  case (Suc n)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4241
  from Suc.prems have ifCond: "\<not> Suc (length r) \<le> length d"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4242
    by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4243
  with d have r: "r \<noteq> []"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4244
    using Suc_leI length_greater_0_conv list.size(3) by fastforce
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4245
  then obtain rr lcr where r: "r = rr @ [lcr]"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4246
    by (cases r rule: rev_cases) auto
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4247
  from d lc obtain dd where d: "d = dd @ [lc]"
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4248
    by (cases d rule: rev_cases) auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4249
  from Suc(2) ifCond have n: "n = 1 + length rr - length d"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4250
    by (auto simp: r)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4251
  from ifCond have len: "length dd \<le> length rr"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4252
    by (simp add: r d)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4253
  show ?case
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4254
  proof (cases "lcr div lc * lc = lcr")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4255
    case False
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4256
    with r d show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4257
      unfolding Suc(2)[symmetric]
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4258
      by (auto simp add: Let_def nth_default_append)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4259
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4260
    case True
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4261
    with r d have id:
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4262
      "?thesis \<longleftrightarrow>
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4263
        Poly (divide_poly_main_list lc (cCons (lcr div lc) q)
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67369
diff changeset
  4264
          (rev (rev (minus_poly_rev_list (rev rr) (rev (map (( * ) (lcr div lc)) dd))))) (rev d) n) =
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4265
          divide_poly_main lc
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4266
            (monom 1 (Suc n) * Poly q + monom (lcr div lc) n)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4267
            (Poly r - monom (lcr div lc) n * Poly d)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4268
            (Poly d) (length rr - 1) n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4269
      by (cases r rule: rev_cases; cases "d" rule: rev_cases)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4270
        (auto simp add: Let_def rev_map nth_default_append)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4271
    have cong: "\<And>x1 x2 x3 x4 y1 y2 y3 y4. x1 = y1 \<Longrightarrow> x2 = y2 \<Longrightarrow> x3 = y3 \<Longrightarrow> x4 = y4 \<Longrightarrow>
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4272
        divide_poly_main lc x1 x2 x3 x4 n = divide_poly_main lc y1 y2 y3 y4 n"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4273
      by simp
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4274
    show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4275
      unfolding id
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4276
    proof (subst Suc(1), simp add: n,
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4277
        subst minus_poly_rev_list, force simp: len, rule cong[OF _ _ refl], goal_cases)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4278
      case 2
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4279
      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
  4280
        by (simp add: mult_monom len True)
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4281
      then show ?case unfolding r d Poly_append n ring_distribs
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4282
        by (auto simp: Poly_map smult_monom smult_monom_mult)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4283
    qed (auto simp: len monom_Suc smult_monom)
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4284
  qed
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4285
qed simp
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4286
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4287
lemma divide_poly_list[code]: "f div g = divide_poly_list f g"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4288
proof -
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4289
  note d = divide_poly_def divide_poly_list_def
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4290
  show ?thesis
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4291
  proof (cases "g = 0")
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4292
    case True
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4293
    show ?thesis by (auto simp: d True)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4294
  next
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4295
    case False
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4296
    then obtain cg lcg where cg: "coeffs g = cg @ [lcg]"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4297
      by (cases "coeffs g" rule: rev_cases) auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4298
    with False have id: "(g = 0) = False" "(cg @ [lcg] = []) = False"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4299
      by auto
65346
673a7b3379ec misc tuning and modernization;
wenzelm
parents: 64861
diff changeset
  4300
    from cg False have lcg: "coeff g (degree g) = lcg"
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4301
      using last_coeffs_eq_coeff_degree last_snoc by force
65347
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4302
    with False have "lcg \<noteq> 0" by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4303
    from cg Poly_coeffs [of g] have ltp: "Poly (cg @ [lcg]) = g"
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4304
      by auto
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4305
    show ?thesis
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4306
      unfolding d cg Let_def id if_False poly_of_list_def
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4307
      by (subst divide_poly_main_list, insert False cg \<open>lcg \<noteq> 0\<close>)
d27f9b4e027d misc tuning and modernization;
wenzelm
parents: 65346
diff changeset
  4308
        (auto simp: lcg ltp, simp add: degree_eq_length_coeffs)
64795
8e7db8df16a0 tuned structure
haftmann
parents: 64794
diff changeset
  4309
  qed
63317
ca187a9f66da Various additions to polynomials, FPSs, Gamma function
eberlm
parents: 63145
diff changeset
  4310
qed
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  4311
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4312
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4313
subsection \<open>Primality and irreducibility in polynomial rings\<close>
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4314
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4315
lemma prod_mset_const_poly: "(\<Prod>x\<in>#A. [:f x:]) = [:prod_mset (image_mset f A):]"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4316
  by (induct A) (simp_all add: ac_simps)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4317
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4318
lemma irreducible_const_poly_iff:
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4319
  fixes c :: "'a :: {comm_semiring_1,semiring_no_zero_divisors}"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4320
  shows "irreducible [:c:] \<longleftrightarrow> irreducible c"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4321
proof
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4322
  assume A: "irreducible c"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4323
  show "irreducible [:c:]"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4324
  proof (rule irreducibleI)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4325
    fix a b assume ab: "[:c:] = a * b"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4326
    hence "degree [:c:] = degree (a * b)" by (simp only: )
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4327
    also from A ab have "a \<noteq> 0" "b \<noteq> 0" by auto
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4328
    hence "degree (a * b) = degree a + degree b" by (simp add: degree_mult_eq)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4329
    finally have "degree a = 0" "degree b = 0" by auto
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4330
    then obtain a' b' where ab': "a = [:a':]" "b = [:b':]" by (auto elim!: degree_eq_zeroE)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4331
    from ab have "coeff [:c:] 0 = coeff (a * b) 0" by (simp only: )
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4332
    hence "c = a' * b'" by (simp add: ab' mult_ac)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4333
    from A and this have "a' dvd 1 \<or> b' dvd 1" by (rule irreducibleD)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4334
    with ab' show "a dvd 1 \<or> b dvd 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4335
      by (auto simp add: is_unit_const_poly_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4336
  qed (insert A, auto simp: irreducible_def is_unit_poly_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4337
next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4338
  assume A: "irreducible [:c:]"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4339
  then have "c \<noteq> 0" and "\<not> c dvd 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4340
    by (auto simp add: irreducible_def is_unit_const_poly_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4341
  then show "irreducible c"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4342
  proof (rule irreducibleI)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4343
    fix a b assume ab: "c = a * b"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4344
    hence "[:c:] = [:a:] * [:b:]" by (simp add: mult_ac)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4345
    from A and this have "[:a:] dvd 1 \<or> [:b:] dvd 1" by (rule irreducibleD)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4346
    then show "a dvd 1 \<or> b dvd 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4347
      by (auto simp add: is_unit_const_poly_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4348
  qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4349
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4350
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4351
lemma lift_prime_elem_poly:
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4352
  assumes "prime_elem (c :: 'a :: semidom)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4353
  shows   "prime_elem [:c:]"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4354
proof (rule prime_elemI)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4355
  fix a b assume *: "[:c:] dvd a * b"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4356
  from * have dvd: "c dvd coeff (a * b) n" for n
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4357
    by (subst (asm) const_poly_dvd_iff) blast
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4358
  {
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4359
    define m where "m = (GREATEST m. \<not>c dvd coeff b m)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4360
    assume "\<not>[:c:] dvd b"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4361
    hence A: "\<exists>i. \<not>c dvd coeff b i" by (subst (asm) const_poly_dvd_iff) blast
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4362
    have B: "\<forall>i. \<not>c dvd coeff b i \<longrightarrow> i \<le> degree b"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4363
      by (auto intro: le_degree)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4364
    have coeff_m: "\<not>c dvd coeff b m" unfolding m_def by (rule GreatestI_ex_nat[OF A B])
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4365
    have "i \<le> m" if "\<not>c dvd coeff b i" for i
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4366
      unfolding m_def by (rule Greatest_le_nat[OF that B])
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4367
    hence dvd_b: "c dvd coeff b i" if "i > m" for i using that by force
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4368
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4369
    have "c dvd coeff a i" for i
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4370
    proof (induction i rule: nat_descend_induct[of "degree a"])
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4371
      case (base i)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4372
      thus ?case by (simp add: coeff_eq_0)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4373
    next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4374
      case (descend i)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4375
      let ?A = "{..i+m} - {i}"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4376
      have "c dvd coeff (a * b) (i + m)" by (rule dvd)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4377
      also have "coeff (a * b) (i + m) = (\<Sum>k\<le>i + m. coeff a k * coeff b (i + m - k))"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4378
        by (simp add: coeff_mult)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4379
      also have "{..i+m} = insert i ?A" by auto
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4380
      also have "(\<Sum>k\<in>\<dots>. coeff a k * coeff b (i + m - k)) =
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4381
                   coeff a i * coeff b m + (\<Sum>k\<in>?A. coeff a k * coeff b (i + m - k))"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4382
        (is "_ = _ + ?S")
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4383
        by (subst sum.insert) simp_all
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4384
      finally have eq: "c dvd coeff a i * coeff b m + ?S" .
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4385
      moreover have "c dvd ?S"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4386
      proof (rule dvd_sum)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4387
        fix k assume k: "k \<in> {..i+m} - {i}"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4388
        show "c dvd coeff a k * coeff b (i + m - k)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4389
        proof (cases "k < i")
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4390
          case False
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4391
          with k have "c dvd coeff a k" by (intro descend.IH) simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4392
          thus ?thesis by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4393
        next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4394
          case True
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4395
          hence "c dvd coeff b (i + m - k)" by (intro dvd_b) simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4396
          thus ?thesis by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4397
        qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4398
      qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4399
      ultimately have "c dvd coeff a i * coeff b m"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4400
        by (simp add: dvd_add_left_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4401
      with assms coeff_m show "c dvd coeff a i"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4402
        by (simp add: prime_elem_dvd_mult_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4403
    qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4404
    hence "[:c:] dvd a" by (subst const_poly_dvd_iff) blast
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4405
  }
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4406
  then show "[:c:] dvd a \<or> [:c:] dvd b" by blast
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4407
next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4408
  from assms show "[:c:] \<noteq> 0" and "\<not> [:c:] dvd 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4409
    by (simp_all add: prime_elem_def is_unit_const_poly_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4410
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4411
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4412
lemma prime_elem_const_poly_iff:
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4413
  fixes c :: "'a :: semidom"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4414
  shows   "prime_elem [:c:] \<longleftrightarrow> prime_elem c"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4415
proof
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4416
  assume A: "prime_elem [:c:]"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4417
  show "prime_elem c"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4418
  proof (rule prime_elemI)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4419
    fix a b assume "c dvd a * b"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4420
    hence "[:c:] dvd [:a:] * [:b:]" by (simp add: mult_ac)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4421
    from A and this have "[:c:] dvd [:a:] \<or> [:c:] dvd [:b:]" by (rule prime_elem_dvd_multD)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4422
    thus "c dvd a \<or> c dvd b" by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4423
  qed (insert A, auto simp: prime_elem_def is_unit_poly_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4424
qed (auto intro: lift_prime_elem_poly)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4425
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4426
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4427
subsection \<open>Content and primitive part of a polynomial\<close>
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4428
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4429
definition content :: "'a::semiring_gcd poly \<Rightarrow> 'a"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4430
  where "content p = gcd_list (coeffs p)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4431
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4432
lemma content_eq_fold_coeffs [code]: "content p = fold_coeffs gcd p 0"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4433
  by (simp add: content_def Gcd_fin.set_eq_fold fold_coeffs_def foldr_fold fun_eq_iff ac_simps)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4434
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4435
lemma content_0 [simp]: "content 0 = 0"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4436
  by (simp add: content_def)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4437
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4438
lemma content_1 [simp]: "content 1 = 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4439
  by (simp add: content_def)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4440
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4441
lemma content_const [simp]: "content [:c:] = normalize c"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4442
  by (simp add: content_def cCons_def)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4443
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4444
lemma const_poly_dvd_iff_dvd_content: "[:c:] dvd p \<longleftrightarrow> c dvd content p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4445
  for c :: "'a::semiring_gcd"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4446
proof (cases "p = 0")
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4447
  case True
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4448
  then show ?thesis by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4449
next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4450
  case False
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4451
  have "[:c:] dvd p \<longleftrightarrow> (\<forall>n. c dvd coeff p n)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4452
    by (rule const_poly_dvd_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4453
  also have "\<dots> \<longleftrightarrow> (\<forall>a\<in>set (coeffs p). c dvd a)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4454
  proof safe
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4455
    fix n :: nat
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4456
    assume "\<forall>a\<in>set (coeffs p). c dvd a"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4457
    then show "c dvd coeff p n"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4458
      by (cases "n \<le> degree p") (auto simp: coeff_eq_0 coeffs_def split: if_splits)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4459
  qed (auto simp: coeffs_def simp del: upt_Suc split: if_splits)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4460
  also have "\<dots> \<longleftrightarrow> c dvd content p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4461
    by (simp add: content_def dvd_Gcd_fin_iff dvd_mult_unit_iff)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4462
  finally show ?thesis .
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4463
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4464
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4465
lemma content_dvd [simp]: "[:content p:] dvd p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4466
  by (subst const_poly_dvd_iff_dvd_content) simp_all
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4467
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4468
lemma content_dvd_coeff [simp]: "content p dvd coeff p n"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4469
proof (cases "p = 0")
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4470
  case True
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4471
  then show ?thesis
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4472
    by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4473
next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4474
  case False
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4475
  then show ?thesis
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4476
    by (cases "n \<le> degree p")
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4477
      (auto simp add: content_def not_le coeff_eq_0 coeff_in_coeffs intro: Gcd_fin_dvd)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4478
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4479
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4480
lemma content_dvd_coeffs: "c \<in> set (coeffs p) \<Longrightarrow> content p dvd c"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4481
  by (simp add: content_def Gcd_fin_dvd)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4482
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4483
lemma normalize_content [simp]: "normalize (content p) = content p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4484
  by (simp add: content_def)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4485
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4486
lemma is_unit_content_iff [simp]: "is_unit (content p) \<longleftrightarrow> content p = 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4487
proof
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4488
  assume "is_unit (content p)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4489
  then have "normalize (content p) = 1" by (simp add: is_unit_normalize del: normalize_content)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4490
  then show "content p = 1" by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4491
qed auto
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4492
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4493
lemma content_smult [simp]: "content (smult c p) = normalize c * content p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4494
  by (simp add: content_def coeffs_smult Gcd_fin_mult)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4495
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4496
lemma content_eq_zero_iff [simp]: "content p = 0 \<longleftrightarrow> p = 0"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4497
  by (auto simp: content_def simp: poly_eq_iff coeffs_def)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4498
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4499
definition primitive_part :: "'a :: semiring_gcd poly \<Rightarrow> 'a poly"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4500
  where "primitive_part p = map_poly (\<lambda>x. x div content p) p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4501
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4502
lemma primitive_part_0 [simp]: "primitive_part 0 = 0"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4503
  by (simp add: primitive_part_def)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4504
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4505
lemma content_times_primitive_part [simp]: "smult (content p) (primitive_part p) = p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4506
  for p :: "'a :: semiring_gcd poly"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4507
proof (cases "p = 0")
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4508
  case True
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4509
  then show ?thesis by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4510
next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4511
  case False
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4512
  then show ?thesis
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4513
  unfolding primitive_part_def
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4514
  by (auto simp: smult_conv_map_poly map_poly_map_poly o_def content_dvd_coeffs
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4515
      intro: map_poly_idI)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4516
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4517
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4518
lemma primitive_part_eq_0_iff [simp]: "primitive_part p = 0 \<longleftrightarrow> p = 0"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4519
proof (cases "p = 0")
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4520
  case True
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4521
  then show ?thesis by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4522
next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4523
  case False
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4524
  then have "primitive_part p = map_poly (\<lambda>x. x div content p) p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4525
    by (simp add:  primitive_part_def)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4526
  also from False have "\<dots> = 0 \<longleftrightarrow> p = 0"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4527
    by (intro map_poly_eq_0_iff) (auto simp: dvd_div_eq_0_iff content_dvd_coeffs)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4528
  finally show ?thesis
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4529
    using False by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4530
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4531
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4532
lemma content_primitive_part [simp]:
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4533
  assumes "p \<noteq> 0"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4534
  shows "content (primitive_part p) = 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4535
proof -
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4536
  have "p = smult (content p) (primitive_part p)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4537
    by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4538
  also have "content \<dots> = content (primitive_part p) * content p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4539
    by (simp del: content_times_primitive_part add: ac_simps)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4540
  finally have "1 * content p = content (primitive_part p) * content p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4541
    by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4542
  then have "1 * content p div content p = content (primitive_part p) * content p div content p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4543
    by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4544
  with assms show ?thesis
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4545
    by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4546
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4547
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4548
lemma content_decompose:
68790
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4549
  obtains p' :: "'a::semiring_gcd poly"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4550
  where "p = smult (content p) p'" "content p' = 1"
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4551
proof (cases "p = 0")
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4552
  case True
68790
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4553
  then have "p = smult (content p) 1" "content 1 = 1"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4554
    by simp_all
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4555
  then show ?thesis ..
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4556
next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4557
  case False
68790
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4558
  then have "p = smult (content p) (primitive_part p)" "content (primitive_part p) = 1"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4559
    by simp_all
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4560
  then show ?thesis ..
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4561
qed
68790
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4562
  
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4563
lemma content_dvd_contentI [intro]: "p dvd q \<Longrightarrow> content p dvd content q"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4564
  using const_poly_dvd_iff_dvd_content content_dvd dvd_trans by blast
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4565
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4566
lemma primitive_part_const_poly [simp]: "primitive_part [:x:] = [:unit_factor x:]"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4567
  by (simp add: primitive_part_def map_poly_pCons)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4568
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4569
lemma primitive_part_prim: "content p = 1 \<Longrightarrow> primitive_part p = p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4570
  by (auto simp: primitive_part_def)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4571
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4572
lemma degree_primitive_part [simp]: "degree (primitive_part p) = degree p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4573
proof (cases "p = 0")
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4574
  case True
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4575
  then show ?thesis by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4576
next
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4577
  case False
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4578
  have "p = smult (content p) (primitive_part p)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4579
    by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4580
  also from False have "degree \<dots> = degree (primitive_part p)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4581
    by (subst degree_smult_eq) simp_all
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4582
  finally show ?thesis ..
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4583
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4584
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4585
lemma smult_content_normalize_primitive_part [simp]:
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4586
  "smult (content p) (normalize (primitive_part p)) = normalize p"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4587
proof -
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4588
  have "smult (content p) (normalize (primitive_part p)) =
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4589
      normalize ([:content p:] * primitive_part p)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4590
    by (subst normalize_mult) (simp_all add: normalize_const_poly)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4591
  also have "[:content p:] * primitive_part p = p" by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4592
  finally show ?thesis .
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4593
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4594
68790
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4595
context
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4596
begin
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4597
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4598
private
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4599
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4600
lemma content_1_mult:
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4601
  fixes f g :: "'a :: {semiring_gcd, factorial_semiring} poly"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4602
  assumes "content f = 1" "content g = 1"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4603
  shows   "content (f * g) = 1"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4604
proof (cases "f * g = 0")
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4605
  case False
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4606
  from assms have "f \<noteq> 0" "g \<noteq> 0" by auto
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4607
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4608
  hence "f * g \<noteq> 0" by auto
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4609
  {
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4610
    assume "\<not>is_unit (content (f * g))"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4611
    with False have "\<exists>p. p dvd content (f * g) \<and> prime p"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4612
      by (intro prime_divisor_exists) simp_all
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4613
    then obtain p where "p dvd content (f * g)" "prime p" by blast
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4614
    from \<open>p dvd content (f * g)\<close> have "[:p:] dvd f * g"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4615
      by (simp add: const_poly_dvd_iff_dvd_content)
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4616
    moreover from \<open>prime p\<close> have "prime_elem [:p:]" by (simp add: lift_prime_elem_poly)
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4617
    ultimately have "[:p:] dvd f \<or> [:p:] dvd g"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4618
      by (simp add: prime_elem_dvd_mult_iff)
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4619
    with assms have "is_unit p" by (simp add: const_poly_dvd_iff_dvd_content)
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4620
    with \<open>prime p\<close> have False by simp
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4621
  }
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4622
  hence "is_unit (content (f * g))" by blast
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4623
  hence "normalize (content (f * g)) = 1" by (simp add: is_unit_normalize del: normalize_content)
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4624
  thus ?thesis by simp
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4625
qed (insert assms, auto)
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4626
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4627
lemma content_mult:
68790
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4628
  fixes p q :: "'a :: {factorial_semiring, semiring_gcd} poly"
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4629
  shows "content (p * q) = content p * content q"
68790
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4630
proof (cases "p * q = 0")
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4631
  case False
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4632
  then have "p \<noteq> 0" and "q \<noteq> 0"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4633
    by simp_all
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4634
  then have *: "content (primitive_part p * primitive_part q) = 1"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4635
    by (auto intro: content_1_mult)
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4636
  have "p * q = smult (content p) (primitive_part p) * smult (content q) (primitive_part q)"
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4637
    by simp
68790
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4638
  also have "\<dots> = smult (content p * content q) (primitive_part p * primitive_part q)"
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4639
    by (metis mult.commute mult_smult_right smult_smult)
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4640
  with * show ?thesis
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4641
  by (simp add: normalize_mult)
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4642
next
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4643
  case True
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4644
  then show ?thesis
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4645
    by auto
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4646
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4647
68790
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4648
end
851a9d9746c6 prefer constructive primitive_part over implicit content_decompose
haftmann
parents: 68534
diff changeset
  4649
66805
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4650
lemma primitive_part_mult:
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4651
  fixes p q :: "'a :: {factorial_semiring, semiring_Gcd, ring_gcd, idom_divide} poly"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4652
  shows "primitive_part (p * q) = primitive_part p * primitive_part q"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4653
proof -
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4654
  have "primitive_part (p * q) = p * q div [:content (p * q):]"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4655
    by (simp add: primitive_part_def div_const_poly_conv_map_poly)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4656
  also have "\<dots> = (p div [:content p:]) * (q div [:content q:])"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4657
    by (subst div_mult_div_if_dvd) (simp_all add: content_mult mult_ac)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4658
  also have "\<dots> = primitive_part p * primitive_part q"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4659
    by (simp add: primitive_part_def div_const_poly_conv_map_poly)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4660
  finally show ?thesis .
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4661
qed
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4662
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4663
lemma primitive_part_smult:
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4664
  fixes p :: "'a :: {factorial_semiring, semiring_Gcd, ring_gcd, idom_divide} poly"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4665
  shows "primitive_part (smult a p) = smult (unit_factor a) (primitive_part p)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4666
proof -
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4667
  have "smult a p = [:a:] * p" by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4668
  also have "primitive_part \<dots> = smult (unit_factor a) (primitive_part p)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4669
    by (subst primitive_part_mult) simp_all
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4670
  finally show ?thesis .
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4671
qed  
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4672
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4673
lemma primitive_part_dvd_primitive_partI [intro]:
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4674
  fixes p q :: "'a :: {factorial_semiring, semiring_Gcd, ring_gcd, idom_divide} poly"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4675
  shows "p dvd q \<Longrightarrow> primitive_part p dvd primitive_part q"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4676
  by (auto elim!: dvdE simp: primitive_part_mult)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4677
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4678
lemma content_prod_mset: 
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4679
  fixes A :: "'a :: {factorial_semiring, semiring_Gcd} poly multiset"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4680
  shows "content (prod_mset A) = prod_mset (image_mset content A)"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4681
  by (induction A) (simp_all add: content_mult mult_ac)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4682
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4683
lemma content_prod_eq_1_iff: 
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4684
  fixes p q :: "'a :: {factorial_semiring, semiring_Gcd} poly"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4685
  shows "content (p * q) = 1 \<longleftrightarrow> content p = 1 \<and> content q = 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4686
proof safe
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4687
  assume A: "content (p * q) = 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4688
  {
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4689
    fix p q :: "'a poly" assume "content p * content q = 1"
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4690
    hence "1 = content p * content q" by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4691
    hence "content p dvd 1" by (rule dvdI)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4692
    hence "content p = 1" by simp
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4693
  } note B = this
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4694
  from A B[of p q] B [of q p] show "content p = 1" "content q = 1" 
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4695
    by (simp_all add: content_mult mult_ac)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4696
qed (auto simp: content_mult)
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4697
274b4edca859 Polynomial_Factorial does not depend on Field_as_Ring as such
haftmann
parents: 66799
diff changeset
  4698
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  4699
no_notation cCons (infixr "##" 65)
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  4700
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  4701
end