Desmos in Python

If you don’t know what Python is, its a coding language, and I made a simple graphing calculator that can plot simple quadratics, linear, and implict equation. And yes, I used AI for a bit of my project :sweat_smile: (for the graphing part, since it is pretty far beyond my brain)

Its is very slow. Sometimes, if the equation is complex, like y=1/x, it will have many gaps and half the curve won’t render.

This shows how impressive of a project Desmos is. It can plot a curve in milliseconds with unbelievable accuracy (well a big reason is cause it has like a GPU but I’m just using a brute-force technique which runs at like O(n^9999)). Enjoy!

(To install numpy, use pip install numpy)

import turtle as t
import numpy as np

DISTANCE = 20
GRAPH_RANGE = 20

screen = t.Screen()
t.speed(0)
t.pensize(3)


def takeInput():
    exp = input("Enter equation: ")
    return exp.split("=")


def parse(exp):
    result = ["", ""]
    for side in [0, 1]:
        text = exp[side]
        for i, c in enumerate(text):
            if c in "xy":
                if i > 0 and (text[i - 1].isdigit() or text[i - 1] in "xy"):
                    result[side] += "*"
                result[side] += c
            else:
                result[side] += c

    result[0] = result[0].replace("^", "**")
    result[1] = result[1].replace("^", "**")
    return result
def draw_axes(axis_len=400):

    t.hideturtle()
    t.pensize(2)
    t.penup()
    t.goto(-axis_len, 0)
    t.pendown()
    t.goto(axis_len, 0)
    t.penup()
    t.goto(0, -axis_len)
    t.pendown()
    t.goto(0, axis_len)
    for x in range(-axis_len, axis_len + 1, DISTANCE):
        t.penup()
        t.goto(x, -5)
        t.pendown()
        t.goto(x, 5)
    for y in range(-axis_len, axis_len + 1, DISTANCE):
        t.penup()
        t.goto(-5, y)
        t.pendown()
        t.goto(5, y)
    t.penup()
def solve(exp):
    lhs, rhs = parse(exp)
    xs = np.linspace(-GRAPH_RANGE, GRAPH_RANGE, 2000)
    t.penup()
    for x in xs:
        ys = np.linspace(-GRAPH_RANGE, GRAPH_RANGE, 200)
        for y in ys:
            try:
                if abs(eval(lhs) - eval(rhs)) < 0.01:
                    screen_x = x * DISTANCE
                    screen_y = y * DISTANCE
                    t.goto(screen_x, screen_y)
                    t.pendown()
            except Exception:
                pass


draw_axes()
equation = takeInput()
solve(equation)

t.done()
2 Likes

This reminds me of the project I’m working on in JavaScript (my current preferred programming language).
It represents numbers by their factors (not just integers, but all numbers).
Currently, I have a system to factor integers (it’s a very simple system; it calculates primes as needed).
I’m struggling to figure out how factoring out rationals will work, but I’ll figure it out eventually.
After that, I’ll work on factoring out irrationals, then I’ll add operators to it.
It’s a complicated system, but if my plan is complete, it should work for all real numbers.

I doubt if I will be of any help, but maybe you could show me what the problem was/an example of the problem you are trying to figure out? Maybe I can give some inspiration.

1 Like

I saw a video of the Casio pi mystery, where rational numbers would rarely be shown as absurd fractions of pi.
I remembered that Desmos can turn decimals into fractions, but not express them in terms of pi, so I decided to take the task into my own hands.
I figured, while I was at it, I could try to extend it to include all numbers, like Wolfram Alpha.
I do not know how they do that, so I decided to make it from scratch, as any amateur with a bit too much confidence would.
This is the fifth time I have started a number library project, and I have never completed any of them.
Besides, I do not really need this number library. It would be cool, though, to attempt making it.

Maybe you could just make it be able to express in pi, instead of trying to do every single number. A smaller goal is better

1 Like

True. I guess I’ll start with the simpler goal first. After all, once I’ve added pi detection, it should be easy to add more numbers later.

1 Like