001/*
002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Olivier Grisel
018 */
019
020package org.nuxeo.ecm.platform.categorization.categorizer.tfidf;
021
022public class PrimitiveVectorHelper {
023
024    public static void add(long[] target, long[] increment) {
025        for (int i = 0; i < target.length; i++) {
026            target[i] += increment[i];
027        }
028    }
029
030    public static float dot(float[] v1, float[] v2) {
031        float sum = 0;
032        for (int i = 0; i < v1.length; i++) {
033            sum += v1[i] * v2[i];
034        }
035        return sum;
036    }
037
038    public static float normOf(float[] vector) {
039        float sum = 0f;
040        for (int i = 0; i < vector.length; i++) {
041            sum += vector[i] * vector[i];
042        }
043        return (float) Math.sqrt(sum);
044    }
045
046    public static long sum(long[] counts) {
047        long sum = 0;
048        for (int i = 0; i < counts.length; i++) {
049            sum += counts[i];
050        }
051        return sum;
052    }
053
054}