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 updating a code from a 5.0 version to a 9.0 version. I am speaking about "Advanced visual quantum mechanics".

There is a package with this code:


`

VQMmsgon  = Head[General::"spell"]  =!= $Off;
    VQMmsgon1 = Head[General::"spell1"] =!= $Off;

Off[General::spell1, General::spell];


(*-----------------------------------*)
BeginPackage["VQM`ArgColorPlot`"];
(*-----------------------------------*)
    ClearAll[QArgColorPlot,QListArgColorPlot];

(*-----------------------------------*)
Begin["`Private`"];
(*-----------------------------------*)
   If[!ValueQ[$UseListLinePlot],
        $UseListLinePlot=True;
];

Options[QArgColorPlot] := Sort @ {JoinOptions[
        {
        PlotStyle -> None
        },
        Options[ If[$UseListLinePlot, ListLinePlot, Plot] ],
        {
        Opacity -> 1,         
        Compiled->True, 
        QSaturation->1, QBrightness->1, QBottomLine->0, QShiftPlot->0, QPlotDown->False, 
        QSquared->False
        } ] };

Options[QListArgColorPlot] =  Sort @  Join[Options[QArgColorPlot],{QHorizontalRange->All}];

SetAttributes[QArgColorPlot,HoldAll];

QArgColorPlot[func_,{x_Symbol,xmin_,xmax_},opts___?OptionQ] :=
    Module[{comp,plot1,opts1,xvars,yvals},
        comp = Compiled/.Join[{opts}, Options[QArgColorPlot]];
        If[comp === True,
          abf = Compile[{x},Abs[func],{{func,_Complex}}];
          fnc = Compile[{x},func,{{func,_Complex}}], (*else*)
          abf = Abs[func];
          fnc = Function[{x},func];
        ];
        opts1   = FilterRules[{opts},Options@Plot];
        plot1   = Plot @@ { abf, {x,xmin,xmax}, 
                            {opts1} /. ( PlotStyle -> _ ) :> Sequence[]};
        xvars   = First /@ Level[plot1[[1]], {5}];
        yvals   = fnc /@ xvars; 

      QListArgColorPlot[{xvars,yvals}//Transpose, opts]
    ]/;NumericQ[xmin] && NumericQ[xmax]
EndPackage[]

`

share|improve this question
1  
Please use the Code Sample button in the toolbar (the one with the {} icon) on your code so that it is formatted properly. –  Rahul Narain Dec 2 '13 at 22:03
    
There is no End[] nor EndPackage[], I presume they follow the current code immediately, correct? –  rcollyer Dec 2 '13 at 22:07
    
yes there is EndPackage. The new error code is $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >> –  Raffaele Carlone Dec 2 '13 at 22:13
    
You just messed up the styling in its entirety with your update. Please go back and fix it. Lead every line of code with four spaces. –  rcollyer Dec 3 '13 at 17:59

1 Answer 1

I'd make several structural changes. First, instead of

Options[QArgColorPlot] := Sort @ {JoinOptions[ ... ]}

I'd use

Options[QArgColorPlot] = Sort @ Flatten @ { ... };

since rules are not flattened. Also, I changed it to Set (=), not SetDelayed (:=) as the options list is not likely to change during execution, so why bother recalculating it every time you use it. Second, I'd use the new options constructs, OptionsPattern and OptionValue as they provide automatic messages if an option is unknown, and they eliminate constructs like

comp = Compiled/.Join[{opts}, Options[QArgColorPlot]];

which can be source of errors. So, the signature of QArgColorPlot becomes

QArgColorPlot[func_,{x_Symbol,xmin_,xmax_},opts:OptionsPattern[]]

and the line to access the Compiled option then becomes

comp = OptionValue[Compiled];

Note, this automatically pulls in the pre-set values for the options. Third, Plot doesn't process CompiledFunction directly, but it can be made to work if an argument is passed to it. This requires to changes: first, the uncompiled code has to be treated consistently, so abf becomes

abf = Function[{x}, Abs[func]];

and, second, the call to Plot becomes

plot1 = Quiet[Plot@@{abf[x], {x,xmin,xmax}, opts1}, CompiledFunction::cfsa];

I had to add Quiet to get around a Message. Lastly, in processing the options for Plot, I moved the ReplaceAll up one line

opts1 = FilterRules[{opts},Options@Plot] /. ( PlotStyle -> _ ) :> Sequence[];

Of course, that could be used directly in the call to Plot.

However, I was not able to reproduce the messages you saw. By any chance, are you resetting Plot?


As there is some question on what is or is not working, here is the full code with my changes:

BeginPackage["VQM`ArgColorPlot`"];
(*-----------------------------------*)
ClearAll[QArgColorPlot,QListArgColorPlot];

(*-----------------------------------*)
Begin["`Private`"];
(*-----------------------------------*)
   If[!ValueQ[$UseListLinePlot],
            $UseListLinePlot=True;
];

Options[QArgColorPlot] := Sort @ Flatten @{
    {
        PlotStyle -> None
    },
    Options[ If[$UseListLinePlot, ListLinePlot, Plot] ],
    {
        Opacity -> 1,         
        Compiled->True, 
        QSaturation->1, QBrightness->1, QBottomLine->0, 
        QShiftPlot->0, QPlotDown->False, 
        QSquared->False
    }
};

Options[QListArgColorPlot] = Sort@Join[Options[QArgColorPlot],{QHorizontalRange->All}];

SetAttributes[QArgColorPlot,HoldAll];

QArgColorPlot[func_,{x_Symbol,xmin_,xmax_},opts:OptionsPattern[]] :=
    Module[{comp,plot1,opts1,xvars,yvals},
        comp = OptionValue[Compiled];
        If[comp === True,
          abf = Compile[{x},Abs[func],{{func,_Complex}}];
          fnc = Compile[{x},func,{{func,_Complex}}], (*else*)
          abf = Function[{x}, Abs[func]];
          fnc = Function[{x},func];
        ];
        opts1   = FilterRules[{opts},Options@Plot]/. ( PlotStyle -> _ ) :> Sequence[];
        plot1   = Quiet[Plot @@ { abf[x], {x,xmin,xmax}, {opts1} }, 
                    CompiledFunction::cfsa];
        xvars   = First /@ Level[plot1[[1]], {5}];
        yvals   = fnc /@ xvars; 

      QListArgColorPlot[{xvars,yvals}//Transpose, opts]
    ]/;NumericQ[xmin] && NumericQ[xmax]
End[];
EndPackage[];

Then, using the now deleted example,

QArgColorPlot[x, {x, 0, 1}, Frame -> True]

I get

(* 
QListArgColorPlot[{{2.04082*10^-8, 2.04082*10^-8}, ..., {1., 1.}}, 
 Frame -> True] 
*)

where the ... are elided pairs of points. Obviously, QListArgColorPlot is not defined, yet, but running

ListLinePlot @@ QArgColorPlot[x, {x, 0, 1}, Frame -> True]

I get

enter image description here

share|improve this answer
    
I still have problems when I call this function. FilterRules::rep: ListLinePlot is not a valid replacement rule. >> ListLinePlot::nonopt: Options expected (instead of FilterRules[ListLinePlot,Axes->True]) beyond position 3 in ListLinePlot[{{2.04082*10^-8,2.04082*10^-8},{0.000306718,0.000306718},{0.0006134‌​15,0.000613415},<<45>>,{0.857186,0.857186},{0.876771,0.876771},<<27>>},<<4>>,Filt‌​erRules[ListLinePlot,Axes->True]]. An option must be a rule or a list of rules. >> FilterRules::rep: Graphics is not a valid replacement rule. >> Show::gcomb: Could not combine the graphics objects in –  Raffaele Carlone Dec 2 '13 at 18:28
    
Two things: do you run Clear[QListArgColorPlot, QArgColorPlot] anywhere? If not, please do so prior to running the code. Second, have you run the revised code in isolation from the rest of your package? Do you get the same errors? –  rcollyer Dec 2 '13 at 18:38
    
I changed the code and you can read now the new version. But it is still not working –  Raffaele Carlone Dec 2 '13 at 21:43
    
But, I still don't know if the code I gave you works in isolation from the rest of the package. Could you test that, please? As it works for me. –  rcollyer Dec 2 '13 at 22:09
    
It is isolated. In the package now there is only your code –  Raffaele Carlone Dec 2 '13 at 22:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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