Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 00fc23d

Browse files
document main.py and solve constant function problem
1 parent d197d9a commit 00fc23d

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

‎src/main.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,29 @@
1616

1717

1818
class FunctionPlotter(QMainWindow):
19+
"""
20+
A class representing the main window for the function plotter application.
21+
22+
This class handles the GUI elements, user inputs, and plotting of mathematical functions
23+
using PySide2 for the GUI and Matplotlib for plotting.
24+
"""
25+
1926
def __init__(self):
2027
super().__init__()
2128
self.setWindowTitle("Function Plotter")
2229

23-
# Central widget
30+
# Initialize the central widget and main layout
2431
self.central_widget = QWidget()
2532
self.setCentralWidget(self.central_widget)
26-
27-
# Layouts
2833
self.layout = QVBoxLayout()
2934
self.input_layout = QHBoxLayout()
3035
self.central_widget.setLayout(self.layout)
3136

32-
# Function input
37+
# Initialize input fields for the function and range values
3338
self.function_input = QLineEdit()
3439
self.function_input.setPlaceholderText("Enter function of x, e.g., 5*x^3 + 2*x")
3540
self.input_layout.addWidget(self.function_input)
3641

37-
# Min and max inputs
3842
self.min_input = QLineEdit()
3943
self.min_input.setPlaceholderText("Enter min value of x")
4044
self.input_layout.addWidget(self.min_input)
@@ -45,19 +49,21 @@ def __init__(self):
4549

4650
self.layout.addLayout(self.input_layout)
4751

48-
# Plot button
52+
# Initialize the plot button
4953
self.plot_button = QPushButton("Plot Function")
5054
self.plot_button.clicked.connect(self.plot_function)
5155
self.layout.addWidget(self.plot_button)
5256

53-
# Matplotlib figure
57+
# Initialize the Matplotlib figure and canvas
5458
self.figure, self.ax = plt.subplots()
5559
self.canvas = FigureCanvas(self.figure)
5660
self.layout.addWidget(self.canvas)
5761

5862
def plot_function(self):
59-
function = self.function_input.text()
60-
function = function.replace(" ", "")
63+
"""
64+
Handles the plotting of the function based on user inputs.
65+
"""
66+
function = self.function_input.text().replace(" ", "")
6167
min_x = self.min_input.text()
6268
max_x = self.max_input.text()
6369

@@ -67,7 +73,12 @@ def plot_function(self):
6773

6874
try:
6975
min_x = float(min_x)
76+
# Make range from -1e20 to 1e20 whatever the input
77+
min_x = max(min(1e20, min_x), -1e20)
7078
max_x = float(max_x)
79+
# Make range from -1e20 to 1e20 whatever the input
80+
max_x = max(min(1e20, max_x), -1e20)
81+
7182
except ValueError:
7283
self.show_error_message("Min and Max values must be numbers.")
7384
return
@@ -89,7 +100,7 @@ def plot_function(self):
89100
self.show_error_message(f"Error in function evaluation: {e}")
90101
return
91102

92-
# Clear previous plot
103+
# Clear previous plot and plot new function
93104
self.ax.clear()
94105
self.ax.plot(x, y)
95106
self.ax.set_title(f"Plot of {function}")
@@ -98,6 +109,17 @@ def plot_function(self):
98109
self.canvas.draw()
99110

100111
def validate_inputs(self, function, min_x, max_x):
112+
"""
113+
Validates the user inputs for the function and range values.
114+
115+
Parameters:
116+
- function (str): The mathematical function entered by the user.
117+
- min_x (str): The minimum value of x entered by the user.
118+
- max_x (str): The maximum value of x entered by the user.
119+
120+
Returns:
121+
- bool: True if inputs are valid, False otherwise.
122+
"""
101123
if not function:
102124
self.show_error_message("Function cannot be empty.")
103125
return False
@@ -113,12 +135,31 @@ def validate_inputs(self, function, min_x, max_x):
113135
return True
114136

115137
def prepare_function(self, function, x):
138+
"""
139+
Prepares the function for evaluation by replacing operators with their numpy equivalents.
140+
141+
Parameters:
142+
- function (str): The mathematical function entered by the user.
143+
- x (np.ndarray): The array of x values.
144+
145+
Returns:
146+
- str: The prepared function.
147+
"""
116148
function = function.replace("^", "**")
117149
function = function.replace("log10", "np.log10")
118150
function = function.replace("sqrt", "np.sqrt")
151+
# if function is a constant (e.g. y=5)
152+
if "x" not in function:
153+
function = f"{function}*np.ones_like(x)"
119154
return function
120155

121156
def show_error_message(self, message):
157+
"""
158+
Displays an error message dialog.
159+
160+
Parameters:
161+
- message (str): The error message to display.
162+
"""
122163
QMessageBox.critical(self, "Error", message)
123164

124165

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /