Skip to content

Commit

Permalink
splitting up api endpoint into two resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
krachwal committed Jan 2, 2024
1 parent 89b57b3 commit 4053d73
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions python-backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from sympy import simplify, sympify, oo, Symbol
from sympy import simplify, sympify, oo, Symbol, Union, Add, Mul, Float

import constants
import logger
Expand All @@ -19,7 +19,7 @@

logger = logger.config()

PRECISION = constants.PRECISION
mpmath.mp.dps = constants.PRECISION

origins = ["http://localhost:5173", "127.0.0.1:5173"]

Expand All @@ -31,6 +31,21 @@
allow_headers=["*"])


def parse(data: Input) -> ( Union<int, float, Float, Add, Mul>, Union<int, float, Float, Add, Mul>, Symbol):
"""
Process user inputs into math expressions
:param data: User form inputs
:return: tuple including p/q, simplified q and the symbol/variable used in these expressions
"""
x = Symbol(data.symbol, real=True)
p = sympify(convert(data.p), {data.symbol: x})
q = sympify(convert(data.q), {data.symbol: x})
simple_q = simplify(q)
expression = p / simple_q
simple = simplify(expression)
return simple, simple_q, x


@app.post("/analyze")
async def analyze(request: Request):
"""
Expand All @@ -39,26 +54,18 @@ async def analyze(request: Request):
"""
# parse posted body as Input

mpmath.mp.dps = PRECISION

try:
data = Input(**(await request.json()))
x = Symbol(data.symbol, real=True)
p = sympify(convert(data.p), {data.symbol: x})
q = sympify(convert(data.q), {data.symbol: x})
simple_q = simplify(q)
expression = p / simple_q
simple = simplify(expression)
(expression, denominator, symbol) = parse(data)

q_limit = sympy.limit(simple_q, x, oo)
limit = sympy.limit(simple, x, oo)
q_limit = sympy.limit(denominator, symbol, oo)
limit = sympy.limit(expression, symbol, oo)

body = {
"wolfram_limit": WolframClient.limit(str(simple)),
"limit": json.dumps(float(limit)),
"denominator_limit": json.dumps(float(q_limit)),
"log_error": json.dumps(error_coordinates(simple, x, limit)),
"delta": json.dumps(delta_coordinates(simple, simple_q, x, limit)),
"log_error": json.dumps(error_coordinates(expression, symbol, limit)),
"delta": json.dumps(delta_coordinates(expression, denominator, symbol, limit)),
"computed_value": json.dumps(float(limit)) # @TODO: replace with actual computation to i
}

Expand All @@ -68,5 +75,31 @@ async def analyze(request: Request):

except Exception as e:
logger.warning(e)
response = JSONResponse(status_code=500, content={"error": "Failed to parse p / q"})
response = JSONResponse(status_code=500, content={"error": "Failed to parse inputs"})
return response


@app.post("/verify")
async def analyze(request: Request):
"""
:param request: HTTP request
:return: HTTP response indicating success of parsing inputs with a 200 or a 500 to indicate failure parsing inputs
"""
# parse posted body as Input

try:
data = Input(**(await request.json()))
(expression, _, _) = parse(data)

body = {
"wolfram_limit": WolframClient.limit(str(expression))
}

response = JSONResponse(content=body)
response.set_cookie(key="trm", value=str(uuid.uuid4()))
return response

except Exception as e:
logger.warning(e)
response = JSONResponse(status_code=500, content={"error": "Failed to parse inputs"})
return response

0 comments on commit 4053d73

Please sign in to comment.