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

Overlap of behavior between simplifyCore and simplifyConstant? #2459

Closed
gwhitney opened this issue Mar 2, 2022 · 3 comments
Closed

Overlap of behavior between simplifyCore and simplifyConstant? #2459

gwhitney opened this issue Mar 2, 2022 · 3 comments
Labels
category:symbolic Issues regarding symbolic operations like symbolic equation solving or symbolic differentiation

Comments

@gwhitney
Copy link
Collaborator

gwhitney commented Mar 2, 2022

Currently, the operations of simplifyCore and simplifyConstant overlap with each other, but neither subsumes the other:

expression simplifyConstant simplifyCore
x+0 x+0 x
1+2 3 3
[1,2]+[3,4] [4,6] [1,2]+[3,4]

Basically, simplifyConstant doesn't touch any non-constant subexpressions, whereas simplifyCore does do some straightforward one-pass simplifications like 1*n -> n. On the other hand, simplifyConstant tries essentially to fully evaluate all subexpressions that involve only constants (not certain that it's yet 100% comprehensive, but that seems to be the aspiration), whereas simplifyCore just collapses a few node types all of whose children are just numeric constants.

This seems a bit confusing to me, and not the most "clean." It seems to me that either simplifyCore should do everything that simplifyConstant does plus a bit more, for the most obvious straightforward things like additive and multiplicative identities; or simplifyCore should not touch constant-only subexpressions and leave them to simplifyConstant to handle, focusing just on the things that simplifyConstant can't do.

Do you agree? If so, please just let me know which organization of the behavior of simplifyCore and simplifyConstant you prefer, and I will file a PR establishing and documenting that relationship between them. One final issue is that simplifyCore is exported as a mathJS function but simplifyConstant is not. If we go with simplifyCore subsuming simplifyConstant, that seems fine; but if they are changed to be disjoint, or if the current status quo is otherwise left, then I would recommend exporting simplifyConstant so that mathjs clients can obtain the behavior of fully simplifying constant expressions.

Looking forward to your feedback.

@gwhitney gwhitney added design decision category:symbolic Issues regarding symbolic operations like symbolic equation solving or symbolic differentiation labels Mar 2, 2022
@josdejong
Copy link
Owner

I understand the confusion. If simplifyConstant does not have a clear meaningful purpose it could be merged into simplifyCore. Or maybe some other abstraction makes more sense. (on the other hand, if simplifyCore grows too large and does do too much it can also become complicated again).

Feel free to play around and see if a refactoring works out nicely 👍 . By now I think you're more authoritative in this piece of code than me (I have never worked on the code myself 😉), so please let me know what you think would be a better separation of functionality.

@gwhitney
Copy link
Collaborator Author

gwhitney commented Mar 9, 2022

I think that simplifyConstant is a very natural operation, partly because of its role in the demo symbolic computation #2470, and is worth having in its own right. I don't see any need to partially replicate its behavior in simplifyCore. But there is a need for a function that performs a number of "non-controversial" single-pass simplifications, that can be used as one rule in the full-blown simplify. So when I have a chance I will issue a PR that separates their behavior and exports them both as mathjs typed functions.

@josdejong
Copy link
Owner

Thanks 👍

gwhitney added a commit that referenced this issue Mar 20, 2022
  Now that simplifyCore does not do any constant folding, clients may
  wish to access that behavior via simplifyConstant. Moreover, exporting it
  makes it easier to use in custom rule lists for simplify().

  Also adds docs, embedded docs, and tests for simplifyConstant().

  Also fixes simplifyCore() on logical functions (they always return boolean,
  rather than "short-circuiting").

  Resolves #2459.
josdejong pushed a commit that referenced this issue Mar 21, 2022
* refactor: don't simplify constants in simplifyCore

  Keeps the operation of simplifyCore cleanly separate from
  simplifyConstant.

* fix; handle multiple consecutive operations in simplifyCore()

   Also adds support for logical operators.
   Resolves #2484.

* feat: export simplifyConstant

  Now that simplifyCore does not do any constant folding, clients may
  wish to access that behavior via simplifyConstant. Moreover, exporting it
  makes it easier to use in custom rule lists for simplify().

  Also adds docs, embedded docs, and tests for simplifyConstant().

  Also fixes simplifyCore() on logical functions (they always return boolean,
  rather than "short-circuiting").

  Resolves #2459.
gwhitney added a commit that referenced this issue Apr 13, 2022
* refactor: don't simplify constants in simplifyCore

  Keeps the operation of simplifyCore cleanly separate from
  simplifyConstant.

* fix; handle multiple consecutive operations in simplifyCore()

   Also adds support for logical operators.
   Resolves #2484.

