gonum.org/v1/gonum@v0.14.0/lapack/gonum/dlapmr.go (about)

     1  // Copyright ©2022 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gonum
     6  
     7  import "gonum.org/v1/gonum/blas/blas64"
     8  
     9  // Dlapmr rearranges the rows of the m×n matrix X as specified by the permutation
    10  // k[0],k[1],...,k[m-1] of the integers 0,...,m-1.
    11  //
    12  // If forward is true, a forward permutation is applied:
    13  //
    14  //	X[k[i],0:n] is moved to X[i,0:n] for i=0,1,...,m-1.
    15  //
    16  // If forward is false, a backward permutation is applied:
    17  //
    18  //	X[i,0:n] is moved to X[k[i],0:n] for i=0,1,...,m-1.
    19  //
    20  // k must have length m, otherwise Dlapmr will panic.
    21  func (impl Implementation) Dlapmr(forward bool, m, n int, x []float64, ldx int, k []int) {
    22  	switch {
    23  	case m < 0:
    24  		panic(mLT0)
    25  	case n < 0:
    26  		panic(nLT0)
    27  	case ldx < max(1, n):
    28  		panic(badLdX)
    29  	}
    30  
    31  	// Quick return if possible.
    32  	if m == 0 || n == 0 {
    33  		return
    34  	}
    35  
    36  	switch {
    37  	case len(x) < (m-1)*ldx+n:
    38  		panic(shortX)
    39  	case len(k) != m:
    40  		panic(badLenK)
    41  	}
    42  
    43  	// Quick return if possible.
    44  	if m == 1 {
    45  		return
    46  	}
    47  
    48  	bi := blas64.Implementation()
    49  
    50  	for i, ki := range k {
    51  		k[i] = -(ki + 1)
    52  	}
    53  	if forward {
    54  		for i, ki := range k {
    55  			if ki >= 0 {
    56  				continue
    57  			}
    58  			j := i
    59  			k[j] = -k[j] - 1
    60  			in := k[j]
    61  			for {
    62  				if k[in] >= 0 {
    63  					break
    64  				}
    65  				bi.Dswap(n, x[j*ldx:], 1, x[in*ldx:], 1)
    66  				k[in] = -k[in] - 1
    67  				j = in
    68  				in = k[in]
    69  			}
    70  		}
    71  	} else {
    72  		for i, ki := range k {
    73  			if ki >= 0 {
    74  				continue
    75  			}
    76  			k[i] = -ki - 1
    77  			j := k[i]
    78  			for {
    79  				if j == i {
    80  					break
    81  				}
    82  				bi.Dswap(n, x[i*ldx:], 1, x[j*ldx:], 1)
    83  				k[j] = -k[j] - 1
    84  				j = k[j]
    85  			}
    86  		}
    87  	}
    88  }