bool AkimaPeriodic::algorithm() {

  KstVectorPtr x_array        = inputVector(X_ARRAY);
  KstVectorPtr y_array        = inputVector(Y_ARRAY);
  KstVectorPtr x1_array       = inputVector(X1_ARRAY);
  KstVectorPtr y_interpolated = outputVector(Y_INTERPOLATED);

  return interpolate( x_array, y_array, x1_array, y_interpolated, gsl_interp_akima_periodic);
}
void BasicPlugin::updateOutput() const {
  //output vectors...
  //FIXME: _outputVectors should be used, not this string list!
  QStringList ov = outputVectorList();
  QStringList::ConstIterator ovI = ov.begin();
  for (; ovI != ov.end(); ++ovI) {
    if (VectorPtr o = outputVector(*ovI)) {
      Q_ASSERT(o->myLockStatus() == KstRWLock::WRITELOCKED);
      vectorRealloced(o, o->value(), o->length()); // why here?
      o->setNewAndShift(o->length(), o->numShift()); // why here?
    }
  }
}
Beispiel #3
0
//swap input vector
bool Reverse::algorithm() {

  KstVectorPtr input = inputVector(INPUT);
  KstVectorPtr output  = outputVector(OUTPUT);
  output->resize(input->length());
  int length = input->length();

  for (int i = 0; i < length; i++){
    output->value()[length-i-1] = input->value()[i];
  }

  return true;
}
QVector<QVariant> MasterSlaveCommunicator::master_command_loop(int taskIndex, QVector<QVariant> inputVector)
{
    // prepare an output vector of the appropriate size
    int numitems = inputVector.size();
    QVector<QVariant> outputVector(numitems);

    // prepare a vector to remember the index of most recent item handed out to each slave
    QVector<int> itemForSlave(size());

    // the index of the next item to be handed out
    int numsent = 0;

    // hand out an item to each slave (unless there are less items than slaves)
    for (int slave=1; slave<size() && numsent<numitems; slave++,numsent++)
    {
        QByteArray buffer = toByteArray(_bufsize, inputVector[numsent]);

        ProcessManager::sendByteBuffer(buffer, slave, taskIndex);

        itemForSlave[slave] = numsent;
    }

    // receive results, handing out more items until all have been handed out
    QByteArray resultbuffer(_bufsize, 0);
    for (int i=0; i<numitems; i++)
    {
        // receive a message from any slave
        int slave;
        ProcessManager::receiveByteBuffer(resultbuffer, slave);

        // put the result in the output vector
        outputVector[itemForSlave[slave]] = toVariant(resultbuffer);

        // if more items are available, hand one to this slave
        if (numsent<numitems)
        {
            QByteArray buffer = toByteArray(_bufsize, inputVector[numsent]);

            ProcessManager::sendByteBuffer(buffer, slave, taskIndex);

            itemForSlave[slave] = numsent;
            numsent++;
        }
    }
    return outputVector;
}
Beispiel #5
0
// Remove some elements from the vector starting from vector[0]
bool Trim::algorithm() {
  KstVectorPtr input = inputVector(INPUT);
  KstScalarPtr remove = inputScalar(REMOVE);
  KstVectorPtr cut = outputVector(CUT);
  bool rc = false;

  if (input->length() > remove->value()) {
    int cutSize = (int)input->length() - (int)remove->value();
    cut->resize( cutSize, false );
    for (int j=0; j<cutSize; j++) {
      cut->value()[j] = input->value()[(int)remove->value()+j];
    }

    rc = true;
  }

  return rc;
}
//
// UseStmt
//
void AstDumpToHtml::visitUseStmt(UseStmt* node) {
  if (isBlockStmt(node->parentExpr)) {
    fprintf(mFP, "<DL>\n");
  }

  fprintf(mFP, " (%d 'use' ", node->id);

  node->mod->accept(this);

  if (!node->isPlainUse()) {
    node->writeListPredicate(mFP);
    outputVector(mFP, node->named);
  }

  fprintf(mFP, ")");

  if (isBlockStmt(node->parentExpr)) {
    fprintf(mFP, "</DL>\n");
  }
}
Beispiel #7
0
bool CumulativeSum::algorithm() {

  KstVectorPtr inputvector    = inputVector(INPUTVECTOR);
  KstScalarPtr scale_factor   = inputScalar(SCALE_FACTOR);
  KstVectorPtr cumulative_sum = outputVector(CUMULATIVE_SUM);

  /* Memory allocation */
  if (cumulative_sum->length() != inputvector->length()) {
    cumulative_sum->resize(inputvector->length()+1, true);
  }

  cumulative_sum->value()[0] = 0.0;

  for (int i = 0; i < inputvector->length(); i++) {
    cumulative_sum->value()[i+1] =
      inputvector->value()[i]*scale_factor->value() + cumulative_sum->value()[i];
  }

  return true;
}
vector<double> CounterpropNetwork::testing( vector<double>& inputVector )
{
	DWORD i, j;
		
	vector<double> outputVector( m_dwOutputSize );

	// present input vector and calculate hidden layer activations
	vector<double> hiddenActivations( m_dwHiddenSize );

	for( i = 0; i < m_dwHiddenSize; i++ )
	{
		for( j = 0; j < m_dwInputSize; j++ )
		{
			hiddenActivations[i] += m_vvdKohonenMatrix[i][j] * inputVector[j];
		}
	}

	// determine the winning hidden layer neuron
	// (the one with the maximum activation)
	int winningIndex = 0;
	double maxActivation = hiddenActivations[0];

	for( i = 0; i < m_dwHiddenSize; i++ )
	{
		if(hiddenActivations[i] > maxActivation )
		{
			maxActivation = hiddenActivations[i];
			winningIndex = i;
		}
	}

	// calculate the output vector
	// (the synaptic connections from the winning hidden neuron
	// to the output layer)
	for( i = 0; i < m_dwOutputSize; i++ )
	{
		outputVector[i] = m_vvdGrossbergMatrix[i][winningIndex];
	}

	return outputVector;
}
Beispiel #9
0
//==============================================================================
Eigen::VectorXd Spline::evaluatePolynomial(
    const Eigen::MatrixXd& _coefficients, double _t, int _derivative)
{
  const auto numOutputs = _coefficients.rows();
  const auto numCoeffs = _coefficients.cols();

  const auto timeVector
      = common::SplineProblem<>::createTimeVector(_t, _derivative, numCoeffs);
  const auto derivativeMatrix
      = common::SplineProblem<>::createCoefficientMatrix(numCoeffs);
  const auto derivativeVector = derivativeMatrix.row(_derivative);
  const auto evaluationVector
      = derivativeVector.cwiseProduct(timeVector.transpose());

  Eigen::VectorXd outputVector(numOutputs);

  for (int ioutput = 0; ioutput < numOutputs; ++ioutput)
    outputVector[ioutput] = _coefficients.row(ioutput).dot(evaluationVector);

  return outputVector;
}
Beispiel #10
0
//
// UseStmt
//
void AstDumpToHtml::visitUseStmt(UseStmt* node) {
  if (isBlockStmt(node->parentExpr)) {
    fprintf(mFP, "%s\n", HTML_DL_open_tag);
  }

  fprintf(mFP, " (%d 'use' ", node->id);

  node->src->accept(this);

  if (!node->isPlainUse()) {
    node->writeListPredicate(mFP);
    bool first = outputVector(mFP, node->named);
    outputRenames(mFP, node->renamed, first);
  }

  fprintf(mFP, ")");

  if (isBlockStmt(node->parentExpr)) {
    fprintf(mFP, "%s\n", HTML_DL_close_tag);
  }
}
bool Differentiation::algorithm() {

  KstVectorPtr inputvector  = inputVector(INPUTVECTOR);
  KstScalarPtr time_step    = inputScalar(TIME_STEP);
  KstVectorPtr derivative   = outputVector(DERIVATIVE);

  /* Memory allocation */
  if (derivative->length() != inputvector->length()) {
    derivative->resize(inputvector->length(), true);
  }

  derivative->value()[0] = (inputvector->value()[1] - inputvector->value()[0]) / time_step->value();

  int i = 1;
  for (; i < inputvector->length()-1; i++) {
      derivative->value()[i] = (inputvector->value()[i+1] - inputvector->value()[i-1])/(2*time_step->value());
  }

  derivative->value()[i] = (inputvector->value()[i] - inputvector->value()[i-1]) / time_step->value();

  return true;
}
Beispiel #12
0
bool NoiseAddition::algorithm() {
  KstVectorPtr array    = inputVector(ARRAY);
  KstScalarPtr sigma    = inputScalar(SIGMA);
  KstVectorPtr output   = outputVector(OUTPUT);

  const gsl_rng_type* pGeneratorType;
  gsl_rng* pRandomNumberGenerator;
  double* pResult[1];
  int iRetVal = false;
  int iLength = array->length();

  pResult[0] = 0L;

  if (iLength > 0) {
    if (output->length() != iLength) {
      output->resize(iLength, false);
      pResult[0] = (double*)realloc( output->value(), iLength * sizeof( double ) );
    } else {
      pResult[0] = output->value();
    }
  }

  pGeneratorType = gsl_rng_default;
  pRandomNumberGenerator = gsl_rng_alloc( pGeneratorType );
  if (pRandomNumberGenerator != NULL) {
    if (pResult[0] != NULL) {
      for (int i=0; i<iLength; i++) {
        output->value()[i] = array->value()[i] + gsl_ran_gaussian( pRandomNumberGenerator, sigma->value() );
      }

      iRetVal = true;
    }
    gsl_rng_free( pRandomNumberGenerator );
  }

  return iRetVal;
}
bool CrossCorrelate::algorithm() {

  KstVectorPtr array_one    = inputVector(ARRAY_ONE);
  KstVectorPtr array_two    = inputVector(ARRAY_TWO);
  KstVectorPtr step_value   = outputVector(STEP_VALUE);
  KstVectorPtr correlated   = outputVector(CORRELATED);

  if (array_one->length() <= 0               ||
      array_two->length() <= 0               ||
      array_one->length() != array_two->length()) {
      return false;
  }

  double* pdArrayOne;
  double* pdArrayTwo;
  double* pdResult[2];
  double  dReal;
  double  dImag;

  int iLength;
  int iLengthNew;

  bool iReturn = false;

  //
  // zero-pad the array...
  //
  iLength  = array_one->length();
  iLength *= 2;

  step_value->resize(array_one->length(), false);
  correlated->resize(array_two->length(), false);

  //
  // round iLength up to the nearest power of two...
  //
  iLengthNew = 64;
  while( iLengthNew < iLength && iLengthNew > 0) {
    iLengthNew *= 2;
  }
  iLength = iLengthNew;

  if (iLength <= 0)
    return false;

  pdArrayOne = new double[iLength];
  pdArrayTwo = new double[iLength];
  if (pdArrayOne != NULL && pdArrayTwo != NULL) {
    //
    // zero-pad the two arrays...
    //
    memset( pdArrayOne, 0, iLength * sizeof( double ) );
    memcpy( pdArrayOne, array_one->value(), array_one->length() * sizeof( double ) );

    memset( pdArrayTwo, 0, iLength * sizeof( double ) );
    memcpy( pdArrayTwo, array_two->value(), array_two->length() * sizeof( double ) );

    //
    // calculate the FFTs of the two functions...
    //
    if (gsl_fft_real_radix2_transform( pdArrayOne, 1, iLength ) == 0) {
      if (gsl_fft_real_radix2_transform( pdArrayTwo, 1, iLength ) == 0) {
        //
        // multiply one FFT by the complex conjugate of the other...
        //
        for (int i=0; i<iLength/2; i++) {
          if (i==0 || i==(iLength/2)-1) {
            pdArrayOne[i] = pdArrayOne[i] * pdArrayTwo[i];
          } else {
            dReal = pdArrayOne[i] * pdArrayTwo[i] + pdArrayOne[iLength-i] * pdArrayTwo[iLength-i];
            dImag = pdArrayOne[i] * pdArrayTwo[iLength-i] - pdArrayOne[iLength-i] * pdArrayTwo[i];

            pdArrayOne[i] = dReal;
            pdArrayOne[iLength-i] = dImag;
          }
        }

        //
        // do the inverse FFT...
        //
        if (gsl_fft_halfcomplex_radix2_inverse( pdArrayOne, 1, iLength ) == 0) {
          if (step_value->length() != array_one->length()) {
            pdResult[0] = (double*)realloc( step_value->value(), array_one->length() * sizeof( double ) );
          } else {
            pdResult[0] = step_value->value();
          }

          if (correlated->length() != array_two->length()) {
            pdResult[1] = (double*)realloc( correlated->value(), array_two->length() * sizeof( double ) );
          } else {
            pdResult[1] = correlated->value();
          }

          if (pdResult[0] != NULL && pdResult[1] != NULL) {
            for (int i = 0; i < array_one->length(); ++i) {
              step_value->value()[i] = pdResult[0][i];
            }
            for (int i = 0; i < array_two->length(); ++i) {
              correlated->value()[i] = pdResult[1][i];
            }

            for (int i = 0; i < array_one->length(); i++) {
                step_value->value()[i] = (double)( i - ( array_one->length() / 2 ) );
            }

            memcpy( &(correlated->value()[array_one->length() / 2]),
                    &(pdArrayOne[0]),
                    ( ( array_one->length() + 1 ) / 2 ) * sizeof( double ) );

            memcpy( &(correlated->value()[0]),
                    &(pdArrayOne[iLength - (array_one->length() / 2)]),
                    ( array_one->length() / 2 ) * sizeof( double ) );

            iReturn = true;
          }
        }
      }
    }
  }
  delete[] pdArrayOne;
  delete[] pdArrayTwo;

  return iReturn;
}
void main(int argc, char *argv[])
{
	// --- define training and testing sets ---
	unsigned int init = static_cast<unsigned int> ( time(NULL) );
	srand( init );

	int i, pair;
	char str[8] = {0};

	// number of input-output vector pairs in the training set
	int numberOfTrainingPairs = HIDDEN;

	// number of input-output vector pairs in the testing set
	int numberOfTestingPairs  = 6;

	// training set
	vector<vector<double>> trainingInputVector;
	vector<vector<double>> trainingOutputVector;

	// testing set
	vector<vector<double>> testingInputVector;
	vector<vector<double>> testingOutputVector;

	// --- read in the training set ---
	try
	{ 
		ifstream hfile( "train.txt" );

		if( !hfile.good() ) 
		{
			printf( "File not exists\n" );

			return;
		}

		// read each line of the file
		// each line contains an input-output vector pair of the form:
		//
		// input_0 input_1 ... input_n output_0 output_1 ... output_n
		trainingInputVector.resize( numberOfTrainingPairs );
		trainingOutputVector.resize( numberOfTrainingPairs );

		for( pair = 0; pair < numberOfTrainingPairs; pair++)
		{
			string line;


			getline( hfile, line );

			istringstream split(line);
			string token;

			// read in the input vector
			for( i = 0; i < INPUT; i++ )
			{
				getline( split, token, ' ' );
				trainingInputVector[pair].push_back( atof( token.c_str() ) );
			}

			// read in the output vector
			for( i = 0; i < OUTPUT; i++ )
			{
				getline( split, token, ' ' );
				trainingOutputVector[pair].push_back( atof( token.c_str() ) );
			}
		} 
	}
	catch (exception e) 
	{ 
		printf( e.what() ); 
	}

	// --- read in the testing set ---

	try
	{
		ifstream hfile( "test.txt" );
		if( !hfile.good() ) 
		{
			printf( "File not exists\n" );
			return;
		}

		// read each line of the file
		// each line contains an input-output vector pair of the form:
		//
		// input_0 input_1 ... input_n output_0 output_1 ... output_n
		testingInputVector.resize( numberOfTestingPairs );
		testingOutputVector.resize( numberOfTestingPairs );

		for( pair = 0; pair < numberOfTestingPairs; pair++ )
		{
			string line;
			getline( hfile, line );

			istringstream iss(line);
			string token;

			// read in the input vector
			for( int i = 0; i < INPUT; i++ )
			{
				getline(iss, token, ' ');
				testingInputVector[pair].push_back( atof(token.c_str() ) );
			}

			// read in the output vector
			for( int i = 0; i < OUTPUT; i++ )
			{
				getline( iss, token, ' ' );
				testingOutputVector[pair].push_back( atof( token.c_str() ) );
			}


		} 
	}
	catch ( exception e ) 
	{ 
		printf( e.what() ); 
	}

	// network init

	CounterpropNetwork *c = new CounterpropNetwork();

	// train the network
	c->training( trainingInputVector, trainingOutputVector );

	printf("Testing network...\n");

	// test the network

	double error = 0;

	vector<double> v_error(OUTPUT);
	vector<int>    v_numCorrect(OUTPUT);

	for( i = 0; i < numberOfTestingPairs; i++ )
	{

		vector<double> outputVector( OUTPUT );
		
		outputVector = c->testing( testingInputVector[i] );

		printf( "Output for line #%d: ", i+1 );

		for( int x = 0; x < OUTPUT; ++x )
		{
			printf( "%.1f ", outputVector[x] );
		}

		printf( "(Real: " );

		for( int x = 0; x < OUTPUT; ++x )
		{
			printf( "%.1f ", testingOutputVector[i][x] );
		}

		printf( ")" );

		printf( "\n" );

		for( int x = 0; x < OUTPUT; ++x )
		{
			error = fabs( outputVector[x] - testingOutputVector[i][x] );

			if( error <= c->m_dbTolerance )
				v_numCorrect[x]++;
		}
	}

	// output testing accuracy
	for( int x = 0; x < OUTPUT; ++x )
	{
		v_error[x] = (double) v_numCorrect[x] / numberOfTestingPairs * 100.00;

		printf( "Accuracy Output %d = %.0f%%\n", x, v_error[x] );
	}
}
Beispiel #15
0
bool Phase::algorithm() {

  KstVectorPtr time     = inputVector(TIME);
  KstVectorPtr data_i   = inputVector(DATA_I);
  KstScalarPtr period   = inputScalar(PERIOD);
  KstScalarPtr zero     = inputScalar(ZERO);
  KstVectorPtr phase    = outputVector(PHASE);
  KstVectorPtr data_o   = outputVector(DATA_O);

  double* pResult[2];
  double  dPhasePeriod = period->value();
  double dPhaseZero = zero->value();
  int iLength;

  bool iRetVal = false;

  if (dPhasePeriod > 0.0) {
    if (time->length() == data_i->length()) {
      iLength = time->length();

      if (phase->length() != iLength) {
        phase->resize(iLength, true);
        pResult[0] = (double*)realloc( phase->value(), iLength * sizeof( double ) );
      } else {
        pResult[0] = phase->value();
      }

      if (data_o->length() != iLength) {
        data_o->resize(iLength, true);
        pResult[1] = (double*)realloc( data_o->value(), iLength * sizeof( double ) );
      } else {
        pResult[1] = data_o->value();
      }

      if (pResult[0] != NULL && pResult[1] != NULL) {
        for (int i = 0; i < phase->length(); ++i) {
          phase->value()[i] = pResult[0][i];
        }
        for (int i = 0; i < data_o->length(); ++i) {
          data_o->value()[i] = pResult[1][i];
        }

        /*
        determine the phase...
        */
        for (int i=0; i<iLength; i++) {
          phase->value()[i] = fmod( ( time->value()[i] - dPhaseZero ) / dPhasePeriod, 1.0 );
        }

        /*
        sort by phase...
        */
        memcpy( data_o->value(), data_i->value(), iLength * sizeof( double ) );
        double* sort[2];
        sort[0] = phase->value();
        sort[1] = data_o->value();
        quicksort( sort, 0, iLength-1 );

        iRetVal = true;
      }
    }
  }

  return iRetVal;
}
Beispiel #16
0
bool Normalization::algorithm() {
  KstVectorPtr vectorIn = inputVector(VECTOR_IN);
  KstVectorPtr vectorOut = outputVector(VECTOR_OUT);
  double *arr;
  double *Yi;
  int iLength = vectorIn->length();
  int w = 1;

  arr = new double[iLength];
  Yi = new double[iLength];

  for(int i=0; i<iLength; i++)
  {
    Yi[i] = vectorIn->value()[i];
  }

  //
  // exclude peak values
  //
  for(int loop=0; loop<2; loop++)
  {
    for(int i=0; i<iLength; i++)
    {
      arr[i] = Yi[i];
    }

    for(int i=0; i<iLength; i++)
    {
      if(isMin(Yi, i, iLength) || isMax(Yi, i, iLength))
      {
        excludePts(arr, i, w, iLength);
      }
    }

    searchHighPts(arr, iLength);
    interpolate(Yi, arr, iLength);
  }

  //
  // do a piecewise linear fit
  //
  vectorOut->resize(iLength, false);

  int L = 3;
  double cof[2] = { 0.0, 0.0 };

  for(int i=0; i<iLength; i=i+L)
  {
    fit(i, L, iLength, Yi, cof, vectorOut);
  }

  //
  // normalize
  //
  for(int i=0; i<iLength; i++)
  {
    vectorOut->value()[i] = vectorIn->value()[i] / vectorOut->value()[i];
  }

  //
  // exclude off points
  //
  for(int i=0; i<iLength; i++)
  {
    if(vectorOut->value()[i] < 0.0 || vectorOut->value()[i] > 1.2)
    {
      vectorOut->value()[i] = NOPOINT;
    }
  }

  delete[] arr;
  delete[] Yi;

  return true;
}
Beispiel #17
0
bool AutoCorrelate::algorithm() {

  KstVectorPtr array            = inputVector(ARRAY);
  KstVectorPtr step_value       = outputVector(STEP_VALUE);
  KstVectorPtr auto_correlated  = outputVector(AUTO_CORRELATED);

  if (array->length() <= 0) {
    return false;
  }

  double* pdArrayOne;
  double* pdResult;
  double* pdCorrelate;
  double  dReal;
  double  dImag;
  double  sigmaSquared = 0.0;

  int iLength;
  int iLengthNew;

  bool iReturn = false;

  //
  // zero-pad the array...
  //
  iLength  = array->length();
  iLength *= 2;

  step_value->resize(array->length(), false);
  auto_correlated->resize(array->length(), false);

  //
  // round iLength up to the nearest power of two...
  //
  iLengthNew = 64;
  while( iLengthNew < iLength && iLengthNew > 0) {
    iLengthNew *= 2;
  }
  iLength = iLengthNew;

  if (iLength <= 0) {
    return false;
  }

  pdArrayOne = new double[iLength];
  if (pdArrayOne != NULL) {
    //
    // zero-pad the two arrays...
    //
    memset( pdArrayOne, 0, iLength * sizeof( double ) );
    memcpy( pdArrayOne, array->value(), array->length() * sizeof( double ) );

    //
    // calculate the FFTs of the two functions...
    //
    if (gsl_fft_real_radix2_transform( pdArrayOne, 1, iLength ) == 0) {
      //
      // multiply the FFT by its complex conjugate...
      //
      for (int i=0; i<iLength/2; i++) {
        if (i==0 || i==(iLength/2)-1) {
          pdArrayOne[i] *= pdArrayOne[i];
        } else {
          dReal = pdArrayOne[i] * pdArrayOne[i] + pdArrayOne[iLength-i] * pdArrayOne[iLength-i];
          dImag = pdArrayOne[i] * pdArrayOne[iLength-i] - pdArrayOne[iLength-i] * pdArrayOne[i];

          pdArrayOne[i] = dReal;
          pdArrayOne[iLength-i] = dImag;
        }
      }

      //
      // do the inverse FFT...
      //
      if (gsl_fft_halfcomplex_radix2_inverse( pdArrayOne, 1, iLength ) == 0) {
        if (step_value->length() != array->length()) {
          pdResult = (double*)realloc( step_value->value(), array->length() * sizeof( double ) );
        } else {
          pdResult = step_value->value();
        }

        if (auto_correlated->length() != array->length()) {
          pdCorrelate = (double*)realloc( auto_correlated->value(), array->length() * sizeof( double ) );
        } else {
          pdCorrelate = auto_correlated->value();
        }

        if (pdResult != NULL && pdCorrelate != NULL) {
          sigmaSquared = pdArrayOne[0];

          memcpy( &(auto_correlated->value()[array->length() / 2]),
                  &(pdArrayOne[0]),
                  ( ( array->length() + 1 ) / 2 ) * sizeof( double ) );

          memcpy( &(auto_correlated->value()[0]),
                  &(pdArrayOne[iLength - (array->length() / 2)]),
                  ( array->length() / 2 ) * sizeof( double ) );

          for (int i = 0; i < array->length(); i++) {
            auto_correlated->value()[i] /= sigmaSquared;
            step_value->value()[i] = (double)( i - ( array->length() / 2 ) );
          }

          iReturn = true;
        }
      }
    }
  }
  delete[] pdArrayOne;

  return iReturn;
}