Github Interesting Repository ① ~ I found a graphic repository that looks interesting, so I tried it ~

Background

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. ** **

What is taichi

** 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. ** **

I've tried

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: fractal.gif I don't know what it is, but it's amazing (laughs).

Other

Here are some other use cases.

** An interesting guy with dots connected ** mass_spring_game.gif

** Something like water ripples ** waterwave.gif

** Manipulate smoke ** stable_fluid2.gif

Summary

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

Github Interesting Repository ① ~ I found a graphic repository that looks interesting, so I tried it ~
I tried using pipenv, so a memo
I tried to publish my own module so that I can pip install it
I uploaded a module to pypl that deletes Japanese stop words, so share it
A Python beginner made a chat bot, so I tried to summarize how to make it.
I tried a visual regression test on GitHub Pages
AWS Lambda now supports Python so I tried it
When I tried the AtCoder Beginner Contest, it was a terrible result, so I look back
There was a doppelganger, so I tried to distinguish it with artificial intelligence (laughs) (Part 1)
I tried "a program that removes duplicate statements in Python"
I stumbled when I tried to install Basemap, so a memorandum
I tried to make a site that makes it easy to see the update information of Azure
I tried to expand the database so that it can be used with PES analysis software
I tried to create a server environment that runs on Windows 10
I tried running the Python Package Repository (Warehouse) that supports PyPI
A memorandum when I tried to get it automatically with selenium
[Python] I tried to implement stable sorting, so make a note
[Python] A memo that I tried to get started with asyncio
The diagrams were interesting so I tried wrapping them with flask
I tried a tool that imitates Van Gogh's pattern with AI
I made a segment tree with python, so I will introduce it