예제 #1
0
VECTOR CTempConvs::dotdiv(VECTOR& a, VECTOR& b)
{
	
	///*
	VECTOR div(a.size());
	for (int i = 0; i < (int)a.size(); i++)
	{
		// Armadillo elementwise division gives NaN for 0/0
		if ((a(i) == 0))
			div(i) = 0;
		else if (b(i) == 0)
			div(i) = INF;
		else div(i) = a(i) / b(i);
	}
	//*/

	// Alternatively, the following gives identicle results to Matlab
	/*
	VECTOR div(a.size());
	for (int i = 0; i<(int)a.size(); i++){
		div(i) = 0;
	}
	if (a.size() != b.size())
		return	div;

	for (int i = 0; i<(int)a.size(); i++)
	{
		if (a(i) == NaN)
			div(i) = NaN;
		if (a(i) == INF)
		{
			if (b(i) == INF || b(i) == NaN)
				div(i) = NaN;
			else
				div(i) = INF;
		}
		if (a(i) == 0)
		{
			if (b(i) != 0 || b(i) == INF)
				div(i) = 0;
			else
				div(i) = NaN;
		}
		else
		{
			if (b(i) == 0 || b(i) == NaN)
				div(i) = NaN;
			if (b(i) == INF)
				div(i) = 0;
			else
				div(i) = a(i) / b(i);
		}
	}
	//*/

	//a.print("a:"); b.print("b:"); div.print("div:");

	return div;

}
예제 #2
0
void Backpropagation::trainOnlineCV(Mlp& network, 
	MATRIX& trainingInputs, 
	VECTOR& trainingTargets,
	MATRIX& testInputs,
	VECTOR& testTargets)
{
	VECTOR trainingOutputs(trainingTargets.size(), 0.0);
	VECTOR testOutputs(testTargets.size(), 0.0);
	
	while(error > tolerance && testCount < maxTestCount)
	{
		VECTOR::iterator output = trainingOutputs.begin();
		VECTOR::iterator target = trainingTargets.begin();
		for(MATRIX::iterator input = trainingInputs.begin(); 
			input != trainingInputs.end(); 
			++input, ++target, ++output)
		{
			*output = network(*input);
			double err = *output - *target;
			
			getWeightUpdates(network, *input, err);
			
			applyWeightUpdates(network);
			
			++iteration;
			
			if(iteration >= maxIterations)
				break;
		}
		
		++epoch;
		
		error = mse(trainingTargets, trainingOutputs);
		
		// Early-stopping using test (cross-validation) error
		testOutputs = network(testInputs);
		testError = mse(testTargets, testOutputs);
		if(testError < minTestError)
		{
			// Preserve test error and network weights
			minTestError = testError;
			W = network.W;			
			V = network.V;
			biasW = network.biasW;
			biasV = network.biasV;
			testCount = 0;
		}
		else
		{
			++testCount;
		}
	}
	
	network.W = W;
	network.V = V;
	network.biasW = biasW;
	network.biasV = biasV;
	testError = minTestError;
}
예제 #3
0
//////////////////////////////////////////////////////////////////////
// scale a vector
//////////////////////////////////////////////////////////////////////
VECTOR operator*(const Real& scalar, const VECTOR& x) 
{
  VECTOR z(x.size());

  for (int i = 0; i < x.size(); i++)
    //z(i) = x(i) * scalar;
    z(i) = x[i] * scalar;

  return z;
}
예제 #4
0
//////////////////////////////////////////////////////////////////////
// subtract two vectors
//////////////////////////////////////////////////////////////////////
VECTOR operator-(const VECTOR& x, const VECTOR& y) 
{
  VECTOR z(x.size());

  for (int i = 0; i < x.size(); i++)
    //z(i) = x(i) - y(i);
    z(i) = x[i] - y[i];

  return z;
}
예제 #5
0
//////////////////////////////////////////////////////////////////////
// compute the 2 norm
//////////////////////////////////////////////////////////////////////
Real operator^(const VECTOR& x, const VECTOR& y)
{
  assert(x.size() == y.size());
#if __APPLE__
#ifdef SINGLE_PRECISION
	return cblas_sdot (x.size(), x.dataConst(), 1, y.dataConst(), 1);
#else
	return cblas_ddot (x.size(), x.dataConst(), 1, y.dataConst(), 1);
#endif
#else
  return x * y;
#endif
}
예제 #6
0
bool CheckEqualVector(
    const VECTOR& rActualVector
  , const VECTOR& rExpectedVector)
{
  // Check that the two array are of equal length
  auto ret = rActualVector.size() == rExpectedVector.size();
  if (ret) {
    for (auto i = 0u; i < rExpectedVector.size(); ++i) {
      ret = ret && rActualVector[i] == rExpectedVector[i];
      if (!ret) break;
    }
  }
  else {
    std::cout << "Vectors have different size!" << std::endl;
  }
  return ret;
}
예제 #7
0
		/**
		* Re-order a vector of labels according to the permutation.
		* perm[i] = k means that label L(i) initially at position i must be put at pos k.
		*
		* see getPermute() for the opposite transformation.
		**/
		template<typename VECTOR> VECTOR getAntiPermute(const VECTOR & labels) const
			{
			const size_t l = labels.size();
			MTOOLS_INSURE(_perm.size() == l);
			VECTOR res(l);
			for (size_t i = 0; i < l; i++) { res[_perm[i]] = labels[i]; }
			return res;
			}
예제 #8
0
//////////////////////////////////////////////////////////////////////
// Vector-matrix multiply
//////////////////////////////////////////////////////////////////////
VECTOR operator*(VECTOR& x, MATRIX& A)
{
  assert(A.rows() == x.size());

  VECTOR y(A.cols());
  for (int i = 0; i < A.cols(); i++)
    for (int j = 0; j < A.rows(); j++)
      y[i] += A(j, i) * x(j);
  return y;
}
예제 #9
0
bool CheckCloseVector(
    const VECTOR& rActualVector
  , const VECTOR& rExpectedVector
  , double delta)
{
  // Check that the two array are of equal length
  auto ret = rActualVector.size() == rExpectedVector.size();
  if (ret) {
    for (auto i = 0u; i < rExpectedVector.size(); ++i) {
      ret = ret && std::abs(rActualVector[i] - rExpectedVector[i]) <=
          std::abs(rExpectedVector[i]) * delta;
      if (!ret) break;
    }
  }
  else {
    std::cout << "Vectors have different size!" << std::endl;
  }
  return ret;
}
예제 #10
0
bool is_sorted_and_unique(const VECTOR& vec, COMPARE compare)
{
    bool sorted_and_unique = true;
    for(size_t i=1; i<vec.size(); ++i) {
        if (!compare(vec[i-1],vec[i])) {
            sorted_and_unique = false;
        }
    }
    return sorted_and_unique;
}
예제 #11
0
// Calculate equal categories
void MgFeatureNumericFunctions::GetEqualCategories(VECTOR &values, int numCats, double dataMin, double dataMax, VECTOR &distValues)
{
    // Expected categories should be more than zero
    if (numCats <= 0)
    {
        STRING message = MgServerFeatureUtil::GetMessage(L"MgInvalidComputedProperty");

        MgStringCollection arguments;
        arguments.Add(message);
        throw new MgFeatureServiceException(L"MgServerSelectFeatures.GetEqualCategories", __LINE__, __WFILE__, &arguments, L"", NULL);
    }

    // find the range of the data values
    double min = DoubleMaxValue;
    double max = -DoubleMaxValue;

    int cnt = (int)values.size();
    if (cnt <= 0) { return; } // Nothing to do, we just send back Property Definition to clients from reader

    for (int i=0; i < cnt; i++)
    {
        double val = values[i];

        if (val > max)
            max = val;
        if (val < min)
            min = val;
    }

    // expand the range a little to account for numerical instability
    double delta = 0.0001 * (max - min);
    min -= delta;
    max += delta;
    // but don't let the values extend beyond the data min/max
    if (min < dataMin)
        min = dataMin;
    if (max > dataMax)
        max = dataMax;

    // This method ignores dataMin and dataMax.  A different "Equal" distribution
    // might ignore the actual data values and create categories based on dataMin
    // and dataMax when those values are not +/- infinity.

    // fill in the categories
    distValues.push_back(min);
    delta = (max - min) / (double)numCats;
    for (int i=1; i<numCats; i++)
    {
        double nextval = distValues[i-1] + delta;
        distValues.push_back(nextval);
    }
    distValues.push_back(max);
}
예제 #12
0
VECTOR<TYPE>::VECTOR(const VECTOR& copy)
{
	VECTOR();
	if(this != &copy)
	{
		this->reserve(copy.capacity());
		for(unsigned int i = 0; i < copy.size(); i++)
		{
			this->mpData[i] = copy[i];
		}
	}
}
예제 #13
0
 void unwrap2PiSequence(VECTOR &x)
 {
     const size_t N=x.size();
     for (size_t i=0;i<N;i++)
     {
         mrpt::math::wrapToPiInPlace(x[i]); // assure it's in the -pi,pi range.
         if (!i) continue;
         double Ap = x[i]-x[i-1];
         if (Ap>M_PI)  x[i]-=2.*M_PI;
         if (Ap<-M_PI) x[i]+=2.*M_PI;
     }
 }
