// Copyright (C) 1999 Jean-Marc Valin #include "kmeans.h" #include "ObjectParser.h" using namespace std; namespace FD { DECLARE_TYPE(KMeans) //@implements VQ void KMeans::split (const vector &data, int len) { int nbMeans = means.size(); #ifdef STACK_ALLOC float totalDist[nbMeans]; int belongs[data.size()]; int accum[data.size()]; #else float *totalDist = new float [nbMeans]; int *belongs = new int [data.size()]; int *accum = new int [data.size()]; #endif int i; for (i=0; i max_dist) { max_dist=totalDist[i]; maxID=i; } /* for (i=0; i max_dist) { max_dist=totalDist[i]/accum[i]; maxID=i; } */ /*cerr << "about to perform split\n"; cerr << "nbMeans = " << nbMeans << endl; cerr << "length = " << length << endl; cerr << "maxID = " << maxID << endl;*/ means.resize(nbMeans+1); means[nbMeans].resize(length); for (i=0; i &KMeans::operator[] (int i) const { return means[i]; } void KMeans::bsplit () { int nbMeans = means.size(); int i; means.resize(nbMeans*2); for (i=nbMeans;i &data, int len) { int nbMeans = means.size(); #ifdef STACK_ALLOC float totalDist[nbMeans]; int belongs[data.size()]; int accum[data.size()]; #else float *totalDist = new float [nbMeans]; int *belongs = new int [data.size()]; int *accum = new int [data.size()]; #endif int i,j; for (i=0; i &data, int len, bool binary) { int i,j; //cerr << "void KMeans::train (" << codeSize << ", " << data << ", "< &w, Vector &out) const { if ( !(w.size() == means.size() && out.size() == means[0].size()) ) { cerr << "sizes don't match in KMeans::weightMeans\n"; cerr << w.size() << " " << means.size() << " " << out.size() << " " << means[0].size() << endl; } for (int j=0;j" << endl; out << "" << endl; out << ">\n"; } void KMeans::readFrom (istream &in) { string tag; while (1) { char ch; in >> ch; if (ch == '>') break; else if (ch != '<') throw new ParsingException ("KMeans::readFrom : Parse error: '<' expected"); in >> tag; if (tag == "length") in >> length; else if (tag == "means") in >> means; else throw new ParsingException ("KMeans::readFrom : unknown argument: " + tag); if (!in) throw new ParsingException ("KMeans::readFrom : Parse error trying to build " + tag); in >> tag; if (tag != ">") throw new ParsingException ("KMeans::readFrom : Parse error: '>' expected "); } } istream &operator >> (istream &in, KMeans &mdl) { if (!isValidType(in, "KMeans")) return in; mdl.readFrom(in); return in; } }//namespace FD