Skip to content

Commit 137ef4d

Browse files
author
Tommy TIAN
authored
Merge pull request #2 from txaty/bugfix/hurwitz_prod
Fix Hurwitz integer product issue
2 parents 1d1e4d6 + 780358b commit 137ef4d

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Go Reference](https://pkg.go.dev/badge/github.com/txaty/go-bigcomplex.svg)](https://pkg.go.dev/github.com/txaty/go-bigcomplex)
44
[![Go Report Card](https://goreportcard.com/badge/github.com/txaty/go-bigcomplex)](https://goreportcard.com/report/github.com/txaty/go-bigcomplex)
5-
![Coverage](https://img.shields.io/badge/Coverage-47.4%25-yellow)
5+
![Coverage](https://img.shields.io/badge/Coverage-45.8%25-yellow)
66

77
Big complex number calculation library for Go (with [math/big](https://pkg.go.dev/math/big)).
88

hurwitz_integer.go

+3-16
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,7 @@ func (h *HurwitzInt) Copy() *HurwitzInt {
298298
// the product (a1 + b1j + c1k + d1)(a2 + b2j + c2k + d2) is determined by the products of the
299299
// basis elements and the distributive law
300300
func (h *HurwitzInt) Prod(a, b *HurwitzInt) *HurwitzInt {
301-
if h.dblR == nil {
302-
h.dblR = new(big.Int)
303-
}
304-
r := h.dblR
305-
if h.dblI == nil {
306-
h.dblI = new(big.Int)
307-
}
308-
i := h.dblI
309-
if h.dblJ == nil {
310-
h.dblJ = new(big.Int)
311-
}
312-
j := h.dblJ
313-
if h.dblK == nil {
314-
h.dblK = new(big.Int)
315-
}
316-
k := h.dblK
301+
r, i, j, k := new(big.Int), new(big.Int), new(big.Int), new(big.Int)
317302
opt := iPool.Get().(*big.Int)
318303
defer iPool.Put(opt)
319304
// 1 part
@@ -344,6 +329,8 @@ func (h *HurwitzInt) Prod(a, b *HurwitzInt) *HurwitzInt {
344329
k.Add(k, opt.Mul(a.dblK, b.dblR))
345330
k.Rsh(k, 1)
346331

332+
h.dblR, h.dblI, h.dblJ, h.dblK = r, i, j, k
333+
347334
return h
348335
}
349336

hurwitz_integer_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestHurwitzInt_Prod(t *testing.T) {
2424
want *HurwitzInt
2525
}{
2626
{
27-
name: "test_(1+i+j+k)+(1+i+j+k)",
27+
name: "test_(1+i+j+k) * (1+i+j+k)",
2828
fields: fields{
2929
dblR: nil,
3030
dblI: nil,
@@ -37,6 +37,20 @@ func TestHurwitzInt_Prod(t *testing.T) {
3737
},
3838
want: NewHurwitzInt(big.NewInt(-2), big.NewInt(2), big.NewInt(2), big.NewInt(2), false),
3939
},
40+
{
41+
name: "test_2i * 1",
42+
fields: fields{
43+
dblR: nil,
44+
dblI: nil,
45+
dblJ: nil,
46+
dblK: nil,
47+
},
48+
args: args{
49+
a: NewHurwitzInt(big.NewInt(0), big.NewInt(2), big.NewInt(0), big.NewInt(0), false),
50+
b: NewHurwitzInt(big.NewInt(1), big.NewInt(0), big.NewInt(0), big.NewInt(0), false),
51+
},
52+
want: NewHurwitzInt(big.NewInt(0), big.NewInt(2), big.NewInt(0), big.NewInt(0), false),
53+
},
4054
}
4155
for _, tt := range tests {
4256
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)