Vec RQR_Multiply(const VECTOR &v,
                 const SparseKalmanMatrix &RQR,
                 const SparseVector &Z,
                 double H) {
    int state_dim = Z.size();
    if(v.size() != state_dim + 2) {
        report_error("wrong sizes in RQR_Multiply");
    }
    // Partition v = [eta, epsilon, 0]
    ConstVectorView eta(v, 0, state_dim);
    double epsilon = v[state_dim];

    // Partition this
    Vec RQRZ = RQR * Z.dense();
    double ZRQRZ_plus_H = Z.dot(RQRZ) + H;

    Vec ans(v.size());
    VectorView(ans, 0, state_dim) = (RQR * eta).axpy(RQRZ, epsilon);
    ans[state_dim] = RQRZ.dot(eta) + ZRQRZ_plus_H * epsilon;
    return ans;
}
예제 #15
0
// Calculate Standard Deviation for the values
void MgFeatureNumericFunctions::GetStandardDeviation(VECTOR &values, VECTOR &distValues)
{
    double mean = 0;

    int cnt = (int)values.size();
    if (cnt <= 0) { return; } // Nothing to do, we just send back Property Definition to clients from reader

    double min = DoubleMaxValue;
    double max = -DoubleMaxValue;

    for (int i=0; i < cnt; i++)
    {
        double val = values[i];

        if (val > max)
            max = val;

        if (val < min)
            min = val;

        mean += val;
    }

    // expand min and max a little to account for numerical instability
    double delta = 0.0001 * (max - min);
    min -= delta;
    max += delta;

    // compute the mean, variance and standard deviation
    double count = (double)cnt;  // (guaranteed to be > 0)
    mean /= count;
    double variance = 0;

    for (int i=0; i < cnt; i++)
    {
        variance += (values[i] - mean) * (values[i] - mean);
    }

    double deviation = sqrt((double)(variance / count));

    // Set the base date as min date
    if (m_type == MgPropertyType::DateTime)
    {
        deviation += min;
    }

    distValues.push_back(deviation);

    return;
}
  void TridiagonalMatrix<C>::apply_transposed(const VECTOR& x, VECTOR& Mtx) const
  {
    assert(Mtx.size() == rowdim_);
    
    for (typename TridiagonalMatrix<C>::size_type i(0); i < rowdim_; i++)
      {
 	Mtx[i] = b_[i] * x[i];
	
	if (i > 0)
	  Mtx[i] += c_[i-1] * x[i-1];

	if (i+1 < rowdim_)
	  Mtx[i] += a_[i] * x[i+1];
      }
  }
