void rl_nac::solve_for_grad( vector_t& guess ) {

  matrix_t copyA;
  //vector_t guess;

  LapackInfo info(0);

  copyA = A;
  guess = b;
  
  // Regularize this: A = A + REGULARIZER*eye(N)
  int cnt = A.GetM();
  for ( int i=0; i<cnt; i++ ) {
    copyA(i,i) = copyA(i,i) + REGULARIZER;
  }

#ifdef DEBUG
  printf("=====================================\n");
  printf("Solving for gradient:\n");
  copyA.Print();
  guess.Print();
  printf("=====================================\n");
#endif

  vector_t tau;
  GetQR( copyA, tau, info );

  if ( info.GetInfo() != 0 ) {
    fprintf( stderr, "Error in QR decomposition!\n" );
    for ( int a=0; a<act_cnt; a++ ) {
      (*acts)[a]->natural_grad.Fill(0);
    }
    guess.Fill( 0 );
    return;
  }

  SolveQR( copyA, tau, guess, info );

  if ( info.GetInfo() != 0 ) {
    fprintf( stderr, "Error solving linear system!\n" );
    for ( int a=0; a<act_cnt; a++ ) {
      (*acts)[a]->natural_grad.Fill(0);
    }
    guess.Fill( 0 );
    return;
  }

  // results are stored in guess
#ifdef DEBUG
  printf("Final answer:\n");
  guess.Print();
#endif

  return;
}
Example #2
0
void STWarp<T>::setVideos(const Video<stwarp_video_t> &A, const Video<stwarp_video_t> &B) {
    videoA = new Video<stwarp_video_t>(A);
    videoB = new Video<stwarp_video_t>(B);

    if(videoA->getHeight()!= videoB->getHeight() ||
        videoA->getWidth() != videoB->getWidth() ||
        videoA->frameCount() != videoB->frameCount()){
        if(params.verbosity >0) {
            fprintf(stderr, "Dimensions do not match\n");
        }
    }

    int chan = 3;

    // Use gradient features (requires color)
    if(params.useColor && params.useFeatures) {
        if(params.verbosity > 0) {
            cout << "+ Adding gradient features" << endl;
        }
        videoA->addChannels(3);
        videoB->addChannels(3);

        Video<T> copyA(videoA->size());
        Video<T> copyB(videoA->size());
        copyA.copy(*videoA);
        copyB.copy(*videoB);
        copyA.collapse();
        copyB.collapse();
        Video<T> temp(copyA.size());

        VideoProcessing::dx(copyA, temp);
        videoA->writeChannel(chan, temp);

        VideoProcessing::dy(copyA, temp);
        videoA->writeChannel(chan+1, temp);

        VideoProcessing::dt(copyA, temp);
        videoA->writeChannel(chan+2, temp);

        VideoProcessing::dx(copyB, temp);
        videoB->writeChannel(chan, temp);

        VideoProcessing::dy(copyB, temp);
        videoB->writeChannel(chan+1, temp);

        VideoProcessing::dt(copyB, temp);
        videoB->writeChannel(chan+2, temp);
    }

    // TODO: nullptr
    VideoSize sz  = videoA->size();
    dimensions[0] = sz.height;
    dimensions[1] = sz.width;
    dimensions[2] = sz.nFrames;
    dimensions[3] = sz.nChannels;

    if(params.verbosity > 0) {
        printf("done.\n");
    }
}
Example #3
0
Task * copyQ(Task * task)// duplicate the current task  
{
	Task * currentQ = malloc(sizeof(Task));
	currentQ -> priority = task -> priority;
	currentQ -> iatime = task -> iatime;
	currentQ -> artime = task -> artime;
	currentQ -> nofstasks = task -> nofstasks;
	currentQ -> timelq = task -> timelq;
	currentQ -> ststime = copyA(task->ststime,task->nofstasks);
	currentQ -> next = NULL;
	currentQ -> prev = NULL;
	return currentQ;
}
Example #4
0
bool solveLinear(const DMatZZpFFPACK& A,
                 const DMatZZpFFPACK& B,
                 bool right_side,
                 DMatZZpFFPACK& X,
                 bool declare_A_is_invertible)  // this parameter is unused
{
  //    std::cerr << "inside FFpackSolveLinear" << std::endl;

  size_t a_rows = A.numRows();
  size_t a_cols = A.numColumns();

  size_t b_rows = B.numRows();
  size_t b_cols = B.numColumns();

  DMatZZpFFPACK copyA(A);
  DMatZZpFFPACK copyB(B);

  // preallocate the space for the solutions:
  size_t x_rows = (right_side ? a_cols : b_rows);
  size_t x_cols = (right_side ? b_cols : a_rows);

  X.resize(x_rows, x_cols);  // sets it to 0 too.

  int info = 0;  // >0 if the system is inconsistent, ==0 means success

  FFPACK::fgesv(A.ring().field(),
                (right_side ? FFLAS::FflasLeft : FFLAS::FflasRight),
                a_rows,
                a_cols,
                (right_side ? b_cols : b_rows),
                copyA.array(),
                a_cols,  // leading dim of A
                X.array(),
                x_cols,
                copyB.array(),
                b_cols,
                &info);

  if (info > 0)
    {
      // the system is inconsistent
      return false;
    }

  return true;
}