第一讲的代码在哪里可以找到?

老师您好,在您的课程视频里面有提到第一讲slides里的代码之后会放出来着,想问一下是在哪里可以找到呢

目前是没有给出,我感觉可以直接照着敲一遍。

如果需要我去提个 pr.

我只敲了能运行的示例,忽略了代码片段

p11-Playing_with_tensors.py

Summary
import taichi as ti

ti.init()

a = ti.var(dt=ti.f32, shape=(42, 63)) # A tensor of 42x63 scalars
b = ti.Vector(3, dt=ti.f32, shape=4)  # A tensor of  4x3D vectors
C = ti.Matrix(2, 2, dt=ti.f32, shape=(3, 5)) # A tensor of 3x52x2 matrices
loss = ti.var(dt=ti.f32, shape=()) # A (0−D) tensor of a single scalar

a[3, 4] = 1
print('a[3,4]=', a[3, 4])
# "a[3,4]= 1.0"

b[2] = [6, 7, 8]
print('b[0]=', b[0][0], b[0][1], b[0][2])
# "b[0]= 0.0 0.0 0.0"
# print(b[0]) is not yet supported.

loss[None] = 3
print(loss[None])
# "3.0"

p13-Kernels_examples.py

Summary
import taichi as ti

ti.init(arch=ti.cpu)

@ti.kernel
def hello(i: ti.i32):
    a = 40
    print('Hello world!', a + i)

hello(2)
# "Hello world! 42"


@ti.kernel
def calc() -> ti.i32:
    s = 0
    for i in range(10):
        s += i
    return s

print(calc())
# "45"

p20-Struct-for_loops.py

Summary
import taichi as ti

ti.init(arch=ti.gpu)

n = 320
pixels = ti.var(dt=ti.f32, shape=(n * 2, n))

@ti.kernel
def paint(t: ti.f32):
    for i, j in pixels:
        pixels[i, j] = i * 0.001 + j * 0.002 + t

paint(0.3)
print(pixels.to_numpy())
# [[0.3        0.30200002 0.30400002 ... 0.934      0.93600005 0.938     ]
#  [0.301      0.303      0.305      ... 0.935      0.93700004 0.939     ]
#  [0.30200002 0.30400002 0.30600002 ... 0.93600005 0.938      0.94000006]
#  ...
#  [0.93700004 0.939      0.94100004 ... 1.5710001  1.5730001  1.575     ]
#  [0.938      0.94       0.94200003 ... 1.572      1.574      1.5760001 ]
#  [0.93900007 0.94100004 0.9430001  ... 1.5730001  1.575      1.5770001 ]]

p23-tensors_in_Taichi-scope.py

Summary
import taichi as ti
ti.init()

a = ti.var(dt=ti.f32, shape=(42, 63)) # A tensor of 42x63 scalars
b = ti.Vector(3, dt=ti.f32, shape=4)  # A tensor of  4x3D vectors
C = ti.Matrix(2, 2, dt=ti.f32, shape=(3, 5)) # A tensor of 3x5, 2x2 matrices

@ti.kernel
def foo():
    a[3, 4] = 1
    print('a[3,4] =', a[3, 4])
    # "a[3,4] = 1.000000"

    b[2] = [6, 7, 8]
    print('b[0] =', b[0], ', b[2] =', b[2])
    # "b[0] = [0.000000, 0.000000, 0.000000] , b[2] = [6.000000, 7.000000, 8.000000]"

    C[2, 1][0, 1] = 1
    print('C[2, 1] =', C[2, 1])
    # "C[2, 1] = [[0.000000, 0.000000], [1.000000, 0.000000]]"

foo()

fractal.py 是自带的例子。

通过 ti example 可以

  • 看到例子放在哪里了(win10);
  • ti example fractal 可以运行例子 (mac、linux)

p27-debug.py

Summary
import taichi as ti

ti.init(debug=True, arch=ti.cpu)

a = ti.var(ti.i32, shape=(10))
b = ti.var(ti.i32, shape=(10))

@ti.kernel
def shift():
    for i in range(10):
        a[i] = b[i + 1]  # Runtime error in debug mode

shift()
1 个赞

哇,好的好的,十分感谢分享 :grinning: