I wrote an article that regularly notifies information from the trend repository on github, but through that, I found a repository that seems to be interesting, so I will share it. This time, I would like to introduce taichi. I wonder if this will be a series (laughs). ** Addendum: Made into a series. The table of contents is here. ** **
** Taichi is a programming language designed for high-performance computer graphics. ** It says above at the beginning of the repository, but I don't know if taichi is a programming language. I interpret taichi as a library for computer graphics written in python (laughs). Please let me know if it is different. ** Addendum: Looking at the docs, it says taichi is a domain-specific language (DSL). For DSL, please see here. ** **
To install, just add taichi with pip. Also, since I use gpu, I also need cuda.
pip install taichi
git clone https://github.com/taichi-dev/taichi.git
There is an exmaples folder in the taichi repository, and there are many usage examples in it. This time we will share the code for fractal.py. If you ask a friend who is familiar with mathematics, is it the concept of mathematics called fractal? It seems that you are using. I'll just try this time, so I won't go into it (laughs).
fractal.py
import taichi as ti
ti.init(arch=ti.gpu)
n = 320
pixels = ti.field(dtype=float, shape=(n * 2, n))
@ti.func
def complex_sqr(z):
return ti.Vector([z[0]**2 - z[1]**2, z[1] * z[0] * 2])
@ti.kernel
def paint(t: float):
for i, j in pixels: # Parallized over all pixels
c = ti.Vector([-0.8, ti.cos(t) * 0.2])
z = ti.Vector([i / n - 1, j / n - 0.5]) * 2
iterations = 0
while z.norm() < 20 and iterations < 50:
z = complex_sqr(z) + c
iterations += 1
pixels[i, j] = 1 - iterations * 0.02
gui = ti.GUI("Julia Set", res=(n * 2, n))
for i in range(1000000):
paint(i * 0.03)
gui.set_image(pixels)
gui.show()
When you do this, it looks like this: I don't know what it is, but it's amazing (laughs).
Here are some other use cases.
** An interesting guy with dots connected **
** Something like water ripples **
** Manipulate smoke **
taichi It's very interesting, but I don't have enough knowledge of mathematics and physics used in the program. I will study (laughs).
Recommended Posts