File size: 1,922 Bytes
3b43b98
9d5e30f
 
3b43b98
a8fa158
9d5e30f
9d2ba79
6262fed
a8fa158
9d5e30f
6262fed
 
 
9d5e30f
 
 
 
6262fed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d5e30f
 
6262fed
a8fa158
 
 
6262fed
9d5e30f
 
a8fa158
 
6262fed
 
9d5e30f
6262fed
9d5e30f
a8fa158
6262fed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import streamlit as st
import sympy as sp
import re

# 1. SETUP
st.set_page_config(page_title="SAAD AI ACADEMY", layout="centered")

# 2. THE STABLE SOLVER
def universal_solver(query):
    try:
        x = sp.Symbol('x')
        
        # A. MODULAR CONGRUENCE (The 14x = 30 mod 44 logic)
        if "mod" in query.lower():
            nums = re.findall(r'\d+', query)
            if len(nums) >= 3:
                a, b, n = map(int, nums[:3])
                # Using solveset on a modular equation is the most stable way
                equation = sp.Eq(sp.Mod(a*x, n), b % n)
                sol = sp.solveset(equation, x, domain=sp.S.Integers)
                return f"Result: ${sp.latex(sol)}$"

        # B. CALCULUS (Derivatives/Integrals)
        if "diff" in query.lower() or "derivative" in query.lower():
            expr = sp.sympify(query.split("of")[-1].strip())
            return sp.diff(expr, x)
            
        if "integrate" in query.lower():
            expr = sp.sympify(query.split("integrate")[-1].strip())
            return sp.integrate(expr, x)

        # C. GENERAL ALGEBRA
        clean_query = query.replace("solve", "").replace("find", "").strip()
        if "=" in clean_query:
            parts = clean_query.split("=")
            eq = sp.Eq(sp.sympify(parts[0]), sp.sympify(parts[1]))
            return sp.solve(eq, x)
            
        return sp.simplify(sp.sympify(clean_query))

    except Exception as e:
        return f"Please check syntax. Use x**2 for powers. Error: {e}"

# 3. UI
st.title("📐 SAAD AI ACADEMY")
user_input = st.text_input("Enter problem:", placeholder="e.g. 14x = 30 mod 44")

if user_input:
    result = universal_solver(user_input)
    st.divider()
    if isinstance(result, str) and "Result" in result:
        st.markdown(result)
    else:
        st.latex(sp.latex(result))

# 4. SIDEBAR
st.sidebar.info("v6.1: Stable Solver (Python 3.13 Ready)")