* feat: export simplifyConstant

  Now that simplifyCore does not do any constant folding, clients may
  wish to access that behavior via simplifyConstant. Moreover, exporting it
  makes it easier to use in custom rule lists for simplify().

  Also adds docs, embedded docs, and tests for simplifyConstant().

  Also fixes simplifyCore() on logical functions (they always return boolean,
  rather than "short-circuiting").

  Resolves #2459.
gwhitney added a commit that referenced this issue Apr 30, 2022
* refactor: don't simplify constants in simplifyCore

  Keeps the operation of simplifyCore cleanly separate from
  simplifyConstant.

* fix; handle multiple consecutive operations in simplifyCore()

   Also adds support for logical operators.
   Resolves #2484.

* feat: export simplifyConstant

  Now that simplifyCore does not do any constant folding, clients may
  wish to access that behavior via simplifyConstant. Moreover, exporting it
  makes it easier to use in custom rule lists for simplify().

  Also adds docs, embedded docs, and tests for simplifyConstant().

  Also fixes simplifyCore() on logical functions (they always return boolean,
  rather than "short-circuiting").

  Resolves #2459.
gwhitney added a commit that referenced this issue May 12, 2022
* refactor: don't simplify constants in simplifyCore

  Keeps the operation of simplifyCore cleanly separate from
  simplifyConstant.

* fix; handle multiple consecutive operations in simplifyCore()

   Also adds support for logical operators.
   Resolves #2484.

* feat: export simplifyConstant

  Now that simplifyCore does not do any constant folding, clients may
  wish to access that behavior via simplifyConstant. Moreover, exporting it
  makes it easier to use in custom rule lists for simplify().

  Also adds docs, embedded docs, and tests for simplifyConstant().

  Also fixes simplifyCore() on logical functions (they always return boolean,
  rather than "short-circuiting").

  Resolves #2459.
gwhitney added a commit that referenced this issue May 31, 2022
* refactor: don't simplify constants in simplifyCore

  Keeps the operation of simplifyCore cleanly separate from
  simplifyConstant.

* fix; handle multiple consecutive operations in simplifyCore()

   Also adds support for logical operators.
   Resolves #2484.

* feat: export simplifyConstant

  Now that simplifyCore does not do any constant folding, clients may
  wish to access that behavior via simplifyConstant. Moreover, exporting it
  makes it easier to use in custom rule lists for simplify().

  Also adds docs, embedded docs, and tests for simplifyConstant().

  Also fixes simplifyCore() on logical functions (they always return boolean,
  rather than "short-circuiting").

  Resolves #2459.
gwhitney added a commit that referenced this issue May 31, 2022
* refactor: don't simplify constants in simplifyCore

  Keeps the operation of simplifyCore cleanly separate from
  simplifyConstant.

* fix; handle multiple consecutive operations in simplifyCore()

   Also adds support for logical operators.
   Resolves #2484.

* feat: export simplifyConstant

  Now that simplifyCore does not do any constant folding, clients may
  wish to access that behavior via simplifyConstant. Moreover, exporting it
  makes it easier to use in custom rule lists for simplify().

  Also adds docs, embedded docs, and tests for simplifyConstant().

  Also fixes simplifyCore() on logical functions (they always return boolean,
  rather than "short-circuiting").

  Resolves #2459.
gwhitney added a commit that referenced this issue Jul 13, 2022
* refactor: don't simplify constants in simplifyCore

  Keeps the operation of simplifyCore cleanly separate from
  simplifyConstant.

* fix; handle multiple consecutive operations in simplifyCore()

   Also adds support for logical operators.
   Resolves #2484.

* feat: export simplifyConstant

  Now that simplifyCore does not do any constant folding, clients may
  wish to access that behavior via simplifyConstant. Moreover, exporting it
  makes it easier to use in custom rule lists for simplify().

  Also adds docs, embedded docs, and tests for simplifyConstant().

  Also fixes simplifyCore() on logical functions (they always return boolean,
  rather than "short-circuiting").

  Resolves #2459.
gwhitney added a commit that referenced this issue Jul 15, 2022
* refactor: don't simplify constants in simplifyCore

  Keeps the operation of simplifyCore cleanly separate from
  simplifyConstant.

* fix; handle multiple consecutive operations in simplifyCore()

   Also adds support for logical operators.
   Resolves #2484.

* feat: export simplifyConstant

  Now that simplifyCore does not do any constant folding, clients may
  wish to access that behavior via simplifyConstant. Moreover, exporting it
  makes it easier to use in custom rule lists for simplify().

  Also adds docs, embedded docs, and tests for simplifyConstant().

  Also fixes simplifyCore() on logical functions (they always return boolean,
  rather than "short-circuiting").

  Resolves #2459.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:symbolic Issues regarding symbolic operations like symbolic equation solving or symbolic differentiation
Projects
None yet
Development

No branches or pull requests

2 participants