com.davidbracewell.math.distance.DistanceMeasure.java Source code

Java tutorial

Introduction

Here is the source code for com.davidbracewell.math.distance.DistanceMeasure.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.distance;

import com.davidbracewell.collection.Counter;
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.base.Predicate;
import com.google.common.collect.Maps;

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

/**
 * The type Distance measure.
 *
 * @author David B. Bracewell
 */
public abstract class DistanceMeasure {

    /**
     * Calculate double.
     *
     * @param v1 the v 1
     * @param v2 the v 2
     * @return the double
     */
    public final 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 final 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 final double calculate(Vector v1, Vector v2, Predicate<Double> validValues) {
        Preconditions.checkNotNull(v1);
        Preconditions.checkNotNull(v2);
        Preconditions.checkNotNull(validValues);
        Preconditions.checkArgument(v1.dimension() == v2.dimension(), "Dimension mismatch.");
        return calculate(VectorMap.wrap(v1, validValues), VectorMap.wrap(v2, validValues));
    }

    /**
     * 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 v1 the v 1
     * @param v2 the v 2
     * @return the double
     */
    public abstract double calculate(double[] v1, double[] v2);

    private enum SET_NUMBER implements Function<Object, Number> {
        INSTANCE;

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

    }

}//END OF DistanceMeasure