001/*
002 * (C) Copyright 2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Olivier Grisel
016 */
017
018package org.nuxeo.ecm.platform.categorization.categorizer.tfidf;
019
020public class PrimitiveVectorHelper {
021
022    public static void add(long[] target, long[] increment) {
023        for (int i = 0; i < target.length; i++) {
024            target[i] += increment[i];
025        }
026    }
027
028    public static float dot(float[] v1, float[] v2) {
029        float sum = 0;
030        for (int i = 0; i < v1.length; i++) {
031            sum += v1[i] * v2[i];
032        }
033        return sum;
034    }
035
036    public static float normOf(float[] vector) {
037        float sum = 0f;
038        for (int i = 0; i < vector.length; i++) {
039            sum += vector[i] * vector[i];
040        }
041        return (float) Math.sqrt(sum);
042    }
043
044    public static long sum(long[] counts) {
045        long sum = 0;
046        for (int i = 0; i < counts.length; i++) {
047            sum += counts[i];
048        }
049        return sum;
050    }
051
052}