Beispiel #1
0
// TODO: adjust sign
int mpf_gamma(mp_float * a, mp_float * b)
{
    int err;
    long oldeps, eps;
    mp_float t;

    err = MP_OKAY;

    oldeps = a->radix;
    eps = oldeps + MP_DIGIT_BIT;
    if ((err = mpf_init(&t, oldeps)) != MP_OKAY) {
	return err;
    }
    if ((err = mpf_copy(a, &t)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_normalize_to(&t, eps)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_lngamma(&t, &t)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_exp(&t, &t)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_normalize_to(&t, oldeps)) != MP_OKAY) {
	goto _ERR;
    }
    mpf_exch(&t, b);
_ERR:
    mpf_clear(&t);
    return err;
}
Beispiel #2
0
// only (exp(log(gamma(a)))) for now
int mpf_lngamma(mp_float * a, mp_float * b)
{
    int oldeps, eps, err, n, accuracy;
    size_t A;
    mp_float z, ONE, factrl, e, pi, t1, t2, t3, sum;

    err = MP_OKAY;

    if (mpf_iszero(a)) {
	// raise singularity error
	return MP_VAL;
    }
    // Check is expensive and inexact.
    // if(mpf_isint(a){...}

    // very near zero
    if (a->exp + a->radix < -(a->radix)) {
	if ((err = mpf_ln(a, b)) != MP_OKAY) {
	    return err;
	}
	return err;
    }

    oldeps = a->radix;

    if (reflection == 1) {
	// angst-allowance seemed not necessary
	// TODO: check if that is really true
	eps = oldeps;
    } else {
	accuracy = oldeps + MP_DIGIT_BIT;
	A = (size_t) ceil(0.37714556279552730250018797240191093794 * accuracy);
	// We need to compute the coefficients as exact as possible, so
	// increase the working precision acccording to the largest coefficient
	// TODO: probably too much
	eps = ((accuracy * 116) / 100);
	if ((err = fill_spougecache(A, accuracy, eps)) != MP_OKAY) {
	    return err;
	}
    }


    if ((err =
	 mpf_init_multi(eps, &z, &ONE, &factrl, &e, &t1, &t2, &t3, &sum,
			NULL)) != MP_OKAY) {
	return err;
    }

    if ((err = mpf_copy(a, &z)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_normalize_to(&z, eps)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_set_int(&ONE, 1)) != MP_OKAY) {
	goto _ERR;
    }
    //   printf("splen = %u, A = %u, seps = %ld, acc = %d\n",
    //    spougecache_len , A , spougecache_eps , accuracy);

    if (a->mantissa.sign == MP_NEG) {
	//   eps = oldeps + 10;
	if ((err = mpf_copy(a, &z)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_normalize_to(&z, eps)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_init(&pi, eps)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_const_pi(&pi)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_normalize_to(&ONE, eps)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_sub(&ONE, &z, &t1)) != MP_OKAY) {
	    goto _ERR;
	}

	if ((err = mpf_mul(&pi, &z, &t2)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_sin(&t2, &t2)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_div(&pi, &t2, &t2)) != MP_OKAY) {
	    goto _ERR;
	}
	t2.mantissa.sign = MP_ZPOS;
	if ((err = mpf_ln(&t2, &t2)) != MP_OKAY) {
	    goto _ERR;
	}
	reflection = 1;
	if ((err = mpf_lngamma(&t1, &t1)) != MP_OKAY) {
	    goto _ERR;
	}
	reflection = 0;
	if ((err = mpf_sub(&t2, &t1, &t1)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_normalize_to(&t1, oldeps)) != MP_OKAY) {
	    goto _ERR;
	}
	mpf_exch(&t1, b);
	goto _ERR;
    }

    if ((err = mpf_copy(&(spougecache[0]), &sum)) != MP_OKAY) {
	goto _ERR;
    }
    for (n = 1; n < (int) spougecache_len; n++) {
	if ((err = mpf_const_d(&t1, n)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_add(&z, &t1, &t1)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_div(&(spougecache[n]), &t1, &t1)) != MP_OKAY) {
	    goto _ERR;
	}
	if ((err = mpf_add(&sum, &t1, &sum)) != MP_OKAY) {
	    goto _ERR;
	}
    }
    if ((err = mpf_const_d(&t1, (int) spougecache_len)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_add(&z, &t1, &t1)) != MP_OKAY) {
	goto _ERR;
    }
    // 1/2
    ONE.exp -= 1;

    //ret = log(sum) + (-(t1)) + log(t1) * (z + 1/2);
    //    = log(sum) + t2 + log(t1) * (z + 1/2);
    //    = log(sum) + t2 + log(t1) * t3
    if ((err = mpf_neg(&t1, &t2)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_add(&z, &ONE, &t3)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_ln(&sum, &sum)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_ln(&t1, &t1)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_mul(&t1, &t3, &t3)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_add(&sum, &t2, &sum)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_add(&sum, &t3, &sum)) != MP_OKAY) {
	goto _ERR;
    }
    // ret = ret - log(z)
    if ((err = mpf_ln(&z, &t1)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_sub(&sum, &t1, &t1)) != MP_OKAY) {
	goto _ERR;
    }
    if ((err = mpf_normalize_to(&t1, oldeps)) != MP_OKAY) {
	goto _ERR;
    }
    mpf_exch(&t1, b);
_ERR:
    mpf_clear_multi(&z, &ONE, &factrl, &e, &t1, &t2, &t3, &sum, NULL);
    return err;
}
Beispiel #3
0
/* using sin x == \sum_{n=0}^{\infty} ((-1)^n/(2n+1)!) * x^(2n+1) */
int  mpf_sin(mp_float *a, mp_float *b)
{
   mp_float oldval, tmpovern, tmp, tmpx, res, sqr;
   int      oddeven, err, itts;
   long     n;

   /* initialize temps */
   if ((err = mpf_init_multi(b->radix, &oldval, &tmpx, &tmpovern, &tmp, &res, &sqr, NULL)) != MP_OKAY) {
      return err;
   }

   /* initlialize temps */
   /* this is the 1/n! which starts at 1 */
   if ((err = mpf_const_d(&tmpovern, 1)) != MP_OKAY)                                    { goto __ERR; }

   /* the square of the input, used to save multiplications later */
   if ((err = mpf_sqr(a, &sqr)) != MP_OKAY)                                             { goto __ERR; }

   /* tmpx starts at the input, so we copy and normalize */
   if ((err = mpf_copy(a, &tmpx)) != MP_OKAY)                                           { goto __ERR; }
   tmpx.radix = b->radix;
   if ((err = mpf_normalize(&tmpx)) != MP_OKAY)                                         { goto __ERR; }

   /* the result starts off at a as we skip a term in the series */
   if ((err = mpf_copy(&tmpx, &res)) != MP_OKAY)                                        { goto __ERR; }

   /* this is the denom counter.  Goes up by two per pass */
   n       = 1;

   /* we alternate between adding and subtracting */
   oddeven = 1;

   /* get number of iterations */
   itts = mpf_iterations(b);

   while (itts-- > 0) {
       if ((err = mpf_copy(&res, &oldval)) != MP_OKAY)                                  { goto __ERR; }

       /* compute 1/(2n)! from 1/(2(n-1))! by multiplying by (1/n)(1/(n+1)) */
       if ((err = mpf_const_d(&tmp, ++n)) != MP_OKAY)                                   { goto __ERR; }
       if ((err = mpf_inv(&tmp, &tmp)) != MP_OKAY)                                      { goto __ERR; }
       if ((err = mpf_mul(&tmpovern, &tmp, &tmpovern)) != MP_OKAY)                      { goto __ERR; }
       /* we do this twice */
       if ((err = mpf_const_d(&tmp, ++n)) != MP_OKAY)                                   { goto __ERR; }
       if ((err = mpf_inv(&tmp, &tmp)) != MP_OKAY)                                      { goto __ERR; }
       if ((err = mpf_mul(&tmpovern, &tmp, &tmpovern)) != MP_OKAY)                      { goto __ERR; }

       /* now multiply sqr into tmpx */
       if ((err = mpf_mul(&tmpx, &sqr, &tmpx)) != MP_OKAY)                              { goto __ERR; }

       /* now multiply the two */
       if ((err = mpf_mul(&tmpx, &tmpovern, &tmp)) != MP_OKAY)                          { goto __ERR; }

       /* now depending on if this is even or odd we add/sub */
       oddeven ^= 1;
       if (oddeven  == 1) {
          if ((err = mpf_add(&res, &tmp, &res)) != MP_OKAY)                             { goto __ERR; }
       } else {
          if ((err = mpf_sub(&res, &tmp, &res)) != MP_OKAY)                             { goto __ERR; }
       }

       if (mpf_cmp(&res, &oldval) == MP_EQ) {
          break;
       }
   }
   mpf_exch(&res, b);
__ERR: mpf_clear_multi(&oldval, &tmpx, &tmpovern, &tmp, &res, &sqr, NULL);
   return err;
}