github.com/matrixorigin/matrixone@v1.2.0/pkg/vectorize/moarray/internal.go (about)

     1  // Copyright 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package moarray
    16  
    17  import "gonum.org/v1/gonum/mat"
    18  
    19  // These functions are use internally by the kmeans algorithm and vector index etc. They are not exposed externally.
    20  
    21  // NormalizeGonumVector normalizes a vector in place.
    22  // Note that this function is used by the kmeans algorithm. Here, if we get a zero vector, we do not normalize it and
    23  // return it directly. This is because the zero vector is a valid vector in the kmeans algorithm.
    24  func NormalizeGonumVector(vector *mat.VecDense) {
    25  	norm := mat.Norm(vector, 2)
    26  	if norm != 0 {
    27  		vector.ScaleVec(1/norm, vector)
    28  	}
    29  }
    30  
    31  func NormalizeGonumVectors(vectors []*mat.VecDense) {
    32  	for i := range vectors {
    33  		NormalizeGonumVector(vectors[i])
    34  	}
    35  }
    36  
    37  //// NormalizeMoVecf64 is used only in test functions.
    38  //func NormalizeMoVecf64(vector []float64) []float64 {
    39  //	res := ToGonumVector[float64](vector)
    40  //	//NormalizeGonumVector(res)
    41  //	return ToMoArray[float64](res)
    42  //}