Skip to content

Commit 4268fe3

Browse files
authored
add newton_raphson (#28)
1 parent 0c50fc2 commit 4268fe3

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
with:
1515
submodules: recursive
1616
fetch-depth: 0
17-
- uses: mlugg/setup-zig@v1
17+
- uses: mlugg/setup-zig@v2
1818

1919
- name: Testing
2020
run: zig run runall.zig
@@ -23,5 +23,5 @@ jobs:
2323
runs-on: ubuntu-latest
2424
steps:
2525
- uses: actions/checkout@v4
26-
- uses: mlugg/setup-zig@v1
26+
- uses: mlugg/setup-zig@v2
2727
- run: zig fmt --check --ast-check */*.zig

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
* [Gcd](https://github.com/TheAlgorithms/Zig/blob/HEAD/math/gcd.zig)
2929
* [Primes](https://github.com/TheAlgorithms/Zig/blob/HEAD/math/primes.zig)
3030

31+
## Numerical Methods
32+
* [Newton Raphson Root](https://github.com/TheAlgorithms/Zig/blob/HEAD/numerical_methods/newton_raphson_root.zig)
33+
3134
## [Runall](https://github.com/TheAlgorithms/Zig/blob/HEAD//runall.zig)
3235

3336
## Search

build.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ pub fn build(b: *std.Build) void {
216216
.name = "k_means_clustering.zig",
217217
.category = "machine_learning",
218218
});
219+
// Numerical Methods
220+
if (std.mem.eql(u8, op, "numerical_methods/newton_raphson_root"))
221+
buildAlgorithm(b, .{
222+
.optimize = optimize,
223+
.target = target,
224+
.name = "newton_raphson_root.zig",
225+
.category = "numerical_methods",
226+
});
219227
}
220228

221229
fn buildAlgorithm(b: *std.Build, info: BInfo) void {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const StopCondition = struct {
2+
n_iterations: ?usize = 1000,
3+
epsilon_from_root: ?f32 = 1e-15,
4+
};
5+
6+
fn shouldStop(x: f32, n_iterations: usize, stop_condition: StopCondition, func: fn (f32) f32) bool {
7+
if (stop_condition.n_iterations) |n_iterations_to_stop| {
8+
if (n_iterations_to_stop <= n_iterations) {
9+
return true;
10+
}
11+
}
12+
if (stop_condition.epsilon_from_root) |epsilon_from_root| {
13+
if (@abs(func(x)) <= epsilon_from_root) {
14+
return true;
15+
}
16+
}
17+
return false;
18+
}
19+
20+
fn newtonRaphsonMethod(x: f32, stop_condition: StopCondition, func: fn (f32) f32, derivative: fn (f32) f32) f32 {
21+
var n_iterations: usize = 0;
22+
var guess = x;
23+
while (!shouldStop(guess, n_iterations, stop_condition, func)) : (n_iterations += 1) {
24+
guess -= func(guess) / derivative(guess);
25+
}
26+
return guess;
27+
}
28+
29+
const std = @import("std");
30+
const expectApproxEqAbs = std.testing.expectApproxEqAbs;
31+
32+
test "NewtonRaphsonMethod" {
33+
try expectApproxEqAbs(
34+
1.4142135623730951,
35+
newtonRaphsonMethod(
36+
1,
37+
.{
38+
.epsilon_from_root = 0.0001,
39+
.n_iterations = 10,
40+
},
41+
(struct {
42+
fn func(x: f32) f32 {
43+
return x * x - 2;
44+
}
45+
}).func,
46+
(struct {
47+
fn derivative(x: f32) f32 {
48+
return 2 * x;
49+
}
50+
}).derivative,
51+
),
52+
0.0001,
53+
);
54+
55+
try expectApproxEqAbs(
56+
2,
57+
newtonRaphsonMethod(
58+
1,
59+
.{
60+
.epsilon_from_root = 0.000001,
61+
.n_iterations = 100,
62+
},
63+
(struct {
64+
fn func(x: f32) f32 {
65+
return x * x - 4;
66+
}
67+
}).func,
68+
(struct {
69+
fn derivative(x: f32) f32 {
70+
return 2 * x;
71+
}
72+
}).derivative,
73+
),
74+
0.0001,
75+
);
76+
}

runall.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ pub fn main() !void {
4848

4949
// Machine Learning
5050
try runTest(allocator, "machine_learning/k_means_clustering");
51+
52+
// Numerical Methods
53+
try runTest(allocator, "numerical_methods/newton_raphson");
5154
}
5255

5356
fn runTest(allocator: std.mem.Allocator, comptime algorithm: []const u8) !void {

0 commit comments

Comments
 (0)