TripleBandLinearOp::TripleBandLinearOp(
        Size direction,
        const boost::shared_ptr<FdmMesher>& mesher)
    : direction_(direction),
      i0_       (new Size[mesher->layout()->size()]),
      i2_       (new Size[mesher->layout()->size()]),
      reverseIndex_ (new Size[mesher->layout()->size()]),
      lower_    (new Real[mesher->layout()->size()]),
      diag_     (new Real[mesher->layout()->size()]),
      upper_    (new Real[mesher->layout()->size()]),
      mesher_(mesher) {

        const boost::shared_ptr<FdmLinearOpLayout> layout = mesher->layout();
        const FdmLinearOpIterator endIter = layout->end();

        std::vector<Size> newDim(layout->dim());
        std::iter_swap(newDim.begin(), newDim.begin()+direction_);
        std::vector<Size> newSpacing = FdmLinearOpLayout(newDim).spacing();
        std::iter_swap(newSpacing.begin(), newSpacing.begin()+direction_);

        for (FdmLinearOpIterator iter = layout->begin(); iter!=endIter; ++iter) {
            const Size i = iter.index();

            i0_[i] = layout->neighbourhood(iter, direction, -1);
            i2_[i] = layout->neighbourhood(iter, direction,  1);

            const std::vector<Size>& coordinates = iter.coordinates();
            const Size newIndex =
                  std::inner_product(coordinates.begin(), coordinates.end(),
                                     newSpacing.begin(), Size(0));
            reverseIndex_[newIndex] = i;
        }
    }
Exemple #2
0
void nsViewManager::DoSetWindowDimensions(nscoord aWidth, nscoord aHeight)
{
  nsRect oldDim = mRootView->GetDimensions();
  nsRect newDim(0, 0, aWidth, aHeight);
  // We care about resizes even when one dimension is already zero.
  if (!oldDim.IsEqualEdges(newDim)) {
    // Don't resize the widget. It is already being set elsewhere.
    mRootView->SetDimensions(newDim, true, false);
    if (mPresShell)
      mPresShell->ResizeReflow(aWidth, aHeight);
  }
}
void WindowSFML::hasBeenResized()
{
	V2u newDim(windowObj.getSize().x, windowObj.getSize().y);
	
	if (newDim==dim)
		return;
	
	dim=newDim;
	
	//stop view from scaling with the window
	
	windowObj.setView(sf::View(sf::FloatRect(0, 0, dim.x, dim.y)));
	
	//clear(clr(255, 255, 255));
}
    FdmDirichletBoundary::FdmDirichletBoundary(
                            const boost::shared_ptr<FdmMesher>& mesher,
                            Real valueOnBoundary, Size direction,
                            FdmDirichletBoundary::Side side)
    : side_(side),
      valueOnBoundary_(valueOnBoundary) {
                                
        const boost::shared_ptr<FdmLinearOpLayout> layout = mesher->layout();
                                
        std::vector<Size> newDim(layout->dim());
        newDim[direction] = 1;
        const Size hyperSize = std::accumulate(newDim.begin(), newDim.end(),
                                               Size(1), std::multiplies<Size>());
        indicies_.resize(hyperSize);

        Size i=0;
        const FdmLinearOpIterator endIter = layout->end();
        for (FdmLinearOpIterator iter = layout->begin(); iter != endIter;
            ++iter) {
            if (   (side == Lower && iter.coordinates()[direction] == 0)
                || (side == Upper && iter.coordinates()[direction] 
                                            == layout->dim()[direction]-1)) {

                QL_REQUIRE(hyperSize > i, "index missmatch");
                indicies_[i++] = iter.index();
            }
        }
        
        if (side_ == Lower) {
            xExtreme_ = mesher->locations(direction)[0];
        }
        else if (side_ == Upper) {
            xExtreme_ 
                = mesher->locations(direction)[layout->dim()[direction]-1];
        }
    }
