{"id":13694030,"url":"https://github.com/tdewolff/formulae","last_synced_at":"2025-04-13T02:32:58.809Z","repository":{"id":57506618,"uuid":"138394081","full_name":"tdewolff/formulae","owner":"tdewolff","description":"Parsing and calculating formulae","archived":false,"fork":false,"pushed_at":"2020-06-26T23:34:55.000Z","size":145,"stargazers_count":43,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-26T20:55:41.100Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tdewolff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-23T10:59:24.000Z","updated_at":"2024-11-19T02:51:05.000Z","dependencies_parsed_at":"2022-08-29T20:01:21.607Z","dependency_job_id":null,"html_url":"https://github.com/tdewolff/formulae","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdewolff%2Fformulae","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdewolff%2Fformulae/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdewolff%2Fformulae/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdewolff%2Fformulae/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tdewolff","download_url":"https://codeload.github.com/tdewolff/formulae/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248657830,"owners_count":21140842,"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-08-02T17:01:22.851Z","updated_at":"2025-04-13T02:32:58.483Z","avatar_url":"https://github.com/tdewolff.png","language":"Go","funding_links":[],"categories":["开源类库","Open source library"],"sub_categories":["图形处理","Graphics Processing"],"readme":"# Formulae \u003ca name=\"formulae\"\u003e\u003c/a\u003e [![Build Status](https://travis-ci.org/tdewolff/formulae.svg?branch=master)](https://travis-ci.org/tdewolff/formulae) [![GoDoc](http://godoc.org/github.com/tdewolff/formulae?status.svg)](http://godoc.org/github.com/tdewolff/formulae)\n\n**[Online demo](https://go.tacodewolff.nl/formulae) if you need to draw formulae *now*.**\n\n---\n\nFormulae is a formula parser and calculator and can optimize and derive (to `x`) formulae.\n\n### From\n`sin(cos(x))^2+1/x-1`\n\n### To\n![example.png](example.png)\n\n## Installation\nRun the following command\n\n\tgo get github.com/tdewolff/formulae\n\nor add the following import and run the project with `go get`\n``` go\nimport (\n\t\"github.com/tdewolff/formulae\"\n)\n```\n\n## Usage\n### Parse\nParse a formula from string and return a `Function`.\n``` go\nf := formulae.Parse(\"sin(cos(x))^2+1/x-1\")\n```\n\n### Calculate\nCalculate the function for a single `x` value.\n``` go\ny, err := f.Calc(5+0i) // -0.722...\nif err != nil {\n    panic(err)\n}\n```\n\n### Interval\nCalculate the function between the interval `a` and `b`, with step-size `step`.\n``` go\nys, errs := f.Interval(a, step, b)\nif len(errs) != 0 {\n    panic(\"errors\")\n}\n```\n\n### Optimize\nOptimize the function by elimination and simplification.\n``` go\nf.Optimize()\n```\n\n### Derive to `x`\nObtain the derivative of `f` to `x`.\n``` go\ndf := f.Derivative()\n```\n\n### LaTeX notation\nExport as LaTeX notation\n``` go\ns := f.LaTeX() // $$f(x) = \\sin(\\cos(x))^{2}+\\frac{1}{x}-1$$\n```\n\n## Example\nBasic example that plots to image.\n``` go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"image/color\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/tdewolff/formulae\"\n\t\"gonum.org/v1/plot\"\n\t\"gonum.org/v1/plot/plotter\"\n\t\"gonum.org/v1/plot/vg\"\n)\n\nfunc main() {\n\t// Parse formula\n\tin := \"sin(cos(x))^2+1/x-1\"\n\tf, errs := formulae.Parse(in)\n\tif len(errs) \u003e 0 {\n\t\tlog.Fatal(errs)\n\t}\n\tdf := f.Derivative()\n\n\t// Calculate function\n\txs, ys, errs := f.Interval(0.5, 0.01, 5.0)\n\tif len(errs) \u003e 0 {\n\t\tlog.Fatal(errs)\n\t}\n\txys := make(plotter.XYs, len(xs))\n\tfor i := range xs {\n\t\txys[i].X = xs[i]\n\t\txys[i].Y = real(ys[i])\n\t}\n\t_, _, ymin, _ := plotter.XYRange(xys)\n\tif ymin \u003e 0 {\n\t\tymin = 0\n\t}\n\n\t// Calculate function derivative\n\txs2, ys2, errs := df.Interval(0.5, 0.01, 5.0)\n\tif len(errs) \u003e 0 {\n\t\tlog.Fatal(errs)\n\t}\n\txys2 := make(plotter.XYs, len(xs2))\n\tfor i := range xs2 {\n\t\txys2[i].X = xs2[i]\n\t\txys2[i].Y = real(ys2[i])\n\t}\n\n\t// Plot functions\n\tp, err := plot.New()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tp.Title.Text = \"Formula\"\n\tp.X.Label.Text = \"x\"\n\tp.Y.Label.Text = \"y\"\n\tp.Y.Min = ymin\n\n\tline, err := plotter.NewLine(xys)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tline2, err := plotter.NewLine(xys2)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tline2.LineStyle.Color = color.Gray{192}\n\n\tp.Add(plotter.NewGrid())\n\tp.Add(line)\n\tp.Add(line2)\n\tp.Legend.Add(\"f\", line)\n\tp.Legend.Add(\"df/dx\", line2)\n\n\tif err := p.Save(8*vg.Inch, 4*vg.Inch, \"formula.png\"); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n## License\nReleased under the [MIT license](LICENSE.md).\n\n[1]: http://golang.org/ \"Go Language\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdewolff%2Fformulae","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftdewolff%2Fformulae","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdewolff%2Fformulae/lists"}