Alternating Total
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
Any cleaner solutions than what I came up with? (Maybe by multiplying every other entry by -1)
altTotal[x_] := Total@x[[;; ;; 2]] - Total@x[[2 ;; ;; 2]]
list-manipulation
add a comment |Â
up vote
3
down vote
favorite
Any cleaner solutions than what I came up with? (Maybe by multiplying every other entry by -1)
altTotal[x_] := Total@x[[;; ;; 2]] - Total@x[[2 ;; ;; 2]]
list-manipulation
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Any cleaner solutions than what I came up with? (Maybe by multiplying every other entry by -1)
altTotal[x_] := Total@x[[;; ;; 2]] - Total@x[[2 ;; ;; 2]]
list-manipulation
Any cleaner solutions than what I came up with? (Maybe by multiplying every other entry by -1)
altTotal[x_] := Total@x[[;; ;; 2]] - Total@x[[2 ;; ;; 2]]
list-manipulation
asked Aug 21 at 21:43
qwr
1878
1878
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
3
down vote
Maybe
altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]
add a comment |Â
up vote
3
down vote
altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]
is a bit faster.
That is a very nice example for floating point arithmetic being significantly faster than memory operations.
– Henrik Schumacher
Aug 22 at 9:47
add a comment |Â
up vote
2
down vote
Here's a way to take + and - each successive member using Riffle:
altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]
add a comment |Â
up vote
2
down vote
altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]
Edit: altTotal4
is quite surprisingly slow. The following Fold
is much better but still not competititve.
altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]
That's slower by more than two orders of magnitude thanaltTotal3
.
– Henrik Schumacher
Aug 22 at 9:44
add a comment |Â
up vote
0
down vote
"Maybe" goes as below:
altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &
Or
altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Maybe
altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]
add a comment |Â
up vote
3
down vote
Maybe
altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Maybe
altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]
Maybe
altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]
answered Aug 21 at 22:01


Henrik Schumacher
36.2k249102
36.2k249102
add a comment |Â
add a comment |Â
up vote
3
down vote
altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]
is a bit faster.
That is a very nice example for floating point arithmetic being significantly faster than memory operations.
– Henrik Schumacher
Aug 22 at 9:47
add a comment |Â
up vote
3
down vote
altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]
is a bit faster.
That is a very nice example for floating point arithmetic being significantly faster than memory operations.
– Henrik Schumacher
Aug 22 at 9:47
add a comment |Â
up vote
3
down vote
up vote
3
down vote
altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]
is a bit faster.
altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]
is a bit faster.
answered Aug 21 at 22:10
JimB
15k12456
15k12456
That is a very nice example for floating point arithmetic being significantly faster than memory operations.
– Henrik Schumacher
Aug 22 at 9:47
add a comment |Â
That is a very nice example for floating point arithmetic being significantly faster than memory operations.
– Henrik Schumacher
Aug 22 at 9:47
That is a very nice example for floating point arithmetic being significantly faster than memory operations.
– Henrik Schumacher
Aug 22 at 9:47
That is a very nice example for floating point arithmetic being significantly faster than memory operations.
– Henrik Schumacher
Aug 22 at 9:47
add a comment |Â
up vote
2
down vote
Here's a way to take + and - each successive member using Riffle:
altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]
add a comment |Â
up vote
2
down vote
Here's a way to take + and - each successive member using Riffle:
altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Here's a way to take + and - each successive member using Riffle:
altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]
Here's a way to take + and - each successive member using Riffle:
altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]
answered Aug 22 at 3:48


bill s
50.6k373142
50.6k373142
add a comment |Â
add a comment |Â
up vote
2
down vote
altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]
Edit: altTotal4
is quite surprisingly slow. The following Fold
is much better but still not competititve.
altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]
That's slower by more than two orders of magnitude thanaltTotal3
.
– Henrik Schumacher
Aug 22 at 9:44
add a comment |Â
up vote
2
down vote
altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]
Edit: altTotal4
is quite surprisingly slow. The following Fold
is much better but still not competititve.
altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]
That's slower by more than two orders of magnitude thanaltTotal3
.
– Henrik Schumacher
Aug 22 at 9:44
add a comment |Â
up vote
2
down vote
up vote
2
down vote
altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]
Edit: altTotal4
is quite surprisingly slow. The following Fold
is much better but still not competititve.
altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]
altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]
Edit: altTotal4
is quite surprisingly slow. The following Fold
is much better but still not competititve.
altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]
edited Aug 22 at 12:38
answered Aug 22 at 0:15
Alan
5,7621022
5,7621022
That's slower by more than two orders of magnitude thanaltTotal3
.
– Henrik Schumacher
Aug 22 at 9:44
add a comment |Â
That's slower by more than two orders of magnitude thanaltTotal3
.
– Henrik Schumacher
Aug 22 at 9:44
That's slower by more than two orders of magnitude than
altTotal3
.– Henrik Schumacher
Aug 22 at 9:44
That's slower by more than two orders of magnitude than
altTotal3
.– Henrik Schumacher
Aug 22 at 9:44
add a comment |Â
up vote
0
down vote
"Maybe" goes as below:
altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &
Or
altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]
add a comment |Â
up vote
0
down vote
"Maybe" goes as below:
altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &
Or
altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]
add a comment |Â
up vote
0
down vote
up vote
0
down vote
"Maybe" goes as below:
altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &
Or
altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]
"Maybe" goes as below:
altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &
Or
altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]
edited Aug 22 at 3:27
answered Aug 22 at 3:21


ΑλÎÂξανδÃÂο Ζεγγ
1,419720
1,419720
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180402%2falternating-total%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password