Skip to content

replace() is mis-matching patterns #19

@bengolds

Description

@bengolds

replace doesn't seem to handle wildcards as documented. Here are two cases where it fails, in slightly different ways:

1 Rewrite "ax + bx" as "(a+b)*x"

const rewriteRule = [
  ['Add', ['Multiply', '_const1', '_var'], ['Multiply', '_const2', '_var']],
  ['Multiply', ['Add', '_const1', '_const2'], '_var']
];

c.replace([myRule], parse('2x+3x'));
// returns [ 'Multiply', [ 'Add', { num: '2' }, { num: '3' } ], 'x' ]
// Good!

c.replace([myRule], parse('2x+x*3');
// returns [ 'Multiply', 3, [ 'Add', 'x', 2 ] ]
// Not good....

It seems like it's not making sure _var is matching the same value.

2 Not handling commutativity:

const rewriteRule = [['Add', '_a', '_a', '_b'], ['Multiply, '_a', '_b']];  // Not valid math, just wanted to test commutativity.

c.replace([commutativity], ['Add', 1, 1, 'x']);
// returns ['Multiply', 1, 'x']
// Good!

c.replace([commutativity], ['Add', 1, 'x', 1]);
// returns ['Add', 1, 'x', 1]
// Not good...

On version 0.4.2.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions