Mathematica code for fractal in Python script

I would like to translate Mathematica code but I don’t know how to write recursions.


<b>TriplexPow[{x_, y_, z_}, n_] := Module[{r = Sqrt[x^2 +  y^2 + z^2], theta = nArcTan[x, y], phi}, phi = nArcSin[z/r];  r^n{Cos[theta]Cos[phi], Sin[theta]Cos[phi], Sin[phi]}];

TriplexMult[{x1_, y1_, z1_}, {x2_, y2_, z2_}] := Module[{r1 =  Sqrt[x1^2 + y1^2], r2 = Sqrt[x2^2 + y2^2], a}, a = 1 -z1 z2/(r1 r2);  {a(x1 x2 - y1 y2), a(x2 y1 + x1 y2), r2 z1 + r1 z2}];

c = {1.0625, 0.2375, 0.0}; n = 100; norm[x_] := x.x;

Lambdabulb[c_] := Module[{p = c, i = 0}, While[i &lt; 40 &&  norm[p] &lt; 4, p = TriplexMult[c,p-TriplexPow[p, 4]]; i++]; i];

image = Table[z = 1.2; While[z &gt;= 0 && Lambdabulb[{x, y,  z}] &lt; 40, z -= 2.4/n]; z, {y, -1.2, 1.2, 2.4/n}, {x, -1.2, 1.2,  2.4/n}];

ListDensityPlot[image, Mesh -&gt; False, Frame -&gt; False, PlotRange -&gt; {0, 1.2}]</b>

TriplexPow and TriplexMult defines new function. Lambdabulb is a new function defined with previous two.Norm should be x_1x_1+x_2x_2+x_3*x_3.
Module function is defined here https://reference.wolfram.com/language/ref/Module.html
Table is acctually a for function https://reference.wolfram.com/language/ref/Table.html?q=Table

Python script should call single vertex function to draw the surface

I’ve found formula on http://bugman123.com/Hypercomplex/ . Just scroll a way down to Lambdabulb.

I would suggest to you to first learn Python in some more detail, maybe even in the context of scientific computing / PyData. I don’t remotely understand what this Mathematica code is supposed to be doing. There is a sort of mathematical curve plugin for Blender somewhere…

You mean XYZ Function Surface plugin ?
I have it. But don’t know how to define new functions and write a recursion.
Can you give me some starting point where to learn this ?

Module[…] defines new variables and constants. Table is a for loop which calls recursion.

probably the most simply recursion you can do in Blender:

import bpy

ob = bpy.context.object

def parent_recurse(ob):
    if ob.parent:
        parent_recurse(ob.parent)
    else:
        print("root:", ob.name)

But you could also do this one in an iterative fashion, which is much better because it can’t blow up the stack:

import bpy

ob = bpy.context.object
while ob.parent:
    ob = ob.parent
print("root:" ob.name)

Thx . This recursion will be the hardest . I’ve rewritten first function in python for the start.
Module means function with local variables which returns something.
Mathematica represents vector in brackets { , , }. TriplexPow is a function which takes a vector and natural number and returns a vector: ( r^n*cos(theta)cos(phi), r^nsin(theta)cos(phi), r^nsin(phi) )

 
<i><b>import bpy
from bpy import context
from math import sin, cos, radians

def TriplexPow(x,y,z,n):
r = sqrt(x**2 +  y**2 + z**2)
theta = atan2(y, x)
phi = asin(z/r)
return r^n*(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi))</b></i>

That’s just a first function.

Mathematica vectors can be represented with tuples and lists in Python, depending on whether you need to mutate them (tuple is immutable).

TriplexPow: do you know if there’s another (mathematical) name for the operation?

r = sqrt(x2 + y2 + z**2) is simply Vector(…).length in Blender (mathutils module).

I don’t know Python very well. Is the sysntax right? Final result should draw surface with x, y, z coordinates as in XYZ surface plugin

TriplexPow: do you know if there’s another (mathematical) name for the operation?

Lambdabulb is a fractal with complex number formula w=c*(w-w^p) where c,p are constants. Surface plot of this lives in 4D. But, we can imagine complex number w as vector w=x+i*y=(x,y) and then extrude it along z axis in our 3D. So, we’ll get 3D representation of this 4D fractal.
So, we get this complex number w imbedded in 3D as (x,y,z). But, the formula to work we have to define power of w : w^p . Power of some vector in 3D is not defined uniquely so we have to define new function for this. TriplexPow is a function which acctually does that: transforms the vector into it’s “power” :


  <b>(x,y,z) ----&gt; TriplexPow(x,y,z) = (   r^n*cos(theta)*cos(phi),   r^n*sin(theta)*cos(phi),   r^n*sin(phi)  ).
</b>

r,n,theta,phi are defined or calculated before formula returns the this new vector.
I used space to read easily.

TriplexMult
defines multiplication of two vectors (we imagine them as 3D complex numbers):
(x_1,y_1,z_1) and(x_2,y_2,z_2)

r = sqrt(x2 + y2 + z**2) is simply Vector(…).length in Blender (mathutils module).

Yeah, this is vector length.

There’s a ^ left in your return statement, which should be ** (pow operator), unless you want to perform a bitwise exclusive-OR.

If you mean by drawing to make it a mesh in Blender, then you’ll have to sample points from that calculated surface (because it has infinite points). Not sure if the the addon vejn mentioned is able to do that for you, but worth a try.