Kronecker delta 克罗内克函数
### Wiki
[*](https://en.wikipedia.org/wiki/Kronecker_delta)
### Kronecker delta 定义
$$\delta _{{ij}}={\begin{cases}0}i\neq j,\\1}i=j.\end{cases}}$$
### Python3 代码实现
**函数设计**
```python
kronecker_delta_ij = lambda i, j: 1 if i==j else 0
```
**函数使用**
```
kronecker_delta_ij(1, 1) # 结果为 1
kronecker_delta_ij(1, 3) # 结果为 0
```
$\delta_{ij}$**张量定义**
```python
def delta_ij(n):
Delta_ij = []
for i in range(n):
for j in range(n):
Delta_ij.append(kronecker_delta_ij(i, j))
return [Delta_ij[:3], Delta_ij[3:6], Delta_ij[-3:]]
```
example:
```
deltaij = delta_ij(3)
print(deltaij)
结果:
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
## 关于克罗内克符号的补充
### 构造方法1
import numpy as np
n = 3
i, k = np.ogrid[:n, :n]
res = np.zeros((n, n, n, n), int)
res[i, i, k, k] = 1
print(res)
Result:
[[[[1 0 0]
[0 1 0]
[0 0 1]]
[[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]]]
[[[0 0 0]
[0 0 0]
[0 0 0]]
[[1 0 0]
[0 1 0]
[0 0 1]]
[[0 0 0]
[0 0 0]
[0 0 0]]]
[[[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]]
[[1 0 0]
[0 1 0]
[0 0 1]]]]
### 构造方法2
import numpy as np
n = 3
i = np.aragne(3)
res = np.zeros((3,3,3), int)
res[i, i, i] = 1
print(res)
<img width="320" height="500" src="https://img2018.cnblogs.com/blog/1372901/201811/1372901-20181121224458867-535758943.jpg">
## 总结
本次分享的知识虽小,但是五脏皆全。涉及到了lambda表达式,三元表达式,for循环,函数定义,以及numpy库的知识。