79225965

Date: 2024-11-26 09:02:30
Score: 0.5
Natty:
Report link

The keyword here were wrappers. I did not know how to use wrappers since I didn't want to write a @plot_interesting_thing on top of every def method(self). However I can change the methods using wrappers externally. For example, what I am interested in plotting right now is a scatter plot that looks like (f_0(1), f_1(1)), (f_1(1), f_2(2)), ... . What I managed to do is wrap the function as follows.

import matplotlib.pyplot as plt

class Experiment:
    def __init__(self):
        self.x = np.linspace(0, 1, 100)
        self.f = lambda x: 0 # Initial function

    def update_f(self):
        # Does something to f
        self.f = lambda x: f(x)**2 + 1

    def main_calculation(self):
        # For some definition of convergence
        while not self.convergence():
             self.update_f()

experiment = Experiment()

fig, ax = plt.subplots()

def fixed_point_wrapper(fun):
    def inner():
        a1 = experiment.f(1)
        experiment.update_f()
        ax.plot(a1, experiment.f(1))

experiment.update_f = fixed_point_wrapper(experiment.update_f)

experiment.main_calculation()
plt.show()

This way I can get the specific plot I want for this calculation

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: dolefeast