📄 distancematrix.java
字号:
/*
Copyright (C) 2004 Julia Handl
Email: Julia.Handl@gmx.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package javaants;
public class DistanceMatrix implements java.io.Serializable {
int N;
float [][] matrix;
public DistanceMatrix(int N, Document [] documents, Configuration conf) {
this.N = N-1;
matrix = new float[this.N][];
float scaleFactor = 0;
for (int i=0; i<this.N; i++) {
matrix[i] = new float[i+1];
}
for (int i=1; i<=this.N; i++) {
for (int j=0; j<i; j++) {
matrix[i-1][j] = (float)documents[i].distance(documents[j],conf.getTextMode(), conf.getMethod());
scaleFactor += matrix[i-1][j];
}
}
// compute the scale factor (average over all inter-document distances)
scaleFactor /= 0.5*(float)(conf.getndocs()*(conf.getndocs()-1));
conf.setdistscale(scaleFactor);
}
public double get(int i, int j) {
if (i==j) return 0;
if (i < j) return matrix[j-1][i];
else return matrix[i-1][j];
}
public void set(int i, int j, double value) {
if (i==j) return;
if (i < j) matrix[j-1][i]=(float)value;
else matrix[i-1][j]=(float)value;
}
public void mult(int i, int j, double value) {
if (i==j) return;
if (i < j) matrix[j-1][i]*=(float)value;
else matrix[i-1][j]*=(float)value;
}
public void add(int i, int j, double value) {
if (i==j) return;
if (i < j) matrix[j-1][i]+=(float)value;
else matrix[i-1][j]+=(float)value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -