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

Add test for a small newton raphson package #1609

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ RUN(NAME test_str_comparison LABELS cpython llvm c)
RUN(NAME test_bit_length LABELS cpython llvm c)
RUN(NAME str_to_list_cast LABELS cpython llvm c)

RUN(NAME test_package_01 LABELS cpython llvm)

RUN(NAME generics_01 LABELS cpython llvm c)
RUN(NAME generics_02 LABELS cpython llvm c)
RUN(NAME generics_array_01 LABELS cpython llvm c)
Expand Down
1 change: 1 addition & 0 deletions integration_tests/nrp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .nr import newton_raphson
20 changes: 20 additions & 0 deletions integration_tests/nrp/nr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from ltypes import i32, f64


def func(x: f64, c: f64) -> f64:
return x**2.0 - c**2.0


def func_prime(x: f64) -> f64:
return 2.0*x


def newton_raphson(x: f64, c: f64, maxiter: i32) -> f64:
h: f64
err: f64 = 1e-5
i: i32 = 0
while abs(func(x, c)) > err and i < maxiter:
h = func(x, c) / func_prime(x)
x -= h
i += 1
return x
13 changes: 13 additions & 0 deletions integration_tests/test_package_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from nrp import newton_raphson
from ltypes import f64, i32


def check():
x0: f64 = 20.0
c: f64 = 3.0
maxiter: i32 = 20
x: f64
x = newton_raphson(x0, c, maxiter)
assert abs(x - 3.0) < 1e-5

check()