From c2ed416436139fb7c0ef821c335cc21535eda930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Wed, 31 Dec 2025 15:44:40 +0800 Subject: [PATCH] =?UTF-8?q?```=20feat(matrix):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=90=91=E9=87=8F=E5=88=9B=E5=BB=BA=E5=92=8C=E7=9F=A9=E9=98=B5?= =?UTF-8?q?=E7=9B=B8=E7=AD=89=E6=AF=94=E8=BE=83=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增NewVector函数用于创建向量,新增Equal方法用于比较两个矩阵是否相等。 ``` --- matrix.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/matrix.go b/matrix.go index 4c28431..34466c5 100644 --- a/matrix.go +++ b/matrix.go @@ -84,6 +84,8 @@ func NewOnes(shape []int) (*Matrix, error) { } return NewMatrix(data, shape) } + +// NewVector 创建一个向量 func NewVector(data []float64) (*Matrix, error) { 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维矩阵) func (m *Matrix) Transpose() (*Matrix, error) { if m.mdim != 2 {