Comments (9)
I also want to apply this method to the equation, what do I have to do to get the following result:
equation: 2x+3x=5 step1: (2+3)*x=5; step2: 5x=5; step3: x=1
I tried to test with the above code, but I got the following error, how to fix it?Sorry, you're reaching beyond the limitations of the current mathy_core code. It has support for balanced move rules, but it looks like the util for finding preferred terms needs to be updated.
if not util.is_preferred_term_form(term):
File "/opt/miniconda3/envs/solver/lib/python3.9/site-packages/mathy_core/util.py", line 314, in is_preferred_term_form
if not is_simple_term(expression):
File "/opt/miniconda3/envs/solver/lib/python3.9/site-packages/mathy_core/util.py", line 280, in is_simple_term
sub_terms = get_sub_terms(node)
File "/opt/miniconda3/envs/solver/lib/python3.9/site-packages/mathy_core/util.py", line 239, in get_sub_terms
assert current is None or isinstance(
AssertionError: get_sub_terms(5x = 5) Expected Mul/Div/Power. Found: <class 'mathy_core.expressions.EqualExpression'Unfortunately, I can't help you with this further unless you make progress on your own. I love for people to use Mathy, but I can't provide step-by-step features and development for specific problems. That's why Mathy is open-source, so others can contribute to making it better.
The issue you're running into is that I added the balanced move rule for dealing with equations relatively late in development and never fully integrated it into the existing environments. The assertion error is likely because get_sub_terms needs adjusting to deal with EqualExpression nodes in the tree.
You could start here with the tests for that function: https://github.com/mathy/mathy_core/blob/master/tests/test_util.py#L13C31-L34
Thank you very much for your guidance. I will try to solve it by myself.
from mathy.
Best of luck, let us know how it turns out! I'll close this issue for now and leave the links to the new docs I added to the main site:
- https://mathy.ai/examples/heuristics/
- https://mathy.ai/examples/mathy_ppo/
- https://mathy.ai/examples/swarm_solver/
The first one is similar to what I posted here for you, and the other two are automated solvers using machine learning and planning algorithms.
from mathy.
Sure, I've got some updates for the main website coming, but in the meantime, you can check out the following:
- https://core.mathy.ai/ for information on the math tokenizer, parser, rules system, tree layout, builtin rules, etc
- https://envs.mathy.ai/ for information on the RL side of things, including the built-in environments and how to construct your own
Both of those sites have some inline code examples (that you can run directly) and API documentation about their various classes/modules. They also have their own separate repositories and test suites. They should be a good reference if you want further examples since both projects have high test coverage.
from mathy.
Sure, I've got some updates for the main website coming, but in the meantime, you can check out the following:
- https://core.mathy.ai/ for information on the math tokenizer, parser, rules system, tree layout, builtin rules, etc
- https://envs.mathy.ai/ for information on the RL side of things, including the built-in environments and how to construct your own
Both of those sites have some inline code examples (that you can run directly) and API documentation about their various classes/modules. They also have their own separate repositories and test suites. They should be a good reference if you want further examples since both projects have high test coverage.
Thank you very much for your patient guidance, but I find that I cannot complete the following test independently.
I want to implement the following step calculation of the formula, eg: 5+2*3-2*(3-1)
, expected output step-by-step:
First step: 5+2*3-2*2
;
Second step: 5+6-4
;
Final step: 7
;
If I wanted to implement the above example, how would I do it? Please give a detailed code example, thank you!
from mathy.
Here's an example of how to solve a slightly more complex problem using mathy core and some random tree transformation heuristics.
import random
from typing import List, Optional
from mathy_core import (
BaseRule,
ExpressionChangeRule,
ExpressionParser,
MathExpression,
util,
)
from mathy_core.rules import (
CommutativeSwapRule,
DistributiveFactorOutRule,
ConstantsSimplifyRule,
)
def is_simplified(expression: Optional[MathExpression]) -> bool:
"""If there are no like terms, consider the expression simplified."""
is_win = False
if expression is None:
return False
if not util.has_like_terms(expression):
term_nodes: List[MathExpression] = util.get_terms(expression)
is_win = True
term: MathExpression
for term in term_nodes:
if not util.is_preferred_term_form(term):
is_win = False
break
return is_win
def simplify_polynomial(input_text: str = "4x + y + 2x + 14x") -> str:
parser = ExpressionParser()
expression: Optional[MathExpression] = parser.parse(input_text)
rules: List[BaseRule] = [
# 1. Factor out common terms
DistributiveFactorOutRule(),
# 2. Simplify constants
ConstantsSimplifyRule(),
# 3. Commute terms
CommutativeSwapRule(),
]
steps = 0
limit = 100
last_action = "input"
while not is_simplified(expression) and steps < limit:
print(f"STEP[{steps}]: {last_action:<25} | {expression}")
steps += 1
for rule in rules:
options = rule.find_nodes(expression)
if len(options) > 0:
option = random.choice(options)
change: ExpressionChangeRule = rule.apply_to(option)
assert change.result is not None, "result should not be None"
expression = change.result.get_root()
last_action = rule.name
break
print(f"FINAL: {expression}")
return str(expression)
simplify_polynomial()
Outputs something like this (random):
STEP[0]: input | 4x + y + 2x + 14x
STEP[1]: Distributive Factoring | 4x + y + (2 + 14) * x
STEP[2]: Constant Arithmetic | 4x + y + 16x
STEP[3]: Commutative Swap | 4x + y + x * 16
STEP[4]: Commutative Swap | 4x + x * 16 + y
STEP[5]: Commutative Swap | x * 16 + 4x + y
STEP[6]: Commutative Swap | 16x + 4x + y
STEP[7]: Distributive Factoring | (16 + 4) * x + y
FINAL: 20x + y
When run it outputs a random solution to the problem, not the optimal one. For that you'd need another approach like a machine learning model, or the mathy CLIs swarm search functionality.
from mathy.
I want to implement the following step calculation of the formula, eg:
5+2*3-2*(3-1)
, expected output step-by-step:
First step:5+2*3-2*2
;
Second step:5+6-4
;
Final step:7
;
If I wanted to implement the above example, how would I do it? Please give a detailed code example, thank you!
The example I gave you does not solve this problem specifically (but may work for it anyway). If it doesn't work out of the box, it includes all the code you should need to adjust it for your problem. Let me know how it goes, good luck!
from mathy.
I want to implement the following step calculation of the formula, eg:
5+2*3-2*(3-1)
, expected output step-by-step:
First step:5+2*3-2*2
;
Second step:5+6-4
;
Final step:7
;
If I wanted to implement the above example, how would I do it? Please give a detailed code example, thank you!The example I gave you does not solve this problem specifically (but may work for it anyway). If it doesn't work out of the box, it includes all the code you should need to adjust it for your problem. Let me know how it goes, good luck!
Thank you very much for sacrificing your spare time to help me solve this problem, I tried to run the code, the results are useful. I also want to apply this method to the equation, what do I have to do to get the following result:
equation: 2x+3x=5 step1: (2+3)*x=5; step2: 5x=5; step3: x=1
Please take the trouble to guide me again, thank you very much!
from mathy.
I want to implement the following step calculation of the formula, eg:
5+2*3-2*(3-1)
, expected output step-by-step:
First step:5+2*3-2*2
;
Second step:5+6-4
;
Final step:7
;
If I wanted to implement the above example, how would I do it? Please give a detailed code example, thank you!The example I gave you does not solve this problem specifically (but may work for it anyway). If it doesn't work out of the box, it includes all the code you should need to adjust it for your problem. Let me know how it goes, good luck!
Thank you very much for sacrificing your spare time to help me solve this problem, I tried to run the code, the results are useful. I also want to apply this method to the equation, what do I have to do to get the following result:
equation: 2x+3x=5 step1: (2+3)*x=5; step2: 5x=5; step3: x=1
I tried to test with the above code, but I got the following error, how to fix it?
STEP[0]: input | 2x + 3x = 5
STEP[1]: Distributive Factoring | (2 + 3) * x = 5
Traceback (most recent call last):
File "/Users/dongyangyang/Documents/dongao_dyy/projects/formula_solver/step_by_step.py", line 69, in
simplify_polynomial()
File "/Users/dongyangyang/Documents/dongao_dyy/projects/formula_solver/step_by_step.py", line 50, in simplify_polynomial
while not is_simplified(expression) and steps < limit:
File "/Users/dongyangyang/Documents/dongao_dyy/projects/formula_solver/step_by_step.py", line 29, in is_simplified
if not util.is_preferred_term_form(term):
File "/opt/miniconda3/envs/solver/lib/python3.9/site-packages/mathy_core/util.py", line 314, in is_preferred_term_form
if not is_simple_term(expression):
File "/opt/miniconda3/envs/solver/lib/python3.9/site-packages/mathy_core/util.py", line 280, in is_simple_term
sub_terms = get_sub_terms(node)
File "/opt/miniconda3/envs/solver/lib/python3.9/site-packages/mathy_core/util.py", line 239, in get_sub_terms
assert current is None or isinstance(
AssertionError: get_sub_terms(5x = 5) Expected Mul/Div/Power. Found: <class 'mathy_core.expressions.EqualExpression'>
from mathy.
I also want to apply this method to the equation, what do I have to do to get the following result:
equation: 2x+3x=5 step1: (2+3)*x=5; step2: 5x=5; step3: x=1
I tried to test with the above code, but I got the following error, how to fix it?
Sorry, you're reaching beyond the limitations of the current mathy_core code. It has support for balanced move rules, but it looks like the util for finding preferred terms needs to be updated.
if not util.is_preferred_term_form(term):
File "/opt/miniconda3/envs/solver/lib/python3.9/site-packages/mathy_core/util.py", line 314, in is_preferred_term_form
if not is_simple_term(expression):
File "/opt/miniconda3/envs/solver/lib/python3.9/site-packages/mathy_core/util.py", line 280, in is_simple_term
sub_terms = get_sub_terms(node)
File "/opt/miniconda3/envs/solver/lib/python3.9/site-packages/mathy_core/util.py", line 239, in get_sub_terms
assert current is None or isinstance(
AssertionError: get_sub_terms(5x = 5) Expected Mul/Div/Power. Found: <class 'mathy_core.expressions.EqualExpression'
Unfortunately, I can't help you with this further unless you make progress on your own. I love for people to use Mathy, but I can't provide step-by-step features and development for specific problems. That's why Mathy is open-source, so others can contribute to making it better.
The issue you're running into is that I added the balanced move rule for dealing with equations relatively late in development and never fully integrated it into the existing environments. The assertion error is likely because get_sub_terms needs adjusting to deal with EqualExpression nodes in the tree.
You could start here with the tests for that function: https://github.com/mathy/mathy_core/blob/master/tests/test_util.py#L13C31-L34
from mathy.
Related Issues (12)
- use underscore_case for python code consistently through API HOT 1
- Update API doc generation script to run with lint-staged
- Integration with fragile v0.0.44 HOT 7
- Error when installing the package HOT 7
- Division not working properly HOT 2
- Adding new mathematical methods to mathy HOT 1
- Broken link HOT 1
- math problem
- Lim. Xln(x-2)/(x+3) X->+∞
- Cannot install mathy HOT 1
- How to use in code? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mathy.