Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am just beginning with Mathematica. I have received this code to study. I am not able to follow this particular section. I have tried looking it up on many forums, with no answer. Can you help?

Clear[CSfunc];
CSfunc::usage="CSfunc[n_] returns {Cn,Sn} up to degree n >= 0"; 
CSfunc[n_] := 
  Module[{cn,sn}, 
    sn = Table[Sin[k*x*Pi/2], {k, n}]; (* anti-symmetric in x *)    
    cn = Table[Cos[k*x*Pi/2], {k, n}];  (* symmetric in x *)  
    cn = Join[{1/Sqrt[2]}, cn]; 
    {cn, sn}];  

<< JavaGraphics`; 

doplot = False;

doplot = True;

If[True,  
  (* check classic orthogonality: 2 on [-2,2], not yet on [-1,1] ... *)
  ntest = 6; 
  xmax = 2;
  csn = CSfunc[ntest];
  If[False && ntest < 5 && doplot,
    Plot[Flatten[csn], {x, -1, 1}, Axes->True, Frame->True]];

In the above portion of the program, I see If[True etc.. If[False etc..], but no flag variable associated with it. So what condition is it checking?

share|improve this question

closed as off-topic by m_goldberg, Yves Klett, Öskå, Jens, Mr.Wizard Jul 29 at 20:15

  • The question does not concern the technical computing software Mathematica by Wolfram Research. Please see the help center to find out about the topics that can be asked here.
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Hi ! As it is currently written this is out of scope for Mathematica.SE. Plus, it is easy enough to check that doplot is not a built-in predicate. You have to narrow it down to a specific problem (not help-me-understand-this-chunk type of problem) and ask an on-scope question. –  Sektor Jul 29 at 9:03
    
Are the True and False under f statements refering to the doplot variable? –  RJSiam Jul 29 at 10:29
    
What has code refactoring got to do with the question? Anyway, I doubt if anyone here can help you divine what the original author's intent was. Why not ask them? –  Oleksandr R. Jul 29 at 14:24
4  
This question appears to be off-topic because it is too localized; i.e, it applies only to the local situation and needs of its poster and answers will not benefit others. –  m_goldberg Jul 29 at 17:14

1 Answer 1

It looks to me that the author of the code you are studying has implemented a crude version of the conditional compilation preprocessor macros that are built into C and C++. I'm thinking of #if and #ifdef.

I suspect that

If[True,  
  (* check classic orthogonality: 2 on [-2,2], not yet on [-1,1] ... *)
  ntest = 6; 
  xmax = 2;
  csn = CSfunc[ntest];
  If[False && ntest < 5 && doplot,
    Plot[Flatten[csn], {x, -1, 1}, Axes->True, Frame->True]];

is intended for use as a debugging aid or perhaps as a form of unit test. The code author controlled what gets evaluated by commenting out one of doplot = ... expressions and by editing the first arguments of the Ifs to suit his immediate needs.

share|improve this answer

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