src/HOL/Analysis/Determinants.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (4 weeks ago)
changeset 69981 3dced198b9ec
parent 69720 be6634e99e09
child 70136 f03a01a18c6e
permissions -rw-r--r--
more strict AFP properties;
hoelzl@63627
     1
(*  Title:      HOL/Analysis/Determinants.thy
lp15@68143
     2
    Author:     Amine Chaieb, University of Cambridge; proofs reworked by LCP
himmelma@33175
     3
*)
himmelma@33175
     4
wenzelm@60420
     5
section \<open>Traces, Determinant of square matrices and some properties\<close>
himmelma@33175
     6
himmelma@33175
     7
theory Determinants
huffman@44228
     8
imports
immler@69680
     9
  Cartesian_Space
wenzelm@66453
    10
  "HOL-Library.Permutations"
himmelma@33175
    11
begin
himmelma@33175
    12
immler@69683
    13
subsection  \<open>Trace\<close>
himmelma@33175
    14
ak2110@68833
    15
definition%important  trace :: "'a::semiring_1^'n^'n \<Rightarrow> 'a"
nipkow@64267
    16
  where "trace A = sum (\<lambda>i. ((A$i)$i)) (UNIV::'n set)"
himmelma@33175
    17
ak2110@69720
    18
lemma  trace_0: "trace (mat 0) = 0"
himmelma@33175
    19
  by (simp add: trace_def mat_def)
himmelma@33175
    20
ak2110@69720
    21
lemma  trace_I: "trace (mat 1 :: 'a::semiring_1^'n^'n) = of_nat(CARD('n))"
himmelma@33175
    22
  by (simp add: trace_def mat_def)
himmelma@33175
    23
ak2110@69720
    24
lemma  trace_add: "trace ((A::'a::comm_semiring_1^'n^'n) + B) = trace A + trace B"
nipkow@64267
    25
  by (simp add: trace_def sum.distrib)
himmelma@33175
    26
ak2110@69720
    27
lemma  trace_sub: "trace ((A::'a::comm_ring_1^'n^'n) - B) = trace A - trace B"
nipkow@64267
    28
  by (simp add: trace_def sum_subtractf)
himmelma@33175
    29
ak2110@69720
    30
lemma  trace_mul_sym: "trace ((A::'a::comm_semiring_1^'n^'m) ** B) = trace (B**A)"
himmelma@33175
    31
  apply (simp add: trace_def matrix_matrix_mult_def)
haftmann@66804
    32
  apply (subst sum.swap)
haftmann@57512
    33
  apply (simp add: mult.commute)
wenzelm@53253
    34
  done
himmelma@33175
    35
ak2110@69720
    36
subsubsection%important  \<open>Definition of determinant\<close>
himmelma@33175
    37
ak2110@68833
    38
definition%important  det:: "'a::comm_ring_1^'n^'n \<Rightarrow> 'a" where
wenzelm@53253
    39
  "det A =
