4
$\begingroup$

Trying to replace some of the expressions wrapped by Hold as below to get $\frac{k d\theta }{dt}$, ReplaceAll does not replacing and keeps outputting the inputted expression. What mistakes did I make? (※Tried Replaceresult the same)

Hold[(\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]]) \[DifferentialD]\[Theta])/(\[DifferentialD]\[Theta] \ \[DifferentialD]t)] /. HoldPattern[\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]])/\[DifferentialD]\[Theta] ] -> k 

hold

Replace

Replace[Hold[(\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]]) \[DifferentialD]\[Theta])/(\[DifferentialD]\[Theta] \ \[DifferentialD]t)], HoldPattern[\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]])/\[DifferentialD]\[Theta] ] -> k] 
$\endgroup$
3
  • 1
    $\begingroup$ The problem is that the occurrences of Times[] inside Hold[] are not flattened because Times[] is not evaluated. The two expressions as posted have a non-matching structure. I suppose you're trying to keep the $d\theta$s from canceling. Algebraic manipulation by replacement rules is tricky, especially if you don't allow algebraic evaluation to occur. $\endgroup$ Commented Nov 16 at 13:51
  • 1
    $\begingroup$ Unheld, algebra-based alternative: First@SolveValues[{z == (\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]]) \[DifferentialD]\[Theta])/(\[DifferentialD]\[Theta] \[DifferentialD]t), \[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]])/ \[DifferentialD] \[Theta] == k}, z, {Cos[\[Theta][x, t]]}] — I'm wondering what you're actually wanting to do. Your question seems very specific, and a very specific solution might apply to a very narrow type of problem and be of little help. If it is this narrow, then I can type the answer faster than I can code the solution. $\endgroup$ Commented Nov 16 at 16:50
  • $\begingroup$ Thank you for pointing out the problem in my expressions;before matching patterns,I need to make sure to always check the structure of algebraic expressions right before doing pattern matching $\endgroup$ Commented Nov 17 at 13:55

2 Answers 2

4
$\begingroup$

Look at your expression and the patter using "FullForm:"

Hold[(\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]]) \[DifferentialD]\[Theta])/(\[DifferentialD]\[Theta]\ \[DifferentialD]t)] // FullForm 

enter image description here

 HoldPattern[\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]])/\[DifferentialD]\[Theta] ] // FullForm 

enter image description here

You will note that the pattern is missing a term in the numerator and denominator. Adding these terms:

Hold[(\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]]) \[DifferentialD]\[Theta] \ )/(\[DifferentialD]\[Theta] \[DifferentialD]t)] /. HoldPattern[(\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]]) c1_)/(c2_ \[DifferentialD]\[Theta])] -> c1 k /c2 

enter image description here

$\endgroup$
5
$\begingroup$

Here's a simple environment in which the OP's original code works:

Block[{Times, Hold, attr = Attributes@Times}, SetAttributes[Times, attr]; Hold[(\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]]) \[DifferentialD]\[Theta])/(\[DifferentialD]\[Theta]\ \[DifferentialD]t)] /. \[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]])/\[DifferentialD]\[Theta] -> k ] (* Hold[(k \[DifferentialD]\[Theta])/\[DifferentialD]t] *) 

In the above we don't need to worry about how the Front End and MakeExpression[] parsed the user's input. If the held expression had been generated by code, then the full-form would almost certainly be different than what you get by pasting the OP's sample code into Mma. Then we might have to inspect the result and construct another replacement pattern. How the expression was typed may result also in a different expression-tree. While equivalent algebraically, different trees probably need different patterns. That does not seem to be the case in the environment above.

Here's an alternate test case:

Hold[\[DifferentialD]\[Theta] (\[DifferentialD](-A \[Omega] Cos[\ \[Theta][x, t]]) )/(\[DifferentialD]\[Theta] \[DifferentialD]t)] /. \ \[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]])/\[DifferentialD]\[Theta] -> k 

The OP's target use-cases are unclear. If a somewhat safer environment is needed in which the OP's variables are not evaluated, here's a trick I picked up from Leonid Shifrin or Mr. Wizard or someone, oh, maybe a dozen years ago:

withHeldSymbolsIn // ClearAll; withHeldSymbolsIn // Attributes = {HoldAll}; withHeldSymbolsIn[expr_, code_, contexts_ : {"Global`"}] := Thread[ DeleteDuplicates@ Cases[Hold[expr] , s_Symbol /; MemberQ[contexts, Context@Unevaluated@s] :> Hold[s] , Infinity] , Hold] /. Hold[v_] :> Block[v, code]; expr = Hold[(\[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]]) \[DifferentialD]\[Theta])/(\[DifferentialD]\[Theta]\ \[DifferentialD]t)]; withHeldSymbolsIn[Evaluate@expr, Block[{Times, Hold, attr = Attributes@Times}, SetAttributes[Times, attr]; expr /. \[DifferentialD](-A \[Omega] Cos[\[Theta][x, t]])/\[DifferentialD]\[Theta] -> k ] ] (* Hold[(k \[DifferentialD]\[Theta])/\[DifferentialD]t] *) 

Note that while the attributes of Times[] are restored inside the environment, like factors do not cancel or combine although arguments are sorted and the nested Times[] instances are flattened:

Block[{Times, Hold, attr = Attributes@Times}, SetAttributes[Times, attr]; expr ] // FullForm (* Hold[ Times[ Power[DifferentialD[t], -1], Power[DifferentialD[\[Theta]], -1], DifferentialD[\[Theta]], DifferentialD[Times[-1, A, \[Omega], Cos[\[Theta][x, t]]]] ]] *) 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.