Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Comments

Expand exponent#194

Merged
ldworkin merged 9 commits intomasterfrom
al_root_squared
Jul 13, 2017
Merged

Expand exponent#194
ldworkin merged 9 commits intomasterfrom
al_root_squared

Conversation

@aliang8
Copy link
Contributor

@aliang8 aliang8 commented Jul 12, 2017

Fix the issue where (nthRoot(x,2))^2 does not evaluate to x (returns no steps).

@aliang8 aliang8 mentioned this pull request Jul 12, 2017
// e.g. 2 * 4x + 2*5 --> 8x + 10 (as part of distribution)
SIMPLIFY_TERMS: 'SIMPLIFY_TERMS',
// e.g. (nthRoot(x, 2))^2 -> nthRoot(x, 2) * nthRoot(x, 2)
EXPAND_EXPONENT: 'EXPAND_EXPONENT',
Copy link
Contributor

@ldworkin ldworkin Jul 12, 2017

Choose a reason for hiding this comment

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

Can you add (2x + 3)^2 -> (2x + 3) (2x + 3) as an example here too? Just cause that might be the more common use case.

}
}

// Expand a power node with a non-constant base
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome docstring 💯

return Node.Status.noChange(node);
}

// If the base is an nthRoot node, it doesn't need the parenthesis
Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely not a big deal, so just mentioning this to make you an even more amazing coder: to eliminate code repetition, you could instead do 1) expandedBase = base if nthRoot else node.args[0] 2) expandedNode = Node.creator.operator ... fill(expandedBase)


describe('solveEquation errors', function() {
const tests = [
['( x + 2) ^ ( 2) - x ^ ( 2) = 4( x + 1)']
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move this test case somewhere else now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, moved.

// ['(nthRoot(x, 2))^3', 'nthRoot(x ^ 3, 2)'],
// ['3 * nthRoot(x, 2) * (nthRoot(x, 2))^2', '3 * nthRoot(x ^ 3, 2)'],
// TODO: fix when base has multiplication
// ['(nthRoot(x, 2) * nthRoot(x, 2))^2', 'x^2'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Why doesn't this work? The inner part should just evaluate to x right (you fixed this in another recent PR), and then you have (x)^2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not exactly, it evaluates to (nthRoot(x^2,2)^2 and then it does (nthRoot(x^2,2) * (nthRoot(x^2, 2)

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, so when we support nthRoot(x^2,2) -> x then it will work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We already support that. I think we're distributing before evaluating nthRoots so that's why we take that route.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok I'm quite confused. Does this case work or not? If not, why not, and can you fix? If so, why is the test commented out?

Copy link
Contributor

Choose a reason for hiding this comment

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

also curious!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not exactly. This is because distribution code runs before function (nthRoot) search. So we would do (nthRoot(x^2 , 2)^2 -> nthRoot(x^2, 2) * nthRoot(x^2, 2).

@ldworkin
Copy link
Contributor

Really pleased with this, well done @aliang8 ! Great comments and test cases. And code was very readable. Just left a few things to address.

['3 * (nthRoot(x, 2))^2', '3x'],
['(nthRoot(x, 2))^6 * (nthRoot(x, 3))^3', 'x^4'],
['(2x + 3)^2','4x^2 + 12x + 9'],
['(x + 3 + 4)^2', 'x^2 + 14x + 49'],
Copy link
Contributor

Choose a reason for hiding this comment

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

if this does what #187 does can you add the tests from that PR here too?

Copy link
Contributor Author

@aliang8 aliang8 Jul 12, 2017

Choose a reason for hiding this comment

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

I did not because apparently there are no substeps for (x-2)^2 anymore. But I will add them into simplify.test.js.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, well shouldn't we have substeps for (x-2)^2? Is this a unary minus issue? If so I think we should fix it.

Copy link
Contributor

Choose a reason for hiding this comment

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

(Just discussed over slack, this does actually work, we're fine)

@ldworkin
Copy link
Contributor

@evykassirer once @aliang8 addresses my confusion above, can this be approved/merged?

@evykassirer
Copy link
Contributor

sorry was falling behind :( didn't get time to review this today. Figured I'd wait for your review to get resolved. If it's urgent feel free to merge, otherwise I might be able to get to it tomorrow before I head to work or latest after work

Copy link
Contributor

@evykassirer evykassirer left a comment

Choose a reason for hiding this comment

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

nvm looked anyways cause I felt bad but also it was small and very clean! looks good to me, the comments are just little cleanups if you want to do them

great work 🎉

}
else if (Node.Type.isOperator(node)) {
else if (Node.Type.isOperator(node, '*')) {
return distributeAndSimplifyOperationNode(node);
Copy link
Contributor

Choose a reason for hiding this comment

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

you could rename this to distributeAndSimplifyMultiplication now


if (!Node.Type.isFunction(base, 'nthRoot') && !Node.Type.isOperator(base, '+')) {
return Node.Status.noChange(node);
}
Copy link
Contributor

@evykassirer evykassirer Jul 13, 2017

Choose a reason for hiding this comment

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

this code is so cleannnn ✨ so easy to read

// If the base is an nthRoot node, it doesn't need the parenthesis
const expandedBase = Node.Type.isFunction(base, 'nthRoot')
? base
: node.args[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: might be a bit more readable to explicitly do Node.Creator(base)

['3 * (nthRoot(x, 2))^4', '3 * nthRoot(x, 2) * nthRoot(x, 2) * nthRoot(x, 2) * nthRoot(x, 2)'],
['(nthRoot(x, 2) + nthRoot(x, 3))^2', '(nthRoot(x, 2) + nthRoot(x, 3)) * (nthRoot(x, 2) + nthRoot(x, 3))'],
['(2x + 3)^2', '(2x + 3) * (2x + 3)'],
['(x + 3 + 4)^2', '(x + 3 + 4) * (x + 3 + 4)']
Copy link
Contributor

Choose a reason for hiding this comment

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

could be worth testing some cases that shouldn't expand here too

// ['(nthRoot(x, 2))^3', 'nthRoot(x ^ 3, 2)'],
// ['3 * nthRoot(x, 2) * (nthRoot(x, 2))^2', '3 * nthRoot(x ^ 3, 2)'],
// TODO: fix when base has multiplication
// ['(nthRoot(x, 2) * nthRoot(x, 2))^2', 'x^2'],
Copy link
Contributor

Choose a reason for hiding this comment

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

also curious!

// -> x * x -> x
['(nthRoot(x, 2) * nthRoot(x, 2))^2', 'x^2'],
// TODO: fix nthRoot to evaluate nthRoot(x^3, 2)
// ['(nthRoot(x, 2))^3', 'nthRoot(x ^ 3, 2)'],
Copy link
Contributor

Choose a reason for hiding this comment

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

This test case also works? Why is it commented out?

['(nthRoot(x, 2) * nthRoot(x, 2))^2', 'x^2'],
// TODO: fix nthRoot to evaluate nthRoot(x^3, 2)
// ['(nthRoot(x, 2))^3', 'nthRoot(x ^ 3, 2)'],
// ['3 * nthRoot(x, 2) * (nthRoot(x, 2))^2', '3 * nthRoot(x ^ 3, 2)'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here ...

@ldworkin ldworkin merged commit a5ac2da into master Jul 13, 2017
@ldworkin ldworkin deleted the al_root_squared branch July 13, 2017 16:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants