com.davidbracewell.math.similarity.SimilarityMeasure.java Source code

Java tutorial

Introduction

Here is the source code for com.davidbracewell.math.similarity.SimilarityMeasure.java

Source

/*
 * (c) 2005 David B. Bracewell
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 com.davidbracewell.math.similarity;

import com.davidbracewell.collection.Counter;
import com.davidbracewell.math.ContingencyTable;
import com.davidbracewell.math.linear.Vector;
import com.davidbracewell.math.linear.VectorMap;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;

import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;

/**
 * The type Similarity measure.
 *
 * @author David B. Bracewell
 */
public abstract class SimilarityMeasure implements Serializable {

    private static final long serialVersionUID = 2678863693902232233L;

    private enum SET_NUMBER implements Function<Object, Number> {
        /**
         * The INSTANCE.
         */
        INSTANCE;

        @Nullable
        @Override
        public Number apply(@Nullable Object input) {
            return 1d;
        }

    }

    /**
     * Calculate double.
     *
     * @param v1 the v 1
     * @param v2 the v 2
     * @return the double
     */
    public double calculate(Counter<?> v1, Counter<?> v2) {
        return calculate(Preconditions.checkNotNull(v1, "Vectors cannot be null").asMap(),
                Preconditions.checkNotNull(v2, "Vectors cannot be null").asMap());
    }

    /**
     * Calculate double.
     *
     * @param s1 the s 1
     * @param s2 the s 2
     * @return the double
     */
    public double calculate(Set<?> s1, Set<?> s2) {
        return calculate(Maps.asMap(Preconditions.checkNotNull(s1, "Vectors cannot be null"), SET_NUMBER.INSTANCE),
                Maps.asMap(Preconditions.checkNotNull(s2, "Vectors cannot be null"), SET_NUMBER.INSTANCE));
    }

    /**
     * Calculate double.
     *
     * @param v1 the v 1
     * @param v2 the v 2
     * @return the double
     */
    public double calculate(Vector v1, Vector v2) {
        Preconditions.checkNotNull(v1);
        Preconditions.checkNotNull(v2);
        Preconditions.checkArgument(v1.dimension() == v2.dimension(), "Dimension mismatch.");
        return calculate(VectorMap.wrap(v1), VectorMap.wrap(v2));
    }

    /**
     * Calculate double.
     *
     * @param m1 the m 1
     * @param m2 the m 2
     * @return the double
     */
    public abstract double calculate(Map<?, ? extends Number> m1, Map<?, ? extends Number> m2);

    /**
     * Calculate double.
     *
     * @param table the table
     * @return the double
     */
    public abstract double calculate(ContingencyTable table);

    /**
     * Calculate double.
     *
     * @param v1 the v 1
     * @param v2 the v 2
     * @return the double
     */
    public abstract double calculate(double[] v1, double[] v2);

}//END OF SimilarityMeasure