예제 #1
0
def fib_table(output=True, decimals=3):
    """Generate table showing reduced recursive invocations of fibonacci."""
    import math

    tbl = DataTable([8, 12, 12], ['N', 'FiRec', 'Model'],
                    output=output,
                    decimals=decimals)
    tbl.format('FiRec', 'd')

    def exp_model(n, a, b):
        """Formula for A*N^B ."""
        return a * math.pow(n, b)

    for n in range(3, 100):
        old = numRecursiveImproved[0]
        fib_with_lucas(n)
        model = exp_model(n, 0.28711343, 2.58031481)
        tbl.row([n, (numRecursiveImproved[0] - old), model])

    if numpy_error:
        pass
    else:
        import numpy as np
        from scipy.optimize import curve_fit

        x_arr = np.array(tbl.column(tbl.labels[0]))
        y_arr = np.array(tbl.column(tbl.labels[1]))

        def np_exp_model(n, a, b):
            """Formula for A*N^B ."""
            return a * np.power(n, b)

        if output:
            [exp_coeffs, _] = curve_fit(np_exp_model, x_arr, y_arr)
            print('A*N^B  = {:.12f}*N^{:f} '.format(exp_coeffs[0],
                                                    exp_coeffs[1]))

    return tbl
예제 #2
0
    def test_table(self):
        tbl = DataTable([8, 8, 8], ['N', 'Another', 'SquareRoot'],
                        output=False,
                        decimals=4)
        tbl.format('Another', 'd')
        for n in range(2, 10):
            tbl.row([n, n, n**0.5])
        self.assertEqual(tbl.entry(3, 'Another'), 3)

        print('Testing that Table is print to console')
        tbl = DataTable([8, 8, 8], ['N', 'Another', 'SquareRoot'], decimals=4)
        tbl.format('Another', 'd')
        for n in range(2, 10):
            tbl.row([n, n, n**0.5])

        self.assertEqual(list(range(2, 10)), tbl.column('Another'))

        model = tbl.best_model('Another')[0]
        if numpy_error:
            pass
        else:
            self.assertEqual(model[0], Model.LINEAR)
            self.assertAlmostEqual(model[3], 1.0000, places=5)