def exercise_non_linear_ls_with_separable_scale_factor_plus_penalty():
    test = test_problems.polynomial_fit_with_penalty(normalised=False)
    test.build_up()
    assert test.n_equations == test.n_data + 1

    eps = 5e-14
    # reference values from tst_normal_equations.nb again

    assert approx_equal(test.optimal_scale_factor(), 0.6148971786833856, eps)
    redu = test.reduced_problem()
    assert test.objective() == redu.objective()
    assert test.step_equations().right_hand_side()\
           .all_eq(redu.step_equations().right_hand_side())
    assert test.step_equations().normal_matrix_packed_u()\
           .all_eq(redu.step_equations().normal_matrix_packed_u())

    assert approx_equal(test.objective(), 1.3196427075343262, eps)
    assert approx_equal(test.chi_sq(), 0.32991067688358156, eps)
    assert approx_equal(
        test.step_equations().right_hand_side(),
        (1.7214991729791487, -1.4619624074720623, 1.5748093588574208), eps)
    assert approx_equal(
        test.step_equations().normal_matrix_packed_u(),
        (1.371944193675858, -0.6093345300213344, 1.107972946555006,
         1.4185925035480405, -0.9192237056192452, 1.1976726805790037), eps)

    test_bis = test_problems.polynomial_fit_with_penalty(normalised=True)
    test_bis.build_up()
    assert approx_equal(test_bis.chi_sq(), test.chi_sq(), eps=1e-15)

    n_equations = test.n_equations
    test.build_up()
    assert test.n_equations == n_equations
def exercise_non_linear_ls_with_separable_scale_factor_plus_penalty():
  test = test_problems.polynomial_fit_with_penalty(normalised=False)
  test.build_up()
  assert test.n_equations == test.n_data + 1

  eps = 5e-14
  # reference values from tst_normal_equations.nb again

  assert approx_equal(test.optimal_scale_factor(), 0.6148971786833856, eps)
  redu = test.reduced_problem()
  assert test.objective() == redu.objective()
  assert test.step_equations().right_hand_side()\
         .all_eq(redu.step_equations().right_hand_side())
  assert test.step_equations().normal_matrix_packed_u()\
         .all_eq(redu.step_equations().normal_matrix_packed_u())

  assert approx_equal(test.objective(), 1.3196427075343262, eps)
  assert approx_equal(test.chi_sq(), 0.32991067688358156, eps)
  assert approx_equal(
    test.step_equations().right_hand_side(),
    (1.7214991729791487, -1.4619624074720623, 1.5748093588574208),
    eps)
  assert approx_equal(
    test.step_equations().normal_matrix_packed_u(),
    (1.371944193675858, -0.6093345300213344,  1.107972946555006,
                         1.4185925035480405, -0.9192237056192452,
                                              1.1976726805790037),
    eps)

  test_bis = test_problems.polynomial_fit_with_penalty(normalised=True)
  test_bis.build_up()
  assert approx_equal(test_bis.chi_sq(), test.chi_sq(), eps=1e-15)

  n_equations = test.n_equations
  test.build_up()
  assert test.n_equations == n_equations