string integer_disp(void* v){ char* disp=fmpz_get_str(NULL,10,(fmpz*)v); string res="\033[34m"; res+=disp; res+="\033[0m"; free(disp); return res; }
/* * Class: com_rebsea_jalint_jni_Jalint * Method: simpleExample_flintPow2 * Signature: (J)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_rebsea_jalint_jni_Jalint_simpleExample_1flintPow2 (JNIEnv *env, jclass obj, jlong var) { UNUSED(obj); fmpz_t x, y; fmpz_init(x); fmpz_init(y); fmpz_set_ui(x, (mp_limb_t)var); fmpz_mul(y, x, x); // Returns the representation of f in base b, which can vary between 2 and 62, inclusive. // If str is NULL, the result string is allocated by the function. Otherwise, it is up to the // caller to ensure that the allocated block of memory is sufficiently large. // // @warning flint::fmpz_get_str() gmp::mpz_get_str() which allocates a buffer, but doesn't do // a NULL check before using the buffer. The process will crash with SIGSEGV in this situation // I can do nothing to help with that here, but I will do a NULL check just in case // the implimentation of _gmp_ library changes char *fmpzStr = fmpz_get_str(NULL, FMPZ_GET_STR_DEFAULT_BASE, y); fmpz_clear(x); fmpz_clear(y); jstring result = NULL; if(fmpzStr != NULL) { // @note result may be NULL after calling this method indicating error. // I don't know what to do with it right now, so just returning it back // @see https://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzaha%2Fjniex.htm result = (*env)->NewStringUTF(env, fmpzStr); free(fmpzStr); } return result; }
void ARingZZ::elem_text_out(buffer &o, const ElementType& a, bool p_one, bool p_plus, bool p_parens) const { char *str; bool is_neg = (fmpz_cmp_si(&a, 0) == -1); bool is_one = (fmpz_cmp_si(&a, 1) == 0 || fmpz_cmp_si(&a, -1) == 0); if (!is_neg && p_plus) o << '+'; if (is_one) { if (is_neg) o << '-'; if (p_one) o << '1'; } else { str = fmpz_get_str(static_cast<char*>(0), 10, &a); o << str; } }
char * _padic_get_str(char * str, const padic_t op, const padic_ctx_t ctx) { const fmpz * u = padic_unit(op); const long v = padic_val(op); if (fmpz_is_zero(u)) { if (!str) { str = flint_malloc(2); } str[0] = '0'; str[1] = '\0'; return str; } if (ctx->mode == PADIC_TERSE) { if (v == 0) { str = fmpz_get_str(str, 10, u); } else if (v > 0) { fmpz_t t; fmpz_init(t); fmpz_pow_ui(t, ctx->p, v); fmpz_mul(t, t, u); str = fmpz_get_str(str, 10, t); fmpz_clear(t); } else /* v < 0 */ { fmpz_t t; fmpz_init(t); fmpz_pow_ui(t, ctx->p, -v); str = _fmpq_get_str(str, 10, u, t); fmpz_clear(t); } } else if (ctx->mode == PADIC_SERIES) { char *s; fmpz_t x; fmpz_t d; long j, N; if (fmpz_sgn(u) < 0) { printf("ERROR (_padic_get_str). u < 0 in SERIES mode.\n"); abort(); } N = fmpz_clog(u, ctx->p) + v; if (!str) { long b = (N - v) * (2 * fmpz_sizeinbase(ctx->p, 10) + z_sizeinbase(FLINT_MAX(FLINT_ABS(v), FLINT_ABS(N)), 10) + 5) + 1; str = flint_malloc(b); if (!str) { printf("ERROR (_padic_get_str). Memory allocation failed.\n"); abort(); } } s = str; fmpz_init(d); fmpz_init(x); fmpz_set(x, u); /* Unroll first step */ j = 0; { fmpz_mod(d, x, ctx->p); /* d = u mod p^{j+1} */ fmpz_sub(x, x, d); /* x = x - d */ fmpz_divexact(x, x, ctx->p); /* x = x / p */ if (!fmpz_is_zero(d)) { if (j + v != 0) { fmpz_get_str(s, 10, d); while (*++s != '\0') ; *s++ = '*'; fmpz_get_str(s, 10, ctx->p); while (*++s != '\0') ; *s++ = '^'; sprintf(s, "%ld", j + v); while (*++s != '\0') ; } else { fmpz_get_str(s, 10, d); while (*++s != '\0') ; } } j++; } for ( ; !fmpz_is_zero(x); j++) { fmpz_mod(d, x, ctx->p); /* d = u mod p^{j+1} */ fmpz_sub(x, x, d); /* x = x - d */ fmpz_divexact(x, x, ctx->p); /* x = x / p */ if (!fmpz_is_zero(d)) { if (j + v != 0) { *s++ = ' '; *s++ = '+'; *s++ = ' '; fmpz_get_str(s, 10, d); while (*++s != '\0') ; *s++ = '*'; fmpz_get_str(s, 10, ctx->p); while (*++s != '\0') ; *s++ = '^'; sprintf(s, "%ld", j + v); while (*++s != '\0') ; } else { *s++ = ' '; *s++ = '+'; *s++ = ' '; fmpz_get_str(s, 10, d); while (*++s != '\0') ; } } } fmpz_clear(x); fmpz_clear(d); } else /* ctx->mode == PADIC_VAL_UNIT */ { if (!str) { long b = fmpz_sizeinbase(u, 10) + fmpz_sizeinbase(ctx->p, 10) + z_sizeinbase(v, 10) + 4; str = flint_malloc(b); if (!str) { printf("ERROR (_padic_get_str). Memory allocation failed.\n"); abort(); } } if (v == 0) { str = fmpz_get_str(str, 10, u); } else if (v == 1) { char *s = str; fmpz_get_str(s, 10, u); while (*++s != '\0') ; *s++ = '*'; fmpz_get_str(s, 10, ctx->p); } else { char *s = str; fmpz_get_str(s, 10, u); while (*++s != '\0') ; *s++ = '*'; fmpz_get_str(s, 10, ctx->p); while (*++s != '\0') ; *s++ = '^'; sprintf(s, "%ld", v); } } return str; }
char * _fmpz_poly_get_str_pretty(const fmpz * poly, long len, const char *x) { char *str; size_t off; long i, bound, nz; if (len == 0) { str = flint_malloc(2); str[0] = '0'; str[1] = '\0'; return str; } if (len == 1) { str = fmpz_get_str(NULL, 10, poly); return str; } nz = 0; bound = 1; for (i = 0; i < len; i++) if (!fmpz_is_zero(poly + i)) { bound += fmpz_sizeinbase(poly + i, 10) + 1; nz++; } bound += nz * (3 + strlen(x) + (long) (ceil(log10(len)))); str = flint_malloc(bound); off = 0; i = len - 1; if (poly[i] == 1L) { } else if (poly[i] == -1L) str[off++] = '-'; else if (!COEFF_IS_MPZ(poly[i])) off += sprintf(str + off, "%ld*", poly[i]); else off += gmp_sprintf(str + off, "%Zd*", COEFF_TO_PTR(poly[i])); if (i > 1) off += sprintf(str + off, "%s^%ld", x, i); else off += sprintf(str + off, "%s", x); for (--i; i > 0; --i) { if (poly[i] == 0L) continue; if (fmpz_sgn(poly + i) > 0) str[off++] = '+'; if (poly[i] == -1L) str[off++] = '-'; if (poly[i] != 1L && poly[i] != -1L) { if (!COEFF_IS_MPZ(poly[i])) off += sprintf(str + off, "%ld*", poly[i]); else off += gmp_sprintf(str + off, "%Zd*", COEFF_TO_PTR(poly[i])); } if (i > 1) off += sprintf(str + off, "%s^%ld", x, i); else off += sprintf(str + off, "%s", x); } if (poly[i] != 0L) { if (fmpz_sgn(poly + i) > 0) str[off++] = '+'; if (!COEFF_IS_MPZ(poly[i])) off += sprintf(str + off, "%ld", poly[i]); else off += gmp_sprintf(str + off, "%Zd", COEFF_TO_PTR(poly[i])); } return str; }