Repository: aunyks/newton-api Branch: master Commit: f25c930bb2f9 Files: 7 Total size: 11.4 KB Directory structure: gitextract_rr_m6713/ ├── .gitignore ├── README.md ├── lib/ │ └── operations.js ├── package.json ├── pages/ │ ├── api/ │ │ └── v2/ │ │ └── [operation]/ │ │ └── [data].js │ └── index.js └── test.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Logs logs *.log npm-debug.log* .next # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # nyc test coverage .nyc_output # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # node-waf configuration .lock-wscript # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directories node_modules jspm_packages # Optional npm cache directory .npm # Optional REPL history .node_repl_history .vercel ================================================ FILE: README.md ================================================ # newton ## A really micro micro-service for advanced math. Newton does anything from numerical calculation to symbolic math parsing. ### How does it work? 1. Send a GET request to newton with a url-encoded math expression and your preferred operation. 2. Get back a JSON response with your problem solved. ### Show me Let's find the derivative of x^2. So, we send a request to the newton url saying just that: -> `https://newton.now.sh/api/v2/derive/x%5E2` (Note the url-encoded `^`) Now, we get the following response: ``` { "operation":"derive", "expression":"x^2", "result":"2 x" } ``` It's that simple! ### Get Started 1. Send a GET request to newton. ``` https://newton.now.sh/api/v2/:operation/:expression ``` Note: `:operation` is the math operation that you want to perform. `:expression` is the *url-encoded* math expression on which you want to operate. **View available operation endpoints below!** 2. That's it! You'll be returned a JSON object with the operation you requested, the expression you provided, and the result of the operation performed on the expression. ### View available endpoints: | Operation | API Endpoint | Result | |:---------:|:-----------------:|:-----------------:| | Simplify | /simplify/2^2+2(2)| 8 | | Factor | /factor/x^2 + 2x | x (x + 2) | | Derive | /derive/x^2+2x | 2 x + 2 | | Integrate | /integrate/x^2+2x | 1/3 x^3 + x^2 + C | | Find 0's | /zeroes/x^2+2x | [-2, 0] | | Find Tangent| /tangent/2lx^3 | 12 x + -16 | | Area Under Curve| /area/2:4lx^3| 60 | | Cosine | /cos/pi | -1 | | Sine | /sin/0 | 0 | | Tangent | /tan/0 | 0 | | Inverse Cosine | /arccos/1 | 0 | | Inverse Sine | /arcsin/0 | 0 | | Inverse Tangent | /arctan/0 | 0 | | Absolute Value | /abs/-1 | 1 | | Logarithm | /log/2l8 | 3 | *Keep in mind:* To find the tangent line of a function at a certain x value, send the request as c|f(x) where c is the given x value and f(x) is the function expression, the separator is a vertical bar '|'. See the table above for an example request. To find the area under a function, send the request as c:d|f(x) where c is the starting x value, d is the ending x value, and f(x) is the function under which you want the curve between the two x values. To compute fractions, enter expressions as numerator(over)denominator. For example, to process 2/4 you must send in your expression as 2(over)4. The result expression will be in standard math notation (1/2, 3/4). ### What have people made? Some cool apps and libraries made with Newton include the following: - [Newton Bot](https://twitter.com/aunyks/status/813127765646082050) (Coming soon!) - [Newton Python Wrapper](https://github.com/ilevn/aionewton) - [Newton Clay Microservice](https://clay.run/services/nicoslepicos/newton-api) - [PyNewtonMath](https://github.com/benpryke/PyNewtonMath) (Python 3 Wrapper) - [NewtonMath.js](https://github.com/benpryke/NewtonMath.js) (Native Node.js Wrapper) - [goNewton](https://github.com/Jay9596/goNewton) (GoLang Wrapper) - [newtonmath](https://github.com/anaskhan96/newtonmath) (Rust Wrapper) - [Newton Telegram Bot](https://t.me/sir_newton_bot) - [Ming Newton](https://ming-newton.herokuapp.com) - Hopefully more to come! ----------------------------------------------------------------- Built using [metadelta](https://github.com/metadelta/metadelta-core). ## Want to support Newton development? Shop [MEZCLA](https://mezcla.xyz)! Licensed under the GNU GPLv3 license. Copyright (C) 2016-2020 Gerald Nash This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ================================================ FILE: lib/operations.js ================================================ var metadelta = require('@metadelta/core'); module.exports = { simplify: metadelta.simplify, factor: metadelta.factor, zeroes: metadelta.zeroes, integrate: metadelta.integrate, derive: metadelta.derive, cos: metadelta.cos, sin: metadelta.sin, tan: metadelta.tan, arccos: metadelta.arccos, arcsin: metadelta.arcsin, arctan: metadelta.arctan, abs: metadelta.abs, log: function(expression){ var base = expression.split('|')[0]; var arg = expression.split('|')[1]; return metadelta.log(base, arg); }, tangent: function(expression){ var data = expression.split('|'); var at = parseInt(data[0]); var f = data[1]; return metadelta.tangent(f, at); }, area: function(expression){ var split = expression.split('|'); var f = split[1]; var from = split[0].split(':')[0]; var to = split[0].split(':')[1]; return '' + metadelta.areaUnder(f, { start: from, finish: to }); } }; ================================================ FILE: package.json ================================================ { "name": "newton-api", "version": "1.0.1", "description": "A really micro micro-service for advanced math.", "main": "app.js", "scripts": { "start": "next start", "dev": "next", "build": "next build" }, "repository": { "type": "git", "url": "git+https://github.com/aunyks/newton.git" }, "keywords": [ "math", "newton", "calculus", "algebra", "geometry", "theory" ], "author": "Gerald Nash", "license": "GPLv3", "private": true, "bugs": { "url": "https://github.com/aunyks/newton/issues" }, "homepage": "https://github.com/aunyks/newton#readme", "dependencies": { "@metadelta/core": "^1.1.2", "express": "^4.14.1", "next": "^9.5.3", "react": "^16.13.1", "react-dom": "^16.13.1" }, "devDependencies": { "colors": "^1.1.2", "request": "^2.79.0" } } ================================================ FILE: pages/api/v2/[operation]/[data].js ================================================ import operations from '../../../../lib/operations.js' module.exports = (req, res) => { res.setHeader('Access-Control-Allow-Origin', '*') res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept') // Get the math function operation based // on the operation that we're given const f = operations[req.query.operation] // If the url isn't malformed, send our response // with an answer. If not, send an error response if (f === undefined) { res.statusCode = 400 res.json({ error: 'Unknown operation' }) } else { // Compute the requested calculation and respond. // If we cannot compute it properly (error thrown), // then tell the client try { // The result from passing the data parameter // to the operation function req.query.data = req.query.data.split('(over)').join('/') const computation = f(req.query.data) res.statusCode = 200 res.json({ operation: req.query.operation, expression: req.query.data, result: computation }) } catch (err) { // Record this error console.error(err) res.statusCode = 500 // Let the client know of our inability to // perform the requested calculation res.json({ error: 'Unable to perform calculation' }) } } } ================================================ FILE: pages/index.js ================================================ import Head from 'next/head' export default function Index({ }) { return ( <> newton

newton

A really micro micro-service for advanced math.

Send this:
            https://newton.now.sh/api/v2/factor/x^2-1
          
Get this:
{`
        {
          "operation":"factor",
          "expression":"x^2 - 1",
          "result":"(x - 1) (x + 1)"
        }
        `}
          

See More


Supported By:
MEZCLA
) } ================================================ FILE: test.js ================================================ var req = require('request'); var colors = require('colors'); // Start server require('./app.js') var newtonUrl = 'http://localhost:3000/'; function request(route, callback){ req(newtonUrl + route, function (error, response, body) { var value = JSON.parse(body).result; callback(value); }); } function assert(expected, route, name){ expected = JSON.stringify(expected); request(route, function(result){ result = JSON.stringify(result); if(expected !== result){ console.log(`Warning: ${name} did not pass test!`.yellow); console.log(`Expected: ${expected}\nReceived: ${result}`.red); } else { console.log(`Test: ${name} passed test`.green); } }); } // Assert that our functions give the proper output // TODO: give functions more than one test case at a time assert('2 x', 'simplify/x+x', 'Simplify'); assert('x (x + 2)', 'factor/x^2 + 2x', 'Factor'); assert('2 x + 2', 'derive/x^2 + 2x', 'Derive'); assert('1/3 x^3 + x^2', 'integrate/x^2 + 2x','Integrate'); assert([-2, 0], 'zeroes/x^2 + 2x', 'Zeroes'); assert('12 x + -16', 'tangent/2|x^3', 'Tangent'); assert('60', 'area/2:4|x^3', 'Area Under Curve'); assert('3', 'log/2|8', 'Logarithm');