Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.7K Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

What's the best way to avoid spurious error messages when using ReplaceAll[]?

Many built-in functions throw spurious errors when they encounter an undefined variable before replacing it with a proper value. Even though the functions still work, the unwanted errors are a bother. I'm not fluent in Wolfram Language and have been using Hold[] and ReleaseHold[] to avoid the errors as in the attached example. There must be a better approach that everyone but me uses.

One of many similar examples:

POSTED BY: Jay Gourley
4 Replies

Great answer. Thanks, Eric.

POSTED BY: Jay Gourley
Posted 1 year ago

I agree with you about Quiet. I virtually never use it. What you use depends on exactly what you're trying to optimize or be efficient about, and that's still not clear to me, but here are some alternatives.

(* to have a clear, consistent place to change arguments... *)
With[
 {m = 20, s = 10},
 RandomVariate[NormalDistribution[m, s]]]

(* to repeat same test with several different argument lists... *)
testCases = {{20, 10}, {30, 10}, {40, 20}};
RandomVariate[NormalDistribution[#[[1]], #[[2]]]] & /@ testCases

(* fancier version of previous... *)
RandomVariate@*Apply[NormalDistribution] /@ testCases

(* table-based version of same idea... *)
Table[RandomVariate[NormalDistribution @@ x], {x, testCases}]

(* to avoid repeating same arguments for several functions... *)
Through[{fnA, fnB, fnC}[m, s]]

(* or if you really want to keep the ReplaceAll, this is slightly less verbose than Hold/ReleaseHold...*)
Unevaluated[RandomVariate[NormalDistribution[m, s]]] /. {m -> 20, s -> 10}
POSTED BY: Eric Rimbey
POSTED BY: Jay Gourley
Posted 1 year ago

Why do you do it that way? Why not just use the values or set the variables to the values prior?

But regardless, you can use Quiet. If you explain why you're doing it that way, I could come up with better suggestions.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard