Created
May 14, 2026 00:42
-
-
Save fredrik-johansson/5d871be9743498bda9c9c305327b2f0d to your computer and use it in GitHub Desktop.
Example program (generated by Claude)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Demonstration of qqbar_sgn_re from FLINT. | |
| * | |
| * We construct three sums of nontrivial complex algebraic numbers whose | |
| * real parts are negative, zero, and positive respectively, using roots | |
| * of unity via qqbar_root_of_unity(res, p, q) = exp(2*pi*i * p/q). | |
| * | |
| * Example 1 (negative): | |
| * zeta_3 + zeta_3^2 = e^(2pi*i/3) + e^(2pi*i*2/3) | |
| * Each summand is a primitive cube root of unity with Re = -1/2. | |
| * Sum has Re = -1. | |
| * | |
| * Example 2 (zero): | |
| * zeta_8 + zeta_8^3 = e^(2pi*i/8) + e^(2pi*i*3/8) | |
| * Re(zeta_8) = cos(pi/4) = +sqrt(2)/2 | |
| * Re(zeta_8^3) = cos(3*pi/4) = -sqrt(2)/2 | |
| * Sum has Re = 0. | |
| * | |
| * Example 3 (positive): | |
| * zeta_5 + zeta_5^4 = e^(2pi*i/5) + e^(2pi*i*4/5) | |
| * These are complex conjugates, so Im cancels. | |
| * Re = 2*cos(2*pi/5) = (sqrt(5)-1)/2 > 0. | |
| */ | |
| #include <stdio.h> | |
| #include <flint/qqbar.h> | |
| static void print_sgn(int s) | |
| { | |
| if (s < 0) printf("negative (-1)\n"); | |
| else if (s > 0) printf("positive (+1)\n"); | |
| else printf("zero (0)\n"); | |
| } | |
| int main(void) | |
| { | |
| qqbar_t a, b, sum; | |
| qqbar_init(a); | |
| qqbar_init(b); | |
| qqbar_init(sum); | |
| /* --- Example 1: Re(zeta_3 + zeta_3^2) = -1 (negative) --- */ | |
| printf("Example 1: zeta_3 + zeta_3^2, where zeta_3 = exp(2*pi*i/3)\n"); | |
| printf(" zeta_3 = "); qqbar_root_of_unity(a, 1, 3); qqbar_printn(a, 6); printf("\n"); | |
| printf(" zeta_3^2 = "); qqbar_root_of_unity(b, 2, 3); qqbar_printn(b, 6); printf("\n"); | |
| qqbar_add(sum, a, b); | |
| printf(" sum = "); qqbar_printn(sum, 6); printf("\n"); | |
| printf(" sgn_re = "); | |
| print_sgn(qqbar_sgn_re(sum)); | |
| printf("\n"); | |
| /* --- Example 2: Re(zeta_8 + zeta_8^3) = 0 (zero) --- */ | |
| printf("Example 2: zeta_8 + zeta_8^3, where zeta_8 = exp(2*pi*i/8)\n"); | |
| printf(" zeta_8 = "); qqbar_root_of_unity(a, 1, 8); qqbar_printn(a, 6); printf("\n"); | |
| printf(" zeta_8^3 = "); qqbar_root_of_unity(b, 3, 8); qqbar_printn(b, 6); printf("\n"); | |
| qqbar_add(sum, a, b); | |
| printf(" sum = "); qqbar_printn(sum, 6); printf("\n"); | |
| printf(" sgn_re = "); | |
| print_sgn(qqbar_sgn_re(sum)); | |
| printf("\n"); | |
| /* --- Example 3: Re(zeta_5 + zeta_5^4) = (sqrt(5)-1)/2 > 0 (positive) --- */ | |
| printf("Example 3: zeta_5 + zeta_5^4, where zeta_5 = exp(2*pi*i/5)\n"); | |
| printf(" zeta_5 = "); qqbar_root_of_unity(a, 1, 5); qqbar_printn(a, 6); printf("\n"); | |
| printf(" zeta_5^4 = "); qqbar_root_of_unity(b, 4, 5); qqbar_printn(b, 6); printf("\n"); | |
| qqbar_add(sum, a, b); | |
| printf(" sum = "); qqbar_printn(sum, 6); printf("\n"); | |
| printf(" sgn_re = "); | |
| print_sgn(qqbar_sgn_re(sum)); | |
| printf("\n"); | |
| qqbar_clear(a); | |
| qqbar_clear(b); | |
| qqbar_clear(sum); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment