Zulip Chat Archive

Stream: general

Topic: Sorting and applying


Yaël Dillies (Jun 27 2021 at 14:00):

I have n naturals a₁, ..., aₙ(not necessarily distinct) and I want to define b₁/1 + ... + bₙ/n where the bs are the as in ascending order. What's the best way to do that? I don't have strong restrictions on the representation of a₁, ..., aₙ, so it can be list ℕ, vector ℕ n, fin n → ℕ...

Kevin Buzzard (Jun 27 2021 at 14:03):

I'm pretty sure there's something like docs#list.sort

Kevin Buzzard (Jun 27 2021 at 14:04):

Dammit the community website search doesn't work on my phone

Adam Topaz (Jun 27 2021 at 14:08):

I think it's docs#list.sorted ?

Adam Topaz (Jun 27 2021 at 14:09):

Oh no that's the prop. I guess docs#list.merge_sort

Adam Topaz (Jun 27 2021 at 14:10):

Do we have docs#list.quick_sort ?

Adam Topaz (Jun 27 2021 at 14:11):

Or docs#list.insertion_sort

Kevin Buzzard (Jun 27 2021 at 14:13):

They're dupes

Kevin Buzzard (Jun 27 2021 at 14:13):

Haven't you heard of functional extensionality? ;-)

Yakov Pechersky (Jun 27 2021 at 14:24):

docs#finset.sort

Yakov Pechersky (Jun 27 2021 at 14:24):

or, since you have not-necessarily distinct nats, docs#multiset.sort

Yaël Dillies (Jun 27 2021 at 15:15):

Yeah, or docs#list.merge_sort, but how do I then divide by a different number everytime?

Yakov Pechersky (Jun 27 2021 at 15:18):

after you sort, list.zip_with (/) (sorted_list) ((list.range _).map _)

Yaël Dillies (Jun 27 2021 at 15:26):

Is list.zip_with the same as list.map₂?

Yaël Dillies (Jun 27 2021 at 15:28):

Here's what I tried.

def energy (pos : vector  n) :  :=
list.sum (list.map₂ (λ a b, b/a) (list.range n) (list.merge_sort () pos.1))

but I wonder whether it will unfold nicely.

Yaël Dillies (Jun 27 2021 at 15:38):

And also, how can I define a constant vector?


Last updated: Dec 20 2023 at 11:08 UTC