2
$\begingroup$

I'm trying to write a custom function but it isn't returning any output.

Here's my code so far:

Options[Alternate] = {AlternationType -> AlternationPlusMinus};

Alternate[Terms_, Exponent_, OptionsPattern[]] := (
   tmpSet = {};
   If[OptionValue[AlternationType] == AlternationPlusMinus, 
Do[If[Mod[counter, 2] != 0, 
  AppendTo[tmpSet, (counter^Exponent) - 1] AppendTo[
    tmpSet, (counter^Exponent) + 1]], {counter, Terms}], 
Do[If[Mod[counter, 2] != 0, 
  AppendTo[tmpSet, (counter^Exponent) + 1] AppendTo[
    tmpSet, (counter^Exponent) - 1]], {counter, Terms}]];
   Return[Expand[InterpolatingPolynomial[tmpSet, x]]];
   );
Alternate[20, 2]

When I execute it, no output block is even generated, let alone any error messages or warnings, is there any way to fix it?

Thanks.

$\endgroup$
  • 1
    $\begingroup$ When I execute it in a fresh Mathematica kernel, I do get some output (a very large polynomial) and some error messages like (Thread::tdlen: Objects of unequal length in {0} {0,2} cannot be combined. >>). Have you tried this in a fresh kernel? Also, you are not localizing tmpSet. Try wrapping your function with Module. Also, try Fold instead of Do and Return. $\endgroup$ – Verbeia Jul 1 '12 at 21:55
4
$\begingroup$

Chris's answer (particularly the missing commas) explains why your version wasn't working. However you should also consider modularising your code so that tmpSet doesn't leak out to have a value elsewhere in your code:

Alternate[Terms_, exponent_, OptionsPattern[]] := Module[{tmpSet = {}},
  If[OptionValue[AlternationType] == "AlternationPlusMinus", 
   Do[If[Mod[counter, 2] != 0, 
     AppendTo[tmpSet, (counter^exponent) - 1], 
     AppendTo[tmpSet, (counter^exponent) + 1]], {counter, Terms}], 
   Do[If[Mod[counter, 2] != 0, 
     AppendTo[tmpSet, (counter^exponent) + 1], 
     AppendTo[tmpSet, (counter^exponent) - 1]], {counter, Terms}]];
  Return[Expand[InterpolatingPolynomial[tmpSet, x]]]]

Also, the whole Do...Return paradigm is almost never the most efficient way to program in Mathematica. Here is a more functional-programming style alternative. It gives the same answers for the simple tests I have done. Notice I have restricted the definition of the function to only match for positive integer values of terms and exponent. You can of course remove them again if you intend for this function to be used for real or negative-valued inputs.

alternateAlternate[terms_Integer?Positive, exponent_Integer?Positive, OptionsPattern[]] := 
 Expand[InterpolatingPolynomial[#, x]] &@
  If[OptionValue[AlternationType] == "AlternationPlusMinus", 
   FoldList[(#2^exponent) + If[OddQ[#2], -1, 1] &, 0, Range[2, terms]], 
   FoldList[(#2^exponent) + If[OddQ[#2], 1, -1] &, 2, Range[2, terms]] ]
$\endgroup$
  • $\begingroup$ Thank you so much! I've used the second solution combined with Chris' quotes around AlternationPlusMinus. It works perfectly now! :) $\endgroup$ – Callum Booth Jul 2 '12 at 15:48
4
$\begingroup$

You could put AlternationPlusMinus in quotes. You also have two missing commas in the If statements. Exponent is a reserved word but doesn't prevent execution. This version seems to work:

Options[Alternate] = {AlternationType -> "AlternationPlusMinus"};

Alternate[Terms_, exponent_, OptionsPattern[]] := (tmpSet = {};
   If[OptionValue[AlternationType] == "AlternationPlusMinus", 
    Do[If[Mod[counter, 2] != 0, 
      AppendTo[tmpSet, (counter^exponent) - 1] , 
      AppendTo[tmpSet, (counter^exponent) + 1]], {counter, Terms}], 
    Do[If[Mod[counter, 2] != 0, 
      AppendTo[tmpSet, (counter^exponent) + 1] , 
      AppendTo[tmpSet, (counter^exponent) - 1]], {counter, Terms}]];
   Return[Expand[InterpolatingPolynomial[tmpSet, x]]];);
Alternate[20, 2]
$\endgroup$
  • $\begingroup$ This still gives the Thread::tdlen error for me, and I do get an output. I'm currently trying to redo this function using FoldList but getting different answers. $\endgroup$ – Verbeia Jul 1 '12 at 22:08
  • $\begingroup$ @ Verbeia - I have added to my answer. Added commas. $\endgroup$ – Chris Degnen Jul 1 '12 at 22:14
  • $\begingroup$ Yes the missing commas was the issue I came up with too. $\endgroup$ – Verbeia Jul 1 '12 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.