Skip to main content
added 878 characters in body
Source Link
Bob Hanlon
  • 167.4k
  • 7
  • 82
  • 213
f[x_?NumericQ] := {x, x^2} 

These three expressions all produce the same output. Quiet is used in each case to suppress the message "Part::partw: "Part 2 of f[x] does not exist."

Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] := (tmp = f[x])[[1]]; fwrap2[x_] := tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] = (tmp = f[x])[[1]]; fwrap2[x_] = tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, PlotLegends -> {x, x^2}]] // Quiet Module[{tmp}, Plot[ {(tmp = f[x])[[1]], tmp[[2]]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet 

enter image description here

EDIT:

Looking at the number of times at f[x] is evaluated

f[x_?NumericQ] := (n++; {x, x^2}) n = 0; Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] := (tmp = f[x])[[1]]; fwrap2[x_] := tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet; n (* 159 *) n = 0; Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] = (tmp = f[x])[[1]]; fwrap2[x_] = tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, PlotLegends -> {x, x^2}]] // Quiet; n (* 159 *) n = 0; Module[{tmp}, Plot[{(tmp = f[x])[[1]], tmp[[2]]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet; n (* 159 *) n = 0; Plot[{f[x][[1]], f[x][[2]]}, {x, 0, 1}]; n (* 238 *) 
f[x_?NumericQ] := {x, x^2} 

These three expressions all produce the same output. Quiet is used in each case to suppress the message "Part::partw: "Part 2 of f[x] does not exist."

Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] := (tmp = f[x])[[1]]; fwrap2[x_] := tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] = (tmp = f[x])[[1]]; fwrap2[x_] = tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, PlotLegends -> {x, x^2}]] // Quiet Module[{tmp}, Plot[ {(tmp = f[x])[[1]], tmp[[2]]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet 

enter image description here

f[x_?NumericQ] := {x, x^2} 

These three expressions all produce the same output. Quiet is used in each case to suppress the message "Part::partw: "Part 2 of f[x] does not exist."

Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] := (tmp = f[x])[[1]]; fwrap2[x_] := tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] = (tmp = f[x])[[1]]; fwrap2[x_] = tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, PlotLegends -> {x, x^2}]] // Quiet Module[{tmp}, Plot[ {(tmp = f[x])[[1]], tmp[[2]]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet 

enter image description here

EDIT:

Looking at the number of times at f[x] is evaluated

f[x_?NumericQ] := (n++; {x, x^2}) n = 0; Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] := (tmp = f[x])[[1]]; fwrap2[x_] := tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet; n (* 159 *) n = 0; Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] = (tmp = f[x])[[1]]; fwrap2[x_] = tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, PlotLegends -> {x, x^2}]] // Quiet; n (* 159 *) n = 0; Module[{tmp}, Plot[{(tmp = f[x])[[1]], tmp[[2]]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet; n (* 159 *) n = 0; Plot[{f[x][[1]], f[x][[2]]}, {x, 0, 1}]; n (* 238 *) 
Source Link
Bob Hanlon
  • 167.4k
  • 7
  • 82
  • 213

f[x_?NumericQ] := {x, x^2} 

These three expressions all produce the same output. Quiet is used in each case to suppress the message "Part::partw: "Part 2 of f[x] does not exist."

Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] := (tmp = f[x])[[1]]; fwrap2[x_] := tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet Module[{fwrap1, fwrap2, tmp}, fwrap1[x_] = (tmp = f[x])[[1]]; fwrap2[x_] = tmp[[2]]; Plot[{fwrap1[x], fwrap2[x]}, {x, 0, 1}, PlotLegends -> {x, x^2}]] // Quiet Module[{tmp}, Plot[ {(tmp = f[x])[[1]], tmp[[2]]}, {x, 0, 1}, Evaluated -> True, PlotLegends -> {x, x^2}]] // Quiet 

enter image description here