Zulip Chat Archive
Stream: new members
Topic: finite sum
Junjie Bai (Mar 13 2024 at 11:09):
How can I write the finite sum from 0 to some integer i?
Johan Commelin (Mar 13 2024 at 12:37):
docs#Finset.sum and docs#Finset.range
Kevin Buzzard (Mar 14 2024 at 00:53):
and docs#Nat.succ :-) (because range i
is )
Junjie Bai (Mar 14 2024 at 03:51):
That helps! but I get a new question: I want to sum some function from \Z to \N, but Finset.range return the type of Finset \N, how can I solve this?
Xiyu Zhai (Mar 14 2024 at 05:22):
Kyle Miller (Mar 14 2024 at 05:28):
There's no problem with Finset \N
, since Lean inserts coercions.
open BigOperators
example (f : ℤ → ℕ) (i : ℕ) := ∑ x in Finset.range (i + 1), f x
-- Equivalent:
example (f : ℤ → ℕ) (i : ℕ) := (Finset.range (i + 1)).sum (fun x => f x)
Kyle Miller (Mar 14 2024 at 05:28):
This is nice too:
example (f : ℤ → ℕ) (i : ℕ) := ∑ x in Finset.Icc 0 i, f x
(Icc is "interval closed to closed")
Kyle Miller (Mar 14 2024 at 05:30):
Or you can use an "Iverson bracket" (indicator function) approach using an infinite sum:
∑' x, (if 0 ≤ x ∧ x ≤ i then 1 else 0) * f x
White Chen (Mar 14 2024 at 05:48):
Maybe this helps: https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there-code-for-X.3F/topic/how.20to.20spell.20tsum.20in.20a.20set.3F
Junjie Bai (Mar 14 2024 at 05:59):
That helps a lot! Thank you!
Last updated: May 02 2025 at 03:31 UTC