Расчет фрактала на Go, Rust, Python

This commit is contained in:
Viner Abubakirov
2025-09-14 21:46:27 +05:00
parent 5cc096ee89
commit 8eccad2523
3 changed files with 309 additions and 0 deletions

52
main.go Normal file
View File

@@ -0,0 +1,52 @@
package main
import (
"flag"
"fmt"
"math/cmplx"
"strconv"
)
func main() {
size := flag.Int("size", 128, "размер")
depth := flag.Int("depth", 8, "глубина")
g2 := flag.Float64("g2", 0.0, "вещественная часть для g2")
g3 := flag.Float64("g3", 1.0, "вещественная часть для g3")
flag.Parse()
// Преобразуем в complex128
cg2 := complex(*g2, 0)
cg3 := complex(*g3, 0)
code := fractal(*size, *depth, cg2, cg3)
fmt.Println(code)
}
func fractal(size int, depth int, g2 complex128, g3 complex128) string {
var zy, zx float64
var z complex128
var yf float64 = 2
var xf float64 = 2
var yi float64 = -2
var xi float64 = -2
var code string
for y := 0; y < size; y++ {
zy = float64(y)*(yf-yi)/float64(size-1) + yi
for x := 0; x < size; x++ {
zx = float64(x)*(xf-xi)/float64(size-1) + xi
z = complex(zx, zy)
for i := 0; i < depth; i++ {
if cmplx.Abs(z) > 2 {
code += strconv.FormatInt(int64(i), 10)
break
}
z = (cmplx.Pow(z, 4) + g2*cmplx.Pow(z, 2)/2 + 2*g3*z + cmplx.Pow(g2, 2)/16) / (4*cmplx.Pow(z, 3) - g2*z - g3)
}
}
}
return code
}

38
main.py Normal file
View File

@@ -0,0 +1,38 @@
import argparse
def fractal(size: int, depth: int, g2: float, g3: float):
xi = yi = -2
xf = yf = 2
code = ""
for y in range(size):
zy = y * (yf - yi) / (size - 1) + yi
for x in range(size):
zx = x * (xf - xi) / (size - 1) + xi
z = zx + zy * 1j
for i in range(depth):
if abs(z) > 2:
code += str(i)
break
z = (z**4 + 0.5 * g2 * z * z + 2 * g3 * z + 1 / 16 * g2**2) / (
4 * z**3 - g2 * z - g3
)
return code
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--size", "-s", type=int, default=128, required=False)
parser.add_argument("--depth", "-d", type=int, default=8, required=False)
parser.add_argument("-g2", type=float, default=0.0, required=False)
parser.add_argument("-g3", type=float, default=1.0, required=False)
args, _ = parser.parse_known_args()
code = fractal(args.size, args.depth, args.g2, args.g3)
print(code)
if __name__ == "__main__":
main()

219
main.rs Normal file
View File

@@ -0,0 +1,219 @@
use std::env;
use std::ops::{Add, Div, Mul, Sub};
#[derive(Debug, Clone, Copy)]
struct Complex {
re: f64,
im: f64,
}
impl Complex {
fn new(re: f64, im: f64) -> Self {
Complex { re, im }
}
fn abs(&self) -> f64 {
(self.re * self.re + self.im * self.im).sqrt()
}
fn powi(&self, mut n: i32) -> Self {
if n == 0 {
return Complex::new(1.0, 0.0);
}
if n < 0 {
return Complex::new(1.0, 0.0) / self.powi(-n);
}
let mut base = *self;
let mut result = Complex::new(1.0, 0.0);
while n > 0 {
if n % 2 == 1 {
result = result * base;
}
base = base * base;
n /= 2;
}
result
}
}
// === Complex to Complex ===
impl Add for Complex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Complex::new(self.re + rhs.re, self.im + rhs.im)
}
}
impl Sub for Complex {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Complex::new(self.re - rhs.re, self.im - rhs.im)
}
}
impl Mul for Complex {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Complex::new(
self.re * rhs.re - self.im * rhs.im,
self.re * rhs.im + self.im * rhs.re,
)
}
}
impl Div for Complex {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
let denom = rhs.re * rhs.re + rhs.im * rhs.im;
Complex::new(
(self.re * rhs.re + self.im * rhs.im) / denom,
(self.im * rhs.re - self.re * rhs.im) / denom,
)
}
}
// === Complex to f64 ===
impl Add<f64> for Complex {
type Output = Self;
fn add(self, rhs: f64) -> Self::Output {
Complex::new(self.re + rhs, self.im)
}
}
impl Sub<f64> for Complex {
type Output = Self;
fn sub(self, rhs: f64) -> Self::Output {
Complex::new(self.re - rhs, self.im)
}
}
impl Mul<f64> for Complex {
type Output = Self;
fn mul(self, rhs: f64) -> Self::Output {
Complex::new(self.re * rhs, self.im * rhs)
}
}
impl Div<f64> for Complex {
type Output = Self;
fn div(self, rhs: f64) -> Self::Output {
Complex::new(self.re / rhs, self.im / rhs)
}
}
// === f64 to Complex ===
impl Add<Complex> for f64 {
type Output = Complex;
fn add(self, rhs: Complex) -> Complex {
Complex::new(self + rhs.re, rhs.im)
}
}
impl Sub<Complex> for f64 {
type Output = Complex;
fn sub(self, rhs: Complex) -> Complex {
Complex::new(self - rhs.re, rhs.im)
}
}
impl Mul<Complex> for f64 {
type Output = Complex;
fn mul(self, rhs: Complex) -> Complex {
Complex::new(self * rhs.re, self * rhs.im)
}
}
impl Div<Complex> for f64 {
type Output = Complex;
fn div(self, rhs: Complex) -> Complex {
Complex::new(self, 0.0) / rhs
}
}
#[derive(Debug)]
struct Args {
size: u8,
depth: u8,
g2: Complex,
g3: Complex,
}
fn parse_args() -> Args {
let mut args_struct = Args {
size: 128,
depth: 8,
g2: Complex::new(0.0, 0.0),
g3: Complex::new(1.0, 0.0),
};
let args: Vec<String> = env::args().skip(1).collect();
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--size" | "-s" => {
if i + 1 < args.len() {
args_struct.size = args[i + 1].parse().unwrap_or(args_struct.size);
i += 1;
}
}
"--depth" | "-d" => {
if i + 1 < args.len() {
args_struct.depth = args[i + 1].parse().unwrap_or(args_struct.depth);
i += 1;
}
}
"-g2" => {
if i + 1 < args.len() {
let val: f64 = args[i + 1].parse().unwrap_or(args_struct.g2.re);
args_struct.g2 = Complex::new(val, 0.0);
i += 1;
}
}
"-g3" => {
if i + 1 < args.len() {
let val: f64 = args[i + 1].parse().unwrap_or(args_struct.g3.re);
args_struct.g3 = Complex::new(val, 0.0);
i += 1;
}
}
_ => {}
}
i += 1;
}
args_struct
}
fn main() {
let args = parse_args();
println!("{}", fractal(args.size, args.depth, args.g2, args.g3));
}
fn fractal(size: u8, depth: u8, g2: Complex, g3: Complex) -> String {
let xi: f64 = -2.0;
let yi: f64 = -2.0;
let xf: f64 = 2.0;
let yf: f64 = 2.0;
let mut code = String::new();
for y in 0..=size - 1 {
let zy = y as f64 * (yf - yi) / (size as f64 - 1.0) + yi;
for x in 0..=size - 1 {
let zx = x as f64 * (xf - xi) / (size as f64 - 1.0) + xi;
let mut z = Complex::new(zx, zy);
for i in 0..=depth - 1 {
if z.abs() > 2.0 {
code.push_str(&i.to_string());
break;
}
z = (z.powi(4) + 0.5 * g2 * z.powi(2) + 2.0 * g3 * z + 1.0 / 16.0 * g2.powi(2))
/ (4.0 * z.powi(3) - g2 * z - g3)
}
}
}
code
}