Stream: General

Topic: summation swaps


view this post on Zulip Craig Alan Feinstein (Sep 03 2024 at 02:43):

The following proof is fine until the last line, which uses the same lemma as the line before sum.swap, except it fails. Is this because the lemma only holds when the summation symbols are first?

lemma brc_equation:
  assumes "x ∈ rat mat 𝗏 1"
  shows "(βˆ‘i ∈ {0..<𝗏}. (L_of x $$ (i,0))^2) =
     of_int Ξ› * (βˆ‘i ∈ {0..<𝗏}.(x $$ (i, 0)))^2 +
     of_int (𝗋 - Ξ›) * (βˆ‘i ∈ {0..<𝗏}. (x $$ (i, 0))^2)"
proof -
  have "(βˆ‘i ∈ {0..<𝗏}. (L_of x $$ (i,0))^2) =
    (βˆ‘i ∈ {0..<𝗏}.(βˆ‘h ∈ {0..<𝗏}. of_int (N $$ (h,i)) * x $$ (h,0))^2)"
    by auto
  then have "... = (βˆ‘i ∈ {0..<𝗏}. (βˆ‘j ∈ {0..<𝗏}. of_int (N $$ (j, i)) *
    x $$ (j, 0)) * (βˆ‘h ∈ {0..<𝗏}. of_int (N $$ (h, i)) * x $$ (h, 0)))"
    by (simp add: power2_eq_square)
  then have "... = (βˆ‘i ∈ {0..<𝗏}. (βˆ‘j ∈ {0..<𝗏}. (βˆ‘h ∈ {0..<𝗏}.
    (of_int (N $$ (j,i)) * x $$ (j,0)) * (of_int (N $$ (h,i)) * x $$ (h,0)))))"
    by (metis (no_types) sum_product)
  then have "... = (βˆ‘j ∈ {0..<𝗏}. (βˆ‘i ∈ {0..<𝗏}. (βˆ‘h ∈ {0..<𝗏}.
    (of_int (N $$ (j,i)) * x $$ (j,0)) * (of_int (N $$ (h,i)) * x $$ (h,0)))))"
    using sum.swap by simp
 then have "... = (βˆ‘j ∈ {0..<𝗏}. (βˆ‘h ∈ {0..<𝗏}. (βˆ‘i ∈ {0..<𝗏}.
    (of_int (N $$ (j,i)) * x $$ (j,0)) * (of_int (N $$ (h,i)) * x $$ (h,0)))))"
    using sum.swap by simp

view this post on Zulip Mathias Fleury (Sep 03 2024 at 04:53):

  1. Your expression is correct, but simp seems not to be able to figure out how to apply sum.swap

view this post on Zulip Mathias Fleury (Sep 03 2024 at 04:54):

  1. Your isar structure is meaningless

view this post on Zulip Mathias Fleury (Sep 03 2024 at 04:56):

You want also have/also have/finally have

view this post on Zulip Mathias Fleury (Sep 03 2024 at 04:56):

With this using it works:

using sum.swap[of β€ΉΞ»h i. (of_int (N $$ (j,i)) * x $$ (j,0)) * (of_int (N $$ (h,i)) * x $$ (h,0))β€Ί for j]

view this post on Zulip Craig Alan Feinstein (Sep 03 2024 at 05:00):

You mean I should use β€œalso have” instead of β€œthen have”?

view this post on Zulip Craig Alan Feinstein (Sep 03 2024 at 05:04):

I noticed in the Fisher inequality library Matrix_Vector_Extras, there is a lemma that is used sum_reorder_triple, that might also work.

view this post on Zulip Chelsea Edmonds (Sep 03 2024 at 11:16):

Craig Alan Feinstein said:

I noticed in the Fisher inequality library Matrix_Vector_Extras, there is a lemma that is used sum_reorder_triple, that might also work.

Summations are definitely something which feels like it should be easy to automate, but in reality often isn't (a problem shared across proof assistants as well). I think it could be an interesting student project to investigate at some point. Instantiating directly the parameters of lemmas (like what Mathias did above) is typically the best strategy for now. The sum_reorder_triple lemma in my library could definitely help depending on what you have to do - both as examples for how to do proofs with summations and potentially in this case (with incidence matrices, such triples are not uncommon).

view this post on Zulip Craig Alan Feinstein (Sep 03 2024 at 21:33):

I just got sum_reorder_triple to work.

view this post on Zulip Craig Alan Feinstein (Sep 04 2024 at 03:57):

Moving around parentheses and taking out expressions to outer parentheses within multiple summations also seems to be difficult in Isabelle.

view this post on Zulip Craig Alan Feinstein (Sep 04 2024 at 03:59):

Every time I solve a hard problem that I didn’t think would be hard, a new hard problem that I didn’t think would be hard comes up.

view this post on Zulip Craig Alan Feinstein (Sep 06 2024 at 01:15):

Instantiation is a new trick for me that I’ll add to my toolbox.

view this post on Zulip Manuel Eberl (Sep 06 2024 at 10:20):

I normally use sum.swap with very explicit proof methods like rule or subst instead.

view this post on Zulip Manuel Eberl (Sep 06 2024 at 10:22):

Moving parentheses and pulling things into and out of (finite) sums is usually easy using simp add: algebra_simps sum_distrib_left sum_distrib_right

view this post on Zulip Manuel Eberl (Sep 06 2024 at 10:22):

While we're at it, if you ever struggle with reindexing sums, sum.reindex_bij_witness is the secret weapon.

view this post on Zulip Craig Alan Feinstein (Sep 06 2024 at 15:43):

@Manuel Eberl I just tried by (simp add: algebra_simps sum_distrib_left sum_distrib_right) and that solved the problem. Thank you.

view this post on Zulip Craig Alan Feinstein (Sep 08 2024 at 17:14):

Now I have the following in which I need to use substitution of matdef into the last expression.

lemma brc_x_equation:
  assumes "x ∈ rat mat 𝗏 1" "h < 𝗏" "j < 𝗏"
  shows "(βˆ‘i ∈ {0..<𝗏}. (L_of x $$ (i,0))^2) =
     of_int Ξ› * (βˆ‘i ∈ {0..<𝗏}.(x $$ (i, 0)))^2 +
     of_int (𝗄 - Ξ›) * (βˆ‘i ∈ {0..<𝗏}. (x $$ (i, 0))^2)"
proof -
  let ?N_rat = "map_mat (of_int) N"
  have matdef: "(?N_rat * ?N_rat⇧T) $$ (j, h) = (βˆ‘i ∈{0..<𝗏} . ?N_rat $$ (j, i) * ?N_rat $$ (h, i))"
    using assms(2) assms(3) transpose_mat_mult_entries[of "j" "?N_rat" "h"]
    local.symmetric by fastforce

  have "(βˆ‘i ∈ {0..<𝗏}. (L_of x $$ (i,0))^2) =
    (βˆ‘i ∈ {0..<𝗏}.(βˆ‘h ∈ {0..<𝗏}. (?N_rat $$ (h,i)) * x $$ (h,0))^2)"
    by auto
  also have "... = (βˆ‘i ∈ {0..<𝗏}. (βˆ‘j ∈ {0..<𝗏}. (?N_rat $$ (j, i)) *
    x $$ (j, 0)) * (βˆ‘h ∈ {0..<𝗏}. (?N_rat $$ (h, i)) * x $$ (h, 0)))"
    by (simp add: power2_eq_square)
  also have "... = (βˆ‘i ∈ {0..<𝗏}. (βˆ‘j ∈ {0..<𝗏}. (βˆ‘h ∈ {0..<𝗏}.
    (?N_rat $$ (j,i) * x $$ (j,0)) * (?N_rat $$ (h,i) * x $$ (h,0)))))"
    by (metis (no_types) sum_product)
  also have "... = (βˆ‘i ∈ {0..<𝗏}. (βˆ‘j ∈ {0..<𝗏}. (βˆ‘h ∈ {0..<𝗏}.
    ?N_rat $$ (j,i) * ?N_rat $$ (h,i) * x $$ (j,0) * x $$ (h,0))))"
    by (simp add: algebra_simps)
  also have "... = (βˆ‘j ∈ {0..<𝗏}. (βˆ‘h ∈ {0..<𝗏}. (βˆ‘i ∈ {0..<𝗏}.
    ?N_rat $$ (j,i) * ?N_rat $$ (h,i) * x $$ (j,0) * x $$ (h,0))))"
    using sum_reorder_triple[of "Ξ» i j h . ?N_rat $$ (j,i) *
      ?N_rat $$ (h,i) * x $$ (j,0) * x $$ (h,0)"
        "{0..<𝗏}" "{0..<𝗏}" "{0..<𝗏}"] by simp
  also have "... = (βˆ‘j ∈ {0..<𝗏}. (βˆ‘h ∈ {0..<𝗏}. (βˆ‘i ∈ {0..<𝗏}.
    ?N_rat $$ (j,i) * ?N_rat $$ (h,i)) * x $$ (j,0) * x $$ (h,0)))"
    by (simp add: algebra_simps sum_distrib_left sum_distrib_right)
  also have "... = (βˆ‘j ∈ {0..<𝗏}. (βˆ‘h ∈ {0..<𝗏}.
  (?N_rat * ?N_rat⇧T) $$ (j, h) * x $$ (j,0) * x $$ (h,0)))"

view this post on Zulip Mathias Fleury (Sep 08 2024 at 17:20):

Shouldn't you forall quantify j and h in matdef?

view this post on Zulip Mathias Fleury (Sep 08 2024 at 17:21):

(or for j h after the have)

view this post on Zulip Mathias Fleury (Sep 08 2024 at 17:22):

otherwise the proof is for a fixed j and a fixed h that have nothing to do with the j/h from the sum in the last step

view this post on Zulip Craig Alan Feinstein (Sep 08 2024 at 17:55):

