Пример #1
0
void VecMinusMatVec( VT &d, const VT &f, const MT &M, const VT &u )
{
	typename VT::Iterator viter(d); 
	typename VT::VectorEntry row; 
	typename MT::MatrixEntry col; 
	double *dptr, *fptr, *uptr, *mptr;
	const FAMGSparseVector *svu  = u.GetSparseVectorPtr();
	const FAMGSparseVector *svf  = f.GetSparseVectorPtr();
	const FAMGSparseVector *svd  = d.GetSparseVectorPtr();
	const FAMGSparseBlock *sb  = M.GetSparseBlockPtr();
    const FAMGSparseBlock *sbd  = M.GetDiagSparseBlockPtr();
    FAMGSparseVector svsum_d, svsum_o;

    svsum_d.Product(sbd,svu);
    svsum_o.Product(sb,svu);

    double *sum_d = new double[svsum_d.Get_maxcomp()+1];
    double *sum_o = new double[svsum_o.Get_maxcomp()+1];
	
	while(viter(row))
	{
		typename MT::Iterator miter(M,row);
		
        dptr = d.GetValuePtr(row);
        fptr = f.GetValuePtr(row);
        
        // diagonal 
        miter(col);
        uptr = u.GetValuePtr(col.dest());
        mptr = M.GetValuePtr(col);
        SparseBlockVSet(&svsum_d,sum_d,0.0);
        SparseBlockVSet(&svsum_o,sum_o,0.0);
        SparseBlockMVAddProduct(&svsum_d,sbd,svu,sum_d,mptr,uptr,1.0);
		while(miter(col))
        {
            uptr = u.GetValuePtr(col.dest());
            mptr = M.GetValuePtr(col);
            SparseBlockMVAddProduct(&svsum_o,sb,svu,sum_o,mptr,uptr,1.0);
        }
        SparseBlockVSub(svd,svf,&svsum_o,dptr,fptr,sum_o);
        SparseBlockVSub(svd,svd,&svsum_d,dptr,dptr,sum_d);
	}

    delete sum_d;
    delete sum_o;
}
Пример #2
0
void AddScaledValue( VT &dest, double scale, const VT &source )
{
    // not really tesyed
	// typename is a new C++ keyword!
	typename VT::Iterator viter(dest); 
	typename VT::VectorEntry ve; 
	short ncmp_d = dest.GetSparseVectorPtr()->Get_n();
	short ncmp_s = source.GetSparseVectorPtr()->Get_n();
	short *comp_d = dest.GetSparseVectorPtr()->Get_comp();
 	short *comp_s = source.GetSparseVectorPtr()->Get_comp();
    short ncmp;
    double *vptr_d, *vptr_s;
	
    ncmp = Min(ncmp_d,ncmp_s);

	while(viter(ve))
    {
        vptr_d = dest.GetValuePtr(ve);
        vptr_s = source.GetValuePtr(ve);
		for(short i = 0; i < ncmp; i++) vptr_d[comp_d[i]] += scale*vptr_s[comp_s[i]];
    }
}
Пример #3
0
/* interprete */
void interprete (VT tokens, map<string, int> labels) {
  stack<int> st, pst;
  map<int,int> heap;

  VT::iterator tok;
  for (tok=tokens.begin(); tok != tokens.end(); tok++) {
    // print_token(tok, st, pst, heap);
    int ppos = tok->get_pos();

    int left, right, tmp, jumpto;
    map<int, int>::iterator mit;
    VT::iterator t;
    switch(tok->get_id()) {
    case PUSH: st.push( numerise( tok->get_param() ) ); break;
    case DUPL: st.push( st.top() ); break;
    case SWAP:
      left  = st.top(); st.pop();
      right = st.top(); st.pop();
      st.push(left); st.push(right); break;
    case POPL: st.pop(); break;
    case ADDB:
      right = st.top(); st.pop();
      left = st.top(); st.pop();
      st.push(left + right); break;
    case SUBB:
      right = st.top(); st.pop();
      left = st.top(); st.pop();
      st.push(left - right); break;
    case MULB:
      right = st.top(); st.pop();
      left = st.top(); st.pop();
      st.push(left * right); break;
    case DIVB:
      right = st.top(); st.pop();
      left = st.top(); st.pop();
      st.push((int)(left / right)); break;
    case MODB:
      right = st.top(); st.pop();
      left = st.top(); st.pop();
      st.push(left % right); break;
    case STOR:
      left = st.top(); st.pop();
       right = st.top(); st.pop();
      heap[ right ] = left; break;
    case RETR:
      tmp = st.top(); st.pop();
      mit = heap.find( tmp );
      if (mit != heap.end()) st.push( mit->second );
      else st.push( 0 ); break;
    case SETL: break;
    case CALL:
      pst.push( ppos );
      jumpto = labels[ tok->get_param() ];

      for (t=tokens.begin(); t != tokens.end(); t++) {
        if (t->get_pos() == jumpto) { break; }
      }
      tok = t;
      break;
    case JUMP:
      jumpto = labels[ tok->get_param() ];

      for (t=tokens.begin(); t != tokens.end(); t++) {
        if (t->get_pos() == jumpto) { break; }
      }
      tok = t;
      break;
    case JMPZ:
      tmp = st.top(); st.pop();
      jumpto = labels[ tok->get_param() ];

      if (tmp==0) {

        for (t=tokens.begin(); t != tokens.end(); t++) {
          if (t->get_pos() == jumpto) { break; }
        }
        tok = t;
      }
      break;
    case JMPN:
      tmp = st.top(); st.pop();
      jumpto = labels[ tok->get_param() ];

      if (tmp < 0) {

        for (t=tokens.begin(); t != tokens.end(); t++) {
          if (t->get_pos() == jumpto) { break; }
        }
        tok = t;
      }
      break;
    case RETC:
      ppos = pst.top(); pst.pop();

      for (t=tokens.begin(); t != tokens.end(); t++) {
        if (t->get_pos() == ppos) { break; }
      }
      tok = t;
      break;
    case ENDC: exit(0); break;
    case PCHR:
      tmp = st.top(); st.pop();
      cout << (char)tmp; break;
    case PNUM:
      tmp = st.top(); st.pop();
      cout << tmp; break;
    case RCHR:
      tmp = (int)getchar();
      heap[ st.top() ] = tmp;
      st.pop(); break;
    case RNUM:
      cin >> tmp;
      heap[ st.top() ] = tmp;
      st.pop(); break;
    }

    /* parse err. some exception here */
  }
}