# 有关ti.root.pointer 的问题

ti.root.pointer一共被调用了两次：

``````self.grid = ti.root.pointer(indices, [self.N_tot // 4]).dense(indices, 4).place(self.x, self.p, self.Ap)
for l in range(self.n_mg_levels):
self.grid = ti.root.pointer(indices,[self.N_tot // (4 * 2**l)]).dense(indices,4).place(self.r[l], self.z[l])
``````

``````ti.root.dense(indices, self.N_tot // 4).dense(indices, 4).place(self.x, self.p, self.Ap)
for l in range(self.n_mg_levels):
ti.root.dense(indices,self.N_tot // (4 * 2**l)).dense(indices,4).place(self.r[l], self.z[l])
``````

`ti.root.pointer` declares a sparse tensor.
`ti.root.dense` declares a dense tensor.

If `x` is a dense tensor, then:

``````for i in x:
print(i)
``````

will be same as:

``````for i in range(N):
print(i)
``````

Otherwise, if `x` is a sparse tensor, then:

``````for i in x:
print(i)
``````

will be same as:

``````for i in range(N):
if ti.is_active(x, i):
print(i)
``````

active是怎么声明的？

``````self.grid = ti.root.pointer(indices, [4]).dense(indices, 4).place(x)
ti.root.dense(indices,4).dense(indices, 4).place(x)
``````

All indices that has been written is active:

``````x[2] = 233
x[4] = 666
x[5] = 888

for i in x:
print(i)  # 2 4 5
``````

Ok, got it.
Thanks a lot.