|
| 1 | +/- |
| 2 | +Copyright (c) 2021 Lu-Ming Zhang. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Lu-Ming Zhang |
| 5 | +
|
| 6 | +! This file was ported from Lean 3 source module data.matrix.hadamard |
| 7 | +! leanprover-community/mathlib commit 3e068ece210655b7b9a9477c3aff38a492400aa1 |
| 8 | +! Please do not edit these lines, except to modify the commit id |
| 9 | +! if you have ported upstream changes. |
| 10 | +-/ |
| 11 | +import Mathlib.LinearAlgebra.Matrix.Trace |
| 12 | + |
| 13 | +/-! |
| 14 | +# Hadamard product of matrices |
| 15 | +
|
| 16 | +This file defines the Hadamard product `Matrix.hadamard` |
| 17 | +and contains basic properties about them. |
| 18 | +
|
| 19 | +## Main definition |
| 20 | +
|
| 21 | +- `Matrix.hadamard`: defines the Hadamard product, |
| 22 | + which is the pointwise product of two matrices of the same size. |
| 23 | +
|
| 24 | +## Notation |
| 25 | +
|
| 26 | +* `⊙`: the Hadamard product `Matrix.hadamard`; |
| 27 | +
|
| 28 | +## References |
| 29 | +
|
| 30 | +* <https://en.wikipedia.org/wiki/hadamard_product_(matrices)> |
| 31 | +
|
| 32 | +## Tags |
| 33 | +
|
| 34 | +hadamard product, hadamard |
| 35 | +-/ |
| 36 | + |
| 37 | + |
| 38 | +variable {α β γ m n : Type _} |
| 39 | + |
| 40 | +variable {R : Type _} |
| 41 | + |
| 42 | +namespace Matrix |
| 43 | + |
| 44 | +open Matrix BigOperators |
| 45 | + |
| 46 | +/-- `Matrix.hadamard` defines the Hadamard product, |
| 47 | + which is the pointwise product of two matrices of the same size.-/ |
| 48 | +def hadamard [Mul α] (A : Matrix m n α) (B : Matrix m n α) : Matrix m n α := |
| 49 | + of fun i j => A i j * B i j |
| 50 | +#align matrix.hadamard Matrix.hadamard |
| 51 | + |
| 52 | +-- TODO: set as an equation lemma for `hadamard`, see mathlib4#3024 |
| 53 | +@[simp] |
| 54 | +theorem hadamard_apply [Mul α] (A : Matrix m n α) (B : Matrix m n α) (i j) : |
| 55 | + hadamard A B i j = A i j * B i j := |
| 56 | + rfl |
| 57 | +#align matrix.hadamard_apply Matrix.hadamard_apply |
| 58 | + |
| 59 | +-- mathport name: matrix.hadamard |
| 60 | +scoped infixl:100 " ⊙ " => Matrix.hadamard |
| 61 | + |
| 62 | +section BasicProperties |
| 63 | + |
| 64 | +variable (A : Matrix m n α) (B : Matrix m n α) (C : Matrix m n α) |
| 65 | + |
| 66 | +-- commutativity |
| 67 | +theorem hadamard_comm [CommSemigroup α] : A ⊙ B = B ⊙ A := |
| 68 | + ext fun _ _ => mul_comm _ _ |
| 69 | +#align matrix.hadamard_comm Matrix.hadamard_comm |
| 70 | + |
| 71 | +-- associativity |
| 72 | +theorem hadamard_assoc [Semigroup α] : A ⊙ B ⊙ C = A ⊙ (B ⊙ C) := |
| 73 | + ext fun _ _ => mul_assoc _ _ _ |
| 74 | +#align matrix.hadamard_assoc Matrix.hadamard_assoc |
| 75 | + |
| 76 | +-- distributivity |
| 77 | +theorem hadamard_add [Distrib α] : A ⊙ (B + C) = A ⊙ B + A ⊙ C := |
| 78 | + ext fun _ _ => left_distrib _ _ _ |
| 79 | +#align matrix.hadamard_add Matrix.hadamard_add |
| 80 | + |
| 81 | +theorem add_hadamard [Distrib α] : (B + C) ⊙ A = B ⊙ A + C ⊙ A := |
| 82 | + ext fun _ _ => right_distrib _ _ _ |
| 83 | +#align matrix.add_hadamard Matrix.add_hadamard |
| 84 | + |
| 85 | +-- scalar multiplication |
| 86 | +section Scalar |
| 87 | + |
| 88 | +@[simp] |
| 89 | +theorem smul_hadamard [Mul α] [SMul R α] [IsScalarTower R α α] (k : R) : (k • A) ⊙ B = k • A ⊙ B := |
| 90 | + ext fun _ _ => smul_mul_assoc _ _ _ |
| 91 | +#align matrix.smul_hadamard Matrix.smul_hadamard |
| 92 | + |
| 93 | +@[simp] |
| 94 | +theorem hadamard_smul [Mul α] [SMul R α] [SMulCommClass R α α] (k : R) : A ⊙ (k • B) = k • A ⊙ B := |
| 95 | + ext fun _ _ => mul_smul_comm _ _ _ |
| 96 | +#align matrix.hadamard_smul Matrix.hadamard_smul |
| 97 | + |
| 98 | +end Scalar |
| 99 | + |
| 100 | +section Zero |
| 101 | + |
| 102 | +variable [MulZeroClass α] |
| 103 | + |
| 104 | +@[simp] |
| 105 | +theorem hadamard_zero : A ⊙ (0 : Matrix m n α) = 0 := |
| 106 | + ext fun _ _ => MulZeroClass.mul_zero _ |
| 107 | +#align matrix.hadamard_zero Matrix.hadamard_zero |
| 108 | + |
| 109 | +@[simp] |
| 110 | +theorem zero_hadamard : (0 : Matrix m n α) ⊙ A = 0 := |
| 111 | + ext fun _ _ => MulZeroClass.zero_mul _ |
| 112 | +#align matrix.zero_hadamard Matrix.zero_hadamard |
| 113 | + |
| 114 | +end Zero |
| 115 | + |
| 116 | +section One |
| 117 | + |
| 118 | +variable [DecidableEq n] [MulZeroOneClass α] |
| 119 | + |
| 120 | +variable (M : Matrix n n α) |
| 121 | + |
| 122 | +theorem hadamard_one : M ⊙ (1 : Matrix n n α) = diagonal fun i => M i i := by |
| 123 | + ext i j |
| 124 | + by_cases h: i = j <;> simp [h] |
| 125 | +#align matrix.hadamard_one Matrix.hadamard_one |
| 126 | + |
| 127 | +theorem one_hadamard : (1 : Matrix n n α) ⊙ M = diagonal fun i => M i i := by |
| 128 | + ext i j |
| 129 | + by_cases h : i = j <;> simp [h] |
| 130 | +#align matrix.one_hadamard Matrix.one_hadamard |
| 131 | + |
| 132 | +end One |
| 133 | + |
| 134 | +section Diagonal |
| 135 | + |
| 136 | +variable [DecidableEq n] [MulZeroClass α] |
| 137 | + |
| 138 | +theorem diagonal_hadamard_diagonal (v : n → α) (w : n → α) : |
| 139 | + diagonal v ⊙ diagonal w = diagonal (v * w) := |
| 140 | + ext fun _ _ => (apply_ite₂ _ _ _ _ _ _).trans (congr_arg _ <| MulZeroClass.zero_mul 0) |
| 141 | +#align matrix.diagonal_hadamard_diagonal Matrix.diagonal_hadamard_diagonal |
| 142 | + |
| 143 | +end Diagonal |
| 144 | + |
| 145 | +section trace |
| 146 | + |
| 147 | +variable [Fintype m] [Fintype n] |
| 148 | + |
| 149 | +variable (R) [Semiring α] [Semiring R] [Module R α] |
| 150 | + |
| 151 | +theorem sum_hadamard_eq : (∑ i : m, ∑ j : n, (A ⊙ B) i j) = trace (A ⬝ Bᵀ) := |
| 152 | + rfl |
| 153 | +#align matrix.sum_hadamard_eq Matrix.sum_hadamard_eq |
| 154 | + |
| 155 | +theorem dotProduct_vecMul_hadamard [DecidableEq m] [DecidableEq n] (v : m → α) (w : n → α) : |
| 156 | + dotProduct (vecMul v (A ⊙ B)) w = trace (diagonal v ⬝ A ⬝ (B ⬝ diagonal w)ᵀ) := by |
| 157 | + rw [← sum_hadamard_eq, Finset.sum_comm] |
| 158 | + simp [dotProduct, vecMul, Finset.sum_mul, mul_assoc] |
| 159 | +#align matrix.dot_product_vec_mul_hadamard Matrix.dotProduct_vecMul_hadamard |
| 160 | + |
| 161 | +end trace |
| 162 | + |
| 163 | +end BasicProperties |
| 164 | + |
| 165 | +end Matrix |
0 commit comments