def test_brenth_solve_from_tuples(self):
        taw = TupleArrayWrapperFactory.create_taw_1()
        k = NumericalSolver.solve(taw.taw, brenth, taw.taw.get_min_x(), taw.taw.get_max_x(), 1e-100, 1e-100)
        assert_ans(self, k, taw.root)

        taw = TupleArrayWrapperFactory.create_taw_2()
        k = NumericalSolver.solve(taw.taw, brenth, taw.taw.get_min_x(), taw.taw.get_max_x(), 1e-100, 1e-100)
        assert_ans(self, k, taw.root)
    def test_data_clear_statistics(self):
        taw = TupleArrayWrapperFactory.create_taw_1()
        k = NumericalSolver.solve_with_methods(taw.taw, taw.taw.get_min_x(), taw.taw.get_max_x(), methods, 1e-100,
                                               1e-100)

        summ = 0
        i = 0
        for ans in k.values():
            summ += ans.statistics.callsNumbers
            i += 1

        for ans in k.values():
            self.assertNotEqual(summ / i, ans.statistics.callsNumbers)
 def test_wrong_bracket(self):
     assert NumericalSolver.solve(LambdaWrapper(lambda x: x), "", 2, 1, 0.1, 0.1).is_ok is False
 def test_solve_from_tuples_2(self):
     taw = TupleArrayWrapperFactory.create_taw_2()
     k = NumericalSolver.solve_with_methods(taw.taw, taw.taw.get_min_x(), taw.taw.get_max_x(), methods, 1e-100,
                                            1e-100)
     self.assert_answer(k, taw)
 def test_newton_small_eps(self):
     eps_x = 1e-6
     self.assertAlmostEqual(
         NumericalSolver.solve(LambdaWrapper(lambda x: x * x + 2 * x - 3), "newton", -5, 0, eps_x, eps_x).root, -3,
         10)
 def test_newton(self):
     self.assertAlmostEqual(
         NumericalSolver.solve(LambdaWrapper(lambda x: x * x + 2 * x - 3), "newton", -5, 0, 0.1, 0.1).root, -3, 1)
 def test_bisections(self):
     eps_x = 1e-6
     ans = NumericalSolver.solve(LambdaWrapper(lambda x: math.sin(x)), "bisections", -1.1, 1, eps_x, eps_x)
     assert_ans(self, ans, 0)
 def test_chords(self):
     assert NumericalSolver.solve(LambdaWrapper(lambda x: x), "bisections", -1, 1, 0.1, 0.1).root == 0
 def test_no_roots(self):
     assert NumericalSolver.solve(LambdaWrapper(lambda x: x), "", 2, 3, 0.1, 0.1).is_ok is False
Пример #10
0
def process_file(file, args, inputp):
    all_results_by_method = dict()
    call_number_by_method = dict()
    iters_by_method = dict()
    fails_by_method = dict()

    for method in args.methods:
        all_results_by_method[method] = list()
        call_number_by_method[method] = list()
        iters_by_method[method] = list()
        fails_by_method[method] = list()

    res = NumericalSolver.solve_from_file_data(os.path.join(inputp, file),
                                               args.epsilon_x, args.epsilon_y,
                                               args.methods)
    for i in res:
        for j in i.values():
            all_results_by_method[j.method].append(j)

    total_eqs = len(all_results_by_method[args.methods[0]])

    methods_output = list()
    methods_output.append(f"Processed {total_eqs} equations.")

    for method in args.methods:
        good = 0
        bad = 0
        calls_number = 0
        iters = 0
        for eqs in all_results_by_method[method]:
            if eqs.is_ok is not True:
                bad += 1
                continue
            else:
                good += 1
                calls_number += eqs.statistics.callsNumbers
                iters += eqs.statistics.iterations

        methods_output.append(
            f"METHOD {method}. Failed: {bad / total_eqs * 100}% | Avg func calls: {calls_number / good} | Avg iters: {iters / good}"
        )

    with open(os.path.join(inputp, f"{file}_REPORT.txt"), "w") as f:
        f.write('\n'.join(methods_output))

    for method in all_results_by_method.values():
        for m in method:
            if m.is_ok is True:
                call_number_by_method[m.method].append(
                    m.statistics.callsNumbers)
                iters_by_method[m.method].append(m.statistics.iterations)

    fig, axes = plt.subplots(len(args.methods), 2)
    fig.set_size_inches(18.5, 10.5)

    i = 0

    max_calls = max(call_number_by_method["bisections"])
    max_iteration = max(iters_by_method["bisections"])

    for method in args.methods:
        axes[i, 1].hist(call_number_by_method[method],
                        density=True,
                        bins=np.arange(0, max_calls, max_calls / 50),
                        color="black")
        axes[i, 1].set(xlabel='Function calls', ylabel="Probability")
        axes[i, 1].set_title(method)
        i += 1

    i = 0

    for method in args.methods:
        axes[i, 0].hist(iters_by_method[method],
                        density=True,
                        bins=np.arange(0, max_iteration, max_iteration / 50),
                        color="black")
        axes[i, 0].set(xlabel='Iterations', ylabel="Probability")
        axes[i, 0].set_title(method)
        i += 1

    plt.tight_layout()
    fig.savefig(os.path.join(inputp, f'{file}_GRAPH.png'), dpi=fig.dpi)