%mathpiper,def="SturmVariations"

10 # SturmVariations(_S,Infinity) <--
[
  Local(i,s);
  s:=FillList(0,Length(S));
  For(i:=1,i<=Length(S),i++)
  [
    s[i] := LeadingCoef(S[i]);
  ];
  SturmVariations(s);
];

10 # SturmVariations(_S,-Infinity) <--
[
  Local(i,s);
  s:=FillList(0,Length(S));
  For(i:=1,i<=Length(S),i++)
  [
    s[i] := ((-1)^Degree(S[i]))*LeadingCoef(S[i]);
  ];
  SturmVariations(s);
];

20 # SturmVariations(_S,_x) <-- SturmVariations(Eval(S));
SturmVariations(_S) <--
[
  Local(result,prev);
//Echo("S = ",S);
  result:=0;
  While(Length(S)>0 And IsZero(S[1])) S:=Rest(S);
//Echo("S = ",S);
  if (Length(S)>0)
  [
    prev:=S[1];
    ForEach(item,Rest(S))
    [
      if(Not IsZero(item))
      [
        if (prev*item < 0) [result++;];
        prev:=item;
      ];
    ];
  ];
  result;
];

%/mathpiper