void CameraTestTool::leftButtonDrag(const TPointD &pos, const TMouseEvent &e) {
  m_dragged = true;
  CleanupParameters *cp =
      CleanupSettingsModel::instance()->getCurrentParameters();

  TPointD dp(pos - m_lastPos);
  if (m_scaling != eNoScale) {
    TDimensionD dim(cp->m_camera.getSize());
    TPointD delta;

    // Mid-edge cases
    if (m_scaling == e1M)
      delta = TPointD(dp.x / Stage::inch, 0);
    else if (m_scaling == e0M)
      delta = TPointD(-dp.x / Stage::inch, 0);
    else if (m_scaling == eM1)
      delta = TPointD(0, dp.y / Stage::inch);
    else if (m_scaling == eM0)
      delta = TPointD(0, -dp.y / Stage::inch);
    else {
      // Corner cases
      if (e.isShiftPressed()) {
        // Free adjust
        if (m_scaling == e11)
          delta = TPointD(dp.x / Stage::inch, dp.y / Stage::inch);
        else if (m_scaling == e00)
          delta = TPointD(-dp.x / Stage::inch, -dp.y / Stage::inch);
        else if (m_scaling == e10)
          delta = TPointD(dp.x / Stage::inch, -dp.y / Stage::inch);
        else if (m_scaling == e01)
          delta = TPointD(-dp.x / Stage::inch, dp.y / Stage::inch);
      } else {
        // A/R conservative
        bool xMaximalDp = (fabs(dp.x) > fabs(dp.y));

        if (m_scaling == e11)
          delta.x = (xMaximalDp ? dp.x : dp.y) / Stage::inch;
        else if (m_scaling == e00)
          delta.x = (xMaximalDp ? -dp.x : -dp.y) / Stage::inch;
        else if (m_scaling == e10)
          delta.x = (xMaximalDp ? dp.x : -dp.y) / Stage::inch;
        else if (m_scaling == e01)
          delta.x = (xMaximalDp ? -dp.x : dp.y) / Stage::inch;

        // Keep A/R
        delta.y = delta.x * dim.ly / dim.lx;
      }
    }

    TDimensionD newDim(dim.lx + 2.0 * delta.x, dim.ly + 2.0 * delta.y);
    if (newDim.lx < 2.0 || newDim.ly < 2.0) return;

    cp->m_camera.setSize(newDim,
                         true,    // Preserve DPI
                         false);  // A/R imposed above in corner cases
  } else {
    if (e.isShiftPressed()) {
      TPointD delta = pos - m_firstPos;
      cp->m_offx    = m_firstCameraOffset.x;
      cp->m_offy    = m_firstCameraOffset.y;
      if (fabs(delta.x) > fabs(delta.y)) {
        if (!cp->m_offx_lock) cp->m_offx += -delta.x * (2.0 / Stage::inch);
      } else {
        if (!cp->m_offy_lock) cp->m_offy += -delta.y * (2.0 / Stage::inch);
      }
    } else {
      if (!cp->m_offx_lock) cp->m_offx += -dp.x * (2.0 / Stage::inch);
      if (!cp->m_offy_lock) cp->m_offy += -dp.y * (2.0 / Stage::inch);
    }
  }

  m_lastPos = pos;

  CleanupSettingsModel::instance()->commitChanges();
  invalidate();
}
bool NNTrainer::checkGradient( const double lambda)
{
    //-- Routine to check the gradient
    //------------------------------------------------------------------------------------
    //-- Store the previous configuration:
    NeuralNetwork* prevNN = nn;
    std::vector<Matrix *> prevWeights = nn->getWeights();
    std::vector<TrainingExample> *prevTraining = trainingSet;

    //-- New configuration:

    //--New neural network:
    int array[] = {3, 5, 3};
    std::vector<int> newDim(array, array+sizeof(array)/sizeof(int));

    NeuralNetwork newNN( newDim );

    //-- New weight matrices:
    Matrix mat1( 5, 4), mat2( 3, 6);
    std::vector< Matrix *> newWeights;
    newWeights.push_back( &mat1);
    newWeights.push_back( &mat2);

    //-- Set new configuration:
    newNN.setWeights( newWeights);
    nn = &newNN;


    //-- New training set (random, of course)
    std::vector<TrainingExample> newTS;

    for (int i = 0; i < 5; i++)
    {
	//-- Construct input
	std::vector<double> x;

	for(int j = 0; j < 3; j++)
	{
	    x.push_back( 2* 0.14 *((rand()/(float)RAND_MAX)-0.5) );
	}

	//-- Construct expected output:
	std::vector<double> y;
	for(int j = 0; j < 3; j++)
	{
	    if ( j == i )
		y.push_back(1);
	    else if ( i > j && i-3 == j)
		y.push_back(1);
	    else
		y.push_back(0);
	}

	//-- Construct training example:
	TrainingExample aux;
	aux.x = x;
	aux.y = y;

	//-- Append example:
	newTS.push_back( aux);
    }
    //-- Set new training set
    trainingSet = &newTS;

    //-- Randomize weights:
    randomWeights();

       //-- Calculate gradients:
    std::vector<double> backprop = gradient( lambda );
    std::vector<double> numerical = numericalGradient( lambda );

    std::cout << "Backprop gradient:" << std::endl;
    std::cout << backprop << std::endl;
    std::cout << "Numerical gradient:" << std::endl;
    std::cout << numerical << std::endl;

    //-- Calculate relative deviation:
    std::vector<double> sum, difference;

    for (int i= 0; i < (int) backprop.size(); i++)
    {
	sum.push_back( pow( ( numerical.at(i)) + backprop.at(i)  , 2)  );
	difference.push_back( pow( ( numerical.at(i) - backprop.at(i)) , 2) );
    }

    double modSum = 0, modDif = 0;
    for (int i= 0; i < (int) backprop.size(); i++)
    {
	modSum+=sum.at(i);
	modDif+=difference.at(i);
    }

    std::cout << "Relative difference is: " << sqrt(modDif) / sqrt(modSum) << std::endl;

    //--Restore the previous configuration:
    nn = prevNN;
    nn->setWeights( prevWeights );
    trainingSet = prevTraining;

    //-- Return if passed the test or not:
    if ( sqrt(modDif) / sqrt(modSum) < 1e-4 )
	return true;
    else
	return false;
}