-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Export the simplify utils as math.simplify.utils
#2282
base: develop
Are you sure you want to change the base?
Export the simplify utils as math.simplify.utils
#2282
Conversation
math.simplify.utils
math.simplify.utils
Thanks @joshhansen . Those functions are really specific for the internals of Can you explain your use case? |
I'd like to add a +1 to this, specifically for the sake of simplify.utils.flatten. I am using mathjs for translating formulas into canonical strings to use as database keys. The first step is simplify to get (many) equivalent formulas into the same form. The next step will be to flatten -- right now, I have had to re-write my own flatten method; it would be better to use the existing one. Then after that I reorder the arguments to commutative operations in a canonical way (so in fact isCommutative will be useful to have, too!) and finally I convert the whole thing to RPN to produce my final key. |
@gwhitney sounds good to me to expose it. Before we can merge this PR we have to document |
245462a
to
bc80676
Compare
Hi, I'm still interested in this. I'll add my typescript typings and try to write some docs. I'll add doc comments to the functions involved that lack them, but I take it something more is required to get the docs to generate? I don't see e.g. Question: why is |
Another question: is |
Since I know Jos has limited time, I'll take a stab at these latest questions; of course, my responses are just attempts to help and express opinions, not authoritative.
Have you tried
I do not know. I can confirm from my work on #2391 that changing this default to false does not break any unit tests. But that doesn't really answer your question.
Well, math.flatten is the function that takes a 2D (or higher-dimensional array) and turns it into a 1D array. I think it would seem odd for just "flatten" to deal with arrays and "utils.flatten" to deal with parse trees. And it seems to me the behavior of the functions is different enough that they should not be collapsed into the same Typed function. So that means the expression-tree 'flatten' is going to need some prefix that indicates what realm it lives in. And since all of these are defined together, it seems like they should all have the same prefix, that should indicate their syntax-related nature. Given those considerations, the "simplify.utils" prefix seems as good as any to me, although something like "syntax.utils" or "expression.utils" seems like it would be fine as well -- just neither "syntax" or "expression" is currently used as a prefix, whereas "simplify" is, so why add a new one? |
Yes true, and frustrating at times. But some big news: starting March 1st, I'll start working full time on my open source projects 🎉 . Can't wait for it 😁
Good question. So far, we try to have a flat list with functions in mathjs, and not nested things like
I have no idea either. I guess we can change it if it doesn't make sense and doesn't break any existing behavior. |
On
|
@gwhitney how do think about keeping all functions of mathjs in a single, flat list? So not |
Your desired naming convention seems fine to me, and I think your judgment should be primary in this sort of thing. In that case, I'd suggest being conservative as to which of the utils move out to the top level: definitely flatten, isCommutative, isAssociative. And then yes, flatten needs a new name. I'd suggest |
Thanks, sounds good 👍 . The names you suggest make sense to me: |
Alright, so, summarizing:
As for the location in the source, Some of these mutate their arguments in place, which goes against the grain of e.g. I'll get to work on the refactor! |
Tangentially: @gwhitney Your use case and mine sound fairly similar. We are doing a lot of canonicalizing of expressions and equations, for equality comparison purposes. I wonder if we might want to collaborate, maybe build an adjunct library to mathjs. Unless mathjs wants to host canonicalization functionality? @josdejong |
Responding: On canonicalize - (1) if all you are after is testing expression equality, I have read that simplifying the difference to 0 is often a more effective method, so you might want to try |
P.S. on the summary of changes -- I should think that once they are top-level functions, those that are should have their source directly in src/functions/algebra. And I agree, for consistency with everything else in mathjs, public functions should not mutate their arguments in place. So perhaps the top-level functions are just wrappers on things from utils, and the utils remain mostly in place (in which case if simplify is just using the util versions, it could continue to load those directly rather than registering a dependency on the top level versions). |
Thanks guys, for all the inputs and work 👍 On building an adjunt library to mathjs for canonicalization: I have the feeling that the current simplification functionality fits just fine in mathjs. I can imagine thought that the CAS system grows bigger and bigger and at some point becomes a thing on it's own. If there are good reasons to split it into a separate repository we could consider that then, just like Eric is working on a standalone units library https://github.com/ericman314/UnitMath that hopefully will replace the older Unit functionality. Feel free to open a separate discussion on separating the CAS functionality from mathjs if you feel that has good benefits. |
@joshhansen is this still on your plate to wrap up at some point? As far as I can see the outstanding issues are:
Feel free to check off anything that is done and/or add any tasks I have missed. |
As I ultimately need these functions exposed, I am willing to complete the task list above to get this ready to merge. |
Thanks Glen! |
This exports the utility functions in
math.simplify
asmath.simplify.utils
.I have found specific use for
isCommutative
,isAssociative
, andflatten
and would rather not implement them myself. The others seem like they might also be useful to people, except maybe notcreateMakeNodeFunction
, but for now it's along for the ride.I can get some Typescript annotations for you as well if you think this is a worthwhile endeavor.