Skip to content

Commit 58e39c9

Browse files
authored
blas/gonum: rewrite drotmg using Hopkins' implementation
Also fix blas.Flag documentation and explicitly state values.
1 parent 5e47fe3 commit 58e39c9

4 files changed

Lines changed: 319 additions & 199 deletions

File tree

‎blas/blas.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ package blas
1010
type Flag int
1111

1212
const (
13-
Identity Flag = iota - 2 // H is the identity matrix; no rotation is needed.
14-
Rescaling // H specifies rescaling.
15-
OffDiagonal // Off-diagonal elements of H are units.
16-
Diagonal // Diagonal elements of H are units.
13+
Identity Flag = -2 // H is the identity matrix; no rotation is needed.
14+
Rescaling Flag = -1 // H specifies rescaling.
15+
OffDiagonal Flag = 0 // Off-diagonal elements of H are non-unit.
16+
Diagonal Flag = 1 // Diagonal elements of H are non-unit.
1717
)
1818

1919
// SrotmParams contains Givens transformation parameters returned

‎blas/gonum/level1double.go‎

Lines changed: 83 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -326,122 +326,108 @@ func (Implementation) Drotg(a, b float64) (c, s, r, z float64) {
326326
// http://www.netlib.org/lapack/explore-html/df/deb/drotmg_8f.html
327327
// for more details.
328328
func (Implementation) Drotmg(d1, d2, x1, y1 float64) (p blas.DrotmParams, rd1, rd2, rx1 float64) {
329-
var p1, p2, q1, q2, u float64
329+
// The implementation of Drotmg used here is taken from Hopkins 1997
330+
// Appendix A: https://doi.org/10.1145/289251.289253
331+
// with the exception of the gam constants below.
330332

331333
const (
332334
gam = 4096.0
333-
gamsq = 16777216.0
334-
rgamsq = 5.9604645e-8
335+
gamsq = gam * gam
336+
rgamsq = 1.0 / gamsq
335337
)
336338

337339
if d1 < 0 {
338-
p.Flag = blas.Rescaling
339-
return
340+
p.Flag = blas.Rescaling // Error state.
341+
return p, 0, 0, 0
340342
}
341343

342-
p2 = d2 * y1
343-
if p2 == 0 {
344+
var h11, h12, h21, h22 float64
345+
if d2 == 0 || y1 == 0 {
344346
p.Flag = blas.Identity
345-
rd1 = d1
346-
rd2 = d2
347-
rx1 = x1
348-
return
347+
return p, d1, d2, x1
348+
} else if (d1 == 0 || x1 == 0) && d2 > 0 {
349+
p.Flag = blas.Diagonal
350+
h12 = 1
351+
h21 = -1
352+
x1 = y1
353+
d1, d2 = d2, d1
354+
} else {
355+
p2 := d2 * y1
356+
p1 := d1 * x1
357+
q2 := p2 * y1
358+
q1 := p1 * x1
359+
if math.Abs(q1) > math.Abs(q2) {
360+
p.Flag = blas.OffDiagonal
361+
h11 = 1
362+
h22 = 1
363+
h21 = -y1 / x1
364+
h12 = p2 / p1
365+
u := 1 - h12*h21
366+
if u <= 0 {
367+
p.Flag = blas.Rescaling // Error state.
368+
return p, 0, 0, 0
369+
} else {
370+
d1 /= u
371+
d2 /= u
372+
x1 *= u
373+
}
374+
} else {
375+
if q2 < 0 {
376+
p.Flag = blas.Rescaling // Error state.
377+
return p, 0, 0, 0
378+
} else {
379+
p.Flag = blas.Diagonal
380+
h21 = -1
381+
h12 = 1
382+
h11 = p1 / p2
383+
h22 = x1 / y1
384+
u := 1 + h11*h22
385+
d1, d2 = d2/u, d1/u
386+
x1 = y1 * u
387+
}
388+
}
349389
}
350-
p1 = d1 * x1
351-
q2 = p2 * y1
352-
q1 = p1 * x1
353390

354-
absQ1 := math.Abs(q1)
355-
absQ2 := math.Abs(q2)
356-
357-
if absQ1 < absQ2 && q2 < 0 {
391+
for d1 <= rgamsq && d1 != 0 {
358392
p.Flag = blas.Rescaling
359-
return
393+
d1 = (d1 * gam) * gam
394+
x1 /= gam
395+
h11 /= gam
396+
h12 /= gam
360397
}
361-
362-
if d1 == 0 {
363-
p.Flag = blas.Diagonal
364-
p.H[0] = p1 / p2
365-
p.H[3] = x1 / y1
366-
u = 1 + p.H[0]*p.H[3]
367-
rd1, rd2 = d2/u, d1/u
368-
rx1 = y1 / u
369-
return
398+
for d1 > gamsq {
399+
p.Flag = blas.Rescaling
400+
d1 = (d1 / gam) / gam
401+
x1 *= gam
402+
h11 *= gam
403+
h12 *= gam
370404
}
371405

372-
// Now we know that d1 != 0, and d2 != 0. If d2 == 0, it would be caught
373-
// when p2 == 0, and if d1 == 0, then it is caught above
374-
375-
if absQ1 > absQ2 {
376-
p.H[1] = -y1 / x1
377-
p.H[2] = p2 / p1
378-
u = 1 - p.H[2]*p.H[1]
379-
rd1 = d1
380-
rd2 = d2
381-
rx1 = x1
382-
p.Flag = blas.OffDiagonal
383-
// u must be greater than zero because |q1| > |q2|, so check from netlib
384-
// is unnecessary
385-
// This is left in for ease of comparison with complex routines
386-
//if u > 0 {
387-
rd1 /= u
388-
rd2 /= u
389-
rx1 *= u
390-
//}
391-
} else {
392-
p.Flag = blas.Diagonal
393-
p.H[0] = p1 / p2
394-
p.H[3] = x1 / y1
395-
u = 1 + p.H[0]*p.H[3]
396-
rd1 = d2 / u
397-
rd2 = d1 / u
398-
rx1 = y1 * u
406+
for math.Abs(d2) <= rgamsq && d2 != 0 {
407+
p.Flag = blas.Rescaling
408+
d2 = (d2 * gam) * gam
409+
h21 /= gam
410+
h22 /= gam
399411
}
400-
401-
for rd1 <= rgamsq || rd1 >= gamsq {
402-
if p.Flag == blas.OffDiagonal {
403-
p.H[0] = 1
404-
p.H[3] = 1
405-
p.Flag = blas.Rescaling
406-
} else if p.Flag == blas.Diagonal {
407-
p.H[1] = -1
408-
p.H[2] = 1
409-
p.Flag = blas.Rescaling
410-
}
411-
if rd1 <= rgamsq {
412-
rd1 *= gam * gam
413-
rx1 /= gam
414-
p.H[0] /= gam
415-
p.H[2] /= gam
416-
} else {
417-
rd1 /= gam * gam
418-
rx1 *= gam
419-
p.H[0] *= gam
420-
p.H[2] *= gam
421-
}
412+
for math.Abs(d2) > gamsq {
413+
p.Flag = blas.Rescaling
414+
d2 = (d2 / gam) / gam
415+
h21 *= gam
416+
h22 *= gam
422417
}
423418

424-
for math.Abs(rd2) <= rgamsq || math.Abs(rd2) >= gamsq {
425-
if p.Flag == blas.OffDiagonal {
426-
p.H[0] = 1
427-
p.H[3] = 1
428-
p.Flag = blas.Rescaling
429-
} else if p.Flag == blas.Diagonal {
430-
p.H[1] = -1
431-
p.H[2] = 1
432-
p.Flag = blas.Rescaling
433-
}
434-
if math.Abs(rd2) <= rgamsq {
435-
rd2 *= gam * gam
436-
p.H[1] /= gam
437-
p.H[3] /= gam
438-
} else {
439-
rd2 /= gam * gam
440-
p.H[1] *= gam
441-
p.H[3] *= gam
442-
}
419+
switch p.Flag {
420+
case blas.Diagonal:
421+
p.H = [4]float64{0: h11, 3: h22}
422+
case blas.OffDiagonal:
423+
p.H = [4]float64{1: h21, 2: h12}
424+
case blas.Rescaling:
425+
p.H = [4]float64{h11, h21, h12, h22}
426+
default:
427+
panic("blas: unexpected blas.Flag")
443428
}
444-
return
429+
430+
return p, d1, d2, x1
445431
}
446432

447433
// Drot applies a plane transformation.

0 commit comments

Comments
 (0)