-
Notifications
You must be signed in to change notification settings - Fork 3
/
cog_math.py
62 lines (55 loc) · 2.34 KB
/
cog_math.py
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
import matplotlib.pyplot as plt
import numpy
import nextcord
import math
from pyfunc.mathfuncs import primefactor
from nextcord.ext import commands
from pyfunc.commanddec import CogCommand
from pyfunc.eval_expr import evaluate,stringifyexpr
class Math(commands.Cog):
def __init__(self, bot:commands.Bot) -> None:
self.bot = bot
@CogCommand("eval")
async def eval(self, ctx:commands.Context, *, formulae:str="3 * ( 1 + 2 )") -> None:
result=evaluate(formulae)
await ctx.send(f"`{formulae} = {stringifyexpr(result)}`")
@CogCommand("plot")
async def plot(self, ctx:commands.Context, slope:int=3, yinter:int=3, min_:int=-20, max_:int=20) -> None:
xaxis = numpy.arange(min_, max_, 0.1)
yaxis = []
for x in xaxis:
yaxis += [slope*x+yinter]
plt.plot(xaxis, yaxis)
showy = f"{slope}x"
if yinter != 0: showy += ["+",""][yinter<0] + str(yinter)
plt.savefig("cache/plot.png")
plt.grid()
await ctx.send(file=nextcord.File("cache/plot.png", filename=f"{showy}.png"))
plt.close()
@CogCommand("prime")
async def prime(self, ctx:commands.Context, n:int=12) -> None:
if n <= 0: raise Exception('Cannot Factor Nonpositive Value')
def handleexpo(expo:int) -> str:
if expo==1:
return ''
return ''.join(list("⁰¹²³⁴⁵⁶⁷⁸⁹")[int(digit)] for digit in str(expo))
await ctx.send(f"{n} = {' * '.join([f'{p}{handleexpo(e)}' for p, e in primefactor(n).items()])}")
@CogCommand("factor")
async def factor(self, ctx:commands.Context, n:int=12) -> None:
if n <= 0: raise Exception('Cannot Factor Nonpositive Value')
pfactors=primefactor(n)
if math.prod([x+1 for x in pfactors.values()])>1_000_000:
raise Exception('Too Many Factors')
factors=[1]
for p,e in pfactors.items():
if p==1: # don't duplicate entries when 1 is present
continue
newfactors=[]
for i in range(e+1): # inclusive
factor=p**i
newfactors+=[factor*f for f in factors]
factors=newfactors
factors.sort() # in place sort
await ctx.send(f"{n} has {len(factors)} factors: \n{', '.join(map(str,factors))}")
def setup(bot:commands.Bot) -> None:
bot.add_cog(Math(bot))