From afbbb87088697958c987ffd5b8db08f95d82b07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=A6=80=20adia-dev?= Date: Wed, 21 Feb 2024 00:05:30 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20NovaNewtonRaphson=20Z3,Z4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/models/fractal/fractal_descriptor.rs | 3 ++ shared/src/models/fractal/mod.rs | 2 + .../models/fractal/nova_newton_raphson_z3.rs | 42 +++++++++++++++++++ .../models/fractal/nova_newton_raphson_z4.rs | 42 +++++++++++++++++++ shared/src/models/fragments/fragment_task.rs | 8 +++- shared/src/networking/server.rs | 4 +- 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 shared/src/models/fractal/nova_newton_raphson_z3.rs create mode 100644 shared/src/models/fractal/nova_newton_raphson_z4.rs diff --git a/shared/src/models/fractal/fractal_descriptor.rs b/shared/src/models/fractal/fractal_descriptor.rs index f33f876..e51d501 100644 --- a/shared/src/models/fractal/fractal_descriptor.rs +++ b/shared/src/models/fractal/fractal_descriptor.rs @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize}; use super::{ iterated_sin_z::IteratedSinZ, julia::Julia, mandelbrot::Mandelbrot, newton_raphson_3::NewtonRaphsonZ3, newton_raphson_4::NewtonRaphsonZ4, + nova_newton_raphson_z3::NovaNewtonRaphsonZ3, nova_newton_raphson_z4::NovaNewtonRaphsonZ4, }; #[derive(Debug, Clone, Serialize, Deserialize)] pub enum FractalDescriptor { @@ -11,4 +12,6 @@ pub enum FractalDescriptor { IteratedSinZ(IteratedSinZ), NewtonRaphsonZ3(NewtonRaphsonZ3), NewtonRaphsonZ4(NewtonRaphsonZ4), + NovaNewtonRapshonZ3(NovaNewtonRaphsonZ3), + NovaNewtonRapshonZ4(NovaNewtonRaphsonZ4), } diff --git a/shared/src/models/fractal/mod.rs b/shared/src/models/fractal/mod.rs index aa65446..7dfc5e4 100644 --- a/shared/src/models/fractal/mod.rs +++ b/shared/src/models/fractal/mod.rs @@ -5,3 +5,5 @@ pub mod julia; pub mod mandelbrot; pub mod newton_raphson_3; pub mod newton_raphson_4; +pub mod nova_newton_raphson_z3; +pub mod nova_newton_raphson_z4; diff --git a/shared/src/models/fractal/nova_newton_raphson_z3.rs b/shared/src/models/fractal/nova_newton_raphson_z3.rs new file mode 100644 index 0000000..bb667a3 --- /dev/null +++ b/shared/src/models/fractal/nova_newton_raphson_z3.rs @@ -0,0 +1,42 @@ +use complex_rs::complex::Complex; +use serde::{Deserialize, Serialize}; + +use super::fractal::Fractal; + +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] +pub struct NovaNewtonRaphsonZ3 {} + +impl NovaNewtonRaphsonZ3 { + pub fn new() -> Self { + Self {} + } + + fn fz(&self, z: Complex) -> Complex { + z * z * z - Complex::new(1.0, 0.0) + } + + fn dfz(&self, z: Complex) -> Complex { + Complex::new(3.0, 0.0) * z * z + } +} + +impl Fractal for NovaNewtonRaphsonZ3 { + fn generate(&self, max_iterations: u32, x: f64, y: f64) -> (f64, f64) { + let c = Complex::new(x, y); + let mut z = Complex::new(1.0, 0.0); + let mut zn_next; + let epsilon = 1e-6; + let mut i = 0; + + loop { + zn_next = z - (self.fz(z) / self.dfz(z)) + c; + if (zn_next - z).arg_sq() < epsilon || i >= max_iterations { + break; + } + z = zn_next; + i += 1; + } + + return (0.0, i as f64); + } +} diff --git a/shared/src/models/fractal/nova_newton_raphson_z4.rs b/shared/src/models/fractal/nova_newton_raphson_z4.rs new file mode 100644 index 0000000..9d00d8c --- /dev/null +++ b/shared/src/models/fractal/nova_newton_raphson_z4.rs @@ -0,0 +1,42 @@ +use complex_rs::complex::Complex; +use serde::{Deserialize, Serialize}; + +use super::fractal::Fractal; + +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] +pub struct NovaNewtonRaphsonZ4 {} + +impl NovaNewtonRaphsonZ4 { + pub fn new() -> Self { + Self {} + } + + fn fz(&self, z: Complex) -> Complex { + z * z * z * z - Complex::new(1.0, 0.0) + } + + fn dfz(&self, z: Complex) -> Complex { + Complex::new(4.0, 0.0) * z * z * z + } +} + +impl Fractal for NovaNewtonRaphsonZ4 { + fn generate(&self, max_iterations: u32, x: f64, y: f64) -> (f64, f64) { + let c = Complex::new(x, y); + let mut z = Complex::new(1.0, 0.0); + let mut zn_next; + let epsilon = 1e-6; + let mut i = 0; + + loop { + zn_next = z - (self.fz(z) / self.dfz(z)) + c; + if (zn_next - z).arg_sq() < epsilon || i >= max_iterations { + break; + } + z = zn_next; + i += 1; + } + + return (0.0, i as f64); + } +} diff --git a/shared/src/models/fragments/fragment_task.rs b/shared/src/models/fragments/fragment_task.rs index 00ab920..3c9adeb 100644 --- a/shared/src/models/fragments/fragment_task.rs +++ b/shared/src/models/fragments/fragment_task.rs @@ -111,9 +111,15 @@ impl FragmentTask { } FractalDescriptor::NewtonRaphsonZ3(newton_raphson_3) => { newton_raphson_3.generate(self.max_iteration, x, y) - } + }, FractalDescriptor::NewtonRaphsonZ4(newton_raphson_4) => { newton_raphson_4.generate(self.max_iteration, x, y) + }, + FractalDescriptor::NovaNewtonRapshonZ3(nova_newton_raphson) => { + nova_newton_raphson.generate(self.max_iteration, x, y) + } + FractalDescriptor::NovaNewtonRapshonZ4(nova_newton_raphson) => { + nova_newton_raphson.generate(self.max_iteration, x, y) } } } diff --git a/shared/src/networking/server.rs b/shared/src/networking/server.rs index 3eef086..4f8c4d4 100644 --- a/shared/src/networking/server.rs +++ b/shared/src/networking/server.rs @@ -10,7 +10,7 @@ use crate::{ models::{ fractal::{ fractal_descriptor::FractalDescriptor, iterated_sin_z::IteratedSinZ, julia::Julia, - mandelbrot::Mandelbrot, newton_raphson_3::NewtonRaphsonZ3, newton_raphson_4::NewtonRaphsonZ4, + mandelbrot::Mandelbrot, newton_raphson_3::NewtonRaphsonZ3, newton_raphson_4::NewtonRaphsonZ4, nova_newton_raphson_z3::NovaNewtonRaphsonZ3, nova_newton_raphson_z4::NovaNewtonRaphsonZ4, }, fragments::fragment_task::FragmentTask, point::Point, @@ -90,6 +90,8 @@ impl Server { })), FractalDescriptor::NewtonRaphsonZ3(NewtonRaphsonZ3::new()), FractalDescriptor::NewtonRaphsonZ4(NewtonRaphsonZ4::new()), + FractalDescriptor::NovaNewtonRapshonZ3(NovaNewtonRaphsonZ3::new()), + FractalDescriptor::NovaNewtonRapshonZ4(NovaNewtonRaphsonZ4::new()), ]; Self {