Message Boards Message Boards

0
|
3608 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Can I prevent numbers from being pulled out of a product inside Sqrt?

Posted 10 years ago
If I write an expression like Sqrt[2 a], Mathematica apparently automatically transforms it into Sqrt[2]Sqrt.  Is there a way to prevent this?

In: Sqrt[2 a] // InputForm
Out: Sqrt[2]*Sqrt

The context: I am trying to write a rule that will take the product of several square roots and turn it into the square root of the product.  This works fine for symbols:
In: Sqrt Sqrt Sqrt //. Sqrt[x_] Sqrt[y_] -> Sqrt[x y] // InputForm
Out: Sqrt

However, it doesn't work if there's a number, e.g.
In: Sqrt[2] Sqrt Sqrt //. Sqrt[x_] Sqrt[y_] -> Sqrt[x y] // InputForm
Out: Sqrt[2]*Sqrt*Sqrt

After much puzzlement, I realized that what is probably happening is:
1) Mathematica uses the first application of the rule to transform Sqrt[2] Sqrt -> Sqrt[2 b]
2) Mathematica automatically splits this Sqrt[2 b] -> Sqrt[2] Sqrt
3) Since the output expression is the same as the input expression, ReplaceRepeated terminates, never processing Sqrt Sqrt.

I can partially fix this particular example with a more complicated rule, but the Sqrt[2] still stays separate

In: Sqrt[2] Sqrt Sqrt //. Sqrt[x_] Sqrt[y_] Sqrt[z_] -> Sqrt[x y z] // InputForm
Out: Sqrt[2]*Sqrt

Thanks,
Bill

P.S. - All the expressions inside the square roots have values that are real and positive, so this is mathematically legitimate! 
POSTED BY: William Simpson
6 Replies
Posted 10 years ago
Would it be acceptable for you to create a function that Mathematica doesn't know how to rewrite?
In[1]:= Sqrt[2] Sqrt[a] Sqrt[b] //. {sqrt[a_] Sqrt[b_] -> sqrt[a b], Sqrt[a_] Sqrt[b_] -> sqrt[a b]}

Out[1]= sqrt[2 a b]

In[2]:= Sqrt[2 a] //. {sqrt[a_] Sqrt[b_] -> sqrt[a b], Sqrt[a_] Sqrt[b_] -> sqrt[a b]}

Out[2]= sqrt[2 a]
If you later need to give that back to Mathematica then another rule to turn sqrt into Sqrt might be enough.
POSTED BY: Bill Simpson
Posted 10 years ago
Sorry folks.  In my previous post, lots of quantities in square brackets disappeared, completely garbling the Mathematica code.  I don't know how to prevent this or even to preview what the post is going to look like.  The short form of the question, replacing all the square brackets with parentheses is:

If I write an expression like Sqrt(2 a), Mathematica apparently automatically transforms it into Sqrt(2)Sqrt(a).  Is there a way to prevent this?

And now I have another question.  How do I put square brackets in a posting without risking them getting dropped?  As an experiment, I'm going to try using the "Insert Special Character" tool.  Test: Sqrt

Bill
POSTED BY: William Simpson
Posted 10 years ago
Use the little <> button near the right end of the second row of buttons to create a box to paste code into.
That also has problems, but fewer of those are silently erasing some characters in your code.
POSTED BY: Bill Simpson
Posted 10 years ago
Thanks for the suggestion. 

Now I have two more questions!

Is there a way that I can display your sqrt function with the usual square root sign?

Did you use the code box (which I just noticed) to prevent the brackets from getting lost?
In[119]:= Sqrt[a]/sqrt[b]

Out[119]= Sqrt[a]/sqrt[b]
 
POSTED BY: William Simpson
Posted 10 years ago
I'm sorry, but I don't "desktop publish" results, like trying to get that square root sign in there, because that only increases the difficulty and errors by a large scale factor.

Yes I used exactly that code box to try to prevent the forum software from mangling the code.
POSTED BY: Bill Simpson
Posted 10 years ago
Thanks for the help.  I'm learning.   
POSTED BY: William Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract