github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/blas/gonum/level1float32.go (about)

     1  // Code generated by "go generate github.com/jingcheng-WU/gonum/blas/gonum”; DO NOT EDIT.
     2  
     3  // Copyright ©2015 The Gonum Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package gonum
     8  
     9  import (
    10  	math "github.com/jingcheng-WU/gonum/internal/math32"
    11  
    12  	"github.com/jingcheng-WU/gonum/blas"
    13  	"github.com/jingcheng-WU/gonum/internal/asm/f32"
    14  )
    15  
    16  var _ blas.Float32Level1 = Implementation{}
    17  
    18  // Snrm2 computes the Euclidean norm of a vector,
    19  //  sqrt(\sum_i x[i] * x[i]).
    20  // This function returns 0 if incX is negative.
    21  //
    22  // Float32 implementations are autogenerated and not directly tested.
    23  func (Implementation) Snrm2(n int, x []float32, incX int) float32 {
    24  	if incX < 1 {
    25  		if incX == 0 {
    26  			panic(zeroIncX)
    27  		}
    28  		return 0
    29  	}
    30  	if len(x) <= (n-1)*incX {
    31  		panic(shortX)
    32  	}
    33  	if n < 2 {
    34  		if n == 1 {
    35  			return math.Abs(x[0])
    36  		}
    37  		if n == 0 {
    38  			return 0
    39  		}
    40  		panic(nLT0)
    41  	}
    42  	if incX == 1 {
    43  		return f32.L2NormUnitary(x[:n])
    44  	}
    45  	return f32.L2NormInc(x, uintptr(n), uintptr(incX))
    46  }
    47  
    48  // Sasum computes the sum of the absolute values of the elements of x.
    49  //  \sum_i |x[i]|
    50  // Sasum returns 0 if incX is negative.
    51  //
    52  // Float32 implementations are autogenerated and not directly tested.
    53  func (Implementation) Sasum(n int, x []float32, incX int) float32 {
    54  	var sum float32
    55  	if n < 0 {
    56  		panic(nLT0)
    57  	}
    58  	if incX < 1 {
    59  		if incX == 0 {
    60  			panic(zeroIncX)
    61  		}
    62  		return 0
    63  	}
    64  	if len(x) <= (n-1)*incX {
    65  		panic(shortX)
    66  	}
    67  	if incX == 1 {
    68  		x = x[:n]
    69  		for _, v := range x {
    70  			sum += math.Abs(v)
    71  		}
    72  		return sum
    73  	}
    74  	for i := 0; i < n; i++ {
    75  		sum += math.Abs(x[i*incX])
    76  	}
    77  	return sum
    78  }
    79  
    80  // Isamax returns the index of an element of x with the largest absolute value.
    81  // If there are multiple such indices the earliest is returned.
    82  // Isamax returns -1 if n == 0.
    83  //
    84  // Float32 implementations are autogenerated and not directly tested.
    85  func (Implementation) Isamax(n int, x []float32, incX int) int {
    86  	if incX < 1 {
    87  		if incX == 0 {
    88  			panic(zeroIncX)
    89  		}
    90  		return -1
    91  	}
    92  	if len(x) <= (n-1)*incX {
    93  		panic(shortX)
    94  	}
    95  	if n < 2 {
    96  		if n == 1 {
    97  			return 0
    98  		}
    99  		if n == 0 {
   100  			return -1 // Netlib returns invalid index when n == 0.
   101  		}
   102  		panic(nLT0)
   103  	}
   104  	idx := 0
   105  	max := math.Abs(x[0])
   106  	if incX == 1 {
   107  		for i, v := range x[:n] {
   108  			absV := math.Abs(v)
   109  			if absV > max {
   110  				max = absV
   111  				idx = i
   112  			}
   113  		}
   114  		return idx
   115  	}
   116  	ix := incX
   117  	for i := 1; i < n; i++ {
   118  		v := x[ix]
   119  		absV := math.Abs(v)
   120  		if absV > max {
   121  			max = absV
   122  			idx = i
   123  		}
   124  		ix += incX
   125  	}
   126  	return idx
   127  }
   128  
   129  // Sswap exchanges the elements of two vectors.
   130  //  x[i], y[i] = y[i], x[i] for all i
   131  //
   132  // Float32 implementations are autogenerated and not directly tested.
   133  func (Implementation) Sswap(n int, x []float32, incX int, y []float32, incY int) {
   134  	if incX == 0 {
   135  		panic(zeroIncX)
   136  	}
   137  	if incY == 0 {
   138  		panic(zeroIncY)
   139  	}
   140  	if n < 1 {
   141  		if n == 0 {
   142  			return
   143  		}
   144  		panic(nLT0)
   145  	}
   146  	if (incX > 0 && len(x) <= (n-1)*incX) || (incX < 0 && len(x) <= (1-n)*incX) {
   147  		panic(shortX)
   148  	}
   149  	if (incY > 0 && len(y) <= (n-1)*incY) || (incY < 0 && len(y) <= (1-n)*incY) {
   150  		panic(shortY)
   151  	}
   152  	if incX == 1 && incY == 1 {
   153  		x = x[:n]
   154  		for i, v := range x {
   155  			x[i], y[i] = y[i], v
   156  		}
   157  		return
   158  	}
   159  	var ix, iy int
   160  	if incX < 0 {
   161  		ix = (-n + 1) * incX
   162  	}
   163  	if incY < 0 {
   164  		iy = (-n + 1) * incY
   165  	}
   166  	for i := 0; i < n; i++ {
   167  		x[ix], y[iy] = y[iy], x[ix]
   168  		ix += incX
   169  		iy += incY
   170  	}
   171  }
   172  
   173  // Scopy copies the elements of x into the elements of y.
   174  //  y[i] = x[i] for all i
   175  //
   176  // Float32 implementations are autogenerated and not directly tested.
   177  func (Implementation) Scopy(n int, x []float32, incX int, y []float32, incY int) {
   178  	if incX == 0 {
   179  		panic(zeroIncX)
   180  	}
   181  	if incY == 0 {
   182  		panic(zeroIncY)
   183  	}
   184  	if n < 1 {
   185  		if n == 0 {
   186  			return
   187  		}
   188  		panic(nLT0)
   189  	}
   190  	if (incX > 0 && len(x) <= (n-1)*incX) || (incX < 0 && len(x) <= (1-n)*incX) {
   191  		panic(shortX)
   192  	}
   193  	if (incY > 0 && len(y) <= (n-1)*incY) || (incY < 0 && len(y) <= (1-n)*incY) {
   194  		panic(shortY)
   195  	}
   196  	if incX == 1 && incY == 1 {
   197  		copy(y[:n], x[:n])
   198  		return
   199  	}
   200  	var ix, iy int
   201  	if incX < 0 {
   202  		ix = (-n + 1) * incX
   203  	}
   204  	if incY < 0 {
   205  		iy = (-n + 1) * incY
   206  	}
   207  	for i := 0; i < n; i++ {
   208  		y[iy] = x[ix]
   209  		ix += incX
   210  		iy += incY
   211  	}
   212  }
   213  
   214  // Saxpy adds alpha times x to y
   215  //  y[i] += alpha * x[i] for all i
   216  //
   217  // Float32 implementations are autogenerated and not directly tested.
   218  func (Implementation) Saxpy(n int, alpha float32, x []float32, incX int, y []float32, incY int) {
   219  	if incX == 0 {
   220  		panic(zeroIncX)
   221  	}
   222  	if incY == 0 {
   223  		panic(zeroIncY)
   224  	}
   225  	if n < 1 {
   226  		if n == 0 {
   227  			return
   228  		}
   229  		panic(nLT0)
   230  	}
   231  	if (incX > 0 && len(x) <= (n-1)*incX) || (incX < 0 && len(x) <= (1-n)*incX) {
   232  		panic(shortX)
   233  	}
   234  	if (incY > 0 && len(y) <= (n-1)*incY) || (incY < 0 && len(y) <= (1-n)*incY) {
   235  		panic(shortY)
   236  	}
   237  	if alpha == 0 {
   238  		return
   239  	}
   240  	if incX == 1 && incY == 1 {
   241  		f32.AxpyUnitary(alpha, x[:n], y[:n])
   242  		return
   243  	}
   244  	var ix, iy int
   245  	if incX < 0 {
   246  		ix = (-n + 1) * incX
   247  	}
   248  	if incY < 0 {
   249  		iy = (-n + 1) * incY
   250  	}
   251  	f32.AxpyInc(alpha, x, y, uintptr(n), uintptr(incX), uintptr(incY), uintptr(ix), uintptr(iy))
   252  }
   253  
   254  // Srotg computes the plane rotation
   255  //   _    _      _ _       _ _
   256  //  |  c s |    | a |     | r |
   257  //  | -s c |  * | b |   = | 0 |
   258  //   ‾    ‾      ‾ ‾       ‾ ‾
   259  // where
   260  //  r = ±√(a^2 + b^2)
   261  //  c = a/r, the cosine of the plane rotation
   262  //  s = b/r, the sine of the plane rotation
   263  //
   264  // NOTE: There is a discrepancy between the reference implementation and the BLAS
   265  // technical manual regarding the sign for r when a or b are zero.
   266  // Srotg agrees with the definition in the manual and other
   267  // common BLAS implementations.
   268  //
   269  // Float32 implementations are autogenerated and not directly tested.
   270  func (Implementation) Srotg(a, b float32) (c, s, r, z float32) {
   271  	if b == 0 && a == 0 {
   272  		return 1, 0, a, 0
   273  	}
   274  	absA := math.Abs(a)
   275  	absB := math.Abs(b)
   276  	aGTb := absA > absB
   277  	r = math.Hypot(a, b)
   278  	if aGTb {
   279  		r = math.Copysign(r, a)
   280  	} else {
   281  		r = math.Copysign(r, b)
   282  	}
   283  	c = a / r
   284  	s = b / r
   285  	if aGTb {
   286  		z = s
   287  	} else if c != 0 { // r == 0 case handled above
   288  		z = 1 / c
   289  	} else {
   290  		z = 1
   291  	}
   292  	return
   293  }
   294  
   295  // Srotmg computes the modified Givens rotation. See
   296  // http://www.netlib.org/lapack/explore-html/df/deb/drotmg_8f.html
   297  // for more details.
   298  //
   299  // Float32 implementations are autogenerated and not directly tested.
   300  func (Implementation) Srotmg(d1, d2, x1, y1 float32) (p blas.SrotmParams, rd1, rd2, rx1 float32) {
   301  	// The implementation of Drotmg used here is taken from Hopkins 1997
   302  	// Appendix A: https://doi.org/10.1145/289251.289253
   303  	// with the exception of the gam constants below.
   304  
   305  	const (
   306  		gam    = 4096.0
   307  		gamsq  = gam * gam
   308  		rgamsq = 1.0 / gamsq
   309  	)
   310  
   311  	if d1 < 0 {
   312  		p.Flag = blas.Rescaling // Error state.
   313  		return p, 0, 0, 0
   314  	}
   315  
   316  	if d2 == 0 || y1 == 0 {
   317  		p.Flag = blas.Identity
   318  		return p, d1, d2, x1
   319  	}
   320  
   321  	var h11, h12, h21, h22 float32
   322  	if (d1 == 0 || x1 == 0) && d2 > 0 {
   323  		p.Flag = blas.Diagonal
   324  		h12 = 1
   325  		h21 = -1
   326  		x1 = y1
   327  		d1, d2 = d2, d1
   328  	} else {
   329  		p2 := d2 * y1
   330  		p1 := d1 * x1
   331  		q2 := p2 * y1
   332  		q1 := p1 * x1
   333  		if math.Abs(q1) > math.Abs(q2) {
   334  			p.Flag = blas.OffDiagonal
   335  			h11 = 1
   336  			h22 = 1
   337  			h21 = -y1 / x1
   338  			h12 = p2 / p1
   339  			u := 1 - float32(h12*h21)
   340  			if u <= 0 {
   341  				p.Flag = blas.Rescaling // Error state.
   342  				return p, 0, 0, 0
   343  			}
   344  
   345  			d1 /= u
   346  			d2 /= u
   347  			x1 *= u
   348  		} else {
   349  			if q2 < 0 {
   350  				p.Flag = blas.Rescaling // Error state.
   351  				return p, 0, 0, 0
   352  			}
   353  
   354  			p.Flag = blas.Diagonal
   355  			h21 = -1
   356  			h12 = 1
   357  			h11 = p1 / p2
   358  			h22 = x1 / y1
   359  			u := 1 + float32(h11*h22)
   360  			d1, d2 = d2/u, d1/u
   361  			x1 = y1 * u
   362  		}
   363  	}
   364  
   365  	for d1 <= rgamsq && d1 != 0 {
   366  		p.Flag = blas.Rescaling
   367  		d1 = (d1 * gam) * gam
   368  		x1 /= gam
   369  		h11 /= gam
   370  		h12 /= gam
   371  	}
   372  	for d1 > gamsq {
   373  		p.Flag = blas.Rescaling
   374  		d1 = (d1 / gam) / gam
   375  		x1 *= gam
   376  		h11 *= gam
   377  		h12 *= gam
   378  	}
   379  
   380  	for math.Abs(d2) <= rgamsq && d2 != 0 {
   381  		p.Flag = blas.Rescaling
   382  		d2 = (d2 * gam) * gam
   383  		h21 /= gam
   384  		h22 /= gam
   385  	}
   386  	for math.Abs(d2) > gamsq {
   387  		p.Flag = blas.Rescaling
   388  		d2 = (d2 / gam) / gam
   389  		h21 *= gam
   390  		h22 *= gam
   391  	}
   392  
   393  	switch p.Flag {
   394  	case blas.Diagonal:
   395  		p.H = [4]float32{0: h11, 3: h22}
   396  	case blas.OffDiagonal:
   397  		p.H = [4]float32{1: h21, 2: h12}
   398  	case blas.Rescaling:
   399  		p.H = [4]float32{h11, h21, h12, h22}
   400  	default:
   401  		panic(badFlag)
   402  	}
   403  
   404  	return p, d1, d2, x1
   405  }
   406  
   407  // Srot applies a plane transformation.
   408  //  x[i] = c * x[i] + s * y[i]
   409  //  y[i] = c * y[i] - s * x[i]
   410  //
   411  // Float32 implementations are autogenerated and not directly tested.
   412  func (Implementation) Srot(n int, x []float32, incX int, y []float32, incY int, c float32, s float32) {
   413  	if incX == 0 {
   414  		panic(zeroIncX)
   415  	}
   416  	if incY == 0 {
   417  		panic(zeroIncY)
   418  	}
   419  	if n < 1 {
   420  		if n == 0 {
   421  			return
   422  		}
   423  		panic(nLT0)
   424  	}
   425  	if (incX > 0 && len(x) <= (n-1)*incX) || (incX < 0 && len(x) <= (1-n)*incX) {
   426  		panic(shortX)
   427  	}
   428  	if (incY > 0 && len(y) <= (n-1)*incY) || (incY < 0 && len(y) <= (1-n)*incY) {
   429  		panic(shortY)
   430  	}
   431  	if incX == 1 && incY == 1 {
   432  		x = x[:n]
   433  		for i, vx := range x {
   434  			vy := y[i]
   435  			x[i], y[i] = c*vx+s*vy, c*vy-s*vx
   436  		}
   437  		return
   438  	}
   439  	var ix, iy int
   440  	if incX < 0 {
   441  		ix = (-n + 1) * incX
   442  	}
   443  	if incY < 0 {
   444  		iy = (-n + 1) * incY
   445  	}
   446  	for i := 0; i < n; i++ {
   447  		vx := x[ix]
   448  		vy := y[iy]
   449  		x[ix], y[iy] = c*vx+s*vy, c*vy-s*vx
   450  		ix += incX
   451  		iy += incY
   452  	}
   453  }
   454  
   455  // Srotm applies the modified Givens rotation to the 2×n matrix.
   456  //
   457  // Float32 implementations are autogenerated and not directly tested.
   458  func (Implementation) Srotm(n int, x []float32, incX int, y []float32, incY int, p blas.SrotmParams) {
   459  	if incX == 0 {
   460  		panic(zeroIncX)
   461  	}
   462  	if incY == 0 {
   463  		panic(zeroIncY)
   464  	}
   465  	if n <= 0 {
   466  		if n == 0 {
   467  			return
   468  		}
   469  		panic(nLT0)
   470  	}
   471  	if (incX > 0 && len(x) <= (n-1)*incX) || (incX < 0 && len(x) <= (1-n)*incX) {
   472  		panic(shortX)
   473  	}
   474  	if (incY > 0 && len(y) <= (n-1)*incY) || (incY < 0 && len(y) <= (1-n)*incY) {
   475  		panic(shortY)
   476  	}
   477  
   478  	if p.Flag == blas.Identity {
   479  		return
   480  	}
   481  
   482  	switch p.Flag {
   483  	case blas.Rescaling:
   484  		h11 := p.H[0]
   485  		h12 := p.H[2]
   486  		h21 := p.H[1]
   487  		h22 := p.H[3]
   488  		if incX == 1 && incY == 1 {
   489  			x = x[:n]
   490  			for i, vx := range x {
   491  				vy := y[i]
   492  				x[i], y[i] = float32(vx*h11)+float32(vy*h12), float32(vx*h21)+float32(vy*h22)
   493  			}
   494  			return
   495  		}
   496  		var ix, iy int
   497  		if incX < 0 {
   498  			ix = (-n + 1) * incX
   499  		}
   500  		if incY < 0 {
   501  			iy = (-n + 1) * incY
   502  		}
   503  		for i := 0; i < n; i++ {
   504  			vx := x[ix]
   505  			vy := y[iy]
   506  			x[ix], y[iy] = float32(vx*h11)+float32(vy*h12), float32(vx*h21)+float32(vy*h22)
   507  			ix += incX
   508  			iy += incY
   509  		}
   510  	case blas.OffDiagonal:
   511  		h12 := p.H[2]
   512  		h21 := p.H[1]
   513  		if incX == 1 && incY == 1 {
   514  			x = x[:n]
   515  			for i, vx := range x {
   516  				vy := y[i]
   517  				x[i], y[i] = vx+float32(vy*h12), float32(vx*h21)+vy
   518  			}
   519  			return
   520  		}
   521  		var ix, iy int
   522  		if incX < 0 {
   523  			ix = (-n + 1) * incX
   524  		}
   525  		if incY < 0 {
   526  			iy = (-n + 1) * incY
   527  		}
   528  		for i := 0; i < n; i++ {
   529  			vx := x[ix]
   530  			vy := y[iy]
   531  			x[ix], y[iy] = vx+float32(vy*h12), float32(vx*h21)+vy
   532  			ix += incX
   533  			iy += incY
   534  		}
   535  	case blas.Diagonal:
   536  		h11 := p.H[0]
   537  		h22 := p.H[3]
   538  		if incX == 1 && incY == 1 {
   539  			x = x[:n]
   540  			for i, vx := range x {
   541  				vy := y[i]
   542  				x[i], y[i] = float32(vx*h11)+vy, -vx+float32(vy*h22)
   543  			}
   544  			return
   545  		}
   546  		var ix, iy int
   547  		if incX < 0 {
   548  			ix = (-n + 1) * incX
   549  		}
   550  		if incY < 0 {
   551  			iy = (-n + 1) * incY
   552  		}
   553  		for i := 0; i < n; i++ {
   554  			vx := x[ix]
   555  			vy := y[iy]
   556  			x[ix], y[iy] = float32(vx*h11)+vy, -vx+float32(vy*h22)
   557  			ix += incX
   558  			iy += incY
   559  		}
   560  	}
   561  }
   562  
   563  // Sscal scales x by alpha.
   564  //  x[i] *= alpha
   565  // Sscal has no effect if incX < 0.
   566  //
   567  // Float32 implementations are autogenerated and not directly tested.
   568  func (Implementation) Sscal(n int, alpha float32, x []float32, incX int) {
   569  	if incX < 1 {
   570  		if incX == 0 {
   571  			panic(zeroIncX)
   572  		}
   573  		return
   574  	}
   575  	if n < 1 {
   576  		if n == 0 {
   577  			return
   578  		}
   579  		panic(nLT0)
   580  	}
   581  	if (n-1)*incX >= len(x) {
   582  		panic(shortX)
   583  	}
   584  	if alpha == 0 {
   585  		if incX == 1 {
   586  			x = x[:n]
   587  			for i := range x {
   588  				x[i] = 0
   589  			}
   590  			return
   591  		}
   592  		for ix := 0; ix < n*incX; ix += incX {
   593  			x[ix] = 0
   594  		}
   595  		return
   596  	}
   597  	if incX == 1 {
   598  		f32.ScalUnitary(alpha, x[:n])
   599  		return
   600  	}
   601  	f32.ScalInc(alpha, x, uintptr(n), uintptr(incX))
   602  }