⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vectorutils.java

📁 Classifier4J是一个很好的基于java的分类器,里面有Native bayes和KNN等方法的文本分类.另外还 提供了分词和自动摘要等功能
💻 JAVA
字号:

package net.sf.classifier4J.vector;


public class VectorUtils {
    public static int scalarProduct(int[] one, int[] two) throws IllegalArgumentException {
        if ((one == null) || (two == null)) {
            throw new IllegalArgumentException("Arguments cannot be null");
        }
        
        if (one.length != two.length) {
            throw new IllegalArgumentException("Arguments of different length are not allowed");
        }
        
        int result = 0;
        for (int i = 0; i < one.length; i++) {
            result += one[i] * two[i];
        }
        return result;
    }
    
    public static double vectorLength(int[] vector) throws IllegalArgumentException {
        if (vector == null) {
            throw new IllegalArgumentException("Arguments cannot be null");
        }
        
        double sumOfSquares = 0d;
        for (int i = 0; i < vector.length; i++) {
            sumOfSquares = sumOfSquares + (vector[i] * vector[i]);
        }
        
        return Math.sqrt(sumOfSquares);
    }
    
    public static double cosineOfVectors(int[] one, int[] two) throws IllegalArgumentException {
        if ((one == null) || (two == null)) {
            throw new IllegalArgumentException("Arguments cannot be null");
        }
        
        if (one.length != two.length) {
            throw new IllegalArgumentException("Arguments of different length are not allowed");
        }     
        double denominater = (vectorLength(one) * vectorLength(two));
        if (denominater == 0) {
            return 0;
        } else {
            return (scalarProduct(one, two)/denominater);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -