6
$\begingroup$

it's known to all that ,sometimes higher version is far more slow than low version

this is a example.

Clear["Global`*"]; Plot[Sin[x], {x, 0, 5}] // AbsoluteTiming

on my computer

enter image description here

enter image description here

So is there a way to use low version in high version?

The reason why I don't use low version directly is that high version has more functions,which is more convenient.

thx to the link in the comment.I find this.

https://stackoverflow.com/questions/4983301/executing-code-in-v-5-2-kernel-from-within-v-7-01-session-through-mathlink

$\endgroup$
  • 2
    $\begingroup$ No, not possible $\endgroup$ – b3m2a1 Jan 8 at 7:37
  • 3
    $\begingroup$ Related: mathematica.stackexchange.com/q/209225/1871 $\endgroup$ – xzczd Jan 8 at 11:17
  • $\begingroup$ @b3m2a1 actually it did work. $\endgroup$ – wuyudi Jan 8 at 11:40
  • $\begingroup$ @wuyudi you’re not using the functions in the old version in the new one, but rather calling into the old one in the new one. That’s gonna introduce calling overhead that will obliterate the performance gain that motivated your question in the first place. $\endgroup$ – b3m2a1 Jan 8 at 17:02
9
$\begingroup$

Yes, it's possible with Graphics`Legacy`Plot:

enter image description here

Update in respond to comment about 3D:

First, @Jens's webpage has some discussion of graphics changes in V5, 6, 7, and 8. It has not been updated since 2012, but for its historical interest, it seems relevant to this Q&A. In particular, the package "Graphics`Graphics3D`" no longer exists AFAICT. You can still override the current plotters with << Version5`Graphics`. (I think you have to quit the kernel to restore them. There's also a "Version6`Graphics`" package to restore V6 graphics. These mainly exist, it seems, to provide some backwards compatibility with old notebooks.)

The legacy 3D plotters produce SurfaceGraphics[..] instead of Graphics3D[..]. There's not much point, other than historical interest, but here's a way to convert them to Graphics3D objects, with different styling. The conversion pretty much takes away any speed gain, so it's pointless as far as that goes.

SurfaceGraphicsToGraphics3D = # /. 
    SurfaceGraphics[data_, opts_] :>
     (*Visualization`Core`*)ListPlot3D[data, 
      DataRange -> OptionValue[SurfaceGraphics, opts, MeshRange], 
      FilterRules[opts, Options@ListPlot3D]] &;

Graphics`Legacy`Plot3D[
  Sin[x y], {x, 0, 5}, {y, 0, 5}] // AbsoluteTiming
Graphics`Legacy`Plot3D[Sin[x y], {x, 0, 5}, {y, 0, 5}] // 
  SurfaceGraphicsToGraphics3D // AbsoluteTiming

enter image description here

Using Visualization`Core`ListPlot3D saves about half the time over ListPlot3D. ListPlot3D, like other plotters, does some preprocessing before calling its internal version Visualization`Core`ListPlot3D. You probably lose some functionality and safeguards bypassing the top level ListPlot3D, but maybe it's worth it to you. Once upon a time, I'd do things in graphics directly and bypass plotters, but computers are pretty fast now. A plot that is too slow is usually due to the function, not to the plotter. Sometimes in Manipulate a tenth of a second can make a difference in the user experience, so to manipulate a fixed example, it might make sense to bypass Plot3D, which takes about a tenth of a second on the example below. Here's a pretty dumb 3D plotter, similar to the legacy one, with only one option implemented:

ClearAll[plot3D];
SetAttributes[plot3D, HoldAll];
plot3D[f_, {x_, x1_, x2_}, {y_, y1_, y2_}, 
   opts : OptionsPattern[Plot3D]] :=
  With[{pp = OptionValue[PlotPoints] /. Automatic -> 25},
   With[{data = Flatten[
       Table[{x, y, f}, {x, Subdivide[N@x1, N@x2, pp - 1]}, {y, 
         Subdivide[N@y1, N@y2, pp - 1]}],
       1]},
    Graphics3D[
     GraphicsComplex[
      data,
      (Flatten /@ Flatten[
           Partition[Partition[Range@Length@data, pp], {2, 2}, {1, 1}],
           1])[[All, {1, 2, 4, 3}]] // Polygon
      ], FilterRules[{opts}~Join~Options@Plot3D, Options@Graphics3D]]
    ]];

plot3D[Sin[x y], {x, 0, 5}, {y, 0, 5}] // AbsoluteTiming

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Unfortunately it will crashed inside Table or Manipulate.For example: Table[Graphics`Legacy`Plot[ Sin[x + a], {x, 0, 2 Pi}], {a, 1, 5}] $\endgroup$ – matrix89 Jan 9 at 4:41
  • $\begingroup$ it seems that this function has a little problem. But it is really fast. Graphics`Legacy`Plot[Tan[x], {x, 0, Pi}] $\endgroup$ – wuyudi Jan 9 at 8:08
  • $\begingroup$ ok,I find this. Visualization`Core`Plot3D[Sin[x y], {x, 0, 5}, {y, 0, 5}] ,which can draw 3D $\endgroup$ – wuyudi Jan 9 at 8:18
  • $\begingroup$ @matrix89 Try it thus: Table[With[{a = a}, Graphics`Legacy`Plot[Sin[a x], {x, 0, 5}]], {a, 2}] $\endgroup$ – Michael E2 Jan 9 at 12:51
  • 1
    $\begingroup$ @wuyudi Do you mean the "little problem" is the extra vertical line in Graphics`Legacy`Plot[Tan[x], {x, 0, Pi}] compared to Plot[Tan[x], {x, 0, Pi}]? One reason the new Plot is slower than the legacy Plot is that it does extra analysis to exclude discontinuities as well as other preprocessing (plot themes, Quantity[], etc.); the new plotters also do better adaptive refinement, too. -- Note that Visualization`Core`Plot3D is pretty much the same as Plot3D minus the automatic option processing. To get closer to the legacy function add the option MaxRecursion -> 0. $\endgroup$ – Michael E2 Jan 9 at 14:05
0
$\begingroup$

I find this link. And change a little.

https://stackoverflow.com/a/4992265

here is the code.

Clear[linkEvaluate]
SetAttributes[linkEvaluate, HoldRest]
linkEvaluate[link_LinkObject, expr_] := 
  Catch[Module[{out = {}, postScript = {}, packet, outputs = {}}, 
    While[LinkReadyQ[link], 
     Print["From the buffer:\t", LinkRead[link]]];
    LinkWrite[link, Unevaluated[EnterExpressionPacket[expr]]];
    While[Not@MatchQ[packet = LinkRead[link], InputNamePacket[_]], 
     Switch[packet, DisplayPacket[_], 
      AppendTo[postScript, First@packet], DisplayEndPacket[_], 
      AppendTo[postScript, First@packet];
      CellPrint@
         Cell[GraphicsData["PostScript", #], "Output", 
          CellLabel -> "Kernel 5.2 PostScript ="] &@
       StringJoin[postScript]; postScript = {}, TextPacket[_], 
      If[StringMatchQ[First@packet, 
        WordCharacter .. ~~ "::" ~~ WordCharacter .. ~~ ": " ~~ __], 
       CellPrint@
        Cell[BoxData@
          RowBox[{StyleBox["Kernel 5.2 Message = ", 
             FontColor -> Blue], First@packet}], "Message"], 
       CellPrint@
        Cell[First@packet, "Output", 
         CellLabel -> "Kernel 5.2 Print"]], OutputNamePacket[_], 
      AppendTo[outputs, First@packet];, ReturnExpressionPacket[_], 
      AppendTo[outputs, First@packet];, _, AppendTo[out, packet]]];
    If[Length[out] > 0, Print[out]];
    Which[(l = Length[outputs]) == 0, Null, l == 2, Last@outputs, 
     True, multipleOutput[outputs]]]];
Clear[kernel8Evaluate]
SetAttributes[kernel8Evaluate, HoldAll]
kernel8Evaluate[expr_] := 
 If[TrueQ[MemberQ[Links[], $kern8]], linkEvaluate[$kern8, expr], 
  Clear[$kern8]; $kern8 = 
   LinkLaunch[
    "C:\\Program Files\\Wolfram \
Research\\Mathematica\\8.0\\MathKernel.exe -mathlink"];(*the address of your MathKernel*)
  LinkRead[$kern8];
  LinkWrite[$kern8, 
   Unevaluated[EnterExpressionPacket[$MessagePrePrint = InputForm;]]];
  LinkRead[$kern8]; kernel8Evaluate[expr]]

and here is the result.

enter image description here

| improve this answer | |
$\endgroup$
  • 3
    $\begingroup$ How useful is this to deal with the performance problem you described? The overhead of calling another kernel process may be significant. Also, definitions will not be shared between the kernels, which limits the usefulness of this approach. $\endgroup$ – Szabolcs Jan 8 at 12:12
  • $\begingroup$ @SzabolcsSz well,it is really sort of useless.But this really happens: some question that new version is short to solve while old version is able to solve. $\endgroup$ – wuyudi Jan 8 at 13:01

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.