-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
168 lines (150 loc) · 4.53 KB
/
main.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
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
import os
import math
import cmath
import re
#from discord import File
#from matplotlib import pyplot as plt
import numpy as np
from discord.ext import commands
from keep_alive import keep_alive
bot = commands.Bot(command_prefix = '$')
def numerical_check(arg1, arg2):
try:
arg1 = float(arg1)
arg2 = float(arg2)
return True
except:
return False
def input_verification(arg1, arg2):
pattern = re.compile('\-?[0-9]+')
x = re.match(pattern, arg1)
y = re.match(pattern, arg2)
return x and y
def multVerification(arg1, arg2, arg3, arg4):
if (input_verification(arg1, arg2) and input_verification(arg3, arg4)):
if numerical_check(arg1, arg2) and numerical_check(arg3, arg4):
return True
return False
def powVerification(arg1, arg2, arg3):
if input_verification(arg1, arg2) and numerical_check(arg1, arg2) and numerical_check(arg3, 1):
return True
return False
def make_rect(r, phi):
r = float(r)
phi = float(phi)
return cmath.rect(r, phi)
def make_complex(re, im):
re = float(re)
im = float(im)
return complex(re, im)
def get_rect(r, phi):
r = float(r)
phi = float(phi)
return cmath.rect(r, phi)
def get_polar(x):
return cmath.polar(x)
@bot.command()
async def multRect(ctx, arg1, arg2, arg3, arg4):
if multVerification(arg1, arg2, arg3, arg4):
arg = [arg1, arg2, arg3, arg4]
for x in range(len(arg)):
arg[x] = float(arg[x])
await ctx.send(make_complex(arg[0], arg[1])*make_complex(arg[2], arg[3]))
else:
await ctx.send("Invalid")
@bot.command()
async def powerRect(ctx, arg1, arg2, arg3):
if powVerification(arg1, arg2, arg3):
new_list = [arg1, arg2, arg3]
for x in range(len(new_list)):
new_list[x] = float(new_list[x])
await ctx.send((make_complex(new_list[0], new_list[1]))**new_list[2])
else:
await ctx.send("Invalid")
@bot.command()
async def multPolar(ctx, arg1, arg2, arg3, arg4):
if multVerification(arg1, arg2, arg3, arg4):
arg = [arg1, arg2, arg3, arg4]
for x in range(len(arg)):
arg[x] = float(arg[x])
await ctx.send(make_rect(arg[0], arg[1])*make_rect(arg[2], arg[3]))
else:
await ctx.send("Invalid")
@bot.command()
async def powerPolar(ctx, arg1, arg2, arg3):
if powVerification(arg1, arg2, arg3):
new_list = [arg1, arg2, arg3]
for x in range(len(new_list)):
new_list[x] = float(new_list[x])
await ctx.send("r, theta(radians): {}".format(get_polar((make_rect(new_list[0], new_list[1]))**new_list[2])))
else:
await ctx.send("Invalid")
@bot.command()
async def inputRectangular(ctx, arg1, arg2):
if input_verification(arg1, arg2):
if numerical_check(arg1, arg2):
arg1 = float(arg1)
arg2 = float(arg2)
await ctx.send("Magnitude : {}".format(get_mag(arg1, arg2)))
await ctx.send("Phase(radians) : {}".format(get_phase(arg1, arg2)))
await ctx.send("Phase(degrees) : {}".format(np.rad2deg(get_phase(arg1, arg2))))
await ctx.send("Polar (Radians): {}".format(get_polar(make_complex(arg1, arg2))))
else:
await ctx.send("Incorrect format. Send two numbers")
else:
await ctx.send("Incorrect format. Send two numbers")
@bot.command()
async def inputPol(ctx, arg1, arg2):
if input_verification(arg1, arg2):
if numerical_check(arg1, arg2):
arg1 = float(arg1)
arg2 = float(arg2)
await ctx.send("Rect : {}".format(get_rect(arg1, arg2)))
else:
await ctx.send("Incorrect format. Send two numbers")
else:
await ctx.send("Incorrect format. Send two numbers")
def get_mag(re, im):
try:
re = float(re)
im = float(im)
except:
return None
return math.sqrt(re**2+im**2)
def get_phase(re, im):
re = float(re)
im = float(im)
val = math.atan2(abs(im), abs(re))
if (re >= 0 and im >= 0):
return val
elif (re < 0 and im>= 0):
return math.pi-val
elif (re < 0 and im < 0):
return val-math.pi
else:
return -1*val
@bot.event
async def on_ready():
print("Logged in as {0.user}".format(bot))
print("Read documentation at GitHub repo Readme")
# @bot.command()
# async def graphRect(ctx, arg1, arg2):
# pattern = re.compile('\-?[0-9]+')
# x = re.match(pattern, arg1)
# y = re.match(pattern, arg2)
# if x and y:
# try:
# arg1 = float(arg1)
# arg2 = float(arg2)
# except:
# await ctx.send("Error")
# await ctx.send("Input is ok")
# graph = File('graph.png')
# ax = plt.axes()
# ax.arrows(0, 0, arg1, arg2)
# plt.savefig('graph.png')
# await ctx.send(file=graph)
# else:
# await ctx.send("Incorrect format. Send two numbers")
keep_alive()
bot.run(os.getenv('TOKEN'))