Skip to content

fix(simplify): Array and object simplification#2382

Merged
josdejong merged 4 commits into
josdejong:developfrom
gwhitney:simplify_arrays_indices
Jan 15, 2022
Merged

fix(simplify): Array and object simplification#2382
josdejong merged 4 commits into
josdejong:developfrom
gwhitney:simplify_arrays_indices

Conversation

@gwhitney
Copy link
Copy Markdown
Collaborator

Allows simplification of expressions inside arrays, objects, and indices; handles operations on constant arrays.

Partially or possibly fully addresses #1913.

gwhitney and others added 4 commits January 12, 2022 17:22
  Mostly ArrayNode, ObjectNode, AccessorNode, and IndexNode nodes are
  simply transparent to simplification -- they simply allow it to occur
  within subexpressions. Then main exception is that if an array or object
  is indexed by a constant, the expression can be replaced by the
  corresponding subitem, e.g. `[3,4,5][2]` simplifies to `4`.

  This at least partially resolves josdejong#1913 (see my latest comment there).
  This involves allowing ArrayNodes containing only constant entries
  to temporarily convert to Matrix type inside of simplifyConstant, so that
  function and operator calls can occur on them.
  I also had to add a special case for the function `size` because
  it can be computed even on symbolic arrays, since the result depends
  only on the shape, not the entries.

  Deals with additional cases of josdejong#1913; unclear if there are remaining
  aspects of that issue on which further work is desirable.
  And restores inadvertent deletion of a blank line.

/* Handles constant indexing of ArrayNodes, matrices, and ObjectNodes */
function _foldAccessor (obj, index, options) {
if (!isIndexNode(index)) { // don't know what to do with that...
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gwhitney I see your comment, does this case still need attention, or is the current behavior ok?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't even know what the semantics are if the index member isn't an IndexNode object. (I don't even know if it could possibly occur that the index is not an IndexNode object.) So given that, there's no possible simplification I could think of performing. That was supposed to be the import of the comment. So let me know whether to leave the comment, delete it, or replace it with // is this even possible?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your implementation is correct. When using an expression parsed with the expression parser I don't think it can occur that index is not an IndexNode. Maybe when someone has manually created all the nodes and simplifies that it could accidentally occur.

Thinking about it, I think you can just leave the comment as is: when the index is not an index node, it makes sense that simplify cannot know what to do with it, and best is to just leave it as is.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, then is there anything else I need to do for this PR so that you can merge it? Thanks for creating mathjs, by the way, it's a big help to our OEIS visualization project.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no all is ready to merge, I'll click the button 👍

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for creating mathjs, by the way, it's a big help to our OEIS visualization project.

Thanks, good to hear 😄

@josdejong
Copy link
Copy Markdown
Owner

Impressive piece of work Glen, thanks a lot. It is really awesome to see expressions like [1,2]+[3,4] resolved into [4,6] 😎

I made one minor comment, but all looks good to merge 👍

@josdejong josdejong merged commit a2fd057 into josdejong:develop Jan 15, 2022
@josdejong
Copy link
Copy Markdown
Owner

Published now in v10.1.0. Really nice to see simplify be improved so much Glen 👍

@gwhitney
Copy link
Copy Markdown
Collaborator Author

Well, simplify is critical to our project as we use it to canonicalize sequence expressions for lookup/caching in our database of sequences. So in fact, you ain't seen nothin' yet ;-) (well, I mean just that there are still a number of ways we're hoping to enhance it).

@josdejong
Copy link
Copy Markdown
Owner

Ha ha, that sounds promising 😄 . I'll try to keep up with you (but all is spare-time work on my side, time is limited).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants