Skip to main content
Additional explanation.
Source Link
E. Chan-López
  • 34.3k
  • 3
  • 38
  • 55
h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

Update 1: Why HoldPattern[3/7] Works

While 3/7 is internally represented as Times[3, Power[7, -1]] (which you correctly saw in FullForm), the pattern HoldPattern[3/7] works because it instructs the pattern matcher to look for the expression that originated as 3/7 , without attempting to evaluate or strictly enforce the internal form on the pattern itself.

Update 2: Copy-Paste Anomaly (Output vs. Input)

Your update is an excellent observation that uncovers a core subtlety: Output Formatting is NOT Input. The difference in behavior is due to how the Frontend (Notebook interface) handles held expressions:

Initial Failure (h reference): When you run h /. HoldPattern[1/6] -> q, the replacement fails because the Kernel's Times sorting (Orderless) groups all factors. 1/6 doesn't exist as a separate replaceable argument; it's just one factor among many within a single Times head, and it's not at the top level of the expression inside Hold.

Internal h:Hold[Times[9,1/6,5]]→No direct match for 1/6 as a subexpression.

Successful Replacement (Copy-Paste): When you copy and paste the output, the Frontend often re-parses the expression and might implicitly change the grouping or re-introduce a Times head, making 1/6 exist as a distinct, replaceable subexpression again:

Pasted Input:Hold[1/6⋅(rest of expression)]→Match succeeds. The expression you pasted looks the same, but its internal tree structure was slightly modified during the copy/paste process, allowing the pattern matcher to finally see 1/6 as an independent part.

h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

Update 1:

While 3/7 is internally represented as Times[3, Power[7, -1]] (which you correctly saw in FullForm), the pattern HoldPattern[3/7] works because it instructs the pattern matcher to look for the expression that originated as 3/7 , without attempting to evaluate or strictly enforce the internal form on the pattern itself.

Update 2:

Your update is an excellent observation that uncovers a core subtlety: Output Formatting is NOT Input. The difference in behavior is due to how the Frontend (Notebook interface) handles held expressions:

Initial Failure (h reference): When you run h /. HoldPattern[1/6] -> q, the replacement fails because the Kernel's Times sorting (Orderless) groups all factors. 1/6 doesn't exist as a separate replaceable argument; it's just one factor among many within a single Times head, and it's not at the top level of the expression inside Hold.

Internal h:Hold[Times[9,1/6,5]]→No direct match for 1/6 as a subexpression.

Successful Replacement (Copy-Paste): When you copy and paste the output, the Frontend often re-parses the expression and might implicitly change the grouping or re-introduce a Times head, making 1/6 exist as a distinct, replaceable subexpression again:

Pasted Input:Hold[1/6⋅(rest of expression)]→Match succeeds. The expression you pasted looks the same, but its internal tree structure was slightly modified during the copy/paste process, allowing the pattern matcher to finally see 1/6 as an independent part.

h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

Update 1: Why HoldPattern[3/7] Works

While 3/7 is internally represented as Times[3, Power[7, -1]] (which you correctly saw in FullForm), the pattern HoldPattern[3/7] works because it instructs the pattern matcher to look for the expression that originated as 3/7 , without attempting to evaluate or strictly enforce the internal form on the pattern itself.

Update 2: Copy-Paste Anomaly (Output vs. Input)

Your update is an excellent observation that uncovers a core subtlety: Output Formatting is NOT Input. The difference in behavior is due to how the Frontend (Notebook interface) handles held expressions:

Initial Failure (h reference): When you run h /. HoldPattern[1/6] -> q, the replacement fails because the Kernel's Times sorting (Orderless) groups all factors. 1/6 doesn't exist as a separate replaceable argument; it's just one factor among many within a single Times head, and it's not at the top level of the expression inside Hold.

Internal h:Hold[Times[9,1/6,5]]→No direct match for 1/6 as a subexpression.

Successful Replacement (Copy-Paste): When you copy and paste the output, the Frontend often re-parses the expression and might implicitly change the grouping or re-introduce a Times head, making 1/6 exist as a distinct, replaceable subexpression again:

Pasted Input:Hold[1/6⋅(rest of expression)]→Match succeeds. The expression you pasted looks the same, but its internal tree structure was slightly modified during the copy/paste process, allowing the pattern matcher to finally see 1/6 as an independent part.

Additional explanation.
Source Link
E. Chan-López
  • 34.3k
  • 3
  • 38
  • 55
h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

Update 1:

While 3/7 is internally represented as Times[3, Power[7, -1]] (which you correctly saw in FullForm), the pattern HoldPattern[3/7] works because it instructs the pattern matcher to look for the expression that originated as 3/7 , without attempting to evaluate or strictly enforce the internal form on the pattern itself.

Update 2:

Your update is an excellent observation that uncovers a core subtlety: Output Formatting is NOT Input. The difference in behavior is due to how the Frontend (Notebook interface) handles held expressions:

Initial Failure (h reference): When you run h /. HoldPattern[1/6] -> q, the replacement fails because the Kernel's Times sorting (Orderless) groups all factors. 1/6 doesn't exist as a separate replaceable argument; it's just one factor among many within a single Times head, and it's not at the top level of the expression inside Hold.

Internal h:Hold[Times[9,1/6,5]]→No direct match for 1/6 as a subexpression.

Successful Replacement (Copy-Paste): When you copy and paste the output, the Frontend often re-parses the expression and might implicitly change the grouping or re-introduce a Times head, making 1/6 exist as a distinct, replaceable subexpression again:

Pasted Input:Hold[1/6⋅(rest of expression)]→Match succeeds. The expression you pasted looks the same, but its internal tree structure was slightly modified during the copy/paste process, allowing the pattern matcher to finally see 1/6 as an independent part.

h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

Update:

While 3/7 is internally represented as Times[3, Power[7, -1]] (which you correctly saw in FullForm), the pattern HoldPattern[3/7] works because it instructs the pattern matcher to look for the expression that originated as 3/7 , without attempting to evaluate or strictly enforce the internal form on the pattern itself.

h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

Update 1:

While 3/7 is internally represented as Times[3, Power[7, -1]] (which you correctly saw in FullForm), the pattern HoldPattern[3/7] works because it instructs the pattern matcher to look for the expression that originated as 3/7 , without attempting to evaluate or strictly enforce the internal form on the pattern itself.

Update 2:

Your update is an excellent observation that uncovers a core subtlety: Output Formatting is NOT Input. The difference in behavior is due to how the Frontend (Notebook interface) handles held expressions:

Initial Failure (h reference): When you run h /. HoldPattern[1/6] -> q, the replacement fails because the Kernel's Times sorting (Orderless) groups all factors. 1/6 doesn't exist as a separate replaceable argument; it's just one factor among many within a single Times head, and it's not at the top level of the expression inside Hold.

Internal h:Hold[Times[9,1/6,5]]→No direct match for 1/6 as a subexpression.

Successful Replacement (Copy-Paste): When you copy and paste the output, the Frontend often re-parses the expression and might implicitly change the grouping or re-introduce a Times head, making 1/6 exist as a distinct, replaceable subexpression again:

Pasted Input:Hold[1/6⋅(rest of expression)]→Match succeeds. The expression you pasted looks the same, but its internal tree structure was slightly modified during the copy/paste process, allowing the pattern matcher to finally see 1/6 as an independent part.

Additional explanation.
Source Link
E. Chan-López
  • 34.3k
  • 3
  • 38
  • 55
h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

Update:

While 3/7 is internally represented as Times[3, Power[7, -1]] (which you correctly saw in FullForm), the pattern HoldPattern[3/7] works because it instructs the pattern matcher to look for the expression that originated as 3/7 , without attempting to evaluate or strictly enforce the internal form on the pattern itself.

h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

Update:

While 3/7 is internally represented as Times[3, Power[7, -1]] (which you correctly saw in FullForm), the pattern HoldPattern[3/7] works because it instructs the pattern matcher to look for the expression that originated as 3/7 , without attempting to evaluate or strictly enforce the internal form on the pattern itself.

Source Link
E. Chan-López
  • 34.3k
  • 3
  • 38
  • 55
Loading