Ejemplo n.º 1
0
// Expression = (Summand {SummOperator Summand}) | Vector
Rpn::CodeThread Parser::expression()
{
	/* Vector */
	if (m_lexer->lexeme().type == LexemeOpeningSquareBracket) {
		return vector();
	}

	/* (Summand {SummOperator Summand}) */

	// First obligatory summand
	Rpn::CodeThread result = summand();

	// {SummOperator Summand} section
	while (CheckLexeme::isSummOperation(m_lexer->lexeme())) {
		BinaryOperation operation = summOperation();
		Rpn::CodeThread rightOperand = summand();
		result = m_codeGenerator->generateBinaryOperation(operation, result, rightOperand);
	}

	return result;
}
Ejemplo n.º 2
0
BVValue operator*(const BVValue& lhs, const BVValue& rhs) {
	assert(lhs.width() == rhs.width());
	BVValue product(lhs.width());
	BVValue::Base summand(lhs.base());

	for (std::size_t i = 0; i < lhs.width(); ++i) {
		if (rhs[i]) {
			product = product + BVValue(BVValue::Base(summand));
		}
		summand <<= 1;
	}

	return product;
}
Ejemplo n.º 3
0
int main()
{
	printf("Enter n : ");
	int n = 0;
	scanf("%d", &n);
	int l = n;
	int a[30];					
	for(int i = 1; i<= l; i++) // 桤磬鬣朦眍 忖邃胳 怦� 邃桧梓觇
		a[i] = 1;
	for(int i = 1; i <= l; i++) // 恹忸� 耋祆� 邃桧梓尻
		{
			if (i == l)
				printf("%d = %d", a[i], n);
			else
				printf("%d + ", a[i]);
		}
	printf("\n");	
	summand(a, l, n);
	scanf("%");
	return 0;
}
Ejemplo n.º 4
0
AlkValue::AlkValue(const QString & str, const QChar & decimalSymbol) :
    d(new Private)
{
  // empty strings are easy
  if (str.isEmpty()) {
    return;
  }

  // take care of mixed prices of the form "5 8/16" as well
  // as own internal string representation
  QRegExp regExp(QLatin1String("^((\\d+)\\s+|-)?(\\d+/\\d+)"));
  //                               +-#2-+        +---#3----+
  //                              +-----#1-----+
  if (regExp.indexIn(str) > -1) {
    d->m_val = qPrintable(str.mid(regExp.pos(3)));
    d->m_val.canonicalize();
    const QString &part1 = regExp.cap(1);
    if (!part1.isEmpty()) {
      if (part1 == QLatin1String("-")) {
        mpq_neg(d->m_val.get_mpq_t(), d->m_val.get_mpq_t());

      } else {
        mpq_class summand(qPrintable(part1));
        mpq_add(d->m_val.get_mpq_t(), d->m_val.get_mpq_t(), summand.get_mpq_t());
        d->m_val.canonicalize();
      }
    }
    return;
  }

  // qDebug("we got '%s' to convert", qPrintable(str));
  // everything else gets down here
  const QString negChars = QString::fromLatin1("\\-\\(\\)");
  const QString validChars = QString::fromLatin1("\\d\\%1%2").arg(decimalSymbol, negChars);
  QRegExp invCharSet(QString::fromLatin1("[^%1]").arg(validChars));
  QRegExp negCharSet(QString::fromLatin1("[%1]").arg(negChars));

  QString res(str);
  // get rid of any character that is not allowed.
  res.remove(invCharSet);

  // qDebug("we reduced it to '%s'", qPrintable(res));
  // check if number is negative
  bool isNegative = false;
  if (res.indexOf(negCharSet) != -1) {
    isNegative = true;
    res.remove(negCharSet);
  }

  // qDebug("and modified it to '%s'", qPrintable(res));
  // if someone uses the decimal symbol more than once, we get
  // rid of them except the right most one
  int pos;
  while (res.count(decimalSymbol) > 1) {
    pos = res.indexOf(decimalSymbol);
    res.remove(pos, 1);
  }

  // take care of any fractional part
  pos = res.indexOf(decimalSymbol);
  int len = res.length();
  QString fraction = QString::fromLatin1("/1");
  if ((pos != -1) && (pos < len)) {
    fraction += QString(len - pos - 1, QLatin1Char('0'));
    res.remove(pos, 1);
  }

  // check if the resulting numerator contains any leading zeros ...
  int cnt = 0;
  len = res.length() - 1;
  while (res[cnt] == QLatin1Char('0') && cnt < len) {
    ++cnt;
  }

  // ... and remove them
  if (cnt) {
    res.remove(0, cnt);
  }

  // in case the numerator is empty, we convert it to "0"
  if (res.isEmpty()) {
    res = QLatin1Char('0');
  }
  res += fraction;

  // looks like we now have a pretty normalized string that we
  // can convert right away
  // qDebug("and try to convert '%s'", qPrintable(res));
  try {
    d->m_val = mpq_class(qPrintable(res));
  } catch (const std::invalid_argument &) {
    qWarning("Invalid argument '%s' to mpq_class() in AlkValue. Arguments to ctor: '%s', '%c'", qPrintable(res), qPrintable(str), decimalSymbol.toLatin1());
    d->m_val = mpq_class(0);
  }
  d->m_val.canonicalize();

  // now we make sure that we use the right sign
  if (isNegative) {
    d->m_val = -d->m_val;
  }
}
Ejemplo n.º 5
0
void coeff(int p, int d, struct item* coeff_list) {
    int i, j, u, n, index_coeff_list, order_im, order_km, address;
    int* jm = (int*) malloc(p*sizeof(int));     /* Multiindex j */
    int* im = (int*) malloc(p*sizeof(int));     /* Multiindex i */
    int* km = (int*) malloc(p*sizeof(int));     /* Multiindex k */
    struct item* ptr;
    double sum;
    long binomiZ;           /* whole number binomial coefficient */

    jm[0] = d;
    for (i=1; i<p; i++) jm[i] = im[i] = 0;
    for (i=0; i<p; i++) km[i] = 0;
    order_km = 0;

    for (index_coeff_list = 0; 1; index_coeff_list++) {  /* travers coeff_list, i.e. create all j with |j| = d. */
        ptr = NULL;
        for (order_im=1; order_im<=d; order_im++) {  /* travers all orders from 1 to d for i */
            im[p-1]=0;
            im[0] = order_im;
            while (1) {   /* create all i with order order_im. */
                sum = 0;
                binomiZ = 1;
                for (i=0; i<p; i++) /* check, whether i valid. */
                    if ((jm[i]>0)&&(im[i]==0)) break;
                if (i==p)
                    while (1) {   /* create all k where 0<k<=i */
                        for (i=p-1; i>=0; i--)
                            if (km[i]<im[i]) {
                                km[i]++;
                                order_km++;
                                binomiZ *= im[i]-km[i]+1;  /* for (i over k)/(i over k-1) = (i-k+1)/k */
                                binomiZ /= km[i];
                                break;
                            } else {
                                /* binomiZ doesn't change, for (i over k) = 1 if k=0 and also if k=i */
                                order_km -= km[i];
                                km[i] = 0;
                            };
                        if (i==-1) break;

                        sum += summand(p,d,jm,km,order_im,order_km,binomiZ);
                    };

                if (sum!=0) { /* Store coefficient */
                    if (ptr==NULL)
                        ptr = &coeff_list[index_coeff_list];
                    else {
                        ptr->next = (struct item*) malloc(sizeof(struct item));
                        ptr = ptr->next;
                    };

                    address = 0; /* calculate address for ptr->a */
                    j = d-order_im+1;
                    for (u=0; u<p; u++)  /* It is sum(binomial(i+k,j+k),k=0..n-1) = */
                        if (im[u]!=0)      /* = ((j+n)*binomial(i+n,j+n)-j*binomial(i,j))/(1+i-j) */
                        {
                            i = u+j;
                            n = im[u];
                            address += ((j+n)*binomi(i+n,j+n)-j*binomi(i,j))/(1+i-j);
                            j += n;
                        };
                    ptr->a = address;
                    ptr->b = order_im;
                    ptr->c = sum;
                };

                if ((im[p-1]==order_im)||(p==1)) break;
                for (i=p-2; im[i]==0; i--); /* find first nonvanishing entry on the right. */
                im[i]--;
                im[i+1] = im[p-1]+1;
                if (i!=p-2) im[p-1] = 0;
            };
        };

        ptr->next = NULL; /* mark end of queue. */

        if ((jm[p-1]==d)||(p==1)) break;
        for (i=p-2; jm[i]==0; i--); /* find first nonvanishing entry on the right. */
        jm[i]--;
        jm[i+1] = jm[p-1]+1;
        if (i!=p-2) jm[p-1] = 0;
    };

    free((char*) jm);
    free((char*) im);
    free((char*) km);
};
Ejemplo n.º 6
0
Archivo: main.cpp Proyecto: CCJY/coliru
 static int get(T0 t) {
     return summand(t);
 }
Ejemplo n.º 7
0
Archivo: main.cpp Proyecto: CCJY/coliru
 static int get(T0 arg, T... args) {
     return summand(arg) + Sum<N - 1, T...>::get(args...);
 }