@@ -44,9 +44,9 @@ func Zero(rows, cols int) Matrix {
4444}
4545
4646// Identity returns an identity matrix.
47- func Identity (rows , cols int ) Matrix {
48- m := Zero (rows , cols )
49- for i := range rows {
47+ func Identity (size int ) Matrix {
48+ m := Zero (size , size )
49+ for i := range size {
5050 m .Set (i , i , 1 )
5151 }
5252
@@ -68,6 +68,16 @@ func (m Matrix) Set(i, j int, v complex128) {
6868 m .Data [i * m .Cols + j ] = v
6969}
7070
71+ // AddAt adds a value of matrix at (i,j).
72+ func (m Matrix ) AddAt (i , j int , v complex128 ) {
73+ m .Data [i * m .Cols + j ] += v
74+ }
75+
76+ // MulAt multiplies a value of matrix at (i,j).
77+ func (m Matrix ) MulAt (i , j int , v complex128 ) {
78+ m .Data [i * m .Cols + j ] *= v
79+ }
80+
7181// Seq2 returns a sequence of rows.
7282func (m Matrix ) Seq2 () iter.Seq2 [int , []complex128 ] {
7383 return func (yield func (int , []complex128 ) bool ) {
@@ -169,7 +179,7 @@ func (m Matrix) IsUnitary(eps ...float64) bool {
169179 }
170180
171181 mmd := m .Apply (m .Dagger ())
172- id := Identity (m .Dimension () )
182+ id := Identity (m .Rows )
173183 return mmd .Equals (id , epsilon .E13 (eps ... ))
174184}
175185
@@ -181,13 +191,11 @@ func (m Matrix) Apply(n Matrix) Matrix {
181191
182192 out := Zero (a , p )
183193 for i := range a {
184- for j := range p {
185- var c complex128
186- for k := range b {
187- c = c + n . At (i , k ) * m .At (k , j )
194+ for k := range b {
195+ nik := n . At ( i , k )
196+ for j := range p {
197+ out . AddAt (i , j , nik * m .At (k , j ) )
188198 }
189-
190- out .Set (i , j , c )
191199 }
192200 }
193201
@@ -276,12 +284,12 @@ func (m Matrix) Inverse() Matrix {
276284 p , q := m .Dimension ()
277285 mm := m .Clone ()
278286
279- out := Identity (p , q )
287+ out := Identity (p )
280288 for i := range p {
281289 c := 1 / mm .At (i , i )
282290 for j := range q {
283- mm .Set (i , j , c * mm . At ( i , j ) )
284- out .Set (i , j , c * out . At ( i , j ) )
291+ mm .MulAt (i , j , c )
292+ out .MulAt (i , j , c )
285293 }
286294
287295 for j := range q {
@@ -291,8 +299,8 @@ func (m Matrix) Inverse() Matrix {
291299
292300 c := mm .At (j , i )
293301 for k := range q {
294- mm .Set (j , k , mm . At ( j , k ) - c * mm .At (i , k ))
295- out .Set (j , k , out . At ( j , k ) - c * out .At (i , k ))
302+ mm .AddAt (j , k , - c * mm .At (i , k ))
303+ out .AddAt (j , k , - c * out .At (i , k ))
296304 }
297305 }
298306 }
@@ -306,14 +314,14 @@ func (m Matrix) TensorProduct(n Matrix) Matrix {
306314 a , b := n .Dimension ()
307315 rows , cols := p * a , q * b
308316
309- var idx int
310317 data := make ([]complex128 , rows * cols )
311318 for i := range p {
312- for k := range a {
313- for j := range q {
319+ for j := range q {
320+ mij := m .At (i , j )
321+ for k := range a {
314322 for l := range b {
315- data [ idx ] = m . At ( i , j ) * n . At ( k , l )
316- idx ++
323+ row , col := i * a + k , j * b + l
324+ data [ row * cols + col ] = mij * n . At ( k , l )
317325 }
318326 }
319327 }
0 commit comments