Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divide by zero error in line search #8

Open
dspoel opened this issue Apr 15, 2024 · 2 comments
Open

Divide by zero error in line search #8

dspoel opened this issue Apr 15, 2024 · 2 comments

Comments

@dspoel
Copy link

dspoel commented Apr 15, 2024

At line 40 in the linesearch.cpp code there is a divide by zero occurring.

    double find_quadratic_minimizer(double a, double ga, double b, double gb) {
        return b + ((b - a)*gb)/(ga - gb);
    }

I tried to code a work around with an if (ga == gb) statement, but don't know what the return value should be in that case. Suggestions?

    double find_quadratic_minimizer(double a, double ga, double b, double gb) {
        if (ga == gb) {
          return 0;
        }
        else {
          return b + ((b - a)*gb)/(ga - gb);
        }
    }
@ssloy
Copy link
Member

ssloy commented Apr 15, 2024

This functions looks for the minimizer of a quadratic function f(x) defined over the interval [a,b] with given derivatives f'(a) and f'(b). In order to have the problem well-posed the derivatives must obey the condition f'(a)<0 and f'(b)>0.

find_quadratic_minimizer is called from lines 51 and 46 ; 51 should not pose any problem since the condition is verified explicitly, and a call from 46 guarantees two different values f(a) and f(b), and therefore the derivatives must differ.

It does seem that you are working beyond the limits of machine precision.

It is a bad idea to have this workaround. Linesearch must fail in this case. Division by zero is supposed to be checked in lines 90-98 of linesearch.cpp, it would be a good idea to investigate. Do you get an exception?

@dspoel
Copy link
Author

dspoel commented Apr 15, 2024

Yes, the code crashes with some specific input with a 1/0 error.

I do think precision may be the issue and will try to move to an integer x vector.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants