Definite integrals

Discussion thread for Definite integrals.

I’m having difficulty with this problem; it passes all of the test cases except for sine.

I tried with a one-liner and then with a more conventional loop to sum up the areas, and both came out to the same answer.


As a sidenote, the provided arguments for the template in the program are confusing. They’re portrayed as

def area_of_rectangles(f, dx):

which might make the reader think that f should be a function. It should instead be something like

def area_of_rectangles(rects, dx):

Alternatively, you can make it a problem about higher-order functions, which are cute.

1 Like

I’m having the same problem with the sine case. I tried the usual way, but that didn’t work, so I tried some stable summation methods, like Kahan and Neumaier, but those didn’t work. So I tried math.fsum, but that also didn’t work. I looked into other methods of summation (that claimed to have been proven to be exact), but they also haven’t worked.

Perhaps there’s a problem with the checker.

1 Like

Hey @POGtastic and @firas5673, thanks for reporting this! There was indeed a problem with the checker.

The sine function was being integrated over a cycle so the area comes out to zero, but we were evaluating answers by checking the relative error which isn’t going to work here since we’re comparing two numbers that are close to zero (or machine epsilon).

I fixed it so it evaluates your answer using the absolute error instead. Your code should work now!

And good point about the function signature, just changed it to def area_of_rectangles(rects, dx) to be clearer, thank you!

This challenge seems to be unable to accept JavaScript submissions (by either file or in-page textarea). Upon submission of the following code, an error is reported which makes it seem as though the site is attempting to interpret the JavaScript as Python.

function area_of_rectangles(rects, dx) {
    return rects.map(rect => rect * dx)
                .reduce((a, b) => a + b, 0);
}

Error: Traceback (most recent call last):
File “/root/lovelace-engine/engine/api.py”, line 129, in on_post
user_outputs, p_infos = runner.run(self.container_name, code_filename, function_name, input_tuples, output_tuples)
File “/root/lovelace-engine/engine/code_runner.py”, line 104, in run
raise EngineExecutionError(exec_stdout)
engine.code_runner.EngineExecutionError: Traceback (most recent call last):
File “/root/f91f451a060137f2f65562bf1560d4c9bad312ab.run.py”, line 18, in
func_call_str = “area_of_rectangles(” + ", ".join([json.dumps(arg) for arg in input_tuple]) + “)”
File “/root/f91f451a060137f2f65562bf1560d4c9bad312ab.run.py”, line 18, in
func_call_str = “area_of_rectangles(” + ", ".join([json.dumps(arg) for arg in input_tuple]) + “)”
File “/usr/lib/python3.7/json/init.py”, line 231, in dumps
return _default_encoder.encode(obj)
File “/usr/lib/python3.7/json/encoder.py”, line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File “/usr/lib/python3.7/json/encoder.py”, line 257, in iterencode
return _iterencode(o, 0)
File “/usr/lib/python3.7/json/encoder.py”, line 179, in default
raise TypeError(f’Object of type {o.class.name} ’
TypeError: Object of type ndarray is not JSON serializable

Submitting the same code to a different challenge does not result in this error.

Moreover, when the in-page textarea is set to JavaScript, the provided template is invalid:

def area_of_rectangles(rects, dx):
    var area = 0
    // Your code goes here!
    return area
}

Thanks for reporting this @dhughes! We’ve done some cleanup recently so the code stub is now valid Javascript code and your mapreduce solution passes.