📄 syntheticdata2.cpp
字号:
/* Scalable K-means clustering softwareCopyright (C) 2000 Fredrik Farnstrom and James LewisThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.See the file README.TXT for more information.*//* syntheticdata.cpp */#include <math.h>#include <stdio.h>#include <stdlib.h>#include "singleton.h"#include "subcluster.h"#include "database.h"#include "syntheticdata.h"SyntheticData::SyntheticData(int models, int dim, int points) : models(models), dimensions(dim), numPoints(points){ data = new float[points * dim];}SyntheticData::~SyntheticData(void){ delete []data;}// Initialize the data points.void SyntheticData::initData(float meanRange, float stdDevRange){ int i,j,k; int model; float x1, x2, z1, z2; srand(10); float mean[models][dimensions]; float stdDev[models][dimensions]; // Initialize mean and standard deviation for each model. for (i = 0; i < models; i++) { for (j = 0; j < dimensions; j++) { mean[i][j] = (float) (drand48() * meanRange); stdDev[i][j] = (float) (drand48() * stdDevRange);// printf("Model (%d,%d): %f, %f\n", i, j, mean[i][j], stdDev[i][j]); } } for(i = 0; i < numPoints; i++) { model = (int) (drand48() * models); for (j = 0; j < dimensions; j += 2) { x1 = (float) drand48(); x2 = (float) drand48(); z1 = (float) (sqrt(-2.0 * log (x1)) * cos(2.0 * M_PI * x2)); z2 = (float) (sqrt(-2.0 * log (x1)) * sin(2.0 * M_PI * x2));// printf("%f %f %f %f\n", x1, x2, z1, z2); z1 = z1 * stdDev[model][j] + mean[model][j]; z2 = z2 * stdDev[model][j + 1] + mean[model][j + 1]; data[i * dimensions + j] = z1; if (j < dimensions - 1) { data[i * dimensions + j + 1] = z2; } } }}// Reset counter, i.e return first point next time getPoint is called.void SyntheticData::reset(void){ current = 0;}// Return a pointer to an array of floats containing the current point.// The allocated memory may be read by the user but belongs to the// Database class.float *SyntheticData::getPoint(void){ if (current < numPoints) return &data[current++ * dimensions]; else return 0;}/* End of file syntheticdata.cpp */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -