def exercise_non_linear_ls_with_separable_scale_factor():
  test = test_problems.polynomial_fit(normalised=False)
  test.build_up()

  assert test.n_equations == test.n_data;

  # Reference values computed in tst_normal_equations.nb
  eps = 5e-14
  assert approx_equal(test.optimal_scale_factor(), 0.6148971786833856, eps)
  assert approx_equal(test.objective(), 0.039642707534326034, eps)
  assert approx_equal(test.chi_sq(), 0.011326487866950296, eps)


  assert not test.step_equations().solved
  try:
    test.step_equations().cholesky_factor_packed_u()
    raise Exception_expected
  except RuntimeError:
    pass
  try:
    test.step_equations().solution()
    raise Exception_expected
  except RuntimeError:
    pass

  assert approx_equal(
    list(test.step_equations().normal_matrix_packed_u()),
    [ 0.371944193675858, 0.39066546997866547  , 0.10797294655500618,
                           0.41859250354804045, 0.08077629438075473,
                                                0.19767268057900367 ],
    eps)
  assert approx_equal(
    list(test.step_equations().right_hand_side()),
    [ 0.12149917297914861, 0.13803759252793774, -0.025190641142579157 ],
    eps)

  test.step_equations().solve()

  assert test.step_equations().solved
  try:
    test.step_equations().normal_matrix_packed_u()
    raise Exception_expected
  except RuntimeError:
    pass
  try:
    test.step_equations().right_hand_side()
    raise Exception_expected
  except RuntimeError:
    pass

  assert approx_equal(
    list(test.step_equations().cholesky_factor_packed_u()),
    [ 0.6098722765266986, 0.6405693208478925   ,  0.1770418999366983 ,
                            0.09090351333425013, -0.3589664912436558 ,
                                                  0.19357661121640218 ],
    eps)
  assert approx_equal(
    list(test.step_equations().solution()),
    [ 1.2878697604109028, -0.7727798877778043, -0.5151113342942297 ],
    eps=1e-12)

  test_bis = test_problems.polynomial_fit(normalised=True)
  test_bis.build_up()
  assert approx_equal(test_bis.objective(),
                      test.objective()/test.sum_w_yo_sq(),
                      eps=1e-15)
  assert approx_equal(test_bis.chi_sq(), test.chi_sq(), eps=1e-15)
示例#2
0
def exercise_non_linear_ls_with_separable_scale_factor():
    for impl in non_linear_ls_with_separable_scale_factor__impls:
        test = test_problems.polynomial_fit(impl)(normalised=False)
        test.build_up()

        assert test.n_equations == test.n_data

        # Reference values computed in tst_normal_equations.nb
        eps = 5e-14
        assert approx_equal(test.optimal_scale_factor(), 0.6148971786833856,
                            eps)
        assert approx_equal(test.objective(), 0.039642707534326034, eps)
        assert approx_equal(test.chi_sq(), 0.011326487866950296, eps)

        assert not test.step_equations().solved
        try:
            test.step_equations().cholesky_factor_packed_u()
            raise Exception_expected
        except RuntimeError:
            pass
        try:
            test.step_equations().solution()
            raise Exception_expected
        except RuntimeError:
            pass

        assert approx_equal(
            list(test.step_equations().normal_matrix_packed_u()), [
                0.371944193675858, 0.39066546997866547, 0.10797294655500618,
                0.41859250354804045, 0.08077629438075473, 0.19767268057900367
            ], eps)
        assert approx_equal(
            list(test.step_equations().right_hand_side()),
            [0.12149917297914861, 0.13803759252793774, -0.025190641142579157],
            eps)

        test.step_equations().solve()

        assert test.step_equations().solved
        try:
            test.step_equations().normal_matrix_packed_u()
            raise Exception_expected
        except RuntimeError:
            pass
        try:
            test.step_equations().right_hand_side()
            raise Exception_expected
        except RuntimeError:
            pass

        assert approx_equal(
            list(test.step_equations().cholesky_factor_packed_u()), [
                0.6098722765266986, 0.6405693208478925, 0.1770418999366983,
                0.09090351333425013, -0.3589664912436558, 0.19357661121640218
            ], eps)
        assert approx_equal(
            list(test.step_equations().solution()),
            [1.2878697604109028, -0.7727798877778043, -0.5151113342942297],
            eps=1e-12)

        test_bis = test_problems.polynomial_fit(impl)(normalised=True)
        test_bis.build_up()
        assert approx_equal(test_bis.objective(),
                            test.objective() / test.sum_w_yo_sq(),
                            eps=1e-15)
        assert approx_equal(test_bis.chi_sq(), test.chi_sq(), eps=1e-15)