# 作业1

Code

## 问题

``````pixels = ti.Vector.field(3, ti.f32, shape=(n * 2, n))
``````

``````pp = ti.types.vector(3,ti.f32)
pixels =  ti.root.pointer(ti.ij, (n*2, n)).place(p)
``````

``````raise ValueError(f'{arg} cannot be placed')
`ValueError: <taichi.lang.matrix.MatrixType object at 0x0000029978CCBFD0> cannot be placed`
``````

``````pp = ti.field(ti.f32)
pixels =  ti.root.pointer(ti.ijk, (n*2, n, 3)).place(pp)
``````

``````gui.set_image(pixels)
``````

pp = ti.Vector.field(3,ti.f32)
pixels = ti.root.pointer(ti.ij, (n*2, n)).place(pp)

//=================================================================

``````pp = ti.Vector.field(3,ti.f32)
pixels = ti.root.pointer(ti.ij, (n*2, n)).place(pp)
``````

``````pixels[i, j] =ti.Vector([0,0,0])    #clear screen
``````

``````On line 31 of file "Z:\_PROJECT_\Python\taichi_course\example\chaos\logistics.py":
pixels[i, j] =ti.Vector([0,0,0]) #clear screen  清屏
^^^^^^^^^^^^
Traceback (most recent call last):
File "D:\_PRO\_DEVELOPER\Miniconda3\envs\taichienv\lib\site-packages\taichi\lang\ast\ast_transformer_utils.py", line 24, in __call__
return method(ctx, node)
File "D:\_PRO\_DEVELOPER\Miniconda3\envs\taichienv\lib\site-packages\taichi\lang\ast\ast_transformer.py", line 169, in build_Subscript
node.ptr = impl.subscript(node.value.ptr, *node.slice.ptr)
File "D:\_PRO\_DEVELOPER\Miniconda3\envs\taichienv\lib\site-packages\taichi\lang\util.py", line 214, in
wrapped
return func(*args, **kwargs)
File "D:\_PRO\_DEVELOPER\Miniconda3\envs\taichienv\lib\site-packages\taichi\lang\impl.py", line 211, in
subscript
raise IndexError(
IndexError: Field with dim 0 accessed with indices of dim 2
``````

`pixels = ti.Vector.field(3, ti.f32, shape=(n * 3, n)) `

//=================================================================

``````pixels[i, j] =pp(0,0,0)    # clear screen
``````

``````taichi.lang.exception.TaichiCompilationError:
On line 32 of file "Z:\_PROJECT_\Python\taichi_course\example\chaos\logistics.py":
pixels[i, j] =pp(0,0,0) #clear screen  清屏
^^^^^^^^^
Traceback (most recent call last):
File "D:\_PRO\_DEVELOPER\Miniconda3\envs\taichienv\lib\site-packages\taichi\lang\ast\ast_transformer_utils.py", line 24, in __call__
return method(ctx, node)
File "D:\_PRO\_DEVELOPER\Miniconda3\envs\taichienv\lib\site-packages\taichi\lang\ast\ast_transformer.py", line 384, in build_Call
node.ptr = func(*args, **keywords)
TypeError: 'MatrixField' object is not callable
``````

//=================================================================
emmmmmm 我现在依旧怀疑是否要从定义上下手 …

pp = ti.Vector.field(3,ti.f32)
pixels = ti.root.pointer(ti.ij, (n*2, n)).place(pp)
pp[i,j]=ti.Vector([0,0,0])

1 Like

Hi @sky92, 我看了下你的代码，在定义稀疏数据结构的时候是不对的。这里我给出了一个例子，里面有5种方式来定义数据结构，你可以参考着来。

``````import taichi as ti
ti.init(arch=ti.cuda)

n = 1024
# 最简单的定义field方式，dense的数据结构
pixels = ti.Vector.field(3, ti.f32, shape=(n,n))

# 比较高级的定义field的方式，也是dense的数据结构
# pixels = ti.Vector.field(3, ti.f32)
# block = ti.root.dense(ti.ij, (n, n)).place(pixels)

#使用pointer的稀疏数据结构
# pixels = ti.Vector.field(3, ti.f32)
# block = ti.root.pointer(ti.ij, (n, n)).place(pixels)

# pixels = ti.Vector.field(3, ti.f32)
# block = ti.root.bitmasked(ti.ij, (n, n)).place(pixels)

#使用复合类型的数据结构
# vec3f = ti.types.vector(3, ti.f32)
# pixels = vec3f.field(shape=(n, n))

@ti.kernel
def paint():
for i, j in ti.ndrange((100,400), (100, 400)):
pixels[i, j] = ti.Vector([1, 0.5, 0])

gui=ti.GUI("Test", res=(n,n))
for i in range(100000):
paint()
gui.set_image(pixels)
gui.show()

``````
2 Likes

`for i, j in pixels:`

``````import taichi as ti

ti.init(arch=ti.cuda)

n = 360

# pixels = ti.Vector.field(3, ti.f32, shape=(n * 2, n))

# pp = ti.field(ti.f32, shape=3)
# pixels =  ti.root.pointer(ti.ij, (n*2, n)).place(pp)

# pp = ti.field(ti.f32)
# pixels =  ti.root.pointer(ti.ijk, (n*2, n, 3)).place(pp)

# 该项定义无用 报错
# pp = ti.Vector.field(ti.f32,shape=3)
# pixels =  ti.root.dense(ti.ij, (n*2, n)).place(pp)
pixels =  ti.Vector.field(3,ti.f32 )
ti.root.dense(ti.ij, (n*2, n)).place(pixels)
#这种定义也是不报错的，但是for i, j in pixels:这种是进不去的，因为稀疏数据初始都是没有激活的
# ti.root.pointer(ti.ij, (n*2, n)).place(pixels)

#该项定义正常
# pixels = ti.Vector.field(3, ti.f32, shape=(n * 3, n))

@ti.kernel
def paint(t: float):
for i, j in pixels:
# 若用稀疏数据结构要这样了
# for i, j in ti.ndrange( 720,  360):
pixels[i, j] = [0, 0, 0]  #clear screen  清屏

r = i * (2.2 / n)

#此处不建议使用ti.cos(t*0.1) 取0-1 会出现后期高频振荡没有图像  不过动画有点好玩
# z = 0.9 * ti.cos(t*0.1)  # interest one
z = 0.9 * ti.cos(ti.random())

# 同一个r尝试j次 获得j个收敛值  由于taichi机制建议不要再添加一个循环
# for xx in range(0,j):
#     iterations = 0
#     while iterations < 100:
#         z = r*z*(1-z)
#         iterations+=1

#     jj = ti.cast(z*500.0 , ti.i32)
#     pixels[i,jj]=[20, 0 ,90]

iterations = 0
while iterations < 10000:
z = r * z * (1 - z)
iterations += 1
jj = ti.cast(z * n * 0.9, ti.i32)
pixels[i, jj] = [20, 0, 90]

gui = ti.GUI("logistic  ", pixels.shape)

for i in range(1000000):
# gui.clear(0x000000)
paint(i * 0.03)
gui.set_image(pixels)
gui.show()
``````
3 Likes

@YuPeng @shihuan

1 Like