-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(Data/Matrix/Mul): add diagonal and transpose lemmas for vector operations #34851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…perations Add three lemmas for matrix-vector operations: - `vecMul_diagonal_dotProduct`: weighted inner product identity - `dotProduct_transpose_mulVec`: bilinear form symmetry - `mul_diagonal_mulVec`: column-weighted sum expansion
PR summary 30e22ac079Import changes for modified filesNo significant changes to the import graph Import changes for all files
|
eric-wieser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not yet convinced by these lemmas; they're pretty trivial consequences of existing lemmas, and without choosing a preferred side of Matrix.dotProduct_mulVec, it's not clear to me whether we should write the dot product or matrix product on the left.
| /-- The dot product of `x ᵥ* diagonal d` with `y` equals `∑ i, d i * x i * y i`. -/ | ||
| theorem vecMul_diagonal_dotProduct [Fintype m] [DecidableEq m] [NonUnitalCommSemiring R] | ||
| (d x y : m → R) : x ᵥ* diagonal d ⬝ᵥ y = ∑ i, d i * x i * y i := by | ||
| simp only [dotProduct, vecMul_diagonal, mul_comm (x _) (d _), mul_assoc] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect
| /-- The dot product of `x ᵥ* diagonal d` with `y` equals `∑ i, d i * x i * y i`. -/ | |
| theorem vecMul_diagonal_dotProduct [Fintype m] [DecidableEq m] [NonUnitalCommSemiring R] | |
| (d x y : m → R) : x ᵥ* diagonal d ⬝ᵥ y = ∑ i, d i * x i * y i := by | |
| simp only [dotProduct, vecMul_diagonal, mul_comm (x _) (d _), mul_assoc] | |
| theorem vecMul_diagonal_dotProduct [Fintype m] [DecidableEq m] [NonUnitalSemiring R] | |
| (d x y : m → R) : x ᵥ* diagonal d ⬝ᵥ y = ∑ i, x i * d i * y i := by | |
| simp only [dotProduct, vecMul_diagonal, mul_assoc] |
which holds more generally; or even
| /-- The dot product of `x ᵥ* diagonal d` with `y` equals `∑ i, d i * x i * y i`. -/ | |
| theorem vecMul_diagonal_dotProduct [Fintype m] [DecidableEq m] [NonUnitalCommSemiring R] | |
| (d x y : m → R) : x ᵥ* diagonal d ⬝ᵥ y = ∑ i, d i * x i * y i := by | |
| simp only [dotProduct, vecMul_diagonal, mul_comm (x _) (d _), mul_assoc] | |
| theorem vecMul_diagonal_dotProduct [Fintype m] [DecidableEq m] | |
| (d x y : m → α) : x ᵥ* diagonal d ⬝ᵥ y = ∑ i, (x i * d i) * y i := by | |
| simp only [dotProduct, vecMul_diagonal, mul_assoc] |
|
I think these lemmas should just be inlined into whatever you're trying to prove. Like Eric said, they're pretty trivial from previous lemmas. Maybe |
| /-- Bilinear form identity: `x ⬝ᵥ Aᵀ *ᵥ y = y ⬝ᵥ A *ᵥ x` for commutative semirings. -/ | ||
| theorem dotProduct_transpose_mulVec [Fintype m] (A : Matrix m m α) (x y : m → α) : | ||
| x ⬝ᵥ Aᵀ *ᵥ y = y ⬝ᵥ A *ᵥ x := by | ||
| rw [dotProduct_mulVec, dotProduct_comm, vecMul_transpose] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @themathqueen says, this lemmas is probably the most useful and worth keeping.
Since right now I don't think we declare a preferred spelling, I'd suggest additionally adding the statement that (x ᵥ* Aᵀ) ⬝ᵥ y = (y *ᵥ A) ⬝ᵥ x.
This adds three lemmas for matrix-vector operations that serve as foundational support for future ML formalization:
vecMul_diagonal_dotProduct: weighted inner productx ᵥ* diagonal d ⬝ᵥ y = ∑ i, d i * x i * y idotProduct_transpose_mulVec: bilinear form symmetryx ⬝ᵥ Aᵀ *ᵥ y = y ⬝ᵥ A *ᵥ xmul_diagonal_mulVec: column-weighted sum(A * diagonal d) *ᵥ x = ∑ i, (d i * x i) • A.col iThese are basic linear algebra identities involving diagonal matrices and vector operations that appear frequently in machine learning contexts (weighted inner products, attention mechanisms, feature scaling, diagonal preconditioning).