
// Make newform 1728.3.q.h in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1728_q();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1728_q_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_1728_3_q_h();".
// 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_1728_3_q_h();".
// 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 := [9, -3, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-9, 16, 2, 1], [-9, -2, 2, 1], [12, 8, 1, -4]];
    Rf_basisdens := [1, 6, 6, 3];
    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_1728_q();"
function MakeCharacter_1728_q()
    N := 1728;
    order := 6;
    char_gens := [703, 325, 1217];
    v := [6, 6, 5];
    // 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_1728_q_Hecke();"
function MakeCharacter_1728_q_Hecke(Kf)
    N := 1728;
    order := 6;
    char_gens := [703, 325, 1217];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, -1, 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 := 3;
    raw_aps := [[0, 0, 0, 0], [0, 0, 0, 0], [3, -1, 1, 0], [1, -2, 0, 1], [7, 0, -5, 2], [1, -1, 3, 2], [7, 1, 15, -1], [1, 1, 1, 1], [33, 1, 17, 0], [-7, 0, 14, 7], [-3, 3, -5, -6], [18, 4, 4, 4], [6, -8, -1, 0], [-23, 0, -23, 0], [-15, 0, 12, -3], [16, -8, 24, 8], [-42, 8, -17, 0], [-19, -6, -22, 3], [-6, 6, -61, -12], [14, 2, 30, -2], [23, 3, 3, 3], [-39, -10, -44, 5], [15, 0, -12, 3], [-56, -8, -120, 8], [-99, 4, -97, -2], [73, 0, -74, -1], [-3, 3, -101, -6], [5, 11, 21, -11], [-80, 6, 6, 6], [99, -21, 39, 0], [-20, 18, 18, 18], [-21, -29, -25, 0], [-65, 0, 67, 2], [-2, 2, 43, -4], [-39, -23, -31, 0], [173, 18, 182, -9], [21, -21, -101, 42], [126, -4, -4, -4], [-9, -21, -15, 0], [-87, 0, 66, -21], [68, 20, 156, -20], [-108, -14, -14, -14], [1, 0, -38, -37], [0, 0, -49, 0], [56, -64, 48, 64], [134, 8, 8, 8], [17, -17, 249, 34], [-19, -30, -34, 15], [135, 0, -153, -18], [15, -15, 175, 30], [155, -19, 291, 19], [-135, 9, -63, 0], [65, -64, 33, 32], [77, -61, 93, 61], [-54, -12, -33, 0], [49, 0, -26, 23], [-92, -44, -228, 44], [-56, -42, -42, -42], [-307, 42, -286, -21], [-137, 0, 76, -61], [-3, 3, -367, -6], [123, 7, 65, 0], [-145, 3, 3, 3], [-429, 47, -191, 0], [-295, 8, -291, -4], [-23, 0, 28, 5], [-177, 82, -136, -41], [-38, 38, -29, -76], [234, 84, 159, 0], [13, -86, -30, 43], [223, 0, -257, -34], [270, 18, 558, -18], [113, -66, 80, 33], [1, -1, -141, 2], [225, -31, -31, -31], [-105, -85, -95, 0], [73, 0, -110, -37], [-194, -24, -24, -24], [-12, 10, -1, 0], [-38, 38, -173, -76], [243, -69, 87, 0], [97, 118, 156, -59], [422, -22, 822, 22], [47, 27, 27, 27], [-19, 114, 38, -57], [231, 0, -255, -24], [33, 87, 153, -87], [-107, 12, -101, -6], [137, 0, -148, -11], [19, -19, -393, 38], [185, -25, 345, 25], [81, 0, 18, 99], [218, 68, 68, 68], [264, -10, 127, 0], [54, -54, 107, 108], [-120, 24, -216, -24], [669, 5, 337, 0], [13, 43, 69, -43], [590, 12, 12, 12], [-210, -56, -56, -56], [-191, -72, -227, 36], [-46, 86, -6, -86], [-1134, 12, -561, 0], [287, 0, -151, 136], [-86, 86, 19, -172], [-121, 3, 3, 3], [215, 0, -61, 154], [340, 28, 708, -28], [-417, 67, -175, 0], [349, 132, 415, -66], [29, -29, 587, 58], [154, -36, -36, -36], [852, 10, 431, 0], [517, -60, 487, 30], [-534, 52, 52, 52], [-113, 0, -17, -130], [0, 0, -577, 0], [-378, 90, -666, -90], [915, 79, 497, 0], [-193, 0, 8, -185], [15, -15, -1073, 30], [-297, -158, -376, 79], [505, 0, -524, -19], [39, 57, 135, -57], [679, -54, 652, 27], [232, -80, 384, 80], [-27, 50, -2, -25], [-54, 198, 90, -198], [581, 90, 626, -45], [-105, 105, 223, -210], [575, 3, 3, 3], [-117, 87, -15, 0], [35, -35, 167, 70], [790, 48, 48, 48], [1395, 27, 711, 0], [-43, 43, 705, -86], [-156, -156, -468, 156], [69, -69, 593, 138], [-585, 75, -255, 0], [-551, -17, -1119, 17], [-223, 81, 81, 81], [313, 0, -374, -61], [7, -7, -273, 14], [-284, -92, -660, 92], [-466, -40, -40, -40], [273, 0, -204, 69], [-1171, -6, -1174, 3], [-297, 0, 252, -45], [4, -4, -521, 8], [-226, 50, -402, -50], [5, -5, -757, 10], [-532, 68, -996, -68], [-515, -59, -59, -59], [759, 11, 385, 0], [129, 136, 197, -68], [33, 0, -48, -15], [1252, 42, 42, 42], [279, 0, -180, 99], [-498, 116, 116, 116], [177, -47, 65, 0], [135, 0, -261, -126], [171, -243, 99, 243], [-129, 129, 223, -258], [-108, 324, 108, -324], [-456, -154, -305, 0], [-623, 0, 598, -25], [-248, 150, 150, 150], [917, -30, 902, 15], [410, 120, 120, 120], [237, 149, 193, 0], [-473, 0, 469, -4], [111, -111, 367, 222], [1029, 29, 529, 0], [113, -256, -15, 128], [-65, 65, 159, -130], [-69, -13, -41, 0], [61, 60, 91, -30], [198, 18, 414, -18], [-478, -100, -100, -100], [-247, -162, -328, 81], [-551, 262, -420, -131], [-217, 0, 137, -80], [-33, 33, -497, -66], [33, -345, -279, 345], [183, -181, 1, 0], [-887, 0, 820, -67], [292, -30, -30, -30], [-481, 50, -456, -25], [20, -20, -1833, 40], [-322, -46, -690, 46], [817, -23, -23, -23], [183, 0, 21, 204], [1791, -13, -13, -13], [-647, 0, 520, -127], [-569, 73, -1065, -73], [-1572, -94, -833, 0], [170, -170, 627, 340], [1077, 98, 1126, -49], [-97, 0, 293, 196], [116, -4, 228, 4], [783, -237, 273, 0], [-555, -70, -590, 35], [25, -25, -525, 50], [517, 300, 667, -150], [-691, 35, -1347, -35], [-364, -124, -852, 124], [290, 140, 140, 140], [330, 244, 287, 0], [-433, 0, 695, 262], [172, -172, -905, 344], [-423, -31, -31, -31], [-2055, 73, -991, 0], [1481, -138, 1412, 69], [95, 0, -316, -221], [-531, 213, -159, 0], [157, -492, -89, 246], [-39, 39, 787, -78], [975, 0, -1059, -84], [-596, -188, -1380, 188], [141, 85, 113, 0], [-47, 47, 51, -94], [-69, 69, -857, -138], [-570, -296, -433, 0], [490, -60, -60, -60], [576, -162, 207, 0], [-1047, 62, -1016, -31], [-537, 0, 84, -453], [478, -302, 654, 302], [1057, -50, 1032, 25], [-929, 0, 868, -61], [-85, 85, 791, -170], [1226, 144, 144, 144], [-141, 141, 727, -282], [78, -176, -49, 0], [1727, 2, 1728, -1], [-319, 0, -262, -581], [-43, 43, -591, -86], [6, -174, -162, 174], [1188, -102, 543, 0], [-152, 328, 24, -328], [-217, 0, 353, 136], [-226, 108, 108, 108], [87, -87, -353, 174], [1480, 78, 78, 78], [-132, 386, 127, 0], [-1039, 0, 1124, 85], [1304, 134, 134, 134], [-1704, 166, -769, 0], [-135, 135, -1063, -270], [780, 276, 1836, -276], [147, -147, 151, 294], [735, 105, 1575, -105], [-75, -467, -271, 0], [-641, -54, -668, 27], [-807, 0, 444, -363], [-1097, 265, -1929, -265], [-968, -210, -210, -210], [-257, -126, -320, 63], [-198, -306, -702, 306], [851, -57, -57, -57], [-1347, 2, -1346, -1], [231, 0, 69, 300], [-111, 111, -605, -222], [-294, 172, 172, 172], [-1461, 355, -553, 0], [1741, -324, 1579, 162], [68, 20, 156, -20], [-273, 0, 231, -42], [-24, 24, 95, -48], [-9, 555, 273, 0], [-235, 235, 1067, -470], [-2118, -64, -64, -64], [1321, 312, 1477, -156], [-9, 9, -2993, -18], [-216, 216, 575, -432], [-224, 270, 270, 270], [-453, 115, -169, 0], [1505, -42, 1484, 21], [1127, -45, -45, -45], [311, 0, -307, 4], [1155, 383, 769, 0], [-39, 39, -2381, -78], [-79, 0, 518, 439], [1685, -126, 1622, 63], [-252, 252, -985, -504], [-1308, 204, -2412, -204], [-2162, 4, 4, 4], [-1137, 115, -511, 0], [509, -174, 422, 87], [831, 57, 1719, -57], [-615, 0, 114, -501], [485, 203, 1173, -203], [930, 380, 655, 0], [1278, -384, 447, 0], [2129, -234, 2012, 117], [2805, -179, 1313, 0], [165, 506, 418, -253], [1273, 0, -1394, -121], [336, 120, 792, -120], [-57, 394, 140, -197], [-160, 160, 399, -320], [1008, -216, 1800, 216], [-1490, -96, -96, -96]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1728_q_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1728_3_q_h();".
// 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_1728_3_q_h(:prec:=4)
    chi := MakeCharacter_1728_q();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1728_3_q_h();".
// 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_1728_3_q_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1728_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![324, 162, 9, -9, 1]>,<7,R![5476, 74, 75, -1, 1]>],Snew);
    return Vf;
end function;