示例#1
0
void AdaBoost<MatType, WeakLearner>::Classify(
    const MatType& test,
    arma::Row<size_t>& predictedLabels)
{
  arma::Row<size_t> tempPredictedLabels(test.n_cols);
  arma::mat cMatrix(classes, test.n_cols);

  cMatrix.zeros();
  predictedLabels.set_size(test.n_cols);

  for (size_t i = 0; i < wl.size(); i++)
  {
    wl[i].Classify(test, tempPredictedLabels);

    for (size_t j = 0; j < tempPredictedLabels.n_cols; j++)
      cMatrix(tempPredictedLabels(j), j) += (alpha[i] * tempPredictedLabels(j));
  }

  arma::colvec cMRow;
  arma::uword maxIndex;

  for (size_t i = 0; i < predictedLabels.n_cols; i++)
  {
    cMRow = cMatrix.unsafe_col(i);
    cMRow.max(maxIndex);
    predictedLabels(i) = maxIndex;
  }
}
示例#2
0
void cCritterViewer::setViewpoint(cVector toviewer, cVector lookatpoint, 
	BOOL trytoseewholeworld)
{
//First do some default setup stuff
	_fieldofviewangle = cCritterViewer::STARTFIELDOFVIEWANGLE;
	setSpeed(0.0);
#ifndef THREEDVECTORS //not THREEDVECTORS means the 2D case.
	_attitude = cMatrix(cVector2(1.0, 0.0), cVector2(0.0, 1.0), _position);
#else //THREEDVECTORS
	_attitude = cMatrix(-cVector::ZAXIS, -cVector::XAXIS, cVector::YAXIS, cVector::ZEROVECTOR);
		/* To get a reasonable default orientation, we arrange the viewer axes so that:  
		viewer x axis = world -z axis, viewer y axis = world -x axis, viewer z axis = world y axis.
		We pick this orientation so that if the viewer moves "forward" (along its tangent vector)
		it moves towards the world.  (We correct the mismatch between the coordinate systems in the 
		cCritterViewer::loadViewMatrix method, which has a long comment about this.)
		 Note that we will adjust _position (fourth column) later in this  call
		 with a moveTo, also we may rotate the _attitude a bit. */
	if (!_perspective) //Ortho view, simply move up.
	{
		_proportionofworldtoshow = 1.0; //Show all of a flat world.
		moveTo(lookatpoint + cCritterViewer::ORTHOZOFFSET * cVector::ZAXIS); // Get above the world
		_maxspeed = _maxspeedstandard = 0.5 * cCritterViewer::ORTHOZOFFSET; //Mimic perspective case.
	}
	else //_perspective
	{
		if (toviewer.isPracticallyZero()) //Not usable, so pick a real direction.
			toviewer = cVector::ZAXIS; //Default is straight up.
		if (trytoseewholeworld) /* Treat toviewer as a direction, and back off in that direction
			enough to see the whole world */
		{
			toviewer.normalize(); //Make it a unit vector.
			_proportionofworldtoshow = cCritterViewer::PROPORTIONOFWORLDTOSHOW;
			//Trying to show all of a world when flying around it, often leaves too big a space around it.
			Real furthestcornerdistance = pgame()->border().maxDistanceToCorner(lookatpoint); 
			Real tanangle = tan(_fieldofviewangle/2.0); /* We work with half the fov in this calculation, 
				the tanangle will be the ratio of visible distance to distance above the world,
				that is, tanangle = dr/dz, where
				Our dr is _proportionofworldtoshow * furthestcornerdistance, and
				our dz is the unknown seeallz height we need to back off to. 
				Swap tangangle and dz to get the next formula. */
			ASSERT(tanangle);
			Real seeallz = _proportionofworldtoshow * furthestcornerdistance / tanangle; 
			moveTo(lookatpoint + seeallz * toviewer);
		}
		else /*Not trytoseewholeworld.  In this case we don't normalize toviewer, instead	
			we treat it as a displacment from the lookatpoint. */
			moveTo(lookatpoint + toviewer);
		lookAt(lookatpoint);
		_maxspeed = _maxspeedstandard = 0.5 * (position()-lookatpoint).magnitude(); 
			/* Define the speed like this so it typically takes two seconds (1/0.5)
			to fly in to lookatpoint. */
		_lastgoodplayeroffset = position() - pgame()->pplayer()->position();
	}
#endif //THREEDVECTORS case
}
示例#3
0
	DLLEXPORT void vclGemm(const BlasTranspose transA, const BlasTranspose transB,
						const int m, const int n, const int k, 
						const ScalarType alpha, const ScalarType a[], const int aOffset, 
						const ScalarType b[], const int bOffset,
						const float beta, ScalarType c[], const int cOffset)
	{
		auto aPtr = &(a[0]) + aOffset;
		auto bPtr = &(b[0]) + bOffset;
		auto cPtr = &(c[0]) + cOffset;

		int lda = transA == BlasTranspose::None ? m : k;
		int ldb = transB == BlasTranspose::None ? k : n;

		int aSize1 = (transA == BlasTranspose::Transpose) ? k : m;
		int aSize2 = (transA == BlasTranspose::Transpose) ? m : k;
		
		int bSize1 = (transB == BlasTranspose::Transpose) ? n : k;
		int bSize2 = (transB == BlasTranspose::Transpose) ? k : n;

		viennacl::matrix<ScalarType> aMatrix((ScalarType*)aPtr, viennacl::MAIN_MEMORY, aSize1, aSize2);
		viennacl::matrix<ScalarType> bMatrix((ScalarType*)bPtr, viennacl::MAIN_MEMORY, bSize1, bSize2);
		viennacl::matrix<ScalarType> cMatrix(cPtr, viennacl::MAIN_MEMORY, m, n);


	};
示例#4
0
void FloodPlotColorMap::init()
{
  /// set color stops
  /// scale between 0 and 1
  unsigned int colormapLength = m_colorLevels.size();  // start and end colors
  int r, g, b;
  unsigned int i,j;
  QColor minColor, maxColor; // colors at interval ends
  double min = openstudio::minimum(m_colorLevels);
  double max = openstudio::maximum(m_colorLevels);
  if (max == min)
  {
    return;
  }
  switch (m_colorMap)
  {
    case Gray:
      double gray;
      minColor = QColor(0,0,0);
      maxColor = QColor(255,255,255);
      setColorInterval(minColor, maxColor); // end points
            for (i = 0; i < colormapLength; i++)
            {
                gray = 1.0 * i / (colormapLength - 1);
                r = (int)(255 * gray);
                g = (int)(255 * gray);
                b = (int)(255 * gray);
        addColorStop( (m_colorLevels(i) - min) / (max - min), QColor(r, g, b));
            }
      break;
    case Jet:
      Matrix cMatrix(colormapLength,3);
            unsigned int n = (int)ceil(colormapLength / 4.0);
            int nMod = 0;
            Vector  fArray(3 * n - 1);
      Vector  red(fArray.size());
            Vector  green(fArray.size());
            Vector  blue(fArray.size());


      for (i = 0; i < colormapLength; i++)
            {
        cMatrix(i, 0) = 0;
        cMatrix(i, 1) = 0;
        cMatrix(i, 2) = 0;
            }


            if (colormapLength % 4 == 1)
            {
                nMod = 1;
            }

      for (i = 0; i <fArray.size(); i++)
            {
                if (i < n)
                    fArray(i) = (float)(i + 1) / n;
                else if (i >= n && i < 2 * n - 1)
                    fArray(i) = 1.0;
                else if (i >= 2 * n - 1)
                    fArray(i) = (float)(3 * n - 1 - i) / n;
        green(i) = (int)ceil(n / 2.0) - nMod + i;
                red(i) = green(i) + n;
                blue(i) = green(i) - n;
            }

            int nb = 0;
            for (i = 0; i < blue.size(); i++)
            {
                if (blue(i) > 0)
                    nb++;
            }

            for (i = 0; i < colormapLength; i++)
            {
                for (j = 0; j < red.size(); j++)
                {
                    if (i == red(j) && red(j) < colormapLength)
                    {
                        cMatrix(i, 0) = fArray(i - red(0));
                    }
                }
                for (j = 0; j < green.size(); j++)
                {
                    if (i == green(j) && green(j) < colormapLength)
                        cMatrix(i, 1) = fArray(i - (int)green(0));
                }
                for ( j = 0; j < blue.size(); j++)
                {
                    if (i == blue(j) && blue(j) >= 0)
            cMatrix(i, 2) = fArray(fArray.size() - 1 - nb + i);
                }
            }

      // set before adding color stops
      // default from http://www.matthiaspospiech.de/blog/2008/06/16/qwt-spectrogramm-plot-with-data-arrays/
      minColor = QColor(0,0,189);
      maxColor = QColor(132,0,0);
      int bMin=256;
      int rMin=256;
            for (i = 0; i < colormapLength; i++)
            {
        r = (int)(cMatrix(i, 0) * 255);
        g = (int)(cMatrix(i, 1) * 255);
        b = (int)(cMatrix(i, 2) * 255);
        if ((r==0) && (g==0) && (b<bMin))
        {
          bMin=b;
          minColor = QColor(r,g,b);
        }
        if ((b==0) && (g==0) && (r<rMin))
        {
          rMin=r;
          maxColor = QColor(r,g,b);
        }
            }
      setColorInterval(minColor, maxColor); // end points


            for (i = 0; i < colormapLength; i++)
            {
        r = (int)(cMatrix(i, 0) * 255);
        g = (int)(cMatrix(i, 1) * 255);
        b = (int)(cMatrix(i, 2) * 255);
        addColorStop( (m_colorLevels(i) - min) / (max - min), QColor(r, g, b));
            }
      break;
  }

}
示例#5
0
// matrix multiplication
XSQUARE_MATRIX XSQUARE_MATRIX::operator *(const XSQUARE_MATRIX &i_cRHO) const
{
	XSQUARE_MATRIX cMatrix(*this);
	cMatrix *= i_cRHO;
	return cMatrix;
}
示例#6
0
//------------------------------------------------------------------------------
void MfLowRankApproximation::initialize()
{
    double dx;
    double constValue;
    double endValue;
    double constEnd;
    double epsilon;


    dx = grid.DX;
    try {
        nOrbitals = cfg->lookup("spatialDiscretization.nSpatialOrbitals");
        constEnd = cfg->lookup("meanFieldIntegrator.lowRankApproximation.constEnd");
        constValue = cfg->lookup("meanFieldIntegrator.lowRankApproximation.constValue");
        endValue = cfg->lookup("meanFieldIntegrator.lowRankApproximation.endValue");
        epsilon = cfg->lookup("meanFieldIntegrator.lowRankApproximation.epsilon");
    } catch (const SettingNotFoundException &nfex) {
        cerr << "MfLowRankApproximation::Error reading entry from config object." << endl;
        exit(EXIT_FAILURE);
    }

    int nConst = constEnd/dx;
    int constCenter = nGrid/2;
    Vxy = zeros(nGrid, nGrid);

    for(uint p=0; p<potential.size(); p++) {
        for(int i=0; i<nGrid; i++) {
            for(int j=0; j<nGrid; j++) {
                Vxy(i, j) += potential[p]->evaluate(i, j);
            }
        }
    }

    // Using a simple discretization equal to the discretization of
    // the system.
    mat h = hExactSpatial();
//    mat h = hPiecewiseLinear();
    mat Q = eye(nGrid,nGrid);

    // Using a consant weight in the center of the potential
    // and a linear decrease from the center.
    vec g = gLinear(nGrid, constCenter, nConst, constValue, endValue);
    mat C = cMatrix(g, h, dx);

    vec lambda;
    mat eigvec;
    eig_sym(lambda, eigvec, inv(C.t())*Vxy*inv(C));
    mat Ut = C.t()*eigvec;

    mat QU = inv(Q)*Ut;

    // Sorting the eigenvalues by absoulte value and finding the number
    // of eigenvalues with abs(eigenval(i)) > epsilon
    uvec indices = sort_index(abs(lambda), 1);
    M = -1;
    for(uint m=0; m <lambda.n_rows; m++) {
        cout << abs(lambda(indices(m))) << endl;
        if(abs(lambda(indices(m))) < epsilon) {
            M = m;
            break;
        }
    }
//    cout << min(abs(lambda)) << endl;
//    cout << "hei" << endl;
//    cout << lambda << endl;
//    M = 63;
    if(M < 0) {
        cerr << "MfLowRankApproximation:: no eigenvalues < epsilon found."
             << " Try setting epsilon to a higher number" << endl;
        exit(EXIT_FAILURE);
    }
    eigenval = zeros(M);
    for(int m=0; m <M; m++) {
        eigenval(m) = lambda(indices(m));
    }

    // Calculating  the U matrix
    U = zeros(nGrid, M);
    for(int m=0; m < M; m++) {
        for(uint j=0; j<h.n_rows; j++) {
            U(j,m) = 0;
            for(uint i=0; i<h.n_cols; i++) {
                U(j,m) += h(j,i)*QU(i,indices(m));
            }
        }
    }
    cout << "MfLowRankApproximation:: Trunction of eigenvalues at M = " << M << endl;

#if 1 // For testing the low rank approximation's accuracy
    mat appV = zeros(nGrid, nGrid);
    for(int i=0; i<nGrid; i++) {
        for(int j=0; j<nGrid; j++) {
            appV(i,j) = 0;
            for(int m=0; m<M; m++) {
                appV(i,j) += eigenval(m)*U(i,m)*U(j,m);
            }
        }
    }
    mat diffV = abs(Vxy - appV);
    cout << "max_err = " << max(max(abs(Vxy - appV))) << endl;

    diffV.save("../DATA/diffV.mat");
    appV.save("../DATA/Vapp.mat");
    Vxy.save("../DATA/Vex.mat");
    cout << nGrid << endl;
//    exit(EXIT_SUCCESS);

#endif
    cout << "test" << endl;
    Vm = zeros<cx_vec>(M);
    Vqr = zeros<cx_vec>(nGrid);
}