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

     1  // Copyright ©2017 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  // Dlapmt rearranges the columns of the m×n matrix X as specified by the
    10  // permutation k_0, k_1, ..., k_n-1 of the integers 0, ..., n-1.
    11  //
    12  // If forward is true a forward permutation is performed:
    13  //
    14  //	X[0:m, k[j]] is moved to X[0:m, j] for j = 0, 1, ..., n-1.
    15  //
    16  // otherwise a backward permutation is performed:
    17  //
    18  //	X[0:m, j] is moved to X[0:m, k[j]] for j = 0, 1, ..., n-1.
    19  //
    20  // k must have length n, otherwise Dlapmt will panic. k is zero-indexed.
    21  func (impl Implementation) Dlapmt(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) != n:
    40  		panic(badLenK)
    41  	}
    42  
    43  	// Quick return if possible.
    44  	if n == 1 {
    45  		return
    46  	}
    47  
    48  	for i, v := range k {
    49  		v++
    50  		k[i] = -v
    51  	}
    52  
    53  	bi := blas64.Implementation()
    54  
    55  	if forward {
    56  		for j, v := range k {
    57  			if v >= 0 {
    58  				continue
    59  			}
    60  			k[j] = -v
    61  			i := -v - 1
    62  			for k[i] < 0 {
    63  				bi.Dswap(m, x[j:], ldx, x[i:], ldx)
    64  
    65  				k[i] = -k[i]
    66  				j = i
    67  				i = k[i] - 1
    68  			}
    69  		}
    70  	} else {
    71  		for i, v := range k {
    72  			if v >= 0 {
    73  				continue
    74  			}
    75  			k[i] = -v
    76  			j := -v - 1
    77  			for j != i {
    78  				bi.Dswap(m, x[j:], ldx, x[i:], ldx)
    79  
    80  				k[j] = -k[j]
    81  				j = k[j] - 1
    82  			}
    83  		}
    84  	}
    85  
    86  	for i := range k {
    87  		k[i]--
    88  	}
    89  }