Skip to content

Update newton_raphson.py#8869

Closed
rohan472000 wants to merge 13 commits intoTheAlgorithms:masterfrom
rohan472000:patch-13
Closed

Update newton_raphson.py#8869
rohan472000 wants to merge 13 commits intoTheAlgorithms:masterfrom
rohan472000:patch-13

Conversation

@rohan472000
Copy link
Copy Markdown
Contributor

@rohan472000 rohan472000 commented Jul 16, 2023

Describe your change:

Fixes #8863

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes Refactor arithmetic_analysis/newton_raphson to not use eval #8863".

@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Jul 16, 2023
@rohan472000
Copy link
Copy Markdown
Contributor Author

@cclauss , plz review this.

Comment thread arithmetic_analysis/newton_raphson.py Outdated
Comment thread arithmetic_analysis/newton_raphson.py Outdated
Comment thread arithmetic_analysis/newton_raphson.py Outdated
Comment thread arithmetic_analysis/newton_raphson.py Outdated
Comment thread arithmetic_analysis/newton_raphson.py
Co-authored-by: Christian Clauss <cclauss@me.com>
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Jul 16, 2023
@rohan472000
Copy link
Copy Markdown
Contributor Author

rohan472000 commented Jul 16, 2023

while executing this test print(f"The root of sin(x) = 0 with precision 0 is {newton_raphson('sin(x)', 2, precision=0)}") it took too much time and still not executed...so cancelled that. Same with precision=-1.

Also precision is initially defined in function argument (precision: float = 10**-10) then why we should make a test case with different precisions??

@cclauss
Copy link
Copy Markdown
Member

cclauss commented Jul 16, 2023

why we should make a test case with different precisions??

Because the caller might choose to change the default. Why are there no tests that change the default precision?

Comment thread arithmetic_analysis/newton_raphson.py Outdated
Comment thread arithmetic_analysis/newton_raphson.py
@algorithms-keeper algorithms-keeper Bot added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Jul 16, 2023
rohan472000 and others added 3 commits July 16, 2023 20:42
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass and removed awaiting changes A maintainer has requested changes to this PR labels Jul 16, 2023
Comment thread arithmetic_analysis/newton_raphson.py Outdated
Co-authored-by: Christian Clauss <cclauss@me.com>
Comment thread arithmetic_analysis/newton_raphson.py
@cclauss
Copy link
Copy Markdown
Member

cclauss commented Jul 16, 2023

How does scipy.optimize.newton() deal with a precision of 0 and -1?

@cclauss
Copy link
Copy Markdown
Member

cclauss commented Jul 16, 2023

@QuantumNovice your review of these changes please.

@rohan472000
Copy link
Copy Markdown
Contributor Author

How does scipy.optimize.newton() deal with a precision of 0 and -1?

Screenshot (228)

for -1 it is throwing error.

@QuantumNovice
Copy link
Copy Markdown
Contributor


This should precision <= 0 as python will set the float to zero which is a valid answer.
image

The newton method implemented in scipy is entirely a numerical method.
https://github.com/scipy/scipy/blob/cfe80116aaa145061246b8aec0e98248fbefb835/scipy/optimize/_zeros_py.py#L105

@QuantumNovice
Copy link
Copy Markdown
Contributor

max_iterations = 100

This should be an input variable as it does not generate correct solution for arbitrary function.

@cclauss
Copy link
Copy Markdown
Member

cclauss commented Jul 17, 2023

@QuantumNovice What would be a valid test to replace

    >>> all(newton_raphson("log(x) - 1", 2) == newton("log(x) - 1", 2)
    ...     for precision in (10, 100, 1000, 10000))
    True

@cclauss
Copy link
Copy Markdown
Member

cclauss commented Jul 17, 2023

Why does this test not pass?

>>> from math import log
>>> from scipy.optimize import newton
>>>
>>> def log_x_minus_one(x):
...     return log(x) - 1
...
>>> def compare(precision):
...         a = newton_raphson("log(x) - 1", 2, precision=1)
...         b = newton(log_x_minus_one, 2, tol=1)
...         assert a == b, f"{a = } != {b = }"
...         return a == b
...
>>> all(compare(precision) for precision in (1, 10, 100, 1000, 10000))
True

@tianyizheng02
Copy link
Copy Markdown
Contributor

Closing due to lack of fixes in response to code review. We need to clear out our backlog of PRs as much as possible before Hacktoberfest, and this PR still has failing tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor arithmetic_analysis/newton_raphson to not use eval

4 participants