
// Make newform 24.4.f.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_24_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_24_f_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_24_4_f_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_24_4_f_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        poly := [4096, 0, -640, 0, 120, 0, -10, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [-3, 0, 1, 0, 0, 0, 0, 0], [0, -384, 0, 120, 0, -10, 0, 1], [384, -160, -56, 8, 10, -4, -1, 0], [256, 160, -56, -8, 10, 4, -1, 0], [2048, -192, -16, 24, 44, 2, 2, -1], [-2048, -192, 16, 24, -44, 2, -2, -1]];
    Rf_basisdens := [1, 1, 1, 256, 128, 128, 256, 256];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_24_f();"
function MakeCharacter_24_f()
    N := 24;
    order := 2;
    char_gens := [7, 13, 17];
    v := [1, 1, 1];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_24_f_Hecke();"
function MakeCharacter_24_f_Hecke(Kf)
    N := 24;
    order := 2;
    char_gens := [7, 13, 17];
    char_values := [[-1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 4;
    raw_aps := [[0, 1, 0, 0, 0, 0, 0, 0], [-2, 0, 0, 0, 1, 0, 0, 0], [0, -1, 0, 1, 0, 0, -1, -1], [-2, 0, -2, 0, 0, 0, 1, -1], [-1, 2, 0, 2, 1, -1, 0, 0], [0, 0, -4, 0, 4, 4, -2, 2], [4, 2, 0, 2, -4, 4, 0, 0], [23, 0, 0, 0, 11, 11, 0, 0], [0, 28, 0, -12, 0, 0, 4, 4], [0, -45, 0, 13, 0, 0, 3, 3], [-2, 0, -10, 0, 8, 8, -3, 3], [-16, 0, -12, 0, -4, -4, 10, -10], [-28, 32, 0, 32, 28, -28, 0, 0], [-19, 0, 0, 0, -39, -39, 0, 0], [0, 72, 0, -8, 0, 0, -24, -24], [0, -89, 0, 25, 0, 0, 7, 7], [11, 8, 0, 8, -11, 11, 0, 0], [48, 0, 76, 0, -28, -28, -10, 10], [-187, 0, 0, 0, 9, 9, 0, 0], [0, 68, 0, -52, 0, 0, 44, 44], [134, 0, 0, 0, -148, -148, 0, 0], [14, 0, 70, 0, -56, -56, 21, -21], [-21, 62, 0, 62, 21, -21, 0, 0], [-112, -106, 0, -106, 112, -112, 0, 0], [-484, 0, 0, 0, 90, 90, 0, 0], [0, 127, 0, -31, 0, 0, -17, -17], [-82, 0, -130, 0, 48, 48, 17, -17], [-53, -76, 0, -76, 53, -53, 0, 0], [-80, 0, -132, 0, 52, 52, 14, -14], [144, -124, 0, -124, -144, 144, 0, 0], [-18, 0, -138, 0, 120, 120, -51, 51], [87, 212, 0, 212, -87, 87, 0, 0], [-196, 140, 0, 140, 196, -196, 0, 0], [1751, 0, 0, 0, 3, 3, 0, 0], [0, 599, 0, -215, 0, 0, 23, 23], [174, 0, 238, 0, -64, -64, -55, 55], [-192, 0, -244, 0, 52, 52, 70, -70], [-1441, 0, 0, 0, 99, 99, 0, 0], [0, -604, 0, 268, 0, 0, -100, -100], [0, -229, 0, 165, 0, 0, -133, -133], [-169, -160, 0, -160, 169, -169, 0, 0], [96, 0, 36, 0, 60, 60, -78, 78], [0, 432, 0, -272, 0, 0, 192, 192], [38, 0, 0, 0, -156, -156, 0, 0], [0, -881, 0, 337, 0, 0, -65, -65], [-194, 0, -66, 0, -128, -128, 161, -161], [-1555, 0, 0, 0, 193, 193, 0, 0], [318, 0, 694, 0, -376, -376, 29, -29], [131, -586, 0, -586, -131, 131, 0, 0], [-32, 0, -172, 0, 140, 140, -54, 54], [232, 638, 0, 638, -232, 232, 0, 0], [0, -376, 0, -8, 0, 0, 200, 200], [-778, 0, 0, 0, -348, -348, 0, 0], [559, -154, 0, -154, -559, 559, 0, 0], [-472, 140, 0, 140, 472, -472, 0, 0], [0, 1028, 0, -180, 0, 0, -244, -244], [0, -1781, 0, 757, 0, 0, -245, -245], [-210, 0, -570, 0, 360, 360, -75, 75], [-528, 0, -284, 0, -244, -244, 386, -386], [360, 314, 0, 314, -360, 360, 0, 0], [3167, 0, 0, 0, 667, 667, 0, 0], [0, 1279, 0, -351, 0, 0, -113, -113], [-2203, 0, 0, 0, -487, -487, 0, 0], [0, -2244, 0, 692, 0, 0, 84, 84], [-1672, 0, 0, 0, 366, 366, 0, 0], [0, -781, 0, -51, 0, 0, 467, 467], [7535, 0, 0, 0, -293, -293, 0, 0], [944, 0, 0, 0, -354, -354, 0, 0], [815, 254, 0, 254, -815, 815, 0, 0], [704, 0, 460, 0, 244, 244, -474, 474], [-448, -616, 0, -616, 448, -448, 0, 0], [0, 1204, 0, -932, 0, 0, 796, 796], [-50, 0, -26, 0, -24, -24, 37, -37], [768, 0, 932, 0, -164, -164, -302, 302], [1133, 0, 0, 0, 1961, 1961, 0, 0], [0, -3824, 0, 1296, 0, 0, -32, -32], [0, -257, 0, 257, 0, 0, -257, -257], [-368, 0, -36, 0, -332, -332, 350, -350], [-436, -1378, 0, -1378, 436, -436, 0, 0], [-2350, 0, 0, 0, -696, -696, 0, 0], [219, 1562, 0, 1562, -219, 219, 0, 0], [-512, 0, -812, 0, 300, 300, 106, -106], [0, -664, 0, 920, 0, 0, -1048, -1048], [-7012, 0, 0, 0, -1558, -1558, 0, 0], [-338, 0, -626, 0, 288, 288, 25, -25], [1607, -22, 0, -22, -1607, 1607, 0, 0], [-116, 314, 0, 314, 116, -116, 0, 0], [1634, 0, 0, 0, 2376, 2376, 0, 0], [0, 731, 0, -123, 0, 0, -181, -181], [830, 0, 1382, 0, -552, -552, -139, 139], [115, -1798, 0, -1798, -115, 115, 0, 0], [0, 1552, 0, -784, 0, 0, 400, 400], [974, 0, 318, 0, 656, 656, -815, 815], [-1261, 1616, 0, 1616, 1261, -1261, 0, 0], [-2371, 0, 0, 0, -591, -591, 0, 0], [0, 2748, 0, -1548, 0, 0, 948, 948], [0, 1587, 0, -691, 0, 0, 243, 243], [-788, -1708, 0, -1708, 788, -788, 0, 0], [2645, 0, 0, 0, -1887, -1887, 0, 0], [-944, 0, -1812, 0, 868, 868, 38, -38], [5543, 0, 0, 0, 2475, 2475, 0, 0], [0, -5269, 0, 1397, 0, 0, 539, 539], [-1549, -46, 0, -46, 1549, -1549, 0, 0], [-508, 1124, 0, 1124, 508, -508, 0, 0], [-2065, 0, 0, 0, 1611, 1611, 0, 0], [4880, 0, 0, 0, 302, 302, 0, 0], [-1237, 188, 0, 188, 1237, -1237, 0, 0], [1112, -40, 0, -40, -1112, 1112, 0, 0], [0, 1212, 0, 212, 0, 0, -924, -924], [11222, 0, 0, 0, -1908, -1908, 0, 0], [334, 0, 278, 0, 56, 56, -195, 195], [880, 0, 2100, 0, -1220, -1220, 170, -170], [-784, 1466, 0, 1466, 784, -784, 0, 0], [4991, 0, 0, 0, 267, 267, 0, 0], [-802, 0, -786, 0, -16, -16, 409, -409], [1316, 1406, 0, 1406, -1316, 1316, 0, 0], [-13369, 0, 0, 0, -981, -981, 0, 0], [0, 4820, 0, -2020, 0, 0, 620, 620], [0, -645, 0, -283, 0, 0, 747, 747], [567, 1064, 0, 1064, -567, 567, 0, 0], [304, 0, 740, 0, -436, -436, 66, -66], [7484, 0, 0, 0, 1978, 1978, 0, 0], [0, -1537, 0, -351, 0, 0, 1295, 1295], [-521, -2674, 0, -2674, 521, -521, 0, 0], [11375, 0, 0, 0, -2429, -2429, 0, 0], [0, 4259, 0, -1411, 0, 0, -13, -13], [-2192, 0, -2156, 0, -36, -36, 1114, -1114], [0, -2264, 0, 1528, 0, 0, -1160, -1160], [-546, 0, -434, 0, -112, -112, 329, -329], [480, 0, 2060, 0, -1580, -1580, 550, -550], [6365, 0, 0, 0, -4159, -4159, 0, 0], [0, -1372, 0, 844, 0, 0, -580, -580], [-610, 0, -1594, 0, 984, 984, -187, 187], [-512, 0, -1500, 0, 988, 988, -238, 238], [-1956, -3256, 0, -3256, 1956, -1956, 0, 0], [8390, 0, 0, 0, -1724, -1724, 0, 0], [0, 1903, 0, -527, 0, 0, -161, -161], [-2161, 0, 0, 0, 2563, 2563, 0, 0], [0, -3885, 0, 2957, 0, 0, -2493, -2493], [244, 3608, 0, 3608, -244, 244, 0, 0], [-13531, 0, 0, 0, 1937, 1937, 0, 0], [0, 7895, 0, -2487, 0, 0, -217, -217], [574, 0, 494, 0, 80, 80, -327, 327], [899, 3692, 0, 3692, -899, 899, 0, 0], [512, 0, -468, 0, 980, 980, -746, 746], [0, -1948, 0, 492, 0, 0, 236, 236], [2528, 0, 3268, 0, -740, -740, -894, 894], [2100, -2956, 0, -2956, -2100, 2100, 0, 0], [11693, 0, 0, 0, -1335, -1335, 0, 0], [0, -3840, 0, 2784, 0, 0, -2256, -2256], [-1696, 0, -3940, 0, 2244, 2244, -274, 274], [800, -2500, 0, -2500, -800, 800, 0, 0], [27335, 0, 0, 0, -229, -229, 0, 0], [0, 5564, 0, -2444, 0, 0, 884, 884], [-31651, 0, 0, 0, -2391, -2391, 0, 0], [0, -11016, 0, 3592, 0, 0, 120, 120], [-1058, 0, -3954, 0, 2896, 2896, -919, 919], [-3372, 590, 0, 590, 3372, -3372, 0, 0], [-22348, 0, 0, 0, -1830, -1830, 0, 0], [0, -2997, 0, -427, 0, 0, 2139, 2139], [-2161, -6556, 0, -6556, 2161, -2161, 0, 0], [1204, -3028, 0, -3028, -1204, 1204, 0, 0], [-3666, 0, -4642, 0, 976, 976, 1345, -1345], [3623, 4478, 0, 4478, -3623, 3623, 0, 0], [-4204, 2858, 0, 2858, 4204, -4204, 0, 0], [0, 556, 0, -700, 0, 0, 772, 772], [4142, 0, 3414, 0, 728, 728, -2435, 2435], [3472, 0, 4468, 0, -996, -996, -1238, 1238]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_24_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_24_4_f_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_24_4_f_b(:prec:=8)
    chi := MakeCharacter_24_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_24_4_f_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_24_4_f_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_24_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![26112, 0, -324, 0, 1]>],Snew);
    return Vf;
end function;