Question: I have defined a step function in Python using the following code. The function takes in an array a and x values, applies some calculations, and returns a step function f. Additionally, I have defined two helper functions rect and psi_j_n. I'd like to calculate the signed area of the product of step_function and psi_j_n(x, -10, 0) without using the integral because it is a rectangle, and that's the area I'm looking for. My initial attempt: signed_area = 0 for x_values in x: signed_area += step_function(x_values, a) * psi_j_n(x_values, -10, 0) signed_area  is incorrect because I am missing the length of the base of the rectangle. When I calculate the area by hand, I should get 0.012. Update I used the following code to define the step function: import numpy as np import matplotlib.pyplot as plt # Define the step function def step_function(x, a): def rect(x): return np.where((x >= 0) & (x < 1), 1, 0) f = np.sum([a[k-1] * rect(x - k) for k in range(1, len(a) + 1)], axis=0) return f # Set the random seed for reproducibility np.random.seed(42) # Generate random values for a_k N = 10 a = np.array([-0.25091976, 0.90142861, 0.46398788, 0.19731697, -0.68796272, -0.68801096, -0.88383278, 0.73235229, 0.20223002, 0.41614516]) #a = np.random.uniform(-1, 1, size=N) # Define the x-values for plotting x = np.arange(0, N + 1, 0.01) # Evaluate the step function at x y = step_function(x, a) # Plot the step function plt.plot(x, y) plt.xlabel('x') plt.ylabel('f(x)') plt.title('Step Function Plot') plt.grid(True) plt.show()  It produces the picture  The following code defines instead the function psi_j_n(x, j, n): def psi(x): if 0 <= x < 0.5: return 1 elif 0.5 <= x < 1: return -1 else: return 0 def psi_j_n(x, j, n): return 2**(j/2) * psi(2**j * x - n)  Then, I would like to calculate the product of step_function(x, a) and psi_j_n(x, j, n).

Answer: As you said, the only missing thing in your code is the base of the rectangle. You choose it to 0.01, so why not just multiply the result by 0.01? signed_area = 0 for x_values in x: signed_area += 0.01*step_function(x_values, a) * psi_j_n(x_values, -10, 0) signed_area  Note that I could have multiplied by 0.01 the final result instead, that would be more efficient (but anyway that code is not efficient at all; see next how to make it so). But putting it there make it easier to adapt the code to cases where the area of each rectangle is not constant. More efficient version What you need is a vectorized version of psi. As is, you can call it only with scalar values, and it return a scalar value. psi(0.1) # 1 psi(0.6) # -1 psi(2) # 0  We would like to also be able to call psi([0.1, 0.2, 2]) # array([1,-1,0])  That can be done many way. The trick is to avoid calling for loop at all cost. One way could be def psi(x): return ((x>=0)&(x<1))*(2*(x<0.5)-1)  Or you could use np.where as you already did for another function. Now that psi is "vectorized", and so is therefore psi_j_n, you can just compute the multiplication of psi_j_n(x,-10,0) (which now makes sense, even if x is an array) and step_function(x,a) aka y in your code. So simply psi_j_n(x,-10,0)*y  And then compute the integral of that (you said you didn't want any integrals, but since your attempt is computing that integral, I take you meant that you didn't want to have to calculate the formulation of this integral on a piece of paper before coding). The simpler way to do that with rectangle method is (psy_j_n(x,-10,0)*y).sum()*0.01)  Which is indeed 0.012585459687500009