-
-
Save RandyMcMillan/edc6e11d5c556070a4d4c1d497362998 to your computer and use it in GitHub Desktop.
tri_dragon_fractal.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use num_complex::Complex64; | |
/// Calculates the next iteration of the given complex number z using the formula: | |
/// Z_n+1 = (Z_n^3) / (Z_n^3 + 1) + c | |
/// | |
/// # Arguments | |
/// * `z_n` - The current complex number Z_n. | |
/// * `c` - The complex constant c. | |
/// | |
/// # Returns | |
/// The next complex number Z_n+1. | |
fn iterate_complex_formula(z_n: Complex64, c: Complex64) -> Complex64 { | |
let z_n_cubed = z_n.powi(3); // Calculate Z_n^3 | |
let denominator = z_n_cubed + Complex64::new(1.0, 0.0); // Z_n^3 + 1 | |
// Handle potential division by zero if denominator is very close to zero. | |
// In complex numbers, division by zero is generally undefined. | |
// For practical applications, you might want to return an error, | |
// or a special value like infinity, or just let it panic if that's acceptable. | |
// For this example, we'll just proceed, and if denominator is zero, | |
// the division will result in NaN or +/- Infinity components. | |
if denominator.norm() == 0.0 { | |
eprintln!("Warning: Denominator is zero, division will result in NaNs or infinities."); | |
// You might want to return an error or a specific value here. | |
// For simplicity, we'll let the division proceed, which might yield NaN/Infinity. | |
} | |
let fraction = z_n_cubed / denominator; // (Z_n^3) / (Z_n^3 + 1) | |
fraction + c // Add c | |
} | |
fn main() { | |
// Example usage: | |
let initial_z = Complex64::new(0.5, 0.5); // Z_0 | |
let c_constant = Complex64::new(0.1, 0.0); // The constant c | |
let mut current_z = initial_z; | |
let num_iterations = 10; | |
println!("Starting iteration with Z_0 = {} and c = {}", initial_z, c_constant); | |
for i in 0..num_iterations { | |
let next_z = iterate_complex_formula(current_z, c_constant); | |
println!("Z_{} = {}", i + 1, next_z); | |
current_z = next_z; | |
// You might want to add a check for divergence (e.g., if Z_n gets too large) | |
// or convergence (e.g., if Z_n stops changing significantly). | |
if current_z.norm() > 1e10 { // Example: If magnitude exceeds a large number | |
println!("Diverged quickly at iteration {}", i + 1); | |
break; | |
} | |
} | |
// Example with real numbers (using f64 directly) | |
println!("\n--- Real Number Example ---"); | |
fn iterate_real_formula(z_n: f64, c: f64) -> f64 { | |
let z_n_cubed = z_n.powi(3); | |
let denominator = z_n_cubed + 1.0; | |
if denominator == 0.0 { | |
eprintln!("Warning: Denominator is zero in real number calculation."); | |
// Return a sensible value or handle error. Here, we'll let it be Inf or NaN. | |
} | |
z_n_cubed / denominator + c | |
} | |
let initial_z_real = 0.5; | |
let c_constant_real = 0.1; | |
let mut current_z_real = initial_z_real; | |
println!("Starting iteration with Z_0 = {} and c = {}", initial_z_real, c_constant_real); | |
for i in 0..num_iterations { | |
let next_z_real = iterate_real_formula(current_z_real, c_constant_real); | |
println!("Z_{} = {}", i + 1, next_z_real); | |
current_z_real = next_z_real; | |
if next_z_real.is_nan() || next_z_real.is_infinite() { | |
println!("Result became NaN or Infinity at iteration {}", i + 1); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment