Exemplo n.º 1
0
CSplittingCondition *CKDTreeMedianSplittingFactory::createSplittingCondition(DataSubset *dataSubset)
{
	DataSubset::iterator it = dataSubset->begin(); 
	
	ColumnVector minVector(inputData->getNumDimensions());
	ColumnVector maxVector(inputData->getNumDimensions());
	
	ColumnVector *vector = (*inputData)[*it];
	minVector = *vector;
	maxVector = *vector;

	it ++;

	for (; it != dataSubset->end(); it ++)
	{
		ColumnVector *vector = (*inputData)[*it];
		
		for (int i = 0; i < inputData->getNumDimensions(); i ++)
		{
			if (vector->element(i) < minVector.element(i))
			{
				minVector.element(i) = vector->element(i);
			}
			if (vector->element(i) > maxVector.element(i))
			{
				maxVector.element(i) = vector->element(i);
			}			
		}
	}
	
	maxVector = maxVector - minVector;
	
	//cout << "Dimension Width: " << maxVector.t();


	int index = 0;
	maxVector.maximum1(index);
	index --;
	
	std::vector<double> median;
	
	it = dataSubset->begin(); 
	//printf("Values : ");
	double mean = 0.0;
	for (; it != dataSubset->end(); it ++)
	{	
		ColumnVector *vector = (*inputData)[*it];
//		median.push_back(vector->element(index));
//		printf("%f ", vector->element(index));
		mean += vector->element(index);
	}
	mean /= dataSubset->size();
	//std::sort(median.begin(), median.end());

	double treshold = mean; //median[median.size() / 2];

//	cout << "Dimension " << index << " Median " << treshold << endl;
	
	return new C1DSplittingCondition(index, treshold);
}
Exemplo n.º 2
0
CvPoint3D32f Model::getRealCoordinatesFromVoxelMap(int u, int v, int w) {


    //Matrix worldMat(4, 1);
    //worldMat = 0;

    float modelContents[] = { u, v, w, 1 };
    ColumnVector modelMat(4);
    modelMat << modelContents;

    // [x,y,z,1] ?
    // [x,y,z,1] = convMat^{-1} * [u,v,w,1]

    //cvSolve(&A, &b, &x);    // solve (Ax=b) for x
//	cvSolve(convMat, &modelMat, &worldMat);

    ColumnVector worldMat = convMat.i() * modelMat;

    //printCvMat(&worldMat);

    float x = worldMat.element(0);
    float y = worldMat.element(1);
    float z = worldMat.element(2);

    //LOG4CPLUS_DEBUG(myLogger, "(u,v,w)=("<< u <<","<<v<<","<<w<<") -> (x,y,z)=(" << x <<","<<y<<","<<z<<")");

    CvPoint3D32f p = cvPoint3D32f(x, y, z);
    return p;
}
Exemplo n.º 3
0
void SpinAdapted::xsolve_AxeqB(const Matrix& a, const ColumnVector& b, ColumnVector& x)
{
  FORTINT ar = a.Nrows();
  int bc = 1;
  int info=0;
  FORTINT* ipiv = new FORTINT[ar];
  double* bwork = new double[ar];
  for(int i = 0;i<ar;++i)
    bwork[i] = b.element(i);
  double* workmat = new double[ar*ar];
  for(int i = 0;i<ar;++i)
    for(int j = 0;j<ar;++j)
      workmat[i*ar+j] = a.element(j,i);

  GESV(ar, bc, workmat, ar, ipiv, bwork, ar, info);
  delete[] ipiv;
  delete[] workmat;

  for(int i = 0;i<ar;++i)
    x.element(i) = bwork[i];

  delete[] bwork;

  if(info != 0)
  {
     pout << "Xsolve failed with info error " << info << endl;
     abort();
  }
}
Exemplo n.º 4
0
/** Utility printing function. */
void printColumnVector(ColumnVector &v, std::ostream *out, const std::string& delim) {
  int nRow = v.Nrows();
  int i = 0;
  if(out == NULL) 
    out = &cout;
  for(i = 0; i < nRow - 1; i++)
    (*out) << v.element(i) << delim;
  (*out) << v.element(i);
}
Exemplo n.º 5
0
void CActorFromContinuousActionGradientPolicy::receiveError(double critic, CStateCollection *oldState, CAction *Action, CActionData *data)
{
	gradientETraces->updateETraces(Action->getDuration());
	
	CContinuousActionData *contData = NULL;
	if (data)
	{
		contData = dynamic_cast<CContinuousActionData *>(data);
	}
	else
	{
		contData = dynamic_cast<CContinuousActionData *>(Action->getActionData());

	}

	assert(gradientPolicy->getRandomController());
	ColumnVector *noise = gradientPolicy->getRandomController()->getLastNoise();

	if (DebugIsEnabled('a'))
	{
		DebugPrint('a', "ActorCritic Noise: ");
		policyDifference->saveASCII(DebugGetFileHandle('a'));
	}

	for (int i = 0; i < gradientPolicy->getNumOutputs(); i ++)
	{
		gradientFeatureList->clear();
		gradientPolicy->getGradient(oldState, i, gradientFeatureList);
		
		gradientETraces->addGradientETrace(gradientFeatureList, noise->element(i));
	}

	gradientPolicy->updateGradient(gradientETraces->getGradientETraces(), critic * getParameter("ActorLearningRate"));
}
Exemplo n.º 6
0
double CLocalRBFRegression::doRegression(ColumnVector *vector, DataSubset *subset)
{
//	cout << "NNs for Input " << vector->t() << endl;
	
	DataSubset::iterator it = subset->begin();
/*	for (int i = 0; it != subset->end(); it ++, i++)
	{
		ColumnVector dist(*vector);
		dist = dist - *(*input)[*it];
		printf("(%d %f) ", *it, dist.norm_Frobenius());
	}
	printf("\n"); */

	ColumnVector *rbfFactors = getRBFFactors(vector, subset);
	it = subset->begin();
	double value = 0;
	for (int i = 0; it != subset->end(); it ++, i++)
	{
		value += rbfFactors->element(i) * (*output)[*it];
	}
	double sum = rbfFactors->sum();
	if (sum > 0)
	{
		value = value / sum;
	}
	//printf("Value: %f %f ", value ,sum);
	//cout << rbfFactors->t();
	return value;
}
Exemplo n.º 7
0
bool SpectClust::MaxEigen(const Matrix &M, double &maxValue, ColumnVector &MaxVec, int maxIterations) {
  double maxDelta = 1e-6;
  bool converged = false;
  int i = 0;
  int nRows = M.Ncols();
  if(M.Ncols() != M.Nrows()) 
    Err::errAbort("MaxEigen() - Can't get eigen values of non square matrices.");
  if(M.Ncols() <= 0) 
    Err::errAbort("MaxEigen() - Must have positive number of rows and columns.");
  ColumnVector V(M.Ncols());
  V = 1.0 / M.Nrows(); // any vector really...
  V = V / Norm1(V);
  MaxVec.ReSize(M.Ncols());
  for(i = 0; i < maxIterations; ++i) {
    //    MaxVec = M * V;
    multByMatrix(MaxVec, V, M);
    double delta = 0;
    double norm = sqrt(SumSquare(MaxVec));
    for(int vIx = 0; vIx < nRows; vIx++) {
      MaxVec.element(vIx) = MaxVec.element(vIx) / norm; // scale so we don't get too big.
    }
    for(int rowIx = 0; rowIx < nRows; rowIx++) {
      delta += fabs((double)MaxVec.element(rowIx) - V.element(rowIx));
    }
    if(delta < maxDelta)  
      break; // we've already converged to eigen vector.
    V = MaxVec;
  }
  if(i < maxIterations) {
    converged = true;
  }
  // calculate approximate max eigen value using Rayleigh quotient (x'*M*x/x'*x).
  Matrix num = (MaxVec.t() * M * MaxVec);
  Matrix denom =  (MaxVec.t() * MaxVec);
  maxValue = num.element(0,0) / denom.element(0,0);
  return converged;
}
Exemplo n.º 8
0
void FnirtFileWriter::common_coef_construction(const string&            fname, 
                                               const basisfield&        fieldx,
                                               const basisfield&        fieldy,
                                               const basisfield&        fieldz,
                                               const Matrix&            aff)
{
  volume4D<float>       coefs(fieldx.CoefSz_x(),fieldx.CoefSz_y(),fieldx.CoefSz_z(),3);
  vector<float>         ksp(3,1.0);
  try {
    const splinefield& f = dynamic_cast<const splinefield& >(fieldx);
    ksp[0] = float(f.Ksp_x()); ksp[1] = float(f.Ksp_y()); ksp[2] = float(f.Ksp_z());
    if (f.Order() == 2) coefs.set_intent(FSL_QUADRATIC_SPLINE_COEFFICIENTS,f.Vxs_x(),f.Vxs_y(),f.Vxs_z());
    else if (f.Order() == 3) coefs.set_intent(FSL_CUBIC_SPLINE_COEFFICIENTS,f.Vxs_x(),f.Vxs_y(),f.Vxs_z());
  }
  catch (...) {  
    try {
      const dctfield& f = dynamic_cast<const dctfield& >(fieldx);
      coefs.set_intent(FSL_DCT_COEFFICIENTS,f.Vxs_x(),f.Vxs_y(),f.Vxs_z());
      throw FnirtFileWriterException("common_coef_construction: Saving of DCT coefficients not yet implemented");
    }
    catch (...) {
      throw FnirtFileWriterException("common_coef_construction: Unknown field type");
    }
  } 
  coefs.setxdim(ksp[0]); coefs.setydim(ksp[1]); coefs.setzdim(ksp[2]);
  Matrix  qform(4,4); 
  qform = IdentityMatrix(4);
  qform(1,4) = fieldx.FieldSz_x(); 
  qform(2,4) = fieldx.FieldSz_y(); 
  qform(3,4) = fieldx.FieldSz_z();
  coefs.set_qform(NIFTI_XFORM_SCANNER_ANAT,qform);
  coefs.set_sform(NIFTI_XFORM_SCANNER_ANAT,aff);
  vector<shared_ptr<ColumnVector> > coefp(3);
  coefp[0]=fieldx.GetCoef(); coefp[1]=fieldy.GetCoef(); coefp[2]=fieldz.GetCoef();  
  for (unsigned int v=0; v<3; v++) {
    ColumnVector  c = *(coefp[v]);
    for (unsigned int k=0, vindx=0; k<fieldx.CoefSz_z(); k++) {
      for (unsigned int j=0; j<fieldx.CoefSz_y(); j++) {
        for (unsigned int i=0; i<fieldx.CoefSz_x(); i++) {
          coefs(i,j,k,v) = c.element(vindx++);
	}
      }
    }
  }

  save_orig_volume4D(coefs,fname);
}
Exemplo n.º 9
0
ColumnVector *CLocalRBFRegression::getRBFFactors(ColumnVector *vector, DataSubset *subset)
{
	DataSubset::iterator it = subset->begin();

	for (int i = 0; it != subset->end(); it ++, i ++)
	{
		double malDist = 0;
		for (int j = 0; j < vector->nrows(); j ++)
		{
			ColumnVector *data = (*input)[*it];
			malDist += pow((vector->element(j) - data->element(j)) / sigma->element(j), 2.0);
		}

		rbfFactors->element(i) = exp(- malDist / 2.0);
	}
//	cout << "RBFFactors: " << rbfFactors->t() << endl;

	return rbfFactors;
}
Exemplo n.º 10
0
bool AMC::LeastSquares::doLeastSquares(Matrix& A, double* dataCube, int width, int height, int nBands, int nEndmembers, double* abundanceFractions, int& currentX, int& currentY)
{
	Logger::stringLine("doLeastSquares ...");
	currentX = 0;
	currentY = 0;

	int row, col;
	int nrows = height, ncols = width;
	int band;
	int i, j, k, iterations;
	Matrix* A_tilde;

	int index = -1;
	double max1, max2, max_total = 0.0;
	double change, mu, deviation;

	double anglefield[255*255];

	bool bError=false;

	// here we go... 

	// ATTENTION: Internally we work here with col-oriented matrixfile,
	// but the user has to enter the spectra row-wise for his/her's
	// convenience...  That means: Don't mix row- and col-orientation
	// in source code and modules messages output!
	//
	//Spectral Matrix is stored in A now (diagonally flipped to input
	// file) Generally:    n: cols ....  for matrix A
	//             m: rows
	//                 |
	//                 |
	//

	// 1. Check matrix orthogonality:
	//    Ref: Youngsinn Sohn, Roger M. McCoy 1997: Mapping desert shrub
	//    rangeland using spectral unmixing and modeling spectral
	//    mixtrues with TM data. Photogrammetric Engineering &
	//    Remote Sensing,  Vol.63,  No6.
	//
	//
	// 2. Beside checking matrix orthogonality we find out the maximum
	//    entry of the matrix for configuring stepsize mu later.  

	double curr_angle = 0.0;

	for (i = 0; i < A.Ncols(); i++) // go columnwise through matrix
	{
		MatrixCol Avector1(&A,LoadOnEntry,i);
		max1 = Avector1.Maximum1(INT_MIN, index);  // get the max. element of this vector 
		for (j = 0; j < A.Ncols(); j++)
		{
			if (j != i)
			{
				MatrixCol Avector2(&A,LoadOnEntry,j);  // get next col in A 
				max2 = Avector2.Maximum1(INT_MIN, index); // get the max. element of this vector 
				max_total = find_max(max1, max2); // find max of matrix A 

				curr_angle = Utilities::getSpectralAngle(Avector1, Avector2);                 // check vector angle 
				anglefield[i+ j*255] = ((float)curr_angle / PI) * 180;     // save angle in degree 
			}
		}
	}

	Logger::stringLine("  -Checking linear dependencies (orthogonality check) of Matrix A");

	// print out the result 
	for (i = 0; i < A.Ncols(); i++)
	{
		for (j = 0; j < A.Ncols(); j++)
		{
			if (j != i)
			{
				char buff[500];
				// internally this is col and not row certainly 
				sprintf(buff, "		-Angle between row %d and row %d: %f degree",(i + 1), (j + 1), anglefield[i+ j*255]);
				Logger::stringLine(buff);
			}

		}
	}

	// check it 
	//Logger::stringLine("Checking Orthogonality");
	//bError = false;
	//
	//for (i = 0; i < A.Ncols(); i++)
	//{
	//	for (j = 0; j < A.Ncols(); j++)
	//	{
	//		if (j != i)
	//		{
	//			if (anglefield[i+ j*255] < 8.0) //was 8.0 in the original algorithm of GRASS
	//			{
	//				char buff[500];
	//				// internally this is col and not row certainly
	//				sprintf(buff, "		-ERROR: Spectral entries row %d: and row %d: in your matrix are linear dependent!",(i+1), (j+1));

	//				Logger::stringLine(buff);
	//				Logger::stringLine("		-You have to revise your reference spectra");
	//				bError = true;
	//			}
	//		}
	//	}
	//}
	//


	if (!bError)
	{
		Logger::stringLine("Spectral matrix is o.k. Proceeding...");
	}
	else
	{
		Logger::stringLine("doLeastSquares FAILED !");
		return false;
	}
	//if (!flag.quiet->answer)
	//Console.WriteLine("Spectra matrix is o.k. Proceeding...\n\n");



	/// Begin calculations  //////////////////////////////////////////////////////////////////////////
	// 1. Constraint SUM xi = 1
	//   add last row "1" elements to Matrix A, store in A_tilde
	//   A_tilde is one row-dimension more than A 
	A_tilde = new Matrix(A.Nrows() + 1, A.Ncols());  // memory allocation 

	for (i = 0; i < A.Nrows(); i++)   // copy row wise 
		for (j = 0; j < A.Ncols(); j++)  // copy col wise 
			A_tilde->element(i, j) = A.element(i, j);

	// fill last row with 1 elements 
	for (j = 0; j < A.Ncols(); j++)
		(*A_tilde).element(A.Nrows(), j) = (double)(GAMMA);

	/// ---- now we have an overdetermined (non-square) system 
	// We have a least square problem here: error minimization
	//                             T          -1         T
	// unknown fraction = [A_tilde * A_tilde]  * A_tilde * b
	//
	// A_tilde is the non-square matrix with first constraint in last row.
	// b is pixel vector from satellite image
	//
	// Solve this by deriving above equation and searching the
	// minimum of this error function in an iterative loop within
	// both constraints.


	// calculate the transpose of A_tilde
	Matrix A_tilde_trans = A_tilde->t();

	// initialize some values 
	// step size must be small enough for convergence  of iteration:
	//  mu=0.000001;      step size for spectra in range of W/m^2/um
	//  mu=0.000000001;   step size for spectra in range of mW/m^2/um
	//  mu=0.000001;      step size for spectra in range of reflectance
	////
	// check  max_total for number of digits to configure mu size


	ColumnVector* fraction = NULL;

	//mu = 0.000001 * pow(10, -1 * ceil(log10f((float)max_total)));
	mu = 0.000000001;

	ColumnVector startvector (A.Ncols());                // length: no. of spectra 

	ColumnVector A_times_startvector (A_tilde->Nrows());  // length: no. of bands   
	//MultipliedMatrix* A_times_startvector ;				// length: no. of bands   

	ColumnVector errorvector (A_tilde->Nrows());          // length: no. of bands   
	//SubtractedMatrix* errorvector ;

	ColumnVector temp (A_tilde->Ncols());                 // length: no. of spectra 
	//MultipliedMatrix* temp;

	Matrix A_tilde_trans_mu(A_tilde->Ncols(), A_tilde->Nrows());
	//ScaledMatrix* A_tilde_trans_mu;

	// Now we can calculate the fractions pixelwise 
	//nrows // get geographical region 
	//ncols

	Logger::sstring("mu = ");Logger::decimal(mu);Logger::endl();

	for (row = 0; row < nrows; row++)             // rows loop in images 
	{
		//if (!flag2.veryquiet->answer)
		//G_percent(row, nrows, 1);
		//
		//for (band = 0; band < nBands; band++) //get one row for all bands
		//{
		//if (G_get_map_row (cellfd[band], cell[band], row) < 0)
		//Application.Exit();
		//}

		for (col = 0; col < ncols; col++) // cols loop, work pixelwise for all bands 
		{
			currentX = col;
			currentY = row;

			// get pixel values of each band and store in b vector:
			ColumnVector b_gamma(A_tilde->Nrows());              // length: no. of bands + 1 (GAMMA)
			for (band = 0; band < nBands; band++)
				b_gamma.element(band) = (double)(dataCube[col+ row*width+band*height*width]);
			// add GAMMA for 1. constraint as last element
			b_gamma.element(nBands) = (double)(GAMMA);

			{
				//
				Logger::endl();
				Logger::sstring("  row,  col\n");
				Logger::sstring("( ");Logger::decimal(row+1);Logger::sstring(", ");Logger::decimal(col+1);Logger::sstring(") ,   pixelVector = ");//Logger::openStream() << b_gamma;Logger::closeStream();
				Logger::endl();
				Logger::stringLine("-----------------------------------------");				
			}

			// calculate fraction vector for current pixel
			// Result is stored in fractions vector
			// with second constraint: Sum x_i = 1

			change = 1000;  // initialize 
			deviation = 1000;
			iterations = 0;
			for (k = 0; k < (A_tilde->Ncols()); k++)  // no. of spectra times 
				startvector.element(k) = (double)((1.0 / A_tilde->Ncols()));

			// get start vector and initialize it with equal fractions:
			// using the neighbor pixelvector as startvector

			// solve with iterative solution: 
			while (fabs(change) > LEAST_SQUARES_TOLERANCE) //0.0001
			{
				// go a small step into direction of negative gradient
				A_times_startvector = ((*A_tilde) * startvector);
				errorvector =  *(A_times_startvector.Evaluate()) - b_gamma;
				A_tilde_trans_mu =  mu * *(A_tilde_trans.Evaluate());
				temp = *(A_tilde_trans_mu.Evaluate()) * *(errorvector.Evaluate());

				{
					LOG_DISABLE
						//Logger::getSingletonLoggerStream().width(5);
						//Logger::getSingletonLoggerStream()<<iterations<<":   startV = "<<startvector<<endl;
						//Logger::getSingletonLoggerStream()<<"     :   A*stv = "<<A_times_startvector<<endl;
						//Logger::getSingletonLoggerStream()<<"     :   errorV = "<<errorvector<<endl;
						//Logger::getSingletonLoggerStream()<<"     :   temp = "<<temp<<endl;
						LOG_END
				}
				startvector = startvector - *(temp.Evaluate()); /// update startvector 

				//if one element gets negative, set it to zero
				for (k = 0; k < (A_tilde->Ncols()); k++)  // no. of spectra times
				{
					if (startvector.element(k) < 0.0)
						startvector.element(k) = 0.0;
				}

				// Check the deviation
				MatrixCol errorCol(errorvector.Evaluate(),LoadOnEntry, 0);
				double errorVectorNorm = errorCol.Norm2();
				change = deviation - errorVectorNorm;
				deviation = errorVectorNorm;

				//LOG_START
				//Logger::getSingletonLoggerStream()<<"     :   change = "<<change<<endl;
				//Logger::getSingletonLoggerStream()<<"     :   deviation = "<<deviation<<endl;
				//Logger::LogStringLine("-----------------------------------------");
				//LOG_END

				iterations++;
			} // while

			// length: no. of spectra
			double error = deviation / MatrixCol(&b_gamma,LoadOnEntry).Norm2();
			fraction = &startvector;

			{
				Logger::decimal(iterations,5);Logger::sstring(":");
				Logger::sstring("     :   fraction = ");//Logger::openStream() << *fraction;Logger::closeStream();
				Logger::endl();
				Logger::stringLine("=====================================================");
			}

			///  end of second constraint 
			// store fractions in resulting rows of resulting files
			// (number of bands = vector dimension)

			/// write result percent 
			for (i = 0; i < A.Ncols(); i++)  // no. of spectra 
				abundanceFractions[i + col*nEndmembers+row*width*nEndmembers] = fraction->element(i);

			// save error and iterations
			//error_cell[col] = (CELL) (100 * error);
			//iter_cell[col] = iterations;

		} // columns loop 

		/// write the resulting rows into output files:
		//for (i = 0; i < A.ncols; i++)   // no. of spectra 
		//  G_put_map_row(resultfd[i], result_cell[i]);
		//if (error_fd > 0)
		// G_put_map_row(error_fd, error_cell);
		//if (iter_fd > 0)
		// G_put_map_row(iter_fd, iter_cell);

	} // rows loop 

	Logger::stringLine("doLeastSquares finished successfully !");

	currentX = 0;
	currentY = 0;
	//delete A_tilde_trans_mu;
	delete A_tilde;

	return true;
}
Exemplo n.º 11
0
CSplittingCondition *CExtraTreesSplittingConditionFactory::createSplittingCondition(DataSubset *dataSubset)
{
	ColumnVector minVector(inputData->getNumDimensions());
	ColumnVector maxVector(inputData->getNumDimensions());
	
	DataSubset::iterator it = dataSubset->begin();
	
	
	ColumnVector *vector = (*inputData)[*it];
	minVector = *vector;
	maxVector = *vector;
	
	for (; it != dataSubset->end(); it ++)
	{
		ColumnVector *vector = (*inputData)[*it];
		
		for (int i = 0; i < inputData->getNumDimensions(); i ++)
		{
			if (vector->element(i) < minVector.element(i))
			{
				minVector.element(i) = vector->element(i);
			}
			if (vector->element(i) > maxVector.element(i))
			{
				maxVector.element(i) = vector->element(i);
			}			
		}
	}
	double bestScore = 0.0;
	CSplittingCondition *bestSplit = NULL;
	unsigned int i = 0;	

	int numValid = 0;
	while (i < K || numValid == 0)
	{
		int dim = rand() % inputData->getNumDimensions();
		
		double treshold = (((double) rand()) / RAND_MAX) * (maxVector.element(dim) - minVector.element(dim)) + minVector.element(dim);
		
		CSplittingCondition *newSplit = new C1DSplittingCondition(dim, treshold);
		
		double score = getScore( newSplit, dataSubset);

		if ((score > bestScore || numValid == 0) && score <= 0.5)
		{
			bestScore = score;
			
			if (bestSplit != NULL)
			{
				delete bestSplit;
			}
			bestSplit = newSplit;
		}
		else
		{
			delete newSplit;
		}
		if (score <= 0.5)
		{
			numValid ++;
		}
		i ++;
		if (i > 100)
		{
			//printf("%d: %d %f\n", i, dataSubset->size(), score);
		}
		if (i > 200)
		{
			it = dataSubset->begin();
			printf("Could not find a split for : \n");
			for (; it != dataSubset->end(); it++)
			{
				cout << (*inputData)[*it]->t();
			}
			
			exit(0);
		}
	}
	return bestSplit;
}