alest_t1/code/core.py
2026-09-10 10:06:01 -03:00

60 lines
1.2 KiB
Python

import sys
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.use("TkAgg")
def plot(x, y, filename=None):
fig, ax = plt.subplots()
ax.plot(x, y, 'r+')
ax.set(xlabel="N (input)")
ax.set(ylabel="f(N) (Contagem de Operações)")
if filename is not None:
fig.savefig(filename)
return
fig.show()
plt.show()
def plot_log(x, y, filename=None):
fig, ax = plt.subplots()
ax.plot(x, np.log(y), 'g+')
ax.set(xlabel="N (input)")
ax.set(ylabel="f(N) (Contagem de Operações)")
ax.set_yscale('log')
if filename is not None:
fig.savefig(filename)
return
fig.show()
plt.show()
def plot_exp(x, y, filename=None):
fig, ax = plt.subplots()
ax.plot(x, np.exp(np.clip(y, sys.float_info.min, sys.float_info.max)), 'b+')
ax.set(xlabel="N (input)")
ax.set(ylabel="f(N) (Contagem de Operações)")
ax.set_yscale('log')
if filename is not None:
fig.savefig(filename)
return
fig.show()
plt.show()
def function_result(f, n=100):
x = []
y = []
for i in range(n):
result = f(i)
cont_op = 1 if result == 0 else result
x.append(i)
y.append(cont_op)
return x, y