-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve_x.js
97 lines (81 loc) · 2.26 KB
/
solve_x.js
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
const { SlashCommandBuilder } = require('discord.js')
function formatEquation(equation) {
equation = equation
.replace(/ /g, '')
.replace(/\+\-/g, '-')
.replace(/\*/g, ' * ')
.replace(/ \* \* /g, ' ** ')
.replace(/\+/g, ' + ')
.replace(/\//g, ' / ')
.replace(/-/g, ' - ')
if (equation[0] === '+') {
equation = equation.slice(2)
}
while (equation[0] === ' ') {
equation = equation.slice(1)
}
if (equation[0] === '-') {
equation = '-' + equation.slice(2)
}
return equation
}
module.exports = {
data: new SlashCommandBuilder()
.setName('solve_x')
.setDescription('Solve for p(x) = n')
.addStringOption((option) => option
.setName('polynomial')
.setDescription('The polynomial')
.setRequired(true)
)
.addNumberOption((option) => option
.setName('equal_to')
.setDescription('What the result is equal to')
.setRequired(true)
)
.addNumberOption((option) => option
.setName('x0')
.setDescription('An approximate solution')
.setRequired(true)
),
async execute(interaction) {
let func = interaction.options.getString('polynomial')
let equal_to = interaction.options.getNumber('equal_to')
let x0 = interaction.options.getNumber('x0')
func = func.replace(/ /g, "")
func = func.replace(/pi/g, "3.141592653589793")
func = func.replace(/e/g, "2.718281828459045")
func = func.replace(/tau/g, "6.283185307179586")
func = func.replace(/euler_gamma/g, "0.5772156649015329")
func = func.replace(/phi/g, "1.618033988749894")
const allowed_chars = [
'0', '1', '2',
'3', '4', '5',
'6', '7', '8',
'9', 'x', '*',
'+', '-', '/',
'.', '(', ')'
]
for (let char of func) {
if (!allowed_chars.includes(char)) {
await interaction.reply(`\`${func}\` is invalid`)
return
}
}
function f(n) {
return eval(func.replaceAll('x', `(${n})`) + `-${equal_to}`)
}
const dx = 10 ** -5
function f_prime(n) {
return (f(n + dx) - f(n)) / dx
}
let x_previous = x0
let x_current = 0
for (let i = 1; i < 100; i++) {
x_current = x_previous - f(x_previous) / f_prime(x_previous)
x_previous = x_current
}
func = formatEquation(func)
await interaction.reply('```' + `${func} = ${equal_to}\n=> x = ${x_current}` + '```')
}
}