mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
DO-NOT-MERGE: partial improvements for non-published artifacts against cql vector types
This commit is contained in:
committed by
jeffbanks
parent
1442d13e11
commit
1b042fa2d1
@@ -29,7 +29,7 @@ import java.util.function.Function;
|
||||
*/
|
||||
@ThreadSafeMapper
|
||||
@Categories(Category.experimental)
|
||||
public class NormalizeVector implements Function<List<Double>,List<Double>> {
|
||||
public class NormalizeDoubleVectorList implements Function<List<Double>,List<Double>> {
|
||||
@Override
|
||||
public List<Double> apply(List<Double> doubles) {
|
||||
ArrayList<Double> unit = new ArrayList<>(doubles.size());
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2023 nosqlbench
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_vector;
|
||||
|
||||
import io.nosqlbench.virtdata.api.annotations.Categories;
|
||||
import io.nosqlbench.virtdata.api.annotations.Category;
|
||||
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Normalize a vector.
|
||||
*/
|
||||
@ThreadSafeMapper
|
||||
@Categories(Category.experimental)
|
||||
public class NormalizeFloatVectorList implements Function<List<Float>,List<Float>> {
|
||||
@Override
|
||||
public List<Float> apply(List<Float> floats) {
|
||||
ArrayList<Float> unit = new ArrayList<>(floats.size());
|
||||
float accumulator = 0.0f;
|
||||
for (float scalar : floats) {
|
||||
accumulator+=scalar*scalar;
|
||||
}
|
||||
float scalarLen = (float) Math.sqrt(accumulator);
|
||||
for (float scalarComponent : floats) {
|
||||
unit.add(scalarComponent/scalarLen);
|
||||
}
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public class ToNormalizedVectorTest {
|
||||
|
||||
@Test
|
||||
public void testNormalizeBasic() {
|
||||
NormalizeVector normalize = new NormalizeVector();
|
||||
NormalizeDoubleVectorList normalize = new NormalizeDoubleVectorList();
|
||||
List<Double> normalized = normalize.apply(List.of(1.0d));
|
||||
for (int i = 0; i < normalized.size(); i++) {
|
||||
assertThat(normalized.get(i)).isCloseTo(1.0d, Offset.offset(0.00001d));
|
||||
|
||||
Reference in New Issue
Block a user