about summary refs log tree commit diff stats
path: root/templates/index.html
blob: 594851dbcc75f6c134be2658c13ff674553906c9 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!DOCTYPE html>
<html>
  <head>
    <title>pyMathEngine</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
    <script
      id="MathJax-script"
      async
      src="https://cdn.jsdelivr.net/npm/mathjax@3.0.1/es5/tex-mml-chtml.js"
    ></script>
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM"
      crossorigin="anonymous"
    />
    <script
      src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz"
      crossorigin="anonymous"
    ></script>
    <link
      rel="icon"
      type="image/png"
      href="https://cdn.discordapp.com/attachments/838048982873538572/1131593508759814287/favicon.png"
    />
  </head>
  <body>
    <div class="m-3">
      <div class="alert alert-info mb-2" role="alert">
        Welcome to <b>pyMathEngine</b>, a mathematical engine in the style of
        Wolfram Alpha. <br />
        Currently, the engine can only take univariate expressions and simple
        equations, but additional features are being added over time. , but
        additional features are being added over time.<br />
        Some notes:
        <ul>
          <li>Euler's number (\(e\)) is written as "E" in the box</li>
          <li>Pi (\(\pi\)) is written as "pi" in the box</li>
        </ul>
      </div>
      <div class="row mt-3">
        <div class="col-sm">
          <div class="input-group mb-3">
            <input
              id="expression"
              type="text"
              class="form-control"
              placeholder="Enter a univariate expression..."
              aria-label="Enter a univariate expression..."
              aria-describedby="button-addon"
            />
            <button
              class="btn btn-secondary"
              type="button"
              onclick="get_uni()"
              id="button-addon"
            >
              Search
            </button>
          </div>
        </div>
        <div class="col-sm">
          <div class="input-group mb-3">
            <input
              id="lhs"
              type="text"
              placeholder="LHS of equation"
              class="form-control"
            />
            <span class="input-group-text">=</span>
            <input
              id="rhs"
              type="text"
              placeholder="RHS of equation"
              class="form-control"
            />
            <button
              class="btn btn-secondary"
              type="button"
              onclick="get_eq()"
              id="button-addon"
            >
              Search
            </button>
          </div>
        </div>
      </div>
    </div>
    <div id="response" class="m-3"></div>
  </body>
  <script>
    function get_eq() {
      let lhs = document.getElementById("lhs").value;
      let rhs = document.getElementById("rhs").value;
      let responseBox = document.getElementById("response");
      responseBox.innerHTML = `
      <div class="d-flex justify-content-center">
        <div class="spinner-border" role="status">
          <span class="visually-hidden">Loading...</span>
        </div>
      </div>
      `;

      fetch(
        `https://${"{{ HOSTNAME }}"}/api/equation?lhs=${encodeURIComponent(
          lhs
        )}&rhs=${encodeURIComponent(rhs)}`
      )
        .then((response) => {
          if (!response.ok) {
            let err = new Error("HTTP error: " + response.status);
            err.response = response;
            err.status = response.status;
            throw err;
          } else {
            response.json().then((data) => {
              console.log(data);
              let responseBox = document.getElementById("response");

              if (data.solution.length == 0) {
                solution = "\\text{No solutions exist}";
              } else {
                solution = data.solution;
              }

              responseBox.innerHTML = "";
              responseBox.innerHTML += `
              <div class="card mt-2">
                <div class="card-header">Solutions</div>
                <div class="card-body overflow-auto"><p>
                    $$ ${solution} $$
                    </p></div>
              </div>
              `;

              MathJax.typeset();
            });
          }
        })
        .catch((err) => {
          let responseBox = document.getElementById("response");

          responseBox.innerHTML = "";
          responseBox.innerHTML += `
          <div class="card mt-2">
            <div class="card-header bg-danger text-white">Error</div>
            <div class="card-body">
                <p>${err.message}</p>
                <p>This is likely because you have entered an invalid expression.</p>
            </div>
          </div>
          `;
        });
    }

    function get_uni() {
      let exp = document.getElementById("expression").value;
      let responseBox = document.getElementById("response");
      responseBox.innerHTML = `
      <div class="d-flex justify-content-center">
        <div class="spinner-border" role="status">
          <span class="visually-hidden">Loading...</span>
        </div>
      </div>
      `;
      fetch(
        `http://${"{{ HOSTNAME }}"}/api/univariate?func=${encodeURIComponent(
          exp
        )}`
      )
        .then((response) => {
          if (!response.ok) {
            // create error object and reject if not a 2xx response code
            let err = new Error("HTTP status code: " + response.status);
            err.response = response;
            err.status = response.status;
            throw err;
          } else {
            response.json().then((data) => {
              console.log(data);
              let responseBox = document.getElementById("response");
              if (data.solution.length == 0) {
                solution = "\\text{No solutions exist}";
              } else {
                solution = data.solution;
              }
              responseBox.innerHTML = "";
              responseBox.innerHTML += `
              <div class="card mt-2">
                <div class="card-header">Input</div>
                <div class="card-body">
                    <p>$$ f(${data.variable}) = ${data.expression} $$</p>
                </div>
              </div>

              <div class="card mt-2">
                <div class="card-header">Solutions (where \\(f(${data.variable}) = 0\\))</div>
                <div class="card-body overflow-auto"><p>
                    $$ ${solution} $$
                    </p></div>
              </div>

              <div class="card mt-2">
                <div class="card-header">Plot</div>
                <div class="card-body text-center">
                    <img src="data:image/png;base64,${data.plot}" />
                </div>
              </div>

              <div class="card mt-2">
                <div class="card-header">Differential</div>
                <div class="card-body">
                    <p>$$ \\frac{\\mathrm{d} f(${data.variable}) }{\\mathrm{d} ${data.variable}} = ${data.diff} $$</p>
                </div>
              </div>

              <div class="card mt-2">
                <div class="card-header">Integral</div>
                <div class="card-body">
                    <p>$$ \\int f(${data.variable})\\, \\mathrm{d}${data.variable} = ${data.integral} + c$$</p>
                </div>
              </div>

              <div class="card mt-2">
                <div class="card-header">Limits</div>
                <div class="card-body">
                    <p>$$ \\lim_{${data.variable} \\rightarrow  \\infty} = ${data.limit_inf}$$</p>
                    <p>$$ \\lim_{${data.variable} \\rightarrow  0} = ${data.limit_zero}$$</p>
                </div>
              </div>



              `;
              MathJax.typeset();
            });
          }
        })
        .catch((err) => {
          let responseBox = document.getElementById("response");

          responseBox.innerHTML = "";
          responseBox.innerHTML += `
          <div class="card mt-2">
            <div class="card-header bg-danger text-white">Error</div>
            <div class="card-body">
                <p>${err.message}</p>
                <p>This is likely because you have entered an invalid expression.</p>
            </div>
          </div>
          `;
        });
    }
  </script>
  <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        "CommonHTML": { linebreaks: { automatic: true, width: "container" } },
        "HTML-CSS": { linebreaks: { automatic: true, width: "container" } },
        "SVG": { linebreaks: { automatic: true, width: "container" } }
    });
  </script>
</html>