github.com/kaydxh/golang@v0.0.131/pkg/gocv/vector/vector_test.go (about) 1 /* 2 *Copyright (c) 2023, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 23 package vector_test 24 25 import ( 26 "math/rand" 27 "testing" 28 29 "github.com/kaydxh/golang/pkg/gocv/vector" 30 ) 31 32 func TestVector(t *testing.T) { 33 var ve vector.Vector[float32] 34 for i := 0; i < 1024; i++ { 35 ve.Append(rand.Float32()) 36 } 37 t.Logf("len: %v", ve.Len()) 38 s, err := ve.String() 39 if err != nil { 40 t.Fatalf("failed to get string, err: %v", err) 41 } 42 t.Logf("String: %v", s) 43 44 ve.Norm() 45 s, err = ve.String() 46 if err != nil { 47 t.Fatalf("failed to get norm string, err: %v", err) 48 } 49 t.Logf("Norm string: %v", s) 50 } 51 52 func TestCreateNormalizedVector(t *testing.T) { 53 dim := 1024 54 data := vector.CreateNormalizedVector[float32](dim) 55 t.Logf("data: %v", data) 56 } 57 58 func TestCosineDistance(t *testing.T) { 59 dim := 1024 60 data1 := vector.CreateNormalizedVector[float32](dim) 61 data2 := vector.CreateNormalizedVector[float32](dim) 62 v1 := vector.NewVector(data1...) 63 v2 := vector.NewVector(data2...) 64 distance := v1.CosineDistance(v2) 65 t.Logf("cosine distance: %v", distance) 66 67 distance = vector.CosineDistance(data1, data2) 68 t.Logf("cosine distance: %v", distance) 69 } 70 71 func TestEuclideanDistance(t *testing.T) { 72 dim := 1024 73 data1 := vector.CreateNormalizedVector[float32](dim) 74 data2 := vector.CreateNormalizedVector[float32](dim) 75 v1 := vector.NewVector(data1...) 76 v2 := vector.NewVector(data2...) 77 distance := v1.EuclideanDistance(v2) 78 t.Logf("euclidean distance: %v", distance) 79 80 distance = vector.EuclideanDistance(data1, data2) 81 t.Logf("euclidean distance: %v", distance) 82 }