예제 #17
0
void Backpropagation::trainBatch(Mlp& network, 
	MATRIX& trainingInputs, 
	VECTOR& trainingTargets,
	MATRIX& testingInputs,
	VECTOR& testingTargets)
{
	VECTOR trainingOutputs (trainingTargets.size(), 0.0);
	VECTOR meanInput = ave(trainingInputs);		
	while (epoch < maxEpochs)
	{
		trainingOutputs  = network(trainingInputs);
		error = sse(trainingOutputs, trainingTargets);

		getWeightUpdates(network, meanInput, error);
		applyWeightUpdates(network);
		
		++epoch;
	}
}
예제 #18
0
//////////////////////////////////////////////////////////////////////
// sparse matrix-vector multiply
//////////////////////////////////////////////////////////////////////
VECTOR operator*(const SPARSE_MATRIX& A, const VECTOR& x) 
{
  assert(A.cols() == x.size());

  VECTOR y(A.rows());

  // iterate through all the entries
  map<pair<int,int>, Real>::const_iterator iter;
  const map<pair<int,int>, Real>& data = A.matrix();
  for (iter = data.begin(); iter != data.end(); iter++)
  {
    const pair<int,int> index = iter->first;
    const Real value = iter->second;
    int i = index.first;
    int j = index.second;
    y(i) += x[j] * value;
  }
  return y;
}
예제 #19
0
// Calculate average
void MgFeatureNumericFunctions::GetMeanValue(VECTOR &values, VECTOR &distValues)
{
    double mean = 0;

    int cnt = (int)values.size();
    if (cnt <= 0) { return; } // Nothing to do, we just send back Property Definition to clients from reader

    for (int i=0; i < cnt; i++)
    {
        double val = values[i];
        mean += val;
    }

    // compute the mean, variance and standard deviation
    double count = (double)cnt;  // (guaranteed to be > 0)
    mean /= count;

    distValues.push_back(mean);

    return;
}
예제 #20
0
void Backpropagation::trainOnline(Mlp& network, MATRIX& inputs, VECTOR& targets)
{
	VECTOR outputs(targets.size(), 0.0);
	while(iteration < maxIterations)
	{
		VECTOR::iterator output = outputs.begin();
		VECTOR::iterator target = targets.begin();
		for(MATRIX::iterator input = inputs.begin(); 
			input != inputs.end(); 
			++input, ++target, ++output)
		{
			*output = network(*input);
			double err = *output - *target;
			
			getWeightUpdates(network, *input, err);
			
			applyWeightUpdates(network);
			
			++iteration;
		}
		++epoch;
	}
}
예제 #21
0
파일: main.cpp 프로젝트: jasondegraw/rwPrj
int main()
{
    QFile prj(":prj/barracks-scaled11-elements.prj");
    bool ignoreSystemZones = true;
    bool ignoreAmbientZone = true;
    std::cout << "Opening file... ";
    if(!prj.open(QFile::ReadOnly))
    {
        std::cout << " failed." << std::endl;
        return EXIT_FAILURE;
    }
    std::cout << " succeeded." << std::endl << std::endl;
    QTextStream stream(&prj);

    prj::Reader input(&stream);

    prj::Model airflowModel(input);

    std::cout << "CONTAM version: " + airflowModel.version() << std::endl;
    std::cout << "SketchPad dimensions: " << airflowModel.skwidth() << " cells by "
              << airflowModel.skheight() << " cells" << std::endl;
    std::cout << "SketchPad scale: " << TO_STRING(airflowModel.scale()) << " m" <<std::endl;

    std::cout << "SketchPad scale: " << TO_STRING(airflowModel.scale()) << " m" <<std::endl;

    std::cout << "Active contaminants: " << airflowModel.contaminants().size() << std::endl;
    for(unsigned int i=0;i<airflowModel.contaminants().size();i++)
    {
        int nr = airflowModel.contaminants()[i];
        std::cout << '\t' << nr << ": " << airflowModel.species()[nr-1].name() << std::endl;
    }

    std::cout << std::endl << "Day schedules: " << airflowModel.daySchedules().size()
              << std::endl;

    std::cout << std::endl << "Week schedules: " << airflowModel.weekSchedules().size()
              << std::endl;

    std::cout << std::endl << "Wind pressure profiles: "
              << airflowModel.windPressureProfiles().size()
              << std::endl;
    for(unsigned int i=1;i<=airflowModel.windPressureProfiles().size();i++)
    {
        std::cout << '\t' << i << ": " << airflowModel.windPressureProfiles()[i-1].name()
                  << std::endl;
    }

    VECTOR<prj::Path> paths = airflowModel.paths();
    std::cout << std::endl << "Paths: " << paths.size() << std::endl;

    QFile fp("prj.dot");
    if (fp.open(QFile::WriteOnly))
    {
        QTextStream stream(&fp);
        stream << "graph {\n";
        for(unsigned int i=0;i<paths.size();i++)
        {
            int pzm = paths[i].pzm();
            int pzn = paths[i].pzn();
            if(pzm<0)
            {
                if(ignoreAmbientZone)
                {
                    continue;
                }
                pzm=0;
            }
            if(ignoreSystemZones  && airflowModel.zones()[pzm-1].system())
            {
                continue;
            }
            if(pzn<0)
            {
                if(ignoreAmbientZone)
                {
                    continue;
                }
                pzn=0;
            }
            if(ignoreSystemZones  && airflowModel.zones()[pzn-1].system())
            {
                continue;
            }
            stream << pzn << " -- " << pzm << '\n';
        }
        stream << "}";
    }
    fp.close();

    /*
    VECTOR<prj::CvfDat> cvf = airflowModel.getCvfDat();
    std::cout << std::endl << "CvfDat elements: " << cvf.size() << std::endl;
    for(unsigned int i=0;i<cvf.size();i++)
    {
        std::cout << '\t' << i+1 << ": " << cvf[i].name() << std::endl;
    }

    std::cout << std::endl << "Simple air handling systems: "
              << airflowModel.ahs().size()
              << std::endl;
    for(unsigned int i=1;i<=airflowModel.ahs().size();i++)
    {
        std::cout << '\t' << i << ": " << airflowModel.ahs()[i-1].name()
                  << std::endl;
    }

    VECTOR<prj::Zone> zones = airflowModel.zones();
    std::cout << std::endl << "Zones: " << zones.size() << std::endl;
    for(unsigned int i=0;i<zones.size();i++)
    {
        std::cout << '\t' << i+1 << ": " << zones[i].name() << std::endl;
    }
    */

    std::cout << std::endl << "Done." << std::endl;

    //std::cout << airflowModel.toString();
    //prj::PlrTest1 plr;
    //plr.setName("TEST");
    //airflowModel.addAirflowElement(plr);
/*
    QFile fp("out.prj");
    if (fp.open(QFile::WriteOnly))
    {
        QTextStream stream(&fp);
        stream << QString().fromStdString(airflowModel.toString());
        fp.close();
    }
    else
    {
        std::cout << "Failed to write 'out.prj'" << std::endl;
        return EXIT_FAILURE;
    }
*/
    return EXIT_SUCCESS;
}
예제 #22
0
파일: Estimator.cpp 프로젝트: Re-bort/NNDK
Estimator2D<Mahalanobis2D>::Estimator2D(const VECTOR& X, const VECTOR& Y)
    : X_(&X), Y_(&Y)
{
    setBandwidth(scottBandwidth(X.size(), 2.0, 1.0));
    setCovariance(var(X), var(Y), covar(X, Y));
}
예제 #23
0
void SystematicSampler::sortKey(VECTOR x)
{
    sortedKey_ =  std::sequence<unsigned> (0, x.size());
    std::sort(x, sortedKey_);
}
예제 #24
0
int run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Vector_Test"));

  VECTOR vector;
  size_t i;

  for (i = 0; i < TOP; ++i)
    vector.push_back (i);

  ACE_TEST_ASSERT (vector.size () == TOP);
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Size: %d\n"),
              vector.size ()));

  for (i = 0; i < TOP; ++i)
    ACE_TEST_ASSERT (vector[i] == i);

  // Test to be sure the iterator gets the correct count and entries.
  ITERATOR iter (vector);
  DATA *p_item = 0 ;
  size_t iter_count = 0;
  while (!iter.done ())
    {
      if (iter.next (p_item) == 0)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("Fail to get value on iter pass %d\n"),
                    iter_count));
      if (*p_item != iter_count)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("Iter pass %d got %d\n"),
                    iter_count, *p_item));
      iter_count++;
      iter.advance();
    }
  if (iter_count != TOP)
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("Iterated %d elements; expected %d\n"),
                iter_count, TOP));

  for (i = 0; i < (TOP - LEFT); ++i)
    vector.pop_back ();

  ACE_TEST_ASSERT (vector.size () == LEFT);
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Size: %d\n"),
              vector.size ()));

  for (i = 0; i < LEFT; ++i)
    {
      ACE_TEST_ASSERT (vector[i] == i);
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("vector[%d]:%d\n"),
                  i, vector[i]));
    }

  vector.resize(RESIZE, 0);
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("After resize\n")));

  for (i = 0; i < RESIZE ; ++i)
    {
      // The original vector of size LEFT must have the same original contents
      // the new elements should have the value 0 (this value is passed as
      // second argument of the resize() call.
      if (i < LEFT)
        {
          ACE_TEST_ASSERT (vector[i] == i);
        }
      else
        {
          ACE_TEST_ASSERT (vector[i] == 0);
        }
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("vector[%d]:%d\n"),
                  i, vector[i]));
    }

  vector.clear ();
  ACE_TEST_ASSERT (vector.size () == 0);
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Size: %d\n"),
              vector.size ()));

  // test resize (shrink and enlarge with buffer realloc)
  VECTOR vector2;

  // should be around 32
  size_t boundary = vector2.capacity ();

  // we fill everything up with 1
  // 1, 1, 1, 1, 1, 1, 1, 1,
  // 1, 1, 1, 1, 1, 1, 1, 1,
  // 1, 1, 1, 1, 1, 1, 1, 1,
  // 1, 1, 1, 1, 1, 1, 1, 1,
  for (i = 0; i < boundary; ++i)
    vector2.push_back (FILLER1);

  // we throw almost everything away.
  vector2.resize (1, 0);

  // we fill up with another pattern
  // 1, 2, 2, 2, 2, 2, 2, 2,
  // 2, 2, 2, 2, 2, 2, 2, 2,
  // 2, 2, 2, 2, 2, 2, 2, 2,
  // 2, 2, 2, 2, 2, 2, 2, 2,
  // 2,
  for (i = 0; i < boundary; ++i)
    vector2.push_back (FILLER2);

  // now we check the result
  ACE_TEST_ASSERT (vector2[0] == FILLER1);
  for (i = 0; i < boundary; ++i)
    ACE_TEST_ASSERT (vector2[i+1] == FILLER2);

  VECTOR v1;
  VECTOR v2;
  v1.push_back (1);
  v2.push_back (1);
  v1.push_back (2);
  v2.push_back (2);
  if (v1 != v2)
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("Inequality test failed!\n")));
  if (!(v1 == v2))
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("Equality test failed!\n")));

  v1.push_back (3);
  if (v1.size () != 3)
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("v1's size should be 3\n")));

  v1.swap (v2);
  if (v2.size () != 3)
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("v2's size should be 3\n")));

  ACE_END_TEST;

  return 0;
}
예제 #25
0
//////////////////////////////////////////////////////////////////////
// Output directly to the files that will be sent to SVD
//////////////////////////////////////////////////////////////////////
void FLUID_3D_MIC::appendStreams() const
{
  TIMER functionTimer(__FUNCTION__);
  assert(_snapshotPath.size() > 0);

  cout << " Appending to streams in path " << _snapshotPath.c_str() << " ... " << flush;
  vector<int> finalRows(6);
  vector<int> finalCols(6);

  // read in the initial file dims
  vector<string> filenames;
  filenames.push_back(_snapshotPath + string("velocity.final.matrix.dims"));
  filenames.push_back(_snapshotPath + string("velocity.preproject.matrix.dims"));
  filenames.push_back(_snapshotPath + string("velocity.prediffuse.matrix.dims"));
  filenames.push_back(_snapshotPath + string("velocity.preadvect.matrix.dims"));
  filenames.push_back(_snapshotPath + string("velocity.iop.matrix.dims"));
  filenames.push_back(_snapshotPath + string("pressure.matrix.dims"));
  for (int x = 0; x < filenames.size(); x++)
  {
    FILE* dimsFile = fopen(filenames[x].c_str(), "rb");
    if (dimsFile == NULL) continue;
    int rows, cols;

    // write dimensions
    fread((void*)&rows, sizeof(int), 1, dimsFile);
    fread((void*)&cols, sizeof(int), 1, dimsFile);
    fclose(dimsFile);

    finalRows[x] = rows;
    finalCols[x] = cols;
  }

  FILE* fileFinal;
  FILE* filePreproject;
  FILE* filePrediffuse;
  FILE* filePreadvect;
  FILE* fileIOP;
  FILE* fileNeumannIOP;
  FILE* filePressure;

  string velocityFinalFile = _snapshotPath + string("velocity.final.matrix.transpose");
  string velocityPreprojectFile = _snapshotPath + string("velocity.preproject.matrix.transpose");
  string velocityPrediffuseFile = _snapshotPath + string("velocity.prediffuse.matrix.transpose");
  string velocityPreadvectFile = _snapshotPath + string("velocity.preadvect.matrix.transpose");
  string velocityIOPFile = _snapshotPath + string("velocity.iop.matrix.transpose");
  string neumannIOPFile = _snapshotPath + string("neumann.iop.matrices");
  string pressureFile = _snapshotPath + string("pressure.matrix.transpose");

  // if there's a Neumann matrix at all, it's using IOP
  bool usingIOP = (_neumannIOP.rows() > 0);

  fileFinal      = fopen(velocityFinalFile.c_str(), "ab");
  filePreproject = fopen(velocityPreprojectFile.c_str(), "ab");
  filePrediffuse = fopen(velocityPrediffuseFile.c_str(), "ab");
  filePreadvect  = fopen(velocityPreadvectFile.c_str(), "ab");
  fileIOP        = fopen(velocityIOPFile.c_str(), "ab");
  if (usingIOP)
    fileNeumannIOP = fopen(neumannIOPFile.c_str(), "ab");
  filePressure   = fopen(pressureFile.c_str(), "ab");
    
  VECTOR velocity = _velocity.peelBoundary().flattened();
  int cols = velocity.size();
  assert(cols % 3 == 0);
  int scalarCols = cols / 3;

  // set the rows and cols, in case this is the first time
  for (int x = 0; x < 5; x++)
    if (finalCols[x] == 0)
      finalCols[x] = cols;
  if (finalCols[5] == 0)
    finalCols[5] = scalarCols;

  if (velocity.norm2() > 1e-7)
  {
    fwrite((void*)(velocity.data()), sizeof(double), cols, fileFinal);
    finalRows[0]++;
  }

  velocity = _preprojection.peelBoundary().flattened();
  if (velocity.norm2() > 1e-7)
  {
    fwrite((void*)(velocity.data()), sizeof(double), cols, filePreproject);
    finalRows[1]++;
  }
  
  velocity = _prediffusion.peelBoundary().flattened();
  if (velocity.norm2() > 1e-7)
  {
    fwrite((void*)(velocity.data()), sizeof(double), cols, filePrediffuse);
    finalRows[2]++;
  }
  
  velocity = _preadvection.peelBoundary().flattened();
  if (velocity.norm2() > 1e-7)
  {
    fwrite((void*)(velocity.data()), sizeof(double), cols, filePreadvect);
    finalRows[3]++;
  }
 
  if (usingIOP)
  { 
    velocity = _postIOP.peelBoundary().flattened();
    if (velocity.norm2() > 1e-7)
    {
      fwrite((void*)(velocity.data()), sizeof(double), cols, fileIOP);
      finalRows[4]++;
    }
  }

  VECTOR scalar = _pressure.peelBoundary().flattened();
  if (scalar.norm2() > 1e-7)
  {
    fwrite((void*)(scalar.data()), sizeof(double), scalarCols, filePressure);
    finalRows[5]++;
  }

  scalar = _divergence.peelBoundary().flattened();
  if (scalar.norm2() > 1e-7)
  {
    fwrite((void*)(scalar.data()), sizeof(double), scalarCols, filePressure);
    finalRows[5]++;
  }

  if (usingIOP)
    _neumannIOP.write(fileNeumannIOP);

  fclose(fileFinal);
  fclose(filePreproject);
  fclose(filePrediffuse);
  fclose(filePreadvect);
  fclose(fileIOP);
  if (usingIOP)
    fclose(fileNeumannIOP);
  fclose(filePressure);

  // write out the dimensions of the matrix to a different file
  for (int x = 0; x < filenames.size(); x++)
  {
    FILE* dimsFile = fopen(filenames[x].c_str(), "wb");

    // write dimensions
    fwrite((void*)&finalRows[x], sizeof(int), 1, dimsFile);
    fwrite((void*)&finalCols[x], sizeof(int), 1, dimsFile);
    fclose(dimsFile);
  }
  cout << " done. " << endl;
}
예제 #26
0
//-------------------------------------------------------------------------
// Jenks' Optimization Method
//
//-------------------------------------------------------------------------
void MgFeatureNumericFunctions::GetJenksCategories(  VECTOR &inputData, int numPartsRequested,
                                                 double dataMin, double dataMax,
                                                 VECTOR &distValues )
{
    // numPartsRequested // 2 - 10; 5 is good
    int i = 0;  // index for numObservations (may be very large)
    int j = 0;  // index for numPartsRequested (about 4-8)
    int k = 0;

    // Sort the data values in ascending order
    std::sort(inputData.begin(), inputData.end());

    int numObservations = (int)inputData.size();  // may be very large
    // Possible improvement: Rework the code to use normal 0 based arrays.
    // Actually it doesn't matter much since we have to create two
    // matrices that themselves use more memory than the local copy
    // of inputData.
    //
    // In order to ease the use of original FORTRAN and the later BASIC
    // code, I will use 1 origin arrays and copy the inputData into
    // a local array;
    // I'll dimension the arrays one larger than necessary and use the
    // index values from the original code.
    //
    // The algorithm must calculate with floating point values.
    // If more optimization is attempted in the future, be aware of
    // problems with calculations using mixed numeric types.

    std::vector<double> data;
    data.push_back(0);  // dummy value at index 0
    std::copy(inputData.begin(), inputData.end(), std::back_inserter(data));
    // copy from parameter inputData so that data index starts from 1

    // Note that the Matrix constructors initialize all values to 0.
    // mat1 contains integer values used for indices into data
    // mat2 contains floating point values of data and bigNum
    MgMatrix<int>     mat1(numObservations + 1, numPartsRequested + 1);
    MgMatrix<double>  mat2(numObservations + 1, numPartsRequested + 1);

//  const double bigNum = 1e+14; // from original BASIC code;
//  const double bigNum = std::numeric_limits<double>::max();
    const double bigNum = DBL_MAX;   // compiler's float.h

    for (i = 1; i <= numPartsRequested; ++i)
    {
        mat1.Set(1, i, 1);
        for (j = 2; j <= numObservations; ++j)
        {
            mat2.Set(j, i, bigNum);
        }
    }

    std::vector<int> classBounds;
    classBounds.push_back(-2);  // dummy value
    for (i = 1; i <= numPartsRequested; ++i)
    {
        classBounds.push_back(-1);
    }

    for (int L = 2; L <= numObservations; ++L)
    {
        double s1 = 0;
        double s2 = 0;
        double v = 0;
        int w = 0;

        for (int m = 1; m <= L; ++m)
        {
            int i3 = L - m + 1;
            double val = data[i3];
            s2 += (double(val) * double(val));
            // if datatype of val is ever allowed to be same as template
            // parameter T, make sure multiplication is done in double.
            s1 += val;
            ++w;
            v = s2 - ((s1 * s1) / w);
            int i4 = i3 - 1;

            if (i4 > 0)
            {
                for (j = 2; j <= numPartsRequested; ++j)
                {
                    double tempnum = v + mat2.Get(i4, j - 1);
                    if (double(mat2.Get(L, j)) >= tempnum)
                    {
                        mat1.Set(L, j, i3);
                        mat2.Set(L, j, tempnum);
                    }
                }
            }
        }

        mat1.Set(L, 1, 1);
        mat2.Set(L, 1, v);
    }

    k = numObservations;
    for (j = numPartsRequested; j >= 1; --j)
    {
        if (k >= 0 && k <= numObservations)
        {
            classBounds[j] = mat1.Get(k, j);
            k = mat1.Get(k, j) - 1;
        }
    }

    std::vector<int> indices;

    indices.push_back(0);
    for (i = 2; i <= numPartsRequested; ++i)
    {
        int index = classBounds[i] - 1;
        if (index > indices.back())
        {
            indices.push_back(index);
        }
    }

    FixGroups(inputData, indices);

    double val = 0.0;
    int index = 0;
    int totIndex = (int)indices.size();

    for (int i = 1; i < totIndex; ++i)
    {
        index = indices[i] - 1;

        val = inputData[index];
        distValues.push_back(val);
    }

    index = numObservations - 1;
    val = inputData[index];
    distValues.push_back(val);

    int retCnt = (int)distValues.size();
    int inCnt = (int)inputData.size();

    if (retCnt > 0 && inCnt > 0)
    {
        if (!doubles_equal(distValues[0],inputData[0]))
        {
            distValues.insert(distValues.begin(), inputData[0]);
        }
    }

    return;
}
예제 #27
0
// Calculate Quantile Distribution for the values
void MgFeatureNumericFunctions::GetQuantileCategories(  VECTOR &values, int numCats,
                                                    double dataMin, double dataMax,
                                                    VECTOR &distValues )
{
    // Expected categories should be more than zero
    if (numCats <= 0)
    {
        STRING message = MgServerFeatureUtil::GetMessage(L"MgInvalidComputedProperty");

        MgStringCollection arguments;
        arguments.Add(message);
        throw new MgFeatureServiceException(L"MgServerSelectFeatures.GetEqualCategories", __LINE__, __WFILE__, &arguments, L"", NULL);
    }

    int count = (int)values.size();
    if (count <= 0) { return; } // Nothing to do, we just send back Property Definition to clients from reader

    // Sort the data values in ascending order
    std::sort(values.begin(), values.end());

    // How many go into each full bucket?

    int perBucket = ROUND((double)count/(double)numCats);
    if (perBucket * numCats > count)
        perBucket--;

    // How many buckets are full, and how many are missing one?
    int nearlyFullBuckets = numCats - (count - perBucket * numCats);
    int fullBuckets = numCats - nearlyFullBuckets;

    // expand min and max a little to account for numerical instability
    double delta = 0.0001 * (values[count-1] - values[0]);
    double* categories = new double[numCats+1];

    // the first and last categories are limited by the data method limits
    categories[0] = values[0] - delta;
    if (categories[0] < dataMin)
        categories[0] = dataMin;

    categories[numCats] = values[count-1] + delta;
    if (categories[numCats] > dataMax)
        categories[numCats] = dataMax;

    // Mix full and nearly-full buckets to fill in the categories between the ends.
    int indexOfLast = -1;
    for ( int i = 1; i<numCats; i++)
    {
        bool doingSmallBucket = (nearlyFullBuckets > fullBuckets);
        // find the index of the last element we want in this bucket
        indexOfLast += (doingSmallBucket ? perBucket : perBucket + 1);
        // make category value be halfway between that element and the next
        categories[i] = 0.5 * (values[indexOfLast] + values[indexOfLast+1]);

        // Decrement count of correct bucket type.
        if (doingSmallBucket)
            nearlyFullBuckets--;
        else
            fullBuckets--;
    }

    for (int kk = 0; kk < numCats+1; kk++)
    {
        distValues.push_back(categories[kk]);
    }

    delete[] categories; // Delete the memory allocated before
}
예제 #28
0
// Calculate Standard Deviation for the values
void MgFeatureNumericFunctions::GetStandardDeviationCategories( VECTOR &values, int numCats,
                                                                double dataMin, double dataMax,
                                                                VECTOR &distValues)
{
    // Expected categories should be more than zero
    if (numCats <= 0)
    {
        STRING message = MgServerFeatureUtil::GetMessage(L"MgInvalidComputedProperty");

        MgStringCollection arguments;
        arguments.Add(message);
        throw new MgFeatureServiceException(L"MgServerSelectFeatures.GetEqualCategories", __LINE__, __WFILE__, &arguments, L"", NULL);
    }

    // collect information about the data values
    double min = DoubleMaxValue;
    double max = -DoubleMaxValue;
    double mean = 0;

    int cnt = (int)values.size();
    if (cnt <= 0) { return; } // Nothing to do, we just send back Property Definition to clients from reader

    for (int i=0; i < cnt; i++)
    {
        double val = values[i];

        if (val > max)
            max = val;
        if (val < min)
            min = val;
        mean += val;
    }

    // expand min and max a little to account for numerical instability
    double delta = 0.0001 * (max - min);
    min -= delta;
    max += delta;

    // compute the mean, variance and standard deviation
    double count = (double)cnt;  // (guaranteed to be > 0)
    mean /= count;
    double variance = 0;

    for (int i=0; i < cnt; i++)
    {
        double val = values[i];
        variance += (val - mean) * (val - mean);
    }

    double deviation = sqrt(variance / count);

    // fill in the middle category/categories
    double* cats = new double[numCats+1];
    int midCat, highMidCat;
    if (numCats % 2 == 0)
    {
        midCat = numCats / 2;
        highMidCat = midCat;
        cats[midCat] = mean;
    }
    else
    {
        midCat = (numCats - 1) / 2;
        highMidCat = midCat + 1;
        cats[midCat] = mean - 0.5 * deviation;
        cats[highMidCat] = mean + 0.5 * deviation;
    }

    // fill in the other categories
    for (int i=midCat-1; i>=0; i--)
        cats[i] = cats[i+1] - deviation;

    for (int i=highMidCat; i<=numCats; i++)
        cats[i] = cats[i-1] + deviation;

    // if the data method specifies strict a strict min and/or max, use them
    if (!IsInf(dataMin) && !IsNan(dataMin) && (dataMin != -DoubleMaxValue))
        min = dataMin;
    if (!IsInf(dataMax) && !IsNan(dataMax) && (dataMax != DoubleMaxValue))
        max = dataMax;

    // flatten/clip any categories that extend beyond the min/max range
    for (int i=0; i<=numCats; i++)
    {
        if (cats[i] < min)
            cats[i] = min;
        else if (cats[i] > max)
            cats[i] = max;
    }

    for (int kk = 0; kk < numCats+1; kk++)
    {
        distValues.push_back(cats[kk]);
    }

    delete[] cats; // Delete the memory allocated before
}
예제 #29
0
Grnn::Trainer::Trainer(const MATRIX& X, const VECTOR& y)
    : X_(&X), y_(&y), z_(VECTOR(y.size(), 0.0))
{
    setDefaultParameters();
}