Hot answers tagged replacement
13
I don't think there's a contradiction.
The documentation for Rule says
Symbols that occur as pattern names in lhs are treated as local to the rule.
and as you've already pointed out,
lhs->rhs evaluates rhs immediately
In
ClearAll@x;
{1, 3.5} /. x_?IntegerQ -> {x}
the rule's rhs evaluates first, but there's no global rule associated with x, ...
12
You wish to make substitutions in all except Subscript expressions I believe. This can be done by leveraging the precedence of replacement rules, like this:
{p, Subscript[p, 1], Subscript[p, 2]} /. {x_Subscript :> x, p -> 1}
{1, Subscript[p, 1], Subscript[p, 2]}
The rule x_Subscript :> x acts first, "replacing" any expression with the head ...
6
The general issue, as mentioned by xzczd, is that Manipulate only "notices" explicit visible parameters. This is because when you evaluate something like Manipulate[x, {x, 0, 1}] and start waggling the slider, you are not changing the value of the global symbol x, but instead a temporary symbol called something like x$$15. You can see this like so:
...
6
Perhaps it will seem clearer if the FullForm of the rule is examined. In the first example below, we see that the value of x is the RHS of the rule. So we should expect any integer to be replaced by {2}.
x = 2;
x_?IntegerQ -> {x} // FullForm
Rule[PatternTest[Pattern[x, Blank[]], IntegerQ], List[2]]
Here, if we clear x, the RHS of the rule is {x}. ...
6
One possible way to get some candidates is to test all Options of all System` symbols
extractRuleDelayedOptions[symbol_String] :=
With[{opts = Options @@ MakeExpression[symbol]},
Cases[opts, (p_ :> _) :> p]
];
Union@Flatten[extractRuleDelayedOptions /@ Names["System`*"]]
This produces the following list
{"Compiler", "CompilerWarnings", ...
5
According to @amr 's suggestion, I scanned every .nb files under the Mathematica\9.0\Documentation\English\ directory - which is fairly fast even on my outdated PC. This is what I got:
wsc = (WhitespaceCharacter ...);
prePtn = "RowBox[" ~~ wsc ~~ "{" ~~ wsc ~~ "\"";
inPtn = "\"" ~~ wsc ~~ "," ~~ wsc ~~ "\"";
rdOptExtractor = Function[testfile,
...
5
Preamble
I suggest to use linked lists and recursion. Note that this won't be the fastest possible solution, but it will be idiomatic, rule-based, will have the right asymptotic complexity, and will have decent performance. I want to stress that I am here after these features, rather than raw performance, as those seems to be at the core of your question.
...
4
As expressed in the comments, the Replace functions are not merely "syntactic sugar" for Map. The two are quite different. One primary difference is the order in which expressions are visited. See:
How to perform a depth-first preorder traversal of an expression?
Another is that Replace will go inside held expressions, while Map does not evaluate:
Hold[1 ...
4
Good news! There are ways to protect your DispatchTables! That is, you do not have to make sure that all the symbols in your Dispatch table/list of rules get no assignments or updates made to them until you are done using your Dispatch table.
You simply have to shield them from evaluation and use Unevaluated just before you use them (with Replace etc). Lets ...
4
You could also make one rule that is a bit more specific
{p, Subscript[p, 1], Subscript[p, 2]} //.
head_[x___, p, y___] /; head =!= Subscript :> head[x, 0.5, y]
-> {0.5, Subscript[p, 1], Subscript[p, 2]}
This does not work if p is the entire expression. i.e
p/.head_[x___, p, y___] /; head =!= Subscript :> head[x, 0.5, y]
-> p
In that case you ...
2
I'm going to make some assumptions/guesses based on the code:
The . represents the Dot product.
ψorbR, ψorbT are each a list/vector of complex numbers of length NN.
Since i runs from 1 to NN, Sign[i] may be omitted.
Since a[i] is evaluated only in Abs[a[i]] the -I factor may be omitted.
Since the terms containing a[i] are multiplied by 0 if ε^2 < 2i, ...
2
If this really can be expressed linearly, then expressing in Matrix form is going to be the easiest thing and the best from a computational perspective. For example, with your definitions:
R6 = kf*(z[1] + mu*z[5]) - 2*z[6] + mu*kd^2*z[7] - kd^2*z[6];
R7 = kf*(z[1] + mu*z[6] - 2*z[7]) + kd^2*z[8] + kd^2*z[7];
R8 = kf*z[1] + (mu*z[7] - 2*z[8]) + kd^2*z[9] + ...
1
Does this do the job?
poly=(kz py + py + kr kx)
variables = Variables[poly];
reprule =DeleteCases[If[StringTake[ToString[#], 1] == "k", # ->
Subscript[StringTake[ToString[#], 1],
StringTake[ToString[#], {2, -1}]]] & /@ variables, Null]
poly/.reprule
This will take all the variables in an expression, work out if they are a combination of k ...
1
I don't know why it works that way, and you have the same issue with f[x_]={x}. You will find code here that defines new operators that work like x_?IntegerQ->{X} and f[x_]={x} except the new operators do what you expected.
Only top voted, non community-wiki answers of a minimum length are eligible

