{"id":19461482,"url":"https://github.com/iamtomahawkx/math-parser","last_synced_at":"2025-02-25T12:42:36.870Z","repository":{"id":172360332,"uuid":"358467912","full_name":"IAmTomahawkx/math-parser","owner":"IAmTomahawkx","description":"Parses simple to complex math equations safely, optionally graphing them","archived":false,"fork":false,"pushed_at":"2021-04-22T23:03:01.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-08T02:46:03.973Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/IAmTomahawkx.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-16T03:47:20.000Z","updated_at":"2021-04-22T23:03:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"7e28c4f3-7dc8-4a4c-bfa4-09055d7384b6","html_url":"https://github.com/IAmTomahawkx/math-parser","commit_stats":null,"previous_names":["iamtomahawkx/math-parser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IAmTomahawkx%2Fmath-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IAmTomahawkx%2Fmath-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IAmTomahawkx%2Fmath-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IAmTomahawkx%2Fmath-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IAmTomahawkx","download_url":"https://codeload.github.com/IAmTomahawkx/math-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240672494,"owners_count":19838920,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T17:42:36.010Z","updated_at":"2025-02-25T12:42:36.813Z","avatar_url":"https://github.com/IAmTomahawkx.png","language":"Python","readme":"___\n# Math Parser\n___\nIt parses math through AST.\n\n## Index\n- [Example](#python-example)\n- [Complexities](#complexities)\n- [Built-ins](#built-ins)\n\n## Python Example\n\n```python\nimport asyncio\nimport mathparser\n\nexp = input(\"enter your equation: \")\n\nlex = mathparser.MathLexer()\nparser = mathparser.Parser(exp, lex)\n\nasync def main():\n    try:\n        tokens = list(lex.tokenize(exp))\n        exprs = parser.parse(tokens)\n        resp = \"\"\n        images = []\n        \n        for i, expr in enumerate(exprs):\n            try:\n                e = expr.execute(None, parser)\n            except ZeroDivisionError: # this one is intentionally left unhandled\n                resp += f\"[{i+1}] Zero divison error\"\n                continue\n            \n            if isinstance(e, dict):\n                f = await mathparser.graph.plot(e, i+1)\n                if f:\n                    images.append(f)\n    \n                resp += f\"[{i+1}] See graph {i+1} ({e})\\n\"\n            else:\n                resp += f\"[{i+1}] {e}\\n\"\n        \n        print(resp)\n        # do something with the images\n    except mathparser.UserInputError as e:\n        print(str(e)) # userinputerrors have formatted errors attached to them\n        raise\n\nasyncio.run(main())\n```\n\n## Complexities\nThis parser handles more than just the obvious addition, subtraction, multiplication and division.\nHere is a list of more complex things this can do currently.\n\n- [brackets](#brackets)\n- [exponents](#exponents)\n- [functions](#functions)\n- [graphed functions](#graphed-functions)\n- [geometric sequences](#geometric-sequences)\n\n___\n\n### Brackets\nBrackets are fairly simple, but it's worth mentioning that `2+4*5` (22) will be calculated differently than `(2+4)*5` (30).\n___\n\n### Exponents\nAgain, nothing crazy here, exponents can be indicated with the `^` symbol. Ex. `2^2`\n___\n\n### Functions\nFunctions are created with the syntax \n```\np(x) = ...\n```\np can be whatever letter you wish, except `s`/`S` (these are reserved for sequences).\nThe `...` represents where your expression should go. \\\nA function with multiple variables can be created in the same way:\n```\np(x,y)=...`. Ex. `p(x,y)=x*5+y-4\n```\n\nFunctions can be called in the following manner\n```\np(...)\n```\nThis can be anywhere, excluding the function itself. Ex.\n```\np(x) = x*4\np(4)+5\n```\nwill result in 21.\n___\n\n### Graphed Functions\nGraphed functions are similar to normal functions, but with two major differences. \\\nThe first difference is that graphed functions cannot be called from your expressions. \\\nThe second difference is that graphed functions are declared using `y=...`, instead of `p(x)=...`.\nThe `x` variable is implicitly injected as it's graphed.\n___\n\n### Geometric Sequences\nGeometric sequences are defined with the following syntax\n```\ns=n1,n2,n3\n```\nor\n```\ns=n1,n2\n```\nwhere `s` is a literal `s`, `n1` is the first value in the sequence, `n2` is the second value, and optionally, `n3` is the third value. \\\nWhen a sequence is defined, you can use the following syntaxes\n```\nS(...)\nS?(...)\nS!(...)\nS!!(...)\n```\n\nHere's what each one does:\n`S(...)`: takes 1 number, `n`, and returns its value in the sequence. Ex.\n```\nS=2,4,8\ns(2)\n```\n`s(2)` will be 4, `s(3)` will be 8, and so on.\n\n`S?(...)`: takes 1 number, `tn`, and returns its position in the sequence. Ex.\n```\nS=2,4,8\ns?(4)\n```\n`s?(4)` will be 2, `s?(8)` will be 3, and so on.\n\n`S!(...)`: takes 1 number, `n`, and returns the sum of the sequence up to that position. Ex.\n```\nS=2,4,8\ns!(3)\n```\n`s!(3)` will be 14, `s!(4)` will be 30, and so on.\n\n`S!!(...)`: takes 1 number, `tn`, and returns the sum of the sequence up to that value. Ex.\n```\nS=2,4,8\ns!!(8)\n```\n`s!!(8)` will be 14, `s!!(16)` will be 30.\n\n\n## Built-ins\nThe following functions are currently built into the parser\n- sin(x) / asin(x, y)\n- cos(x) / acos(x, y)\n- tan(x) / atan(x, y)\n- log(n)\n\nThe following variables are currently built in to the parser\n- pi\n- E","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamtomahawkx%2Fmath-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamtomahawkx%2Fmath-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamtomahawkx%2Fmath-parser/lists"}