Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.6K 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
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 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

Great answer. Thanks, Eric.

POSTED BY: Jay Gourley

Thanks, Eric.

The motive for the question was that it's easier to change variable values with a ReplaceAll[] at the end of a function than to enter values directly when experimenting with different values. That's especially true if the same variable is used more than once. In trying to focus my question narrowly, I oversimplified by using an example where the variable was used only once.

My problem with the Quiet[] solution is that I don't want to silence the error when there really is an inappropriate argument.

The predefined variables idea is a good suggestion. I often do that. But if I'm doing different things in in different parts of a notebook, I can make problems for myself by defining too many variables. I could include a Clear[] in the cell after the function to solve that problem. Maybe that's the best way to solve the problem without creating many variables in a notebook. I infer from your post that the answer might not be as simple as I hoped when I posted the question.

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