Skip to content

Commit

Permalink
🎨 NovaNewtonRaphson Z3,Z4
Browse files Browse the repository at this point in the history
  • Loading branch information
adia-dev committed Feb 20, 2024
1 parent 38796e2 commit afbbb87
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 2 deletions.
3 changes: 3 additions & 0 deletions shared/src/models/fractal/fractal_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -11,4 +12,6 @@ pub enum FractalDescriptor {
IteratedSinZ(IteratedSinZ),
NewtonRaphsonZ3(NewtonRaphsonZ3),
NewtonRaphsonZ4(NewtonRaphsonZ4),
NovaNewtonRapshonZ3(NovaNewtonRaphsonZ3),
NovaNewtonRapshonZ4(NovaNewtonRaphsonZ4),
}
2 changes: 2 additions & 0 deletions shared/src/models/fractal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
42 changes: 42 additions & 0 deletions shared/src/models/fractal/nova_newton_raphson_z3.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
42 changes: 42 additions & 0 deletions shared/src/models/fractal/nova_newton_raphson_z4.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
8 changes: 7 additions & 1 deletion shared/src/models/fragments/fragment_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion shared/src/networking/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -90,6 +90,8 @@ impl Server {
})),
FractalDescriptor::NewtonRaphsonZ3(NewtonRaphsonZ3::new()),
FractalDescriptor::NewtonRaphsonZ4(NewtonRaphsonZ4::new()),
FractalDescriptor::NovaNewtonRapshonZ3(NovaNewtonRaphsonZ3::new()),
FractalDescriptor::NovaNewtonRapshonZ4(NovaNewtonRaphsonZ4::new()),
];

Self {
Expand Down

0 comments on commit afbbb87

Please sign in to comment.