when I do the following it cannot find a proof with try0:

  have matdef: "βˆ€ h j. (?N_rat * ?N_rat⇧T) $$ (j, h) = (βˆ‘i ∈{0..<𝗏} . ?N_rat $$ (j, i) * ?N_rat $$ (h, i))"
    using assms(2) assms(3) transpose_mat_mult_entries[of "j" "?N_rat" "h"]
    local.symmetric by try0

view this post on Zulip Mathias Fleury (Sep 08 2024 at 17:59):

You probably want to add assms(2,3) to talk about the new h and j…

view this post on Zulip Craig Alan Feinstein (Sep 08 2024 at 18:06):

Do you mean this? assms(2) and assms(3) are already used.

  have matdef: "βˆ€ h j. (?N_rat * ?N_rat⇧T) $$ (j, h) = (βˆ‘i ∈{0..<𝗏} . ?N_rat $$ (j, i) * ?N_rat $$ (h, i))"
    using transpose_mat_mult_entries[of "j" "?N_rat" "h"]
    local.symmetric by (simp add: assms(2,3))

view this post on Zulip Mathias Fleury (Sep 08 2024 at 18:41):

assms(2,3) are "h < 𝗏" "j < 𝗏"

view this post on Zulip Mathias Fleury (Sep 08 2024 at 18:41):

they talk about a different h j than the one from βˆ€ h j. ...

view this post on Zulip Mathias Fleury (Sep 08 2024 at 18:42):

So if you compare the expression before (which was working) to the one now, you have removed assumptions

view this post on Zulip Mathias Fleury (Sep 08 2024 at 18:43):

so my intuition as someone who has no clue what you are doing: you need to pass the assumptions too here

view this post on Zulip Mathias Fleury (Sep 08 2024 at 18:43):

or maybe it is the fact that transpose_mat_mult_entries[of "j" "?N_rat" "h"] does refer to the wrong j and h too

view this post on Zulip Mathias Fleury (Sep 08 2024 at 18:45):

My intuition is that this:

  have matdef: "(?N_rat * ?N_rat⇧T) $$ (j, h) = (βˆ‘i ∈{0..<𝗏} . ?N_rat $$ (j, i) * ?N_rat $$ (h, i))" if "h < 𝗏" "j < 𝗏" for h j
    using transpose_mat_mult_entries[of "j" "?N_rat" "h"]
    local.symmetric that by fastforce

should work

view this post on Zulip Mathias Fleury (Sep 08 2024 at 18:46):

and be what you want (modulo the fact that unfolding will still not work as you are going to need some congruence stuff)

view this post on Zulip Craig Alan Feinstein (Sep 08 2024 at 20:11):

Yes, that worked, thank you. Just need to get the last substitution part to work now. After that, if I use the fact that N*N^t is a matrix with r on the diagonal and Lambda on the off diagonal, the proof of the lemma will be finished.

view this post on Zulip Craig Alan Feinstein (Sep 08 2024 at 20:26):

The rest of the proof of the BRC is even harder than this part. I haven't been really working on this part so much in the last week.

view this post on Zulip Craig Alan Feinstein (Sep 09 2024 at 01:18):

I have learned from two months plus of working with Isabelle that sometimes it's not a good idea to work to hard to prove a certain point. There might be a way to avoid trying to prove that point and do things a different way. Perhaps I don't even have to even work with (?N_rat * ?N_rat⇧T) $$ (j, h). It all depends on what's in the Fisher's Inequality library, which I will go through again.

view this post on Zulip Mathias Fleury (Sep 09 2024 at 04:17):

The issue here is that you need information about the sum to the replacement

view this post on Zulip Mathias Fleury (Sep 09 2024 at 04:19):

So you need to use the congruence lemma sum.cong to change the sum internally. Or you directly follow @Manuel Eberl advice:

Manuel Eberl said:

While we're at it, if you ever struggle with reindexing sums, sum.reindex_bij_witness is the secret weapon.

Because sum.reindex_bij_witness does congruence + reindexing but you do not have to reindex

view this post on Zulip Mathias Fleury (Sep 09 2024 at 04:21):

Craig Alan Feinstein said:

I have learned from two months plus of working with Isabelle that sometimes it's not a good idea to work to hard to prove a certain point. There might be a way to avoid trying to prove that point and do things a different way. Perhaps I don't even have to even work with (?N_rat * ?N_rat⇧T) $$ (j, h). It all depends on what's in the Fisher's Inequality library, which I will go through again.

it is always hard to say, but after less than 2 months, I would really try to get the proof through to understand how reindexing work, even if you end up deleting the proofs. Reindexing/applying congruence is one of the most critical tools for working with sums

view this post on Zulip Craig Alan Feinstein (Sep 09 2024 at 16:06):

Thank you, I looked up sum.cong and found the Kummer congruence library uses it. It also has more examples of dealing with summations so I’ll study it.

view this post on Zulip Craig Alan Feinstein (Sep 10 2024 at 12:01):

Also found some good stuff with summation in the Gauss sums library


Last updated: Dec 21 2024 at 12:33 UTC