Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify sqrt(x)*sqrt(x) #3231

Open
petervanwylen opened this issue Jul 10, 2024 · 4 comments
Open

simplify sqrt(x)*sqrt(x) #3231

petervanwylen opened this issue Jul 10, 2024 · 4 comments

Comments

@petervanwylen
Copy link

I asked it to simplify sqrt(x)*sqrt(x) and it couldn't do it. Nor could it do really anything with sqrt. As soon as I added the following single rule, it works perfectly and does all the things you would hope it should know how to do!

const simpRules = [...(math.simplify.rules)];
simpRules.push('sqrt(n1) -> n1^0.5');
math.simplify(exp,simpRules);

I suggest adding 'sqrt(n1) -> n1^0.5' to simplify.js

@josdejong
Copy link
Owner

josdejong commented Jul 11, 2024

Thanks for your suggestion. I think adding a rule sqrt(n1) -> n1^0.5 is not always seen as a simplified result.

How about sqrt(n1)^2 -> n1 ?

Anyone interested in creating a PR adding (and testing) this rule?

@josdejong josdejong changed the title sqrt simplification rules missing simplify sqrt(x)*sqrt(x) Jul 11, 2024
@petervanwylen
Copy link
Author

The main thing I'm after here is getting symbolicEqual to handle sqrt properly. Your suggestion of sqrt(n1)^2 -> n1 definitely cleans up some things, but it doesn't help the simplification engine understand that sqrt (and cube root function for that matter) are basically just powers. As soon as you give it my sqrt(n1) -> n1^0.5 then it can use all its power simplification rules and it'll return true for symbolicEqual("x^2*sqrt(x)","x^2.5"). Essentially I'm thinking about the symbolicEqual use case and the only thing I could do to make it handle sqrt intelligently was to give it a hint that sqrt is just shorthand for ^0.5.

I realize it's a bigger project/idea, but it's almost like you need the simplification engine to use all the simplification rules including sqrt(n1) -> n1^0.5 as it does its best to simplify and then at the very end it does one final pass with a list of "display rules" which are just math notation conventions. The display rule would undo this and be n1^0.5 -> sqrt(n1) such that if ^0.5 remains anywhere in the problem it goes back to being a sqrt for display convention purposes. This is because the underlying mathematical concept is the power, but we have certain shorthands like square root and cube root for displaying powers that are 1/2 or 1/3.

Human preference is to display sqrt instead of ^0.5 but the computer's preference for figuring out what it all "means" is ^0.5 instead of sqrt. Let the computer figure out simplification with powers, let people see the final answer the way they were taught to expect it their whole life.

@petervanwylen
Copy link
Author

I'm happy to do the PR once I know what your preference is for how this is handled.

@josdejong
Copy link
Owner

josdejong commented Jul 24, 2024

As soon as you give it my sqrt(n1) -> n1^0.5 then it can use all its power simplification rules

Yes that makes sense.

I think we can take three approaches here:

  1. leave things as they are right now: leave it up to people themselves to write x^(1/2) instead of using sqrt(x) if they need it simplified.
  2. Just add the rule sqrt(n1) -> n1^0.5 and await whether people start complaining about sqrt(x) being simplified into (x)^(1/2).
  3. Apply simplification in two stages: first, a normalization step where we apply sqrt(n1) -> n1^0.5 (and also other normalization rules). Then, apply all the actual simplification rules. And lastly, convert cases like n1^0.5 -> sqrt(n1) again into the most compact, human readable form. We've discussed these ideas before, see: Extend simplify with rules for `log(e)=1`, `sin(pi)=0`, etc #2725.

I think that option (3) would be best in long term, and will most likely improve the simplification process over all. But it may be a lot of work. So before we go in that direction, I think we should do a little proof of concept to figure out what it involves and how it will shape up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants