Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

For this particular case you could also use the form _Plus, which matches any expression with head Plus but not Plus itself:

Sqrt[Plus[x, y]] /. _Plus -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described herehere. An application of that might look like this:

ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

For this particular case you could also use the form _Plus, which matches any expression with head Plus but not Plus itself:

Sqrt[Plus[x, y]] /. _Plus -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described here. An application of that might look like this:

ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

For this particular case you could also use the form _Plus, which matches any expression with head Plus but not Plus itself:

Sqrt[Plus[x, y]] /. _Plus -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described here. An application of that might look like this:

ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u
added 193 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

For this particular case you could also use the form _Plus, which matches any expression with head Plus but not Plus itself:

Sqrt[Plus[x, y]] /. _Plus -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described here. An application of that might look like this:

ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described here. An application of that might look like this:

ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

For this particular case you could also use the form _Plus, which matches any expression with head Plus but not Plus itself:

Sqrt[Plus[x, y]] /. _Plus -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described here. An application of that might look like this:

ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u
added 120 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described here. An application of that might look like this:

ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described here.

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described here. An application of that might look like this:

ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u
added 33 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
deleted 12 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
added 141 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading