[mathjax]
1. 点乘(Dot Product)
代数定义
[latex]mathbf{a} cdot mathbf{b} = sum_{i=1}^n a_i b_i = |mathbf{a}||mathbf{b}|cosθ[/latex]
例如:两个向量 $mathbf{a} = (a_1, a_2, a_3)$ 和 $mathbf{b} = (b_1, b_2, b_3)$ 的点乘计算公式为:
$$
mathbf{a} cdot mathbf{b} = a_1b_1 + a_2b_2 + a_3b_3
$$
该定义表明点乘是各分量乘积之和,结果为标量。
几何定义
点乘的几何意义为:
$$
mathbf{a} cdot mathbf{b} = |mathbf{a}| |mathbf{b}| costheta
$$
其中 $theta$ 为两向量夹角,$|mathbf{a}|$ 表示向量模长
工程应用
- 投影计算:计算向量$mathbf{a}$在$mathbf{b}$方向的投影长度(用于物理引擎的力分解)
- 正交性验证:点乘为零时向量垂直(应用于密码学签名验证)
代码实现
{{< tabs default=”python” >}}
{{< tab lang=”python” >}}
import numpy as np
a = np.array([3, 4])
b = np.array([5, 2])
dot_product = np.dot(a, b)
{{< /tab >}}
{{< tab lang="rust" >}}
```rust
fn dot_product(a: &[f64], b: &[f64]) -> f64 {
a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}
println!("{}", dot_product(&[3.0, 4.0], &[5.0, 2.0]));
{{< /tab >}}
{{< tab lang=”rust with ndarray” >}}
use ndarray::{Array1, array}; // 引入核心类型
fn main() {
// 创建一维数组(向量)
let a: Array1<f64> = array![3.0, 4.0];
let b: Array1<f64> = array![5.0, 2.0];
// 使用内置dot方法
let dot_product = a.dot(&b);
println!("标准点乘结果: {}", dot_product); // 输出23.0
}
{{< /tab >}}
{{< /tabs >}}