Skip to main content
made grammatical and structural changes
Source Link

Applying How to apply a function to successive overlapping list elements?

I have a list of elements and I would like to create a new list by applying a function to successive overlapping pairs from the original list.

Specifically, I am trying to compute the Pythagorean sum of each pair. So, if the original list is {$a,b,c,d,...$} I want {$\sqrt{a^2+b^2},\sqrt{b^2+c^2},\sqrt{c^2+d^2},...$}

Since this is similar to the built-in Differences function, (which turns the original list into {$b-a,c-b,d-c,...$}), I expected to find a built-in function along the lines of BuiltIn[f,{a,b,c,d,...},options] where f is defined by the user. But alas, my search has been fruitless...

Thanks in advance!

-----------EDIT-----------

Right after posting this, I came up with

Sqrt[#1^2 + #2^2] & @@@ Subsequences[#, {2}] &@{a, b, c, d}

which does the trick. I was going to update that here, but you all already came through with your own answers, so I'll try those as well. Thanks!

Applying a function to successive overlapping list elements?

I have a list of elements and I would like to create a new list by applying a function to successive overlapping pairs from the original list.

Specifically, I am trying compute the Pythagorean sum of each pair. So, if the original list is {$a,b,c,d,...$} I want {$\sqrt{a^2+b^2},\sqrt{b^2+c^2},\sqrt{c^2+d^2},...$}

Since this is similar to the built-in Differences function, (which turns the original list into {$b-a,c-b,d-c,...$}), I expected to find a built-in function along the lines of BuiltIn[f,{a,b,c,d,...},options] where f is defined by the user. But alas, my search has been fruitless...

Thanks in advance!

-----------EDIT-----------

Right after posting this, I came up with

Sqrt[#1^2 + #2^2] & @@@ Subsequences[#, {2}] &@{a, b, c, d}

which does the trick. I was going to update that here, but you all already came through with your own answers, so I'll try those as well. Thanks!

How to apply a function to successive overlapping list elements?

I have a list of elements and I would like to create a new list by applying a function to successive overlapping pairs from the original list.

Specifically, I am trying to compute the Pythagorean sum of each pair. So, if the original list is {$a,b,c,d,...$} I want {$\sqrt{a^2+b^2},\sqrt{b^2+c^2},\sqrt{c^2+d^2},...$}

Since this is similar to the built-in Differences function, (which turns the original list into {$b-a,c-b,d-c,...$}), I expected to find a built-in function along the lines of BuiltIn[f,{a,b,c,d,...},options] where f is defined by the user. But alas, my search has been fruitless...

Thanks in advance!

-----------EDIT-----------

Right after posting this, I came up with

Sqrt[#1^2 + #2^2] & @@@ Subsequences[#, {2}] &@{a, b, c, d}

which does the trick. I was going to update that here, but you all already came through with your answers, so I'll try those as well. Thanks!

Tweeted twitter.com/StackMma/status/1253247289394696198
added my own solution
Source Link
Aaron Eiben
  • 541
  • 2
  • 11

I have a list of elements and I would like to create a new list by applying a function to successive overlapping pairs from the original list.

Specifically, I am trying compute the Pythagorean sum of each pair. So, if the original list is {$a,b,c,d,...$} I want {$\sqrt{a^2+b^2},\sqrt{b^2+c^2},\sqrt{c^2+d^2},...$}

Since this is similar to the built-in Differences function, (which turns the original list into {$b-a,c-b,d-c,...$}), I expected to find a built-in function along the lines of BuiltIn[f,{a,b,c,d,...},options] where f is defined by the user. But alas, my search has been fruitless...

Thanks in advance!

-----------EDIT-----------

Right after posting this, I came up with

Sqrt[#1^2 + #2^2] & @@@ Subsequences[#, {2}] &@{a, b, c, d}

which does the trick. I was going to update that here, but you all already came through with your own answers, so I'll try those as well. Thanks!

I have a list of elements and I would like to create a new list by applying a function to successive overlapping pairs from the original list.

Specifically, I am trying compute the Pythagorean sum of each pair. So, if the original list is {$a,b,c,d,...$} I want {$\sqrt{a^2+b^2},\sqrt{b^2+c^2},\sqrt{c^2+d^2},...$}

Since this is similar to the built-in Differences function, (which turns the original list into {$b-a,c-b,d-c,...$}), I expected to find a built-in function along the lines of BuiltIn[f,{a,b,c,d,...},options] where f is defined by the user. But alas, my search has been fruitless...

Thanks in advance!

I have a list of elements and I would like to create a new list by applying a function to successive overlapping pairs from the original list.

Specifically, I am trying compute the Pythagorean sum of each pair. So, if the original list is {$a,b,c,d,...$} I want {$\sqrt{a^2+b^2},\sqrt{b^2+c^2},\sqrt{c^2+d^2},...$}

Since this is similar to the built-in Differences function, (which turns the original list into {$b-a,c-b,d-c,...$}), I expected to find a built-in function along the lines of BuiltIn[f,{a,b,c,d,...},options] where f is defined by the user. But alas, my search has been fruitless...

Thanks in advance!

-----------EDIT-----------

Right after posting this, I came up with

Sqrt[#1^2 + #2^2] & @@@ Subsequences[#, {2}] &@{a, b, c, d}

which does the trick. I was going to update that here, but you all already came through with your own answers, so I'll try those as well. Thanks!

Source Link
Aaron Eiben
  • 541
  • 2
  • 11

Applying a function to successive overlapping list elements?

I have a list of elements and I would like to create a new list by applying a function to successive overlapping pairs from the original list.

Specifically, I am trying compute the Pythagorean sum of each pair. So, if the original list is {$a,b,c,d,...$} I want {$\sqrt{a^2+b^2},\sqrt{b^2+c^2},\sqrt{c^2+d^2},...$}

Since this is similar to the built-in Differences function, (which turns the original list into {$b-a,c-b,d-c,...$}), I expected to find a built-in function along the lines of BuiltIn[f,{a,b,c,d,...},options] where f is defined by the user. But alas, my search has been fruitless...

Thanks in advance!