GraphFunctionCmd: Commander.CommandProc ~ {
xMin: REAL ¬ 0.0;
scale, a, xMax: REAL ¬ 1.0;
fileName: Rope.ROPE ¬ NIL;
function: G2dGraph.Function;
reals: G2dBasic.RealSequence ¬ NIL;
args: CommanderOps.ArgumentVector ¬ CommanderOps.Parse[cmd];
IF args.argc < 2
OR Rope.Equal[args[1], "?"]
THEN RETURN[msg: Rope.Concat["usage: 2dGraphFunction ", GraphFunctionUsage[]]];
FOR n:
NAT
IN [0..args.argc-1)
DO
IF Rope.Equal["-file", args[n], FALSE] THEN {fileName ¬ args[n+1]; EXIT};
ENDLOOP;
IF fileName =
NIL
THEN {
function ¬ G2dGraph.GetFunction[args[1]];
IF Rope.IsEmpty[function.name] THEN RETURN[$Failure, "no such function"];
}
ELSE {
ENABLE IO.Error => GOTO BadData;
s: IO.STREAM ¬ FS.StreamOpen[fileName ! FS.Error => GOTO FSError];
DO
reals ¬ G2dBasic.AddToRealSequence[reals, IO.GetReal[s ! IO.EndOfStream => EXIT]];
ENDLOOP;
IF reals = NIL OR reals.length = 0 THEN RETURN[$Failure, "no data in file"];
function ¬ [fileName, Reals,, reals];
EXITS
FSError => RETURN[$Failure, Rope.Concat["can't open ", fileName]];
BadData => RETURN[$Failure, "bad data in file"];
};
FOR n:
NAT
IN [0..args.argc-1)
DO
ENABLE Convert.Error => GOTO ConvertError;
Test:
PROC [a: Rope.
ROPE, r:
REAL]
RETURNS [s:
REAL] ~ {
s ¬ IF Rope.Equal[a, args[n], FALSE] THEN Convert.RealFromRope[args[n+1]] ELSE r;
};
scale ¬ Test["-scale", scale];
xMin ¬ Test["-xMin", xMin];
xMax ¬ Test["-xMax", xMax];
a ¬ Test["-a", a];
ENDLOOP;
msg ¬ G2dGraph.GraphFunction[function, xMin, xMax, scale, a, reals];
EXITS ConvertError => RETURN[$Failure, "argument conversion error"];
};
Reals: G2dGraph.GraphProc ~ {
reals: G2dBasic.RealSequence ¬ NARROW[g.clientData];
xx: REAL ¬ x*REAL[reals.length-1];
n0: CARD ¬ Real.Floor[xx];
y ¬ reals[n0];
IF n0 < reals.length-1 THEN y ¬ y+(xx-REAL[n0])*(reals[n0+1]-y);
};
GraphFunctionUsage:
PROC
RETURNS [usage: Rope.
ROPE] ~ {
usage ¬ " [function] [-option]\n\tfunctions are:\n";
FOR l:
LIST
OF G2dGraph.Function ¬ G2dGraph.GetFunctions[], l.rest
WHILE l #
NIL
DO
usage ¬
IO.PutFR["%g\t\t%g:\t%g\n",
IO.rope[usage], IO.rope[l.first.name], IO.rope[l.first.use]];
ENDLOOP;
usage ¬ Rope.Concat[usage, "\toptions are:
-file <filename> (file must contain list of reals or integers)
-scale <REAL> (default = 1)
-xMin <REAL> (default = 0)
-xMax <REAL> (default = 1)
-a <REAL> (default = 1)"];
};