feat(matrix): 添加向量创建和矩阵相等比较功能

新增NewVector函数用于创建向量,新增Equal方法用于比较两个矩阵是否相等。
```
This commit is contained in:
程广 2025-12-31 15:44:40 +08:00
parent 2072e3fcc8
commit c2ed416436
1 changed files with 16 additions and 0 deletions

View File

@ -84,6 +84,8 @@ func NewOnes(shape []int) (*Matrix, error) {
} }
return NewMatrix(data, shape) return NewMatrix(data, shape)
} }
// NewVector 创建一个向量
func NewVector(data []float64) (*Matrix, error) { func NewVector(data []float64) (*Matrix, error) {
return NewMatrix(data, []int{len(data), 1}) return NewMatrix(data, []int{len(data), 1})
} }
@ -239,6 +241,20 @@ func (m *Matrix) Scale(factor float64) *Matrix {
} }
} }
func (m *Matrix) Equal(other *Matrix) bool {
if m.shape[0] != other.shape[0] || m.shape[1] != other.shape[1] {
return false
}
for i := 0; i < m.size; i++ {
if m.data[i] != other.data[i] {
return false
}
}
return true
}
// Transpose 矩阵转置仅支持2维矩阵 // Transpose 矩阵转置仅支持2维矩阵
func (m *Matrix) Transpose() (*Matrix, error) { func (m *Matrix) Transpose() (*Matrix, error) {
if m.mdim != 2 { if m.mdim != 2 {