Zulip Chat Archive

Stream: new members

Topic: Simple polynomial computations


Ahmad Alkhalawi (Sep 25 2024 at 05:46):

I was messing around with polynomials and I couldn't figure out how to do some simple computations. For example, I couldn't figure out how to show

example: coeff 1 2 = 0 := by
    sorry

and I felt like the proof I found for

example: leadingCoeff (2:Polynomial ) = 2 := by
  unfold leadingCoeff
  have h: natDegree (2:Polynomial ) = 0 := by
    refine natDegree_eq_zero.mpr ?_
    use 2
    simp
  rw[h]
  simp

should be easier. Is there a simpler way to do this?

Moritz Firsching (Sep 25 2024 at 06:00):

open Polynomial

example : leadingCoeff (2 : Polynomial ) = 2 := by
  exact leadingCoeff_C 2

seems to work

Moritz Firsching (Sep 25 2024 at 06:02):

Instead of writing (2 : Polynomial ℤ) you could also write Polynomial.C 2 (or just C 2 since Polynomial is already open)

Moritz Firsching (Sep 25 2024 at 06:04):

the first one can be done with

example: coeff 1 2 = 0 := by
  simp [coeff_one]

Luigi Massacci (Sep 25 2024 at 06:26):

We have a tactic for that:

import Mathlib
open Polynomial

example: coeff 1 2 = 0 := by
  compute_degree

example: leadingCoeff (2:Polynomial ) = 2 := by
  rw [leadingCoeff]
  compute_degree!
  compute_degree!

Luigi Massacci (Sep 25 2024 at 06:28):

Technically leadingCoeff is not explicitly in scope, but it seems like it should be

Luigi Massacci (Sep 25 2024 at 06:42):

Let us see what @Damiano Testa thinks of it

Damiano Testa (Sep 25 2024 at 08:45):

I agree on all counts: leadingCoeff is not in scope for compute_degree. The related tactic monicity is a simple macro that preprocesses a Monic goal. Probably there could be an easy win with leadingCoeff goals, but I have not gotten around to it.

Damiano Testa (Sep 25 2024 at 08:45):

I suspect that a more systematic approach would be to have an internal representation of polynomials as lists of coefficients and outsource all such computations on polynomials to analogous computations on lists.

Damiano Testa (Sep 25 2024 at 08:46):

This however has been in the air for many years, but no one has really had the time to go through with it.

Damiano Testa (Sep 25 2024 at 08:46):

There are some promising examples scattered on Zulip, though.

Ahmad Alkhalawi (Sep 25 2024 at 10:16):

Moritz Firsching said:

open Polynomial

example : leadingCoeff (2 : Polynomial ) = 2 := by
  exact leadingCoeff_C 2

seems to work

Luigi Massacci said:

We have a tactic for that:

import Mathlib
open Polynomial

example: coeff 1 2 = 0 := by
  compute_degree

example: leadingCoeff (2:Polynomial ) = 2 := by
  rw [leadingCoeff]
  compute_degree!
  compute_degree!

This works, thanks for the help


Last updated: May 02 2025 at 03:31 UTC