nipkow@64272
    40
    sum (\<lambda>p. of_int (sign p) * prod (\<lambda>i. A$i$p i) (UNIV :: 'n set))
wenzelm@53253
    41
      {p. p permutes (UNIV :: 'n set)}"
himmelma@33175
    42
lp15@68134
    43
text \<open>Basic determinant properties\<close>
himmelma@33175
    44
ak2110@69720
    45
lemma  det_transpose [simp]: "det (transpose A) = det (A::'a::comm_ring_1 ^'n^'n)"
ak2110@69720
    46
proof -
himmelma@33175
    47
  let ?di = "\<lambda>A i j. A$i$j"
himmelma@33175
    48
  let ?U = "(UNIV :: 'n set)"
himmelma@33175
    49
  have fU: "finite ?U" by simp
wenzelm@53253
    50
  {
wenzelm@53253
    51
    fix p
wenzelm@53253
    52
    assume p: "p \<in> {p. p permutes ?U}"
wenzelm@53854
    53
    from p have pU: "p permutes ?U"
wenzelm@53854
    54
      by blast
himmelma@33175
    55
    have sth: "sign (inv p) = sign p"
huffman@44260
    56
      by (metis sign_inverse fU p mem_Collect_eq permutation_permutes)
himmelma@33175
    57
    from permutes_inj[OF pU]
wenzelm@53854
    58
    have pi: "inj_on p ?U"
wenzelm@53854
    59
      by (blast intro: subset_inj_on)
himmelma@33175
    60
    from permutes_image[OF pU]
nipkow@64272
    61
    have "prod (\<lambda>i. ?di (transpose A) i (inv p i)) ?U =
nipkow@64272
    62
      prod (\<lambda>i. ?di (transpose A) i (inv p i)) (p ` ?U)"
wenzelm@53854
    63
      by simp
nipkow@64272
    64
    also have "\<dots> = prod ((\<lambda>i. ?di (transpose A) i (inv p i)) \<circ> p) ?U"
nipkow@64272
    65
      unfolding prod.reindex[OF pi] ..
nipkow@64272
    66
    also have "\<dots> = prod (\<lambda>i. ?di A i (p i)) ?U"
wenzelm@53253
    67
    proof -
lp15@68134
    68
      have "((\<lambda>i. ?di (transpose A) i (inv p i)) \<circ> p) i = ?di A i (p i)" if "i \<in> ?U" for i
lp15@68134
    69
        using that permutes_inv_o[OF pU] permutes_in_image[OF pU]
lp15@68134
    70
        unfolding transpose_def by (simp add: fun_eq_iff)
lp15@68134
    71
      then show "prod ((\<lambda>i. ?di (transpose A) i (inv p i)) \<circ> p) ?U = prod (\<lambda>i. ?di A i (p i)) ?U"
nipkow@64272
    72
        by (auto intro: prod.cong)
himmelma@33175
    73
    qed
nipkow@64272
    74
    finally have "of_int (sign (inv p)) * (prod (\<lambda>i. ?di (transpose A) i (inv p i)) ?U) =
nipkow@64272
    75
      of_int (sign p) * (prod (\<lambda>i. ?di A i (p i)) ?U)"
wenzelm@53854
    76
      using sth by simp
wenzelm@53253
    77
  }
wenzelm@53253
    78
  then show ?thesis
wenzelm@53253
    79
    unfolding det_def
lp15@68138
    80
    by (subst sum_permutations_inverse) (blast intro: sum.cong)
himmelma@33175
    81
qed
himmelma@33175
    82
ak2110@69720
    83
lemma  det_lowerdiagonal:
hoelzl@34291
    84
  fixes A :: "'a::comm_ring_1^('n::{finite,wellorder})^('n::{finite,wellorder})"
himmelma@33175
    85
  assumes ld: "\<And>i j. i < j \<Longrightarrow> A$i$j = 0"
nipkow@64272
    86
  shows "det A = prod (\<lambda>i. A$i$i) (UNIV:: 'n set)"
wenzelm@53253
    87
proof -
himmelma@33175
    88
  let ?U = "UNIV:: 'n set"
himmelma@33175
    89
  let ?PU = "{p. p permutes ?U}"
nipkow@64272
    90
  let ?pp = "\<lambda>p. of_int (sign p) * prod (\<lambda>i. A$i$p i) (UNIV :: 'n set)"
wenzelm@53854
    91
  have fU: "finite ?U"
wenzelm@53854
    92
    by simp
wenzelm@53854
    93
  have id0: "{id} \<subseteq> ?PU"
lp15@68138
    94
    by (auto simp: permutes_id)
lp15@68134
    95
  have p0: "\<forall>p \<in> ?PU - {id}. ?pp p = 0"
lp15@68134
    96
  proof
wenzelm@53253
    97
    fix p
lp15@68134
    98
    assume "p \<in> ?PU - {id}"
lp15@68134
    99
    then obtain i where i: "p i > i"
lp15@68134
   100
      by clarify (meson leI permutes_natset_le)
lp15@68134
   101
    from ld[OF i] have "\<exists>i \<in> ?U. A$i$p i = 0"
wenzelm@53253
   102
      by blast
lp15@68134
   103
    with prod_zero[OF fU] show "?pp p = 0"
lp15@68134
   104
      by force
lp15@68134
   105
  qed
lp15@68134
   106
  from sum.mono_neutral_cong_left[OF finite_permutations[OF fU] id0 p0] show ?thesis
himmelma@33175
   107
    unfolding det_def by (simp add: sign_id)
himmelma@33175
   108
qed
himmelma@33175
   109
ak2110@69720
   110
lemma  det_upperdiagonal:
hoelzl@34291
   111
  fixes A :: "'a::comm_ring_1^'n::{finite,wellorder}^'n::{finite,wellorder}"
himmelma@33175
   112
  assumes ld: "\<And>i j. i > j \<Longrightarrow> A$i$j = 0"
nipkow@64272
   113
  shows "det A = prod (\<lambda>i. A$i$i) (UNIV:: 'n set)"
ak2110@69720
   114
proof -
himmelma@33175
   115
  let ?U = "UNIV:: 'n set"
himmelma@33175
   116
  let ?PU = "{p. p permutes ?U}"
nipkow@64272
   117
  let ?pp = "(\<lambda>p. of_int (sign p) * prod (\<lambda>i. A$i$p i) (UNIV :: 'n set))"
wenzelm@53854
   118
  have fU: "finite ?U"
wenzelm@53854
   119
    by simp
wenzelm@53854
   120
  have id0: "{id} \<subseteq> ?PU"
lp15@68138
   121
    by (auto simp: permutes_id)
lp15@68134
   122
  have p0: "\<forall>p \<in> ?PU -{id}. ?pp p = 0"
lp15@68134
   123
  proof
wenzelm@53253
   124
    fix p
wenzelm@53854
   125
    assume p: "p \<in> ?PU - {id}"
lp15@68134
   126
    then obtain i where i: "p i < i"
lp15@68134
   127
      by clarify (meson leI permutes_natset_ge)
lp15@68134
   128
    from ld[OF i] have "\<exists>i \<in> ?U. A$i$p i = 0"
wenzelm@53854
   129
      by blast
lp15@68134
   130
    with prod_zero[OF fU]  show "?pp p = 0"
lp15@68134
   131
      by force
lp15@68134
   132
  qed
lp15@68134
   133
  from sum.mono_neutral_cong_left[OF finite_permutations[OF fU] id0 p0] show ?thesis
himmelma@33175
   134
    unfolding det_def by (simp add: sign_id)
himmelma@33175
   135
qed
himmelma@33175
   136
ak2110@69720
   137
proposition  det_diagonal:
hoelzl@34291
   138
  fixes A :: "'a::comm_ring_1^'n^'n"
himmelma@33175
   139
  assumes ld: "\<And>i j. i \<noteq> j \<Longrightarrow> A$i$j = 0"
nipkow@64272
   140
  shows "det A = prod (\<lambda>i. A$i$i) (UNIV::'n set)"
ak2110@69720
   141
proof -
himmelma@33175
   142
  let ?U = "UNIV:: 'n set"
himmelma@33175
   143
  let ?PU = "{p. p permutes ?U}"
nipkow@64272
   144
  let ?pp = "\<lambda>p. of_int (sign p) * prod (\<lambda>i. A$i$p i) (UNIV :: 'n set)"
himmelma@33175
   145
  have fU: "finite ?U" by simp
himmelma@33175
   146
  from finite_permutations[OF fU] have fPU: "finite ?PU" .
wenzelm@53854
   147
  have id0: "{id} \<subseteq> ?PU"
lp15@68138
   148
    by (auto simp: permutes_id)
lp15@68134
   149
  have p0: "\<forall>p \<in> ?PU - {id}. ?pp p = 0"
lp15@68134
   150
  proof
wenzelm@53253
   151
    fix p
wenzelm@53253
   152
    assume p: "p \<in> ?PU - {id}"
wenzelm@53854
   153
    then obtain i where i: "p i \<noteq> i"
lp15@68134
   154
      by fastforce
lp15@68134
   155
    with ld have "\<exists>i \<in> ?U. A$i$p i = 0"
lp15@68134
   156
      by (metis UNIV_I)
lp15@68134
   157
    with prod_zero [OF fU] show "?pp p = 0"
lp15@68134
   158
      by force
lp15@68134
   159
  qed
nipkow@64267
   160
  from sum.mono_neutral_cong_left[OF fPU id0 p0] show ?thesis
himmelma@33175
   161
    unfolding det_def by (simp add: sign_id)
himmelma@33175
   162
qed
himmelma@33175
   163
ak2110@69720
   164
lemma  det_I [simp]: "det (mat 1 :: 'a::comm_ring_1^'n^'n) = 1"
lp15@67673
   165
  by (simp add: det_diagonal mat_def)
himmelma@33175
   166
ak2110@69720
   167
lemma  det_0 [simp]: "det (mat 0 :: 'a::comm_ring_1^'n^'n) = 0"
lp15@67970
   168
  by (simp add: det_def prod_zero power_0_left)
himmelma@33175
   169
ak2110@69720
   170
lemma  det_permute_rows:
hoelzl@34291
   171
  fixes A :: "'a::comm_ring_1^'n^'n"
himmelma@33175
   172
  assumes p: "p permutes (UNIV :: 'n::finite set)"
wenzelm@53854
   173
  shows "det (\<chi> i. A$p i :: 'a^'n^'n) = of_int (sign p) * det A"
lp15@68134
   174
proof -
himmelma@33175
   175
  let ?U = "UNIV :: 'n set"
himmelma@33175
   176
  let ?PU = "{p. p permutes ?U}"
lp15@68134
   177
  have *: "(\<Sum>q\<in>?PU. of_int (sign (q \<circ> p)) * (\<Prod>i\<in>?U. A $ p i $ (q \<circ> p) i)) =
lp15@68134
   178
           (\<Sum>n\<in>?PU. of_int (sign p) * of_int (sign n) * (\<Prod>i\<in>?U. A $ i $ n i))"
lp15@68134
   179
  proof (rule sum.cong)
lp15@68134
   180
    fix q
lp15@68134
   181
    assume qPU: "q \<in> ?PU"
lp15@68134
   182
    have fU: "finite ?U"
lp15@68134
   183
      by simp
lp15@68134
   184
    from qPU have q: "q permutes ?U"
lp15@68134
   185
      by blast
lp15@68134
   186
    have "prod (\<lambda>i. A$p i$ (q \<circ> p) i) ?U = prod ((\<lambda>i. A$p i$(q \<circ> p) i) \<circ> inv p) ?U"
lp15@68134
   187
      by (simp only: prod.permute[OF permutes_inv[OF p], symmetric])
lp15@68134
   188
    also have "\<dots> = prod (\<lambda>i. A $ (p \<circ> inv p) i $ (q \<circ> (p \<circ> inv p)) i) ?U"
lp15@68134
   189
      by (simp only: o_def)
lp15@68134
   190
    also have "\<dots> = prod (\<lambda>i. A$i$q i) ?U"
lp15@68134
   191
      by (simp only: o_def permutes_inverses[OF p])
lp15@68134
   192
    finally have thp: "prod (\<lambda>i. A$p i$ (q \<circ> p) i) ?U = prod (\<lambda>i. A$i$q i) ?U"
lp15@68134
   193
      by blast
lp15@68134
   194
    from p q have pp: "permutation p" and qp: "permutation q"
lp15@68134
   195
      by (metis fU permutation_permutes)+
lp15@68134
   196
    show "of_int (sign (q \<circ> p)) * prod (\<lambda>i. A$ p i$ (q \<circ> p) i) ?U =
lp15@68134
   197
          of_int (sign p) * of_int (sign q) * prod (\<lambda>i. A$i$q i) ?U"
lp15@68134
   198
      by (simp only: thp sign_compose[OF qp pp] mult.commute of_int_mult)
lp15@68134
   199
  qed auto
lp15@68134
   200
  show ?thesis
lp15@68134
   201
    apply (simp add: det_def sum_distrib_left mult.assoc[symmetric])
lp15@68134
   202
    apply (subst sum_permutations_compose_right[OF p])
lp15@68134
   203
    apply (rule *)
lp15@68134
   204
    done
lp15@68143
   205
qed
himmelma@33175
   206
ak2110@69720
   207
lemma  det_permute_columns:
hoelzl@34291
   208
  fixes A :: "'a::comm_ring_1^'n^'n"
himmelma@33175
   209
  assumes p: "p permutes (UNIV :: 'n set)"
himmelma@33175
   210
  shows "det(\<chi> i j. A$i$ p j :: 'a^'n^'n) = of_int (sign p) * det A"
ak2110@69720
   211
proof -
himmelma@33175
   212
  let ?Ap = "\<chi> i j. A$i$ p j :: 'a^'n^'n"
hoelzl@35150
   213
  let ?At = "transpose A"
hoelzl@35150
   214
  have "of_int (sign p) * det A = det (transpose (\<chi> i. transpose A $ p i))"
hoelzl@35150
   215
    unfolding det_permute_rows[OF p, of ?At] det_transpose ..
himmelma@33175
   216
  moreover
hoelzl@35150
   217
  have "?Ap = transpose (\<chi> i. transpose A $ p i)"
huffman@44228
   218
    by (simp add: transpose_def vec_eq_iff)
wenzelm@53854
   219
  ultimately show ?thesis
wenzelm@53854
   220
    by simp
himmelma@33175
   221
qed
himmelma@33175
   222
ak2110@69720
   223
lemma  det_identical_columns:
immler@68072
   224
  fixes A :: "'a::comm_ring_1^'n^'n"
immler@68072
   225
  assumes jk: "j \<noteq> k"
immler@68072
   226
    and r: "column j A = column k A"
himmelma@33175
   227
  shows "det A = 0"
immler@68072
   228
proof -
immler@68072
   229
  let ?U="UNIV::'n set"
immler@68072
   230
  let ?t_jk="Fun.swap j k id"
immler@68072
   231
  let ?PU="{p. p permutes ?U}"
immler@68072
   232
  let ?S1="{p. p\<in>?PU \<and> evenperm p}"
immler@68072
   233
  let ?S2="{(?t_jk \<circ> p) |p. p \<in>?S1}"
immler@68072
   234
  let ?f="\<lambda>p. of_int (sign p) * (\<Prod>i\<in>UNIV. A $ i $ p i)"
immler@68072
   235
  let ?g="\<lambda>p. ?t_jk \<circ> p"
immler@68072
   236
  have g_S1: "?S2 = ?g` ?S1" by auto
immler@68072
   237
  have inj_g: "inj_on ?g ?S1"
immler@68072
   238
  proof (unfold inj_on_def, auto)
immler@68072
   239
    fix x y assume x: "x permutes ?U" and even_x: "evenperm x"
immler@68072
   240
      and y: "y permutes ?U" and even_y: "evenperm y" and eq: "?t_jk \<circ> x = ?t_jk \<circ> y"
immler@68072
   241
    show "x = y" by (metis (hide_lams, no_types) comp_assoc eq id_comp swap_id_idempotent)
immler@68072
   242
  qed
immler@68072
   243
  have tjk_permutes: "?t_jk permutes ?U" unfolding permutes_def swap_id_eq by (auto,metis)
immler@68072
   244
  have tjk_eq: "\<forall>i l. A $ i $ ?t_jk l  =  A $ i $ l"
immler@68072
   245
    using r jk
immler@68072
   246
    unfolding column_def vec_eq_iff swap_id_eq by fastforce
immler@68072
   247
  have sign_tjk: "sign ?t_jk = -1" using sign_swap_id[of j k] jk by auto
immler@68072
   248
  {fix x
immler@68072
   249
    assume x: "x\<in> ?S1"
immler@68072
   250
    have "sign (?t_jk \<circ> x) = sign (?t_jk) * sign x"
immler@68072
   251
      by (metis (lifting) finite_class.finite_UNIV mem_Collect_eq
immler@68072
   252
          permutation_permutes permutation_swap_id sign_compose x)
lp15@68138
   253
    also have "\<dots> = - sign x" using sign_tjk by simp
lp15@68138
   254
    also have "\<dots> \<noteq> sign x" unfolding sign_def by simp
immler@68072
   255
    finally have "sign (?t_jk \<circ> x) \<noteq> sign x" and "(?t_jk \<circ> x) \<in> ?S2"
lp15@68134
   256
      using x by force+
immler@68072
   257
  }
lp15@68134
   258
  hence disjoint: "?S1 \<inter> ?S2 = {}"
lp15@68134
   259
    by (force simp: sign_def)
immler@68072
   260
  have PU_decomposition: "?PU = ?S1 \<union> ?S2"
immler@68072
   261
  proof (auto)
immler@68072
   262
    fix x
immler@68072
   263
    assume x: "x permutes ?U" and "\<forall>p. p permutes ?U \<longrightarrow> x = Fun.swap j k id \<circ> p \<longrightarrow> \<not> evenperm p"
lp15@68134
   264
    then obtain p where p: "p permutes UNIV" and x_eq: "x = Fun.swap j k id \<circ> p"
immler@68072
   265
      and odd_p: "\<not> evenperm p"
lp15@68134
   266
      by (metis (mono_tags) id_o o_assoc permutes_compose swap_id_idempotent tjk_permutes)
immler@68072
   267
    thus "evenperm x"
lp15@68134
   268
      by (meson evenperm_comp evenperm_swap finite_class.finite_UNIV
immler@68072
   269
          jk permutation_permutes permutation_swap_id)
immler@68072
   270
  next
immler@68072
   271
    fix p assume p: "p permutes ?U"
immler@68072
   272
    show "Fun.swap j k id \<circ> p permutes UNIV" by (metis p permutes_compose tjk_permutes)
immler@68072
   273
  qed
immler@68072
   274
  have "sum ?f ?S2 = sum ((\<lambda>p. of_int (sign p) * (\<Prod>i\<in>UNIV. A $ i $ p i))
immler@68072
   275
  \<circ> (\<circ>) (Fun.swap j k id)) {p \<in> {p. p permutes UNIV}. evenperm p}"
immler@68072
   276
    unfolding g_S1 by (rule sum.reindex[OF inj_g])
lp15@68138
   277
  also have "\<dots> = sum (\<lambda>p. of_int (sign (?t_jk \<circ> p)) * (\<Prod>i\<in>UNIV. A $ i $ p i)) ?S1"
lp15@68138
   278
    unfolding o_def by (rule sum.cong, auto simp: tjk_eq)
lp15@68138
   279
  also have "\<dots> = sum (\<lambda>p. - ?f p) ?S1"
immler@68072
   280
  proof (rule sum.cong, auto)
immler@68072
   281
    fix x assume x: "x permutes ?U"
immler@68072
   282
      and even_x: "evenperm x"
immler@68072
   283
    hence perm_x: "permutation x" and perm_tjk: "permutation ?t_jk"
immler@68072
   284
      using permutation_permutes[of x] permutation_permutes[of ?t_jk] permutation_swap_id
immler@68072
   285
      by (metis finite_code)+
immler@68072
   286
    have "(sign (?t_jk \<circ> x)) = - (sign x)"
immler@68072
   287
      unfolding sign_compose[OF perm_tjk perm_x] sign_tjk by auto
immler@68072
   288
    thus "of_int (sign (?t_jk \<circ> x)) * (\<Prod>i\<in>UNIV. A $ i $ x i)
immler@68072
   289
      = - (of_int (sign x) * (\<Prod>i\<in>UNIV. A $ i $ x i))"
immler@68072
   290
      by auto
immler@68072
   291
  qed
lp15@68138
   292
  also have "\<dots>= - sum ?f ?S1" unfolding sum_negf ..
immler@68072
   293
  finally have *: "sum ?f ?S2 = - sum ?f ?S1" .
immler@68072
   294
  have "det A = (\<Sum>p | p permutes UNIV. of_int (sign p) * (\<Prod>i\<in>UNIV. A $ i $ p i))"
immler@68072
   295
    unfolding det_def ..
lp15@68138
   296
  also have "\<dots>= sum ?f ?S1 + sum ?f ?S2"
immler@68072
   297
    by (subst PU_decomposition, rule sum.union_disjoint[OF _ _ disjoint], auto)
lp15@68138
   298
  also have "\<dots>= sum ?f ?S1 - sum ?f ?S1 " unfolding * by auto
lp15@68138
   299
  also have "\<dots>= 0" by simp
immler@68072
   300
  finally show "det A = 0" by simp
himmelma@33175
   301
qed
himmelma@33175
   302
ak2110@69720
   303
lemma  det_identical_rows:
immler@68072
   304
  fixes A :: "'a::comm_ring_1^'n^'n"
lp15@68134
   305
  assumes ij: "i \<noteq> j" and r: "row i A = row j A"
himmelma@33175
   306
  shows "det A = 0"
lp15@68134
   307
  by (metis column_transpose det_identical_columns det_transpose ij r)
himmelma@33175
   308
ak2110@69720
   309
lemma  det_zero_row:
immler@68072
   310
  fixes A :: "'a::{idom, ring_char_0}^'n^'n" and F :: "'b::{field}^'m^'m"
immler@68072
   311
  shows "row i A = 0 \<Longrightarrow> det A = 0" and "row j F = 0 \<Longrightarrow> det F = 0"
lp15@68138
   312
  by (force simp: row_def det_def vec_eq_iff sign_nz intro!: sum.neutral)+
himmelma@33175
   313
ak2110@69720
   314
lemma  det_zero_column:
immler@68072
   315
  fixes A :: "'a::{idom, ring_char_0}^'n^'n" and F :: "'b::{field}^'m^'m"
immler@68072
   316
  shows "column i A = 0 \<Longrightarrow> det A = 0" and "column j F = 0 \<Longrightarrow> det F = 0"
immler@68072
   317
  unfolding atomize_conj atomize_imp
immler@68072
   318
  by (metis det_transpose det_zero_row row_transpose)
himmelma@33175
   319
ak2110@69720
   320
lemma  det_row_add:
himmelma@33175
   321
  fixes a b c :: "'n::finite \<Rightarrow> _ ^ 'n"
himmelma@33175
   322
  shows "det((\<chi> i. if i = k then a i + b i else c i)::'a::comm_ring_1^'n^'n) =
wenzelm@53253
   323
    det((\<chi> i. if i = k then a i else c i)::'a::comm_ring_1^'n^'n) +
wenzelm@53253
   324
    det((\<chi> i. if i = k then b i else c i)::'a::comm_ring_1^'n^'n)"
nipkow@64267
   325
  unfolding det_def vec_lambda_beta sum.distrib[symmetric]
nipkow@64267
   326
proof (rule sum.cong)
himmelma@33175
   327
  let ?U = "UNIV :: 'n set"
himmelma@33175
   328
  let ?pU = "{p. p permutes ?U}"
himmelma@33175
   329
  let ?f = "(\<lambda>i. if i = k then a i + b i else c i)::'n \<Rightarrow> 'a::comm_ring_1^'n"
himmelma@33175
   330
  let ?g = "(\<lambda> i. if i = k then a i else c i)::'n \<Rightarrow> 'a::comm_ring_1^'n"
himmelma@33175
   331
  let ?h = "(\<lambda> i. if i = k then b i else c i)::'n \<Rightarrow> 'a::comm_ring_1^'n"
wenzelm@53253
   332
  fix p
wenzelm@53253
   333
  assume p: "p \<in> ?pU"
himmelma@33175
   334
  let ?Uk = "?U - {k}"
wenzelm@53854
   335
  from p have pU: "p permutes ?U"
wenzelm@53854
   336
    by blast
wenzelm@53854
   337
  have kU: "?U = insert k ?Uk"
wenzelm@53854
   338
    by blast
lp15@68134
   339
  have eq: "prod (\<lambda>i. ?f i $ p i) ?Uk = prod (\<lambda>i. ?g i $ p i) ?Uk"
lp15@68134
   340
           "prod (\<lambda>i. ?f i $ p i) ?Uk = prod (\<lambda>i. ?h i $ p i) ?Uk"
lp15@68134
   341
    by auto
lp15@68134
   342
  have Uk: "finite ?Uk" "k \<notin> ?Uk"
wenzelm@53854
   343
    by auto
nipkow@64272
   344
  have "prod (\<lambda>i. ?f i $ p i) ?U = prod (\<lambda>i. ?f i $ p i) (insert k ?Uk)"
himmelma@33175
   345
    unfolding kU[symmetric] ..
nipkow@64272
   346
  also have "\<dots> = ?f k $ p k * prod (\<lambda>i. ?f i $ p i) ?Uk"
lp15@68134
   347
    by (rule prod.insert) auto
nipkow@64272
   348
  also have "\<dots> = (a k $ p k * prod (\<lambda>i. ?f i $ p i) ?Uk) + (b k$ p k * prod (\<lambda>i. ?f i $ p i) ?Uk)"
wenzelm@53253
   349
    by (simp add: field_simps)
nipkow@64272
   350
  also have "\<dots> = (a k $ p k * prod (\<lambda>i. ?g i $ p i) ?Uk) + (b k$ p k * prod (\<lambda>i. ?h i $ p i) ?Uk)"
lp15@68134
   351
    by (metis eq)
nipkow@64272
   352
  also have "\<dots> = prod (\<lambda>i. ?g i $ p i) (insert k ?Uk) + prod (\<lambda>i. ?h i $ p i) (insert k ?Uk)"
lp15@68134
   353
    unfolding  prod.insert[OF Uk] by simp
nipkow@64272
   354
  finally have "prod (\<lambda>i. ?f i $ p i) ?U = prod (\<lambda>i. ?g i $ p i) ?U + prod (\<lambda>i. ?h i $ p i) ?U"
wenzelm@53854
   355
    unfolding kU[symmetric] .
nipkow@64272
   356
  then show "of_int (sign p) * prod (\<lambda>i. ?f i $ p i) ?U =
nipkow@64272
   357
    of_int (sign p) * prod (\<lambda>i. ?g i $ p i) ?U + of_int (sign p) * prod (\<lambda>i. ?h i $ p i) ?U"
haftmann@36350
   358
    by (simp add: field_simps)
lp15@68134
   359
qed auto
himmelma@33175
   360
ak2110@69720
   361
lemma  det_row_mul:
himmelma@33175
   362
  fixes a b :: "'n::finite \<Rightarrow> _ ^ 'n"
himmelma@33175
   363
  shows "det((\<chi> i. if i = k then c *s a i else b i)::'a::comm_ring_1^'n^'n) =
wenzelm@53253
   364
    c * det((\<chi> i. if i = k then a i else b i)::'a::comm_ring_1^'n^'n)"
nipkow@64267
   365
  unfolding det_def vec_lambda_beta sum_distrib_left
nipkow@64267
   366
proof (rule sum.cong)
himmelma@33175
   367
  let ?U = "UNIV :: 'n set"
himmelma@33175
   368
  let ?pU = "{p. p permutes ?U}"
himmelma@33175
   369
  let ?f = "(\<lambda>i. if i = k then c*s a i else b i)::'n \<Rightarrow> 'a::comm_ring_1^'n"
himmelma@33175
   370
  let ?g = "(\<lambda> i. if i = k then a i else b i)::'n \<Rightarrow> 'a::comm_ring_1^'n"
wenzelm@53253
   371
  fix p
wenzelm@53253
   372
  assume p: "p \<in> ?pU"
himmelma@33175
   373
  let ?Uk = "?U - {k}"
wenzelm@53854
   374
  from p have pU: "p permutes ?U"
wenzelm@53854
   375
    by blast
wenzelm@53854
   376
  have kU: "?U = insert k ?Uk"
wenzelm@53854
   377
    by blast
lp15@68134
   378
  have eq: "prod (\<lambda>i. ?f i $ p i) ?Uk = prod (\<lambda>i. ?g i $ p i) ?Uk"
lp15@68138
   379
    by auto
lp15@68134
   380
  have Uk: "finite ?Uk" "k \<notin> ?Uk"
wenzelm@53854
   381
    by auto
nipkow@64272
   382
  have "prod (\<lambda>i. ?f i $ p i) ?U = prod (\<lambda>i. ?f i $ p i) (insert k ?Uk)"
himmelma@33175
   383
    unfolding kU[symmetric] ..
nipkow@64272
   384
  also have "\<dots> = ?f k $ p k  * prod (\<lambda>i. ?f i $ p i) ?Uk"
lp15@68134
   385
    by (rule prod.insert) auto
nipkow@64272
   386
  also have "\<dots> = (c*s a k) $ p k * prod (\<lambda>i. ?f i $ p i) ?Uk"
wenzelm@53253
   387
    by (simp add: field_simps)
nipkow@64272
   388
  also have "\<dots> = c* (a k $ p k * prod (\<lambda>i. ?g i $ p i) ?Uk)"
lp15@68134
   389
    unfolding eq by (simp add: ac_simps)
nipkow@64272
   390
  also have "\<dots> = c* (prod (\<lambda>i. ?g i $ p i) (insert k ?Uk))"
lp15@68134
   391
    unfolding prod.insert[OF Uk] by simp
nipkow@64272
   392
  finally have "prod (\<lambda>i. ?f i $ p i) ?U = c* (prod (\<lambda>i. ?g i $ p i) ?U)"
wenzelm@53253
   393
    unfolding kU[symmetric] .
lp15@68134
   394
  then show "of_int (sign p) * prod (\<lambda>i. ?f i $ p i) ?U = c * (of_int (sign p) * prod (\<lambda>i. ?g i $ p i) ?U)"
haftmann@36350
   395
    by (simp add: field_simps)
lp15@68134
   396
qed auto
himmelma@33175
   397
ak2110@69720
   398
lemma  det_row_0:
himmelma@33175
   399
  fixes b :: "'n::finite \<Rightarrow> _ ^ 'n"
himmelma@33175
   400
  shows "det((\<chi> i. if i = k then 0 else b i)::'a::comm_ring_1^'n^'n) = 0"
wenzelm@53253
   401
  using det_row_mul[of k 0 "\<lambda>i. 1" b]
wenzelm@53253
   402
  apply simp
wenzelm@53253
   403
  apply (simp only: vector_smult_lzero)
wenzelm@53253
   404
  done
himmelma@33175
   405
ak2110@69720
   406
lemma  det_row_operation:
immler@68072
   407
  fixes A :: "'a::{comm_ring_1}^'n^'n"
himmelma@33175
   408
  assumes ij: "i \<noteq> j"
himmelma@33175
   409
  shows "det (\<chi> k. if k = i then row i A + c *s row j A else row k A) = det A"
ak2110@69720
   410
proof -
himmelma@33175
   411
  let ?Z = "(\<chi> k. if k = i then row j A else row k A) :: 'a ^'n^'n"
himmelma@33175
   412
  have th: "row i ?Z = row j ?Z" by (vector row_def)
himmelma@33175
   413
  have th2: "((\<chi> k. if k = i then row i A else row k A) :: 'a^'n^'n) = A"
himmelma@33175
   414
    by (vector row_def)
himmelma@33175
   415
  show ?thesis
himmelma@33175
   416
    unfolding det_row_add [of i] det_row_mul[of i] det_identical_rows[OF ij th] th2
himmelma@33175
   417
    by simp
himmelma@33175
   418
qed
himmelma@33175
   419
ak2110@69720
   420
lemma  det_row_span:
immler@68072
   421
  fixes A :: "'a::{field}^'n^'n"
immler@68072
   422
  assumes x: "x \<in> vec.span {row j A |j. j \<noteq> i}"
himmelma@33175
   423
  shows "det (\<chi> k. if k = i then row i A + x else row k A) = det A"
lp15@68069
   424
  using x
immler@68074
   425
proof (induction rule: vec.span_induct_alt)
lp15@68069
   426
  case base
lp15@68134
   427
  have "(if k = i then row i A + 0 else row k A) = row k A" for k
lp15@68134
   428
    by simp
lp15@68069
   429
  then show ?case
lp15@68134
   430
    by (simp add: row_def)
lp15@68069
   431
next
lp15@68069
   432
  case (step c z y)
lp15@68069
   433
  then obtain j where j: "z = row j A" "i \<noteq> j"
lp15@68069
   434
    by blast
lp15@68069
   435
  let ?w = "row i A + y"
lp15@68069
   436
  have th0: "row i A + (c*s z + y) = ?w + c*s z"
lp15@68069
   437
    by vector
lp15@68069
   438
  let ?d = "\<lambda>x. det (\<chi> k. if k = i then x else row k A)"
lp15@68069
   439
  have thz: "?d z = 0"
lp15@68069
   440
    apply (rule det_identical_rows[OF j(2)])
lp15@68069
   441
    using j
lp15@68069
   442
    apply (vector row_def)
himmelma@33175
   443
    done
lp15@68069
   444
  have "?d (row i A + (c*s z + y)) = ?d (?w + c*s z)"
lp15@68069
   445
    unfolding th0 ..
lp15@68069
   446
  then have "?d (row i A + (c*s z + y)) = det A"
lp15@68069
   447
    unfolding thz step.IH det_row_mul[of i] det_row_add[of i] by simp
lp15@68069
   448
  then show ?case
lp15@68069
   449
    unfolding scalar_mult_eq_scaleR .
lp15@68143
   450
qed
himmelma@33175
   451
ak2110@69720
   452
lemma  matrix_id [simp]: "det (matrix id) = 1"
lp15@67673
   453
  by (simp add: matrix_id_mat_1)
lp15@67673
   454
ak2110@69720
   455
proposition  det_matrix_scaleR [simp]: "det (matrix (((*\<^sub>R) r)) :: real^'n^'n) = r ^ CARD('n::finite)"
lp15@67673
   456
  apply (subst det_diagonal)
immler@68072
   457
   apply (auto simp: matrix_def mat_def)
lp15@67673
   458
  apply (simp add: cart_eq_inner_axis inner_axis_axis)
lp15@67673
   459
  done
lp15@67673
   460
wenzelm@60420
   461
text \<open>
wenzelm@53854
   462
  May as well do this, though it's a bit unsatisfactory since it ignores
wenzelm@53854
   463
  exact duplicates by considering the rows/columns as a set.
wenzelm@60420
   464
\<close>
himmelma@33175
   465
ak2110@69720
   466
lemma  det_dependent_rows:
immler@68072
   467
  fixes A:: "'a::{field}^'n^'n"
immler@68072
   468
  assumes d: "vec.dependent (rows A)"
himmelma@33175
   469
  shows "det A = 0"
wenzelm@53253
   470
proof -
himmelma@33175
   471
  let ?U = "UNIV :: 'n set"
immler@68072
   472
  from d obtain i where i: "row i A \<in> vec.span (rows A - {row i A})"
immler@68072
   473
    unfolding vec.dependent_def rows_def by blast
lp15@68134
   474
  show ?thesis
lp15@68134
   475
  proof (cases "\<forall>i j. i \<noteq> j \<longrightarrow> row i A \<noteq> row j A")
lp15@68134
   476
    case True
lp15@68134
   477
    with i have "vec.span (rows A - {row i A}) \<subseteq> vec.span {row j A |j. j \<noteq> i}"
lp15@68138
   478
      by (auto simp: rows_def intro!: vec.span_mono)
lp15@68134
   479
    then have "- row i A \<in> vec.span {row j A|j. j \<noteq> i}"
lp15@68134
   480
      by (meson i subsetCE vec.span_neg)
lp15@68134
   481
    from det_row_span[OF this]
himmelma@33175
   482
    have "det A = det (\<chi> k. if k = i then 0 *s 1 else row k A)"
himmelma@33175
   483
      unfolding right_minus vector_smult_lzero ..
lp15@68134
   484
    with det_row_mul[of i 0 "\<lambda>i. 1"]
lp15@68134
   485
    show ?thesis by simp
lp15@68134
   486
  next
lp15@68134
   487
    case False
lp15@68134
   488
    then obtain j k where jk: "j \<noteq> k" "row j A = row k A"
lp15@68134
   489
      by auto
lp15@68134
   490
    from det_identical_rows[OF jk] show ?thesis .
lp15@68134
   491
  qed
himmelma@33175
   492
qed
himmelma@33175
   493
ak2110@69720
   494
lemma  det_dependent_columns:
immler@68072
   495
  assumes d: "vec.dependent (columns (A::real^'n^'n))"
wenzelm@53253
   496
  shows "det A = 0"
wenzelm@53253
   497
  by (metis d det_dependent_rows rows_transpose det_transpose)
himmelma@33175
   498
lp15@68134
   499
text \<open>Multilinearity and the multiplication formula\<close>
himmelma@33175
   500
ak2110@69720
   501
lemma  Cart_lambda_cong: "(\<And>x. f x = g x) \<Longrightarrow> (vec_lambda f::'a^'n) = (vec_lambda g :: 'a^'n)"
lp15@68134
   502
  by auto
himmelma@33175
   503
ak2110@69720
   504
lemma  det_linear_row_sum:
himmelma@33175
   505
  assumes fS: "finite S"
nipkow@64267
   506
  shows "det ((\<chi> i. if i = k then sum (a i) S else c i)::'a::comm_ring_1^'n^'n) =
nipkow@64267
   507
    sum (\<lambda>j. det ((\<chi> i. if i = k then a  i j else c i)::'a^'n^'n)) S"
lp15@68134
   508
  using fS  by (induct rule: finite_induct; simp add: det_row_0 det_row_add cong: if_cong)
himmelma@33175
   509
ak2110@69720
   510
lemma  finite_bounded_functions:
himmelma@33175
   511
  assumes fS: "finite S"
himmelma@33175
   512
  shows "finite {f. (\<forall>i \<in> {1.. (k::nat)}. f i \<in> S) \<and> (\<forall>i. i \<notin> {1 .. k} \<longrightarrow> f i = i)}"
wenzelm@53253
   513
proof (induct k)
himmelma@33175
   514
  case 0
lp15@68134
   515
  have *: "{f. \<forall>i. f i = i} = {id}"
wenzelm@53854
   516
    by auto
wenzelm@53854
   517
  show ?case
lp15@68138
   518
    by (auto simp: *)
himmelma@33175
   519
next
himmelma@33175
   520
  case (Suc k)
himmelma@33175
   521
  let ?f = "\<lambda>(y::nat,g) i. if i = Suc k then y else g i"
himmelma@33175
   522
  let ?S = "?f ` (S \<times> {f. (\<forall>i\<in>{1..k}. f i \<in> S) \<and> (\<forall>i. i \<notin> {1..k} \<longrightarrow> f i = i)})"
himmelma@33175
   523
  have "?S = {f. (\<forall>i\<in>{1.. Suc k}. f i \<in> S) \<and> (\<forall>i. i \<notin> {1.. Suc k} \<longrightarrow> f i = i)}"
lp15@68138
   524
    apply (auto simp: image_iff)
lp15@68134
   525
    apply (rename_tac f)
lp15@68134
   526
    apply (rule_tac x="f (Suc k)" in bexI)
lp15@68138
   527
    apply (rule_tac x = "\<lambda>i. if i = Suc k then i else f i" in exI, auto)
himmelma@33175
   528
    done
himmelma@33175
   529
  with finite_imageI[OF finite_cartesian_product[OF fS Suc.hyps(1)], of ?f]
wenzelm@53854
   530
  show ?case
wenzelm@53854
   531
    by metis
himmelma@33175
   532
qed
himmelma@33175
   533
himmelma@33175
   534
ak2110@69720
   535
lemma  det_linear_rows_sum_lemma:
wenzelm@53854
   536
  assumes fS: "finite S"
wenzelm@53854
   537
    and fT: "finite T"
nipkow@64267
   538
  shows "det ((\<chi> i. if i \<in> T then sum (a i) S else c i):: 'a::comm_ring_1^'n^'n) =
nipkow@64267
   539
    sum (\<lambda>f. det((\<chi> i. if i \<in> T then a i (f i) else c i)::'a^'n^'n))
wenzelm@53253
   540
      {f. (\<forall>i \<in> T. f i \<in> S) \<and> (\<forall>i. i \<notin> T \<longrightarrow> f i = i)}"
wenzelm@53253
   541
  using fT
ak2110@69720
   542
proof (induct T arbitrary: a c set: finite)
himmelma@33175
   543
  case empty
wenzelm@53253
   544
  have th0: "\<And>x y. (\<chi> i. if i \<in> {} then x i else y i) = (\<chi> i. y i)"
wenzelm@53253
   545
    by vector
wenzelm@53854
   546
  from empty.prems show ?case
lp15@62408
   547
    unfolding th0 by (simp add: eq_id_iff)
himmelma@33175
   548
next
himmelma@33175
   549
  case (insert z T a c)
himmelma@33175
   550
  let ?F = "\<lambda>T. {f. (\<forall>i \<in> T. f i \<in> S) \<and> (\<forall>i. i \<notin> T \<longrightarrow> f i = i)}"
himmelma@33175
   551
  let ?h = "\<lambda>(y,g) i. if i = z then y else g i"
himmelma@33175
   552
  let ?k = "\<lambda>h. (h(z),(\<lambda>i. if i = z then i else h i))"
himmelma@33175
   553
  let ?s = "\<lambda> k a c f. det((\<chi> i. if i \<in> T then a i (f i) else c i)::'a^'n^'n)"
hoelzl@57129
   554
  let ?c = "\<lambda>j i. if i = z then a i j else c i"
wenzelm@53253
   555
  have thif: "\<And>a b c d. (if a \<or> b then c else d) = (if a then c else if b then c else d)"
wenzelm@53253
   556
    by simp
himmelma@33175
   557
  have thif2: "\<And>a b c d e. (if a then b else if c then d else e) =
wenzelm@53253
   558
     (if c then (if a then b else d) else (if a then b else e))"
wenzelm@53253
   559
    by simp
lp15@68134
   560
  from \<open>z \<notin> T\<close> have nz: "\<And>i. i \<in> T \<Longrightarrow> i \<noteq> z"
wenzelm@53253
   561
    by auto
nipkow@64267
   562
  have "det (\<chi> i. if i \<in> insert z T then sum (a i) S else c i) =
nipkow@64267
   563
    det (\<chi> i. if i = z then sum (a i) S else if i \<in> T then sum (a i) S else c i)"
himmelma@33175
   564
    unfolding insert_iff thif ..
nipkow@64267
   565
  also have "\<dots> = (\<Sum>j\<in>S. det (\<chi> i. if i \<in> T then sum (a i) S else if i = z then a i j else c i))"
nipkow@64267
   566
    unfolding det_linear_row_sum[OF fS]
lp15@68134
   567
    by (subst thif2) (simp add: nz cong: if_cong)
himmelma@33175
   568
  finally have tha:
nipkow@64267
   569
    "det (\<chi> i. if i \<in> insert z T then sum (a i) S else c i) =
himmelma@33175
   570
     (\<Sum>(j, f)\<in>S \<times> ?F T. det (\<chi> i. if i \<in> T then a i (f i)
himmelma@33175
   571
                                else if i = z then a i j
himmelma@33175
   572
                                else c i))"
nipkow@64267
   573
    unfolding insert.hyps unfolding sum.cartesian_product by blast
himmelma@33175
   574
  show ?case unfolding tha
wenzelm@60420
   575
    using \<open>z \<notin> T\<close>
nipkow@64267
   576
    by (intro sum.reindex_bij_witness[where i="?k" and j="?h"])
hoelzl@57129
   577
       (auto intro!: cong[OF refl[of det]] simp: vec_eq_iff)
himmelma@33175
   578
qed
himmelma@33175
   579
ak2110@69720
   580
lemma  det_linear_rows_sum:
wenzelm@53854
   581
  fixes S :: "'n::finite set"
wenzelm@53854
   582
  assumes fS: "finite S"
nipkow@64267
   583
  shows "det (\<chi> i. sum (a i) S) =
nipkow@64267
   584
    sum (\<lambda>f. det (\<chi> i. a i (f i) :: 'a::comm_ring_1 ^ 'n^'n)) {f. \<forall>i. f i \<in> S}"
wenzelm@53253
   585
proof -
wenzelm@53253
   586
  have th0: "\<And>x y. ((\<chi> i. if i \<in> (UNIV:: 'n set) then x i else y i) :: 'a^'n^'n) = (\<chi> i. x i)"
wenzelm@53253
   587
    by vector
nipkow@64267
   588
  from det_linear_rows_sum_lemma[OF fS, of "UNIV :: 'n set" a, unfolded th0, OF finite]
wenzelm@53253
   589
  show ?thesis by simp
himmelma@33175
   590
qed
himmelma@33175
   591
ak2110@69720
   592
lemma  matrix_mul_sum_alt:
hoelzl@34291
   593
  fixes A B :: "'a::comm_ring_1^'n^'n"
nipkow@64267
   594
  shows "A ** B = (\<chi> i. sum (\<lambda>k. A$i$k *s B $ k) (UNIV :: 'n set))"
nipkow@64267
   595
  by (vector matrix_matrix_mult_def sum_component)
himmelma@33175
   596
ak2110@69720
   597
lemma  det_rows_mul:
hoelzl@34291
   598
  "det((\<chi> i. c i *s a i)::'a::comm_ring_1^'n^'n) =
nipkow@64272
   599
    prod (\<lambda>i. c i) (UNIV:: 'n set) * det((\<chi> i. a i)::'a^'n^'n)"
nipkow@64272
   600
proof (simp add: det_def sum_distrib_left cong add: prod.cong, rule sum.cong)
himmelma@33175
   601
  let ?U = "UNIV :: 'n set"
himmelma@33175
   602
  let ?PU = "{p. p permutes ?U}"
wenzelm@53253
   603
  fix p
wenzelm@53253
   604
  assume pU: "p \<in> ?PU"
himmelma@33175
   605
  let ?s = "of_int (sign p)"
wenzelm@53253
   606
  from pU have p: "p permutes ?U"
wenzelm@53253
   607
    by blast
nipkow@64272
   608
  have "prod (\<lambda>i. c i * a i $ p i) ?U = prod c ?U * prod (\<lambda>i. a i $ p i) ?U"
nipkow@64272
   609
    unfolding prod.distrib ..
himmelma@33175
   610
  then show "?s * (\<Prod>xa\<in>?U. c xa * a xa $ p xa) =
nipkow@64272
   611
    prod c ?U * (?s* (\<Prod>xa\<in>?U. a xa $ p xa))"
wenzelm@53854
   612
    by (simp add: field_simps)
haftmann@57418
   613
qed rule
himmelma@33175
   614
ak2110@69720
   615
proposition  det_mul:
immler@68072
   616
  fixes A B :: "'a::comm_ring_1^'n^'n"
himmelma@33175
   617
  shows "det (A ** B) = det A * det B"
ak2110@69720
   618
proof -
himmelma@33175
   619
  let ?U = "UNIV :: 'n set"
lp15@68134
   620
  let ?F = "{f. (\<forall>i \<in> ?U. f i \<in> ?U) \<and> (\<forall>i. i \<notin> ?U \<longrightarrow> f i = i)}"
himmelma@33175
   621
  let ?PU = "{p. p permutes ?U}"
lp15@68134
   622
  have "p \<in> ?F" if "p permutes ?U" for p
wenzelm@53854
   623
    by simp
wenzelm@53854
   624
  then have PUF: "?PU \<subseteq> ?F" by blast
wenzelm@53253
   625
  {
wenzelm@53253
   626
    fix f
wenzelm@53253
   627
    assume fPU: "f \<in> ?F - ?PU"
wenzelm@53854
   628
    have fUU: "f ` ?U \<subseteq> ?U"
wenzelm@53854
   629
      using fPU by auto
wenzelm@53253
   630
    from fPU have f: "\<forall>i \<in> ?U. f i \<in> ?U" "\<forall>i. i \<notin> ?U \<longrightarrow> f i = i" "\<not>(\<forall>y. \<exists>!x. f x = y)"
wenzelm@53253
   631
      unfolding permutes_def by auto
himmelma@33175
   632
himmelma@33175
   633
    let ?A = "(\<chi> i. A$i$f i *s B$f i) :: 'a^'n^'n"
himmelma@33175
   634
    let ?B = "(\<chi> i. B$f i) :: 'a^'n^'n"
wenzelm@53253
   635
    {
wenzelm@53253
   636
      assume fni: "\<not> inj_on f ?U"
himmelma@33175
   637
      then obtain i j where ij: "f i = f j" "i \<noteq> j"
himmelma@33175
   638
        unfolding inj_on_def by blast
lp15@68134
   639
      then have "row i ?B = row j ?B"
wenzelm@53854
   640
        by (vector row_def)
lp15@68134
   641
      with det_identical_rows[OF ij(2)]
himmelma@33175
   642
      have "det (\<chi> i. A$i$f i *s B$f i) = 0"
lp15@68134
   643
        unfolding det_rows_mul by force
wenzelm@53253
   644
    }
himmelma@33175
   645
    moreover
wenzelm@53253
   646
    {
wenzelm@53253
   647
      assume fi: "inj_on f ?U"
himmelma@33175
   648
      from f fi have fith: "\<And>i j. f i = f j \<Longrightarrow> i = j"
himmelma@33175
   649
        unfolding inj_on_def by metis
lp15@68134
   650
      note fs = fi[unfolded surjective_iff_injective_gen[OF finite finite refl fUU, symmetric]]
lp15@68134
   651
      have "\<exists>!x. f x = y" for y
lp15@68134
   652
        using fith fs by blast
wenzelm@53854
   653
      with f(3) have "det (\<chi> i. A$i$f i *s B$f i) = 0"
wenzelm@53854
   654
        by blast
wenzelm@53253
   655
    }
wenzelm@53854
   656
    ultimately have "det (\<chi> i. A$i$f i *s B$f i) = 0"
wenzelm@53854
   657
      by blast
wenzelm@53253
   658
  }
wenzelm@53854
   659
  then have zth: "\<forall> f\<in> ?F - ?PU. det (\<chi> i. A$i$f i *s B$f i) = 0"
wenzelm@53253
   660
    by simp
wenzelm@53253
   661
  {
wenzelm@53253
   662
    fix p
wenzelm@53253
   663
    assume pU: "p \<in> ?PU"
wenzelm@53854
   664
    from pU have p: "p permutes ?U"
wenzelm@53854
   665
      by blast
himmelma@33175
   666
    let ?s = "\<lambda>p. of_int (sign p)"
wenzelm@53253
   667
    let ?f = "\<lambda>q. ?s p * (\<Prod>i\<in> ?U. A $ i $ p i) * (?s q * (\<Prod>i\<in> ?U. B $ i $ q i))"
nipkow@64267
   668
    have "(sum (\<lambda>q. ?s q *
wenzelm@53253
   669
        (\<Prod>i\<in> ?U. (\<chi> i. A $ i $ p i *s B $ p i :: 'a^'n^'n) $ i $ q i)) ?PU) =
nipkow@64267
   670
      (sum (\<lambda>q. ?s p * (\<Prod>i\<in> ?U. A $ i $ p i) * (?s q * (\<Prod>i\<in> ?U. B $ i $ q i))) ?PU)"
himmelma@33175
   671
      unfolding sum_permutations_compose_right[OF permutes_inv[OF p], of ?f]
nipkow@64267
   672
    proof (rule sum.cong)
wenzelm@53253
   673
      fix q
wenzelm@53253
   674
      assume qU: "q \<in> ?PU"
wenzelm@53854
   675
      then have q: "q permutes ?U"
wenzelm@53854
   676
        by blast
himmelma@33175
   677
      from p q have pp: "permutation p" and pq: "permutation q"
himmelma@33175
   678
        unfolding permutation_permutes by auto
himmelma@33175
   679
      have th00: "of_int (sign p) * of_int (sign p) = (1::'a)"
himmelma@33175
   680
        "\<And>a. of_int (sign p) * (of_int (sign p) * a) = a"
haftmann@57512
   681
        unfolding mult.assoc[symmetric]
wenzelm@53854
   682
        unfolding of_int_mult[symmetric]
himmelma@33175
   683
        by (simp_all add: sign_idempotent)
wenzelm@53854
   684
      have ths: "?s q = ?s p * ?s (q \<circ> inv p)"
himmelma@33175
   685
        using pp pq permutation_inverse[OF pp] sign_inverse[OF pp]
lp15@68134
   686
        by (simp add: th00 ac_simps sign_idempotent sign_compose)
nipkow@64272
   687
      have th001: "prod (\<lambda>i. B$i$ q (inv p i)) ?U = prod ((\<lambda>i. B$i$ q (inv p i)) \<circ> p) ?U"
lp15@68134
   688
        by (rule prod.permute[OF p])
nipkow@64272
   689
      have thp: "prod (\<lambda>i. (\<chi> i. A$i$p i *s B$p i :: 'a^'n^'n) $i $ q i) ?U =
nipkow@64272
   690
        prod (\<lambda>i. A$i$p i) ?U * prod (\<lambda>i. B$i$ q (inv p i)) ?U"
nipkow@64272
   691
        unfolding th001 prod.distrib[symmetric] o_def permutes_inverses[OF p]
nipkow@64272
   692
        apply (rule prod.cong[OF refl])
wenzelm@53253
   693
        using permutes_in_image[OF q]
wenzelm@53253
   694
        apply vector
wenzelm@53253
   695
        done
nipkow@64272
   696
      show "?s q * prod (\<lambda>i. (((\<chi> i. A$i$p i *s B$p i) :: 'a^'n^'n)$i$q i)) ?U =
nipkow@64272
   697
        ?s p * (prod (\<lambda>i. A$i$p i) ?U) * (?s (q \<circ> inv p) * prod (\<lambda>i. B$i$(q \<circ> inv p) i) ?U)"
himmelma@33175
   698
        using ths thp pp pq permutation_inverse[OF pp] sign_inverse[OF pp]
haftmann@36350
   699
        by (simp add: sign_nz th00 field_simps sign_idempotent sign_compose)
haftmann@57418
   700
    qed rule
himmelma@33175
   701
  }
nipkow@64267
   702
  then have th2: "sum (\<lambda>f. det (\<chi> i. A$i$f i *s B$f i)) ?PU = det A * det B"
nipkow@64267
   703
    unfolding det_def sum_product
nipkow@64267
   704
    by (rule sum.cong [OF refl])
nipkow@64267
   705
  have "det (A**B) = sum (\<lambda>f.  det (\<chi> i. A $ i $ f i *s B $ f i)) ?F"
lp15@68134
   706
    unfolding matrix_mul_sum_alt det_linear_rows_sum[OF finite]
wenzelm@53854
   707
    by simp
nipkow@64267
   708
  also have "\<dots> = sum (\<lambda>f. det (\<chi> i. A$i$f i *s B$f i)) ?PU"
lp15@68134
   709
    using sum.mono_neutral_cong_left[OF finite PUF zth, symmetric]
himmelma@33175
   710
    unfolding det_rows_mul by auto
himmelma@33175
   711
  finally show ?thesis unfolding th2 .
himmelma@33175
   712
qed
himmelma@33175
   713
immler@68072
   714
immler@69683
   715
subsection \<open>Relation to invertibility\<close>
himmelma@33175
   716
ak2110@69720
   717
proposition  invertible_det_nz:
immler@68072
   718
  fixes A::"'a::{field}^'n^'n"
himmelma@33175
   719
  shows "invertible A \<longleftrightarrow> det A \<noteq> 0"
ak2110@69720
   720
proof (cases "invertible A")
lp15@68134
   721
  case True
lp15@68134
   722
  then obtain B :: "'a^'n^'n" where B: "A ** B = mat 1"
lp15@68134
   723
    unfolding invertible_right_inverse by blast
lp15@68134
   724
  then have "det (A ** B) = det (mat 1 :: 'a^'n^'n)"
lp15@68134
   725
    by simp
lp15@68134
   726
  then show ?thesis
lp15@68134
   727
    by (metis True det_I det_mul mult_zero_left one_neq_zero)
lp15@68134
   728
next
lp15@68134
   729
  case False
lp15@68134
   730
  let ?U = "UNIV :: 'n set"
lp15@68134
   731
  have fU: "finite ?U"
lp15@68134
   732
    by simp
lp15@68134
   733
  from False obtain c i where c: "sum (\<lambda>i. c i *s row i A) ?U = 0" and iU: "i \<in> ?U" and ci: "c i \<noteq> 0"
lp15@68134
   734
    unfolding invertible_right_inverse matrix_right_invertible_independent_rows
wenzelm@53854
   735
    by blast
lp15@68134
   736
  have thr0: "- row i A = sum (\<lambda>j. (1/ c i) *s (c j *s row j A)) (?U - {i})"
lp15@68143
   737
    unfolding sum_cmul  using c ci
lp15@68138
   738
    by (auto simp: sum.remove[OF fU iU] eq_vector_fraction_iff add_eq_0_iff)
lp15@68134
   739
  have thr: "- row i A \<in> vec.span {row j A| j. j \<noteq> i}"
lp15@68134
   740
    unfolding thr0 by (auto intro: vec.span_base vec.span_scale vec.span_sum)
lp15@68134
   741
  let ?B = "(\<chi> k. if k = i then 0 else row k A) :: 'a^'n^'n"
lp15@68134
   742
  have thrb: "row i ?B = 0" using iU by (vector row_def)
lp15@68134
   743
  have "det A = 0"
lp15@68134
   744
    unfolding det_row_span[OF thr, symmetric] right_minus
lp15@68134
   745
    unfolding det_zero_row(2)[OF thrb] ..
lp15@68134
   746
  then show ?thesis
lp15@68134
   747
    by (simp add: False)
himmelma@33175
   748
qed
himmelma@33175
   749
lp15@68134
   750
ak2110@69720
   751
lemma  det_nz_iff_inj_gen:
immler@68072
   752
  fixes f :: "'a::field^'n \<Rightarrow> 'a::field^'n"
nipkow@69064
   753
  assumes "Vector_Spaces.linear (*s) (*s) f"
lp15@67990
   754
  shows "det (matrix f) \<noteq> 0 \<longleftrightarrow> inj f"
lp15@67990
   755
proof
lp15@67990
   756
  assume "det (matrix f) \<noteq> 0"
lp15@67990
   757
  then show "inj f"
lp15@67990
   758
    using assms invertible_det_nz inj_matrix_vector_mult by force
lp15@67990
   759
next
lp15@67990
   760
  assume "inj f"
lp15@67990
   761
  show "det (matrix f) \<noteq> 0"
immler@68072
   762
    using vec.linear_injective_left_inverse [OF assms \<open>inj f\<close>]
immler@68072
   763
    by (metis assms invertible_det_nz invertible_left_inverse matrix_compose_gen matrix_id_mat_1)
lp15@67990
   764
qed
lp15@67990
   765
ak2110@69720
   766
lemma  det_nz_iff_inj:
immler@68072
   767
  fixes f :: "real^'n \<Rightarrow> real^'n"
immler@68072
   768
  assumes "linear f"
immler@68072
   769
  shows "det (matrix f) \<noteq> 0 \<longleftrightarrow> inj f"
immler@68072
   770
  using det_nz_iff_inj_gen[of f] assms
immler@68072
   771
  unfolding linear_matrix_vector_mul_eq .
immler@68072
   772
ak2110@69720
   773
lemma  det_eq_0_rank:
lp15@67990
   774
  fixes A :: "real^'n^'n"
lp15@67990
   775
  shows "det A = 0 \<longleftrightarrow> rank A < CARD('n)"
lp15@67990
   776
  using invertible_det_nz [of A]
lp15@67990
   777
  by (auto simp: matrix_left_invertible_injective invertible_left_inverse less_rank_noninjective)
lp15@67990
   778
ak2110@69720
   779
subsubsection%important  \<open>Invertibility of matrices and corresponding linear functions\<close>
lp15@67981
   780
ak2110@69720
   781
lemma  matrix_left_invertible_gen:
immler@68072
   782
  fixes f :: "'a::field^'m \<Rightarrow> 'a::field^'n"
nipkow@69064
   783
  assumes "Vector_Spaces.linear (*s) (*s) f"
nipkow@69064
   784
  shows "((\<exists>B. B ** matrix f = mat 1) \<longleftrightarrow> (\<exists>g. Vector_Spaces.linear (*s) (*s) g \<and> g \<circ> f = id))"
ak2110@69720
   785
proof safe
lp15@67981
   786
  fix B
lp15@67981
   787
  assume 1: "B ** matrix f = mat 1"
nipkow@69064
   788
  show "\<exists>g. Vector_Spaces.linear (*s) (*s) g \<and> g \<circ> f = id"
lp15@67981
   789
  proof (intro exI conjI)
nipkow@69064
   790
    show "Vector_Spaces.linear (*s) (*s) (\<lambda>y. B *v y)"
lp15@68138
   791
      by simp
nipkow@69064
   792
    show "((*v) B) \<circ> f = id"
lp15@67981
   793
      unfolding o_def
immler@68072
   794
      by (metis assms 1 eq_id_iff matrix_vector_mul(1) matrix_vector_mul_assoc matrix_vector_mul_lid)
lp15@67981
   795
  qed
lp15@67981
   796
next
lp15@67981
   797
  fix g
nipkow@69064
   798
  assume "Vector_Spaces.linear (*s) (*s) g" "g \<circ> f = id"
lp15@67981
   799
  then have "matrix g ** matrix f = mat 1"
immler@68072
   800
    by (metis assms matrix_compose_gen matrix_id_mat_1)
lp15@67981
   801
  then show "\<exists>B. B ** matrix f = mat 1" ..
lp15@67981
   802
qed
lp15@67981
   803
ak2110@69720
   804
lemma  matrix_left_invertible:
immler@68072
   805
  "linear f \<Longrightarrow> ((\<exists>B. B ** matrix f = mat 1) \<longleftrightarrow> (\<exists>g. linear g \<and> g \<circ> f = id))" for f::"real^'m \<Rightarrow> real^'n"
immler@68072
   806
  using matrix_left_invertible_gen[of f]
immler@68072
   807
  by (auto simp: linear_matrix_vector_mul_eq)
immler@68072
   808
ak2110@69720
   809
lemma  matrix_right_invertible_gen:
immler@68072
   810
  fixes f :: "'a::field^'m \<Rightarrow> 'a^'n"
nipkow@69064
   811
  assumes "Vector_Spaces.linear (*s) (*s) f"
nipkow@69064
   812
  shows "((\<exists>B. matrix f ** B = mat 1) \<longleftrightarrow> (\<exists>g. Vector_Spaces.linear (*s) (*s) g \<and> f \<circ> g = id))"
lp15@67981
   813
proof safe
lp15@67981
   814
  fix B
lp15@67981
   815
  assume 1: "matrix f ** B = mat 1"
nipkow@69064
   816
  show "\<exists>g. Vector_Spaces.linear (*s) (*s) g \<and> f \<circ> g = id"
lp15@67981
   817
  proof (intro exI conjI)
nipkow@69064
   818
    show "Vector_Spaces.linear (*s) (*s) ((*v) B)"
lp15@68138
   819
      by simp
nipkow@69064
   820
    show "f \<circ> (*v) B = id"
immler@68072
   821
      using 1 assms comp_apply eq_id_iff vec.linear_id matrix_id_mat_1 matrix_vector_mul_assoc matrix_works
immler@68072
   822
      by (metis (no_types, hide_lams))
lp15@67981
   823
  qed
lp15@67981
   824
next
lp15@67981
   825
  fix g
nipkow@69064
   826
  assume "Vector_Spaces.linear (*s) (*s) g" and "f \<circ> g = id"
lp15@67981
   827
  then have "matrix f ** matrix g = mat 1"
immler@68072
   828
    by (metis assms matrix_compose_gen matrix_id_mat_1)
lp15@67981
   829
  then show "\<exists>B. matrix f ** B = mat 1" ..
lp15@67981
   830
qed
lp15@67981
   831
ak2110@69720
   832
lemma  matrix_right_invertible:
immler@68072
   833
  "linear f \<Longrightarrow> ((\<exists>B. matrix f ** B = mat 1) \<longleftrightarrow> (\<exists>g. linear g \<and> f \<circ> g = id))" for f::"real^'m \<Rightarrow> real^'n"
immler@68072
   834
  using matrix_right_invertible_gen[of f]
immler@68072
   835
  by (auto simp: linear_matrix_vector_mul_eq)
immler@68072
   836
ak2110@69720
   837
lemma  matrix_invertible_gen:
immler@68072
   838
  fixes f :: "'a::field^'m \<Rightarrow> 'a::field^'n"
nipkow@69064
   839
  assumes "Vector_Spaces.linear (*s) (*s) f"
nipkow@69064
   840
  shows  "invertible (matrix f) \<longleftrightarrow> (\<exists>g. Vector_Spaces.linear (*s) (*s) g \<and> f \<circ> g = id \<and> g \<circ> f = id)"
immler@68072
   841
    (is "?lhs = ?rhs")
immler@68072
   842
proof
immler@68072
   843
  assume ?lhs then show ?rhs
immler@68072
   844
    by (metis assms invertible_def left_right_inverse_eq matrix_left_invertible_gen matrix_right_invertible_gen)
immler@68072
   845
next
immler@68072
   846
  assume ?rhs then show ?lhs
immler@68072
   847
    by (metis assms invertible_def matrix_compose_gen matrix_id_mat_1)
immler@68072
   848
qed
immler@68072
   849
ak2110@69720
   850
lemma  matrix_invertible:
immler@68072
   851
  "linear f \<Longrightarrow> invertible (matrix f) \<longleftrightarrow> (\<exists>g. linear g \<and> f \<circ> g = id \<and> g \<circ> f = id)"
immler@68072
   852
  for f::"real^'m \<Rightarrow> real^'n"
immler@68072
   853
  using matrix_invertible_gen[of f]
immler@68072
   854
  by (auto simp: linear_matrix_vector_mul_eq)
lp15@67981
   855
ak2110@69720
   856
lemma  invertible_eq_bij:
immler@68072
   857
  fixes m :: "'a::field^'m^'n"
nipkow@69064
   858
  shows "invertible m \<longleftrightarrow> bij ((*v) m)"
immler@68072
   859
  using matrix_invertible_gen[OF matrix_vector_mul_linear_gen, of m, simplified matrix_of_matrix_vector_mul]
immler@68072
   860
  by (metis bij_betw_def left_right_inverse_eq matrix_vector_mul_linear_gen o_bij
immler@68072
   861
      vec.linear_injective_left_inverse vec.linear_surjective_right_inverse)
immler@68072
   862
lp15@67981
   863
immler@69683
   864
subsection \<open>Cramer's rule\<close>
himmelma@33175
   865
ak2110@69720
   866
lemma  cramer_lemma_transpose:
immler@68263
   867
  fixes A:: "'a::{field}^'n^'n"
immler@68263
   868
    and x :: "'a::{field}^'n"
nipkow@64267
   869
  shows "det ((\<chi> i. if i = k then sum (\<lambda>i. x$i *s row i A) (UNIV::'n set)
immler@68263
   870
                             else row i A)::'a::{field}^'n^'n) = x$k * det A"
himmelma@33175
   871
  (is "?lhs = ?rhs")
ak2110@69720
   872
proof -
himmelma@33175
   873
  let ?U = "UNIV :: 'n set"
himmelma@33175
   874
  let ?Uk = "?U - {k}"
wenzelm@53854
   875
  have U: "?U = insert k ?Uk"
wenzelm@53854
   876
    by blast
wenzelm@53854
   877
  have kUk: "k \<notin> ?Uk"
wenzelm@53854
   878
    by simp
himmelma@33175
   879
  have th00: "\<And>k s. x$k *s row k A + s = (x$k - 1) *s row k A + row k A + s"
haftmann@36350
   880
    by (vector field_simps)
wenzelm@53854
   881
  have th001: "\<And>f k . (\<lambda>x. if x = k then f k else f x) = f"
wenzelm@53854
   882
    by auto
himmelma@33175
   883
  have "(\<chi> i. row i A) = A" by (vector row_def)
wenzelm@53253
   884
  then have thd1: "det (\<chi> i. row i A) = det A"
wenzelm@53253
   885
    by simp
himmelma@33175
   886
  have thd0: "det (\<chi> i. if i = k then row k A + (\<Sum>i \<in> ?Uk. x $ i *s row i A) else row i A) = det A"
lp15@68134
   887
    by (force intro: det_row_span vec.span_sum vec.span_scale vec.span_base)
himmelma@33175
   888
  show "?lhs = x$k * det A"
himmelma@33175
   889
    apply (subst U)
lp15@68134
   890
    unfolding sum.insert[OF finite kUk]
himmelma@33175
   891
    apply (subst th00)
haftmann@57512
   892
    unfolding add.assoc
himmelma@33175
   893
    apply (subst det_row_add)
himmelma@33175
   894
    unfolding thd0
himmelma@33175
   895
    unfolding det_row_mul
himmelma@33175
   896
    unfolding th001[of k "\<lambda>i. row i A"]
wenzelm@53253
   897
    unfolding thd1
wenzelm@53253
   898
    apply (simp add: field_simps)
wenzelm@53253
   899
    done
himmelma@33175
   900
qed
himmelma@33175
   901
ak2110@69720
   902
proposition  cramer_lemma:
immler@68263
   903
  fixes A :: "'a::{field}^'n^'n"
immler@68263
   904
  shows "det((\<chi> i j. if j = k then (A *v x)$i else A$i$j):: 'a::{field}^'n^'n) = x$k * det A"
ak2110@69720
   905
proof -
himmelma@33175
   906
  let ?U = "UNIV :: 'n set"
nipkow@64267
   907
  have *: "\<And>c. sum (\<lambda>i. c i *s row i (transpose A)) ?U = sum (\<lambda>i. c i *s column i A) ?U"
immler@68072
   908
    by (auto intro: sum.cong)
wenzelm@53854
   909
  show ?thesis
lp15@67673
   910
    unfolding matrix_mult_sum
wenzelm@53253
   911
    unfolding cramer_lemma_transpose[of k x "transpose A", unfolded det_transpose, symmetric]
wenzelm@53253
   912
    unfolding *[of "\<lambda>i. x$i"]
wenzelm@53253
   913
    apply (subst det_transpose[symmetric])
wenzelm@53253
   914
    apply (rule cong[OF refl[of det]])
wenzelm@53253
   915
    apply (vector transpose_def column_def row_def)
wenzelm@53253
   916
    done
himmelma@33175
   917
qed
himmelma@33175
   918
ak2110@69720
   919
proposition  cramer:
immler@68263
   920
  fixes A ::"'a::{field}^'n^'n"
himmelma@33175
   921
  assumes d0: "det A \<noteq> 0"
huffman@36362
   922
  shows "A *v x = b \<longleftrightarrow> x = (\<chi> k. det(\<chi> i j. if j=k then b$i else A$i$j) / det A)"
ak2110@69720
   923
proof -
himmelma@33175
   924
  from d0 obtain B where B: "A ** B = mat 1" "B ** A = mat 1"
wenzelm@53854
   925
    unfolding invertible_det_nz[symmetric] invertible_def
wenzelm@53854
   926
    by blast
wenzelm@53854
   927
  have "(A ** B) *v b = b"
immler@68072
   928
    by (simp add: B)
wenzelm@53854
   929
  then have "A *v (B *v b) = b"
wenzelm@53854
   930
    by (simp add: matrix_vector_mul_assoc)
wenzelm@53854
   931
  then have xe: "\<exists>x. A *v x = b"
wenzelm@53854
   932
    by blast
wenzelm@53253
   933
  {
wenzelm@53253
   934
    fix x
wenzelm@53253
   935
    assume x: "A *v x = b"
wenzelm@53253
   936
    have "x = (\<chi> k. det(\<chi> i j. if j=k then b$i else A$i$j) / det A)"
wenzelm@53253
   937
      unfolding x[symmetric]
wenzelm@53253
   938
      using d0 by (simp add: vec_eq_iff cramer_lemma field_simps)
wenzelm@53253
   939
  }
wenzelm@53854
   940
  with xe show ?thesis
wenzelm@53854
   941
    by auto
himmelma@33175
   942
qed
himmelma@33175
   943
ak2110@69720
   944
lemma  det_1: "det (A::'a::comm_ring_1^1^1) = A$1$1"
immler@68072
   945
  by (simp add: det_def sign_id)
himmelma@33175
   946
ak2110@69720
   947
lemma  det_2: "det (A::'a::comm_ring_1^2^2) = A$1$1 * A$2$2 - A$1$2 * A$2$1"
wenzelm@53253
   948
proof -
himmelma@33175
   949
  have f12: "finite {2::2}" "1 \<notin> {2::2}" by auto
himmelma@33175
   950
  show ?thesis
wenzelm@53253
   951
    unfolding det_def UNIV_2
nipkow@64267
   952
    unfolding sum_over_permutations_insert[OF f12]
wenzelm@53253
   953
    unfolding permutes_sing
wenzelm@53253
   954
    by (simp add: sign_swap_id sign_id swap_id_eq)
himmelma@33175
   955
qed
himmelma@33175
   956
ak2110@69720
   957
lemma  det_3:
wenzelm@53253
   958
  "det (A::'a::comm_ring_1^3^3) =
wenzelm@53253
   959
    A$1$1 * A$2$2 * A$3$3 +
wenzelm@53253
   960
    A$1$2 * A$2$3 * A$3$1 +
wenzelm@53253
   961
    A$1$3 * A$2$1 * A$3$2 -
wenzelm@53253
   962
    A$1$1 * A$2$3 * A$3$2 -
wenzelm@53253
   963
    A$1$2 * A$2$1 * A$3$3 -
wenzelm@53253
   964
    A$1$3 * A$2$2 * A$3$1"
wenzelm@53253
   965
proof -
wenzelm@53854
   966
  have f123: "finite {2::3, 3}" "1 \<notin> {2::3, 3}"
wenzelm@53854
   967
    by auto
wenzelm@53854
   968
  have f23: "finite {3::3}" "2 \<notin> {3::3}"
wenzelm@53854
   969
    by auto
himmelma@33175
   970
himmelma@33175
   971
  show ?thesis
wenzelm@53253
   972
    unfolding det_def UNIV_3
nipkow@64267
   973
    unfolding sum_over_permutations_insert[OF f123]
nipkow@64267
   974
    unfolding sum_over_permutations_insert[OF f23]
wenzelm@53253
   975
    unfolding permutes_sing
wenzelm@53253
   976
    by (simp add: sign_swap_id permutation_swap_id sign_compose sign_id swap_id_eq)
himmelma@33175
   977
qed
himmelma@33175
   978
ak2110@69720
   979
proposition  det_orthogonal_matrix:
immler@69680
   980
  fixes Q:: "'a::linordered_idom^'n^'n"
immler@69680
   981
  assumes oQ: "orthogonal_matrix Q"
immler@69680
   982
  shows "det Q = 1 \<or> det Q = - 1"
ak2110@69720
   983
proof -
immler@69680
   984
  have "Q ** transpose Q = mat 1"
immler@69680
   985
    by (metis oQ orthogonal_matrix_def)
immler@69680
   986
  then have "det (Q ** transpose Q) = det (mat 1:: 'a^'n^'n)"
immler@69680
   987
    by simp
immler@69680
   988
  then have "det Q * det Q = 1"
immler@69680
   989
    by (simp add: det_mul)
immler@69680
   990
  then show ?thesis
immler@69680
   991
    by (simp add: square_eq_1_iff)
immler@69680
   992
qed
immler@69680
   993
ak2110@69720
   994
proposition  orthogonal_transformation_det [simp]:
immler@69680
   995
  fixes f :: "real^'n \<Rightarrow> real^'n"
immler@69680
   996
  shows "orthogonal_transformation f \<Longrightarrow> \<bar>det (matrix f)\<bar> = 1"
immler@69680
   997
  using%unimportant det_orthogonal_matrix orthogonal_transformation_matrix by fastforce
immler@69680
   998
immler@69683
   999
subsection  \<open>Rotation, reflection, rotoinversion\<close>
immler@69680
  1000
immler@69680
  1001
definition%important  "rotation_matrix Q \<longleftrightarrow> orthogonal_matrix Q \<and> det Q = 1"
immler@69680
  1002
definition%important  "rotoinversion_matrix Q \<longleftrightarrow> orthogonal_matrix Q \<and> det Q = - 1"
immler@69680
  1003
ak2110@69720
  1004
lemma  orthogonal_rotation_or_rotoinversion:
immler@69680
  1005
  fixes Q :: "'a::linordered_idom^'n^'n"
immler@69680
  1006
  shows " orthogonal_matrix Q \<longleftrightarrow> rotation_matrix Q \<or> rotoinversion_matrix Q"
ak2110@69720
  1007
  by (metis rotoinversion_matrix_def rotation_matrix_def det_orthogonal_matrix)
immler@69680
  1008
lp15@68134
  1009
text\<open> Slightly stronger results giving rotation, but only in two or more dimensions\<close>
lp15@67683
  1010
ak2110@69720
  1011
lemma  rotation_matrix_exists_basis:
lp15@67683
  1012
  fixes a :: "real^'n"
lp15@67683
  1013
  assumes 2: "2 \<le> CARD('n)" and "norm a = 1"
lp15@67683
  1014
  obtains A where "rotation_matrix A" "A *v (axis k 1) = a"
lp15@67683
  1015
proof -
lp15@67683
  1016
  obtain A where "orthogonal_matrix A" and A: "A *v (axis k 1) = a"
lp15@67683
  1017
    using orthogonal_matrix_exists_basis assms by metis
lp15@67683
  1018
  with orthogonal_rotation_or_rotoinversion
lp15@67683
  1019
  consider "rotation_matrix A" | "rotoinversion_matrix A"
lp15@67683
  1020
    by metis
lp15@67683
  1021
  then show thesis
lp15@67683
  1022
  proof cases
lp15@67683
  1023
    assume "rotation_matrix A"
lp15@67683
  1024
    then show ?thesis
lp15@67683
  1025
      using \<open>A *v axis k 1 = a\<close> that by auto
lp15@67683
  1026
  next
immler@69680
  1027
    from ex_card[OF 2] obtain h i::'n where "h \<noteq> i"
immler@69680
  1028
      by (auto simp add: eval_nat_numeral card_Suc_eq)
immler@69680
  1029
    then obtain j where "j \<noteq> k"
immler@69680
  1030
      by (metis (full_types))
lp15@67683
  1031
    let ?TA = "transpose A"
lp15@67683
  1032
    let ?A = "\<chi> i. if i = j then - 1 *\<^sub>R (?TA $ i) else ?TA $i"
lp15@67683
  1033
    assume "rotoinversion_matrix A"
lp15@67683
  1034
    then have [simp]: "det A = -1"
lp15@67683
  1035
      by (simp add: rotoinversion_matrix_def)
lp15@67683
  1036
    show ?thesis
lp15@67683
  1037
    proof
lp15@67683
  1038
      have [simp]: "row i (\<chi> i. if i = j then - 1 *\<^sub>R ?TA $ i else ?TA $ i) = (if i = j then - row i ?TA else row i ?TA)" for i
lp15@67683
  1039
        by (auto simp: row_def)
lp15@67683
  1040
      have "orthogonal_matrix ?A"
lp15@67683
  1041
        unfolding orthogonal_matrix_orthonormal_rows
lp15@67683
  1042
        using \<open>orthogonal_matrix A\<close> by (auto simp: orthogonal_matrix_orthonormal_columns orthogonal_clauses)
lp15@67683
  1043
      then show "rotation_matrix (transpose ?A)"
lp15@67683
  1044
        unfolding rotation_matrix_def
lp15@67683
  1045
        by (simp add: det_row_mul[of j _ "\<lambda>i. ?TA $ i", unfolded scalar_mult_eq_scaleR])
lp15@67683
  1046
      show "transpose ?A *v axis k 1 = a"
lp15@67683
  1047
        using \<open>j \<noteq> k\<close> A by (simp add: matrix_vector_column axis_def scalar_mult_eq_scaleR if_distrib [of "\<lambda>z. z *\<^sub>R c" for c] cong: if_cong)
lp15@67683
  1048
    qed
lp15@67683
  1049
  qed
lp15@67683
  1050
qed
lp15@67683
  1051
ak2110@69720
  1052
lemma  rotation_exists_1:
lp15@67683
  1053
  fixes a :: "real^'n"
lp15@67683
  1054
  assumes "2 \<le> CARD('n)" "norm a = 1" "norm b = 1"
lp15@67683
  1055
  obtains f where "orthogonal_transformation f" "det(matrix f) = 1" "f a = b"
lp15@67683
  1056
proof -
lp15@67683
  1057
  obtain k::'n where True
lp15@67683
  1058
    by simp
lp15@67683
  1059
  obtain A B where AB: "rotation_matrix A" "rotation_matrix B"
lp15@67683
  1060
               and eq: "A *v (axis k 1) = a" "B *v (axis k 1) = b"
lp15@67683
  1061
    using rotation_matrix_exists_basis assms by metis
lp15@67683
  1062
  let ?f = "\<lambda>x. (B ** transpose A) *v x"
lp15@67683
  1063
  show thesis
lp15@67683
  1064
  proof
lp15@67683
  1065
    show "orthogonal_transformation ?f"
lp15@67683
  1066
      using AB orthogonal_matrix_mul orthogonal_transformation_matrix rotation_matrix_def matrix_vector_mul_linear by force
lp15@67683
  1067
    show "det (matrix ?f) = 1"
lp15@67683
  1068
      using AB by (auto simp: det_mul rotation_matrix_def)
lp15@67683
  1069
    show "?f a = b"
lp15@67683
  1070
      using AB unfolding orthogonal_matrix_def rotation_matrix_def
lp15@67683
  1071
      by (metis eq matrix_mul_rid matrix_vector_mul_assoc)
lp15@67683
  1072
  qed
lp15@67683
  1073
qed
lp15@67683
  1074
ak2110@69720
  1075
lemma  rotation_exists:
lp15@67683
  1076
  fixes a :: "real^'n"
lp15@67683
  1077
  assumes 2: "2 \<le> CARD('n)" and eq: "norm a = norm b"
lp15@67683
  1078
  obtains f where "orthogonal_transformation f" "det(matrix f) = 1" "f a = b"
lp15@67683
  1079
proof (cases "a = 0 \<or> b = 0")
lp15@67683
  1080
  case True
lp15@67683
  1081
  with assms have "a = 0" "b = 0"
lp15@67683
  1082
    by auto
lp15@67683
  1083
  then show ?thesis
lp15@67683
  1084
    by (metis eq_id_iff matrix_id orthogonal_transformation_id that)
lp15@67683
  1085
next
lp15@67683
  1086
  case False
immler@68072
  1087
  then obtain f where f: "orthogonal_transformation f" "det (matrix f) = 1"
immler@68072
  1088
    and f': "f (a /\<^sub>R norm a) = b /\<^sub>R norm b"
immler@68072
  1089
    using rotation_exists_1 [of "a /\<^sub>R norm a" "b /\<^sub>R norm b", OF 2] by auto
immler@68072
  1090
  then interpret linear f by (simp add: orthogonal_transformation)
immler@68072
  1091
  have "f a = b"
immler@68072
  1092
    using f' False
immler@68072
  1093
    by (simp add: eq scale)
immler@68072
  1094
  with f show thesis ..
lp15@67683
  1095
qed
lp15@67683
  1096
ak2110@69720
  1097
lemma  rotation_rightward_line:
lp15@67683
  1098
  fixes a :: "real^'n"
lp15@67683
  1099
  obtains f where "orthogonal_transformation f" "2 \<le> CARD('n) \<Longrightarrow> det(matrix f) = 1"
lp15@67683
  1100
                  "f(norm a *\<^sub>R axis k 1) = a"
lp15@67683
  1101
proof (cases "CARD('n) = 1")
lp15@67683
  1102
  case True
lp15@67683
  1103
  obtain f where "orthogonal_transformation f" "f (norm a *\<^sub>R axis k (1::real)) = a"
lp15@67683
  1104
  proof (rule orthogonal_transformation_exists)
lp15@67683
  1105
    show "norm (norm a *\<^sub>R axis k (1::real)) = norm a"
lp15@67683
  1106
      by simp
lp15@67683
  1107
  qed auto
lp15@67683
  1108
  then show thesis
lp15@67683
  1109
    using True that by auto
lp15@67683
  1110
next
lp15@67683
  1111
  case False
lp15@67683
  1112
  obtain f where "orthogonal_transformation f" "det(matrix f) = 1" "f (norm a *\<^sub>R axis k 1) = a"
lp15@67683
  1113
  proof (rule rotation_exists)
lp15@67683
  1114
    show "2 \<le> CARD('n)"
lp15@67683
  1115
      using False one_le_card_finite [where 'a='n] by linarith
lp15@67683
  1116
    show "norm (norm a *\<^sub>R axis k (1::real)) = norm a"
lp15@67683
  1117
      by simp
lp15@67683
  1118
  qed auto
lp15@67683
  1119
  then show thesis
lp15@67683
  1120
    using that by blast
lp15@67683
  1121
qed
lp15@67683
  1122
himmelma@33175
  1123
end