
// Make newform 1728.3.q.c 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_c();".
// 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_c();".
// 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 := [4, 0, -2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 3, 0, 0], [0, 0, 1, 0], [0, 0, 0, 3]];
    Rf_basisdens := [1, 1, 2, 2];
    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], [1, 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, 0, -3, 0], [0, 1, -1, 1], [-6, -1, 3, 1], [5, 2, -5, -4], [6, 0, -12, 2], [-10, 4, 0, -2], [3, 1, 3, 0], [6, -2, -3, 2], [-19, -3, 19, 6], [-32, 4, 0, -2], [21, 6, 21, 0], [0, 3, -23, 3], [18, -7, -9, 7], [-30, 0, 60, -10], [-21, -13, -21, 0], [0, -6, -31, -6], [-53, 3, 53, -6], [30, 0, -60, -8], [-52, -12, 0, 6], [0, 5, -7, 5], [126, -5, -63, 5], [-30, 0, 60, -22], [0, -14, 7, -14], [-6, -36, 3, 36], [-37, 9, 37, -18], [-60, 0, 120, 16], [16, -36, 0, 18], [-9, -44, -9, 0], [34, -12, 0, 6], [-69, 27, -69, 0], [-222, -10, 111, 10], [-11, -13, 11, 26], [3, -26, 3, 0], [0, -15, -175, -15], [-109, -12, 109, 24], [-58, -52, 0, 26], [-99, 29, -99, 0], [18, 20, -9, -20], [-96, 0, 192, 28], [-140, 28, 0, -14], [-6, -3, 3, 3], [-125, 30, 125, -60], [-24, 0, 48, 28], [-80, 20, 0, -10], [103, 7, -103, -14], [0, -15, 203, -15], [-54, -29, 27, 29], [143, 36, -143, -72], [48, 0, -96, 16], [-9, 17, -9, 0], [0, 8, -143, 8], [48, 0, -96, -32], [27, -8, 27, 0], [462, -15, -231, 15], [-30, 0, 60, 2], [34, -84, 0, 42], [0, 0, -49, 0], [210, 38, -105, -38], [145, 27, -145, -54], [-87, -32, -87, 0], [110, 12, 0, -6], [-93, 21, -93, 0], [0, -4, 397, -4], [-330, 42, 165, -42], [0, 31, -245, 31], [-335, -4, 335, 8], [-207, -53, -207, 0], [0, 28, 107, 28], [210, -34, -105, 34], [-36, 0, 72, -84], [0, -15, 77, -15], [113, -46, -113, 92], [-328, 44, 0, -22], [-183, -99, -183, 0], [-78, -24, 39, 24], [124, 36, 0, -18], [-195, 18, -195, 0], [385, 8, -385, -16], [153, -65, 153, 0], [0, -2, 269, -2], [-276, 0, 552, 52], [344, -36, 0, 18], [0, 57, -139, 57], [-162, 163, 81, -163], [-54, 0, 108, -150], [0, 42, -401, 42], [-138, -26, 69, 26], [-283, -31, 283, 62], [-60, 0, 120, 172], [558, 41, -279, -41], [190, 44, 0, -22], [-345, 83, -345, 0], [-35, -81, 35, 162], [-342, 0, 684, -72], [195, 126, 195, 0], [168, 0, -336, 20], [488, 108, 0, -54], [274, 64, 0, -32], [0, -69, -41, -69], [318, 0, -636, 34], [333, 7, 333, 0], [6, 60, -3, -60], [7, 11, -7, -22], [-88, -12, 0, 6], [582, 15, -291, -15], [78, 0, -156, 50], [-483, -11, -483, 0], [0, 6, 751, 6], [-745, 61, 745, -122], [-56, 156, 0, -78], [21, 54, 21, 0], [0, 3, 175, 3], [58, 28, 0, -14], [318, 8, -159, -8], [-593, 27, 593, -54], [-36, 0, 72, 84], [-375, 28, -375, 0], [42, -93, -21, 93], [395, -84, -395, 168], [0, 58, -137, 58], [66, 96, -33, -96], [180, 0, -360, -192], [0, -45, -707, -45], [366, 0, -732, -34], [0, -112, -349, -112], [-180, 0, 360, 48], [0, -27, -643, -27], [-55, 90, 55, -180], [272, -180, 0, 90], [297, 125, 297, 0], [-241, -143, 241, 286], [-866, -48, 0, 24], [369, -74, 369, 0], [157, -68, -157, 136], [-234, 0, 468, 30], [-359, 63, 359, -126], [63, -286, 63, 0], [6, 0, -12, -58], [74, 180, 0, -90], [174, 258, -87, -258], [-1579, 5, 1579, -10], [-444, 0, 888, 44], [-980, -28, 0, 14], [-54, 257, 27, -257], [0, 54, 77, 54], [-990, -14, 495, 14], [-245, -97, 245, 194], [-528, 0, 1056, -92], [317, 94, -317, -188], [-510, 0, 1020, 154], [-838, 148, 0, -74], [87, -171, 87, 0], [0, 115, -335, 115], [198, -259, -99, 259], [-38, 348, 0, -174], [-558, 34, 279, -34], [896, 116, 0, -58], [165, -152, 165, 0], [450, 91, -225, -91], [342, 0, -684, 30], [629, 81, -629, -162], [-90, 0, 180, -192], [15, -364, 15, 0], [30, 189, -15, -189], [-650, 60, 0, -30], [0, -132, 275, -132], [-1204, 108, 0, -54], [-489, -90, -489, 0], [1038, 71, -519, -71], [-307, -90, 307, 180], [-129, -123, -129, 0], [0, -166, -89, -166], [-769, -163, 769, 326], [-273, -4, -273, 0], [0, 27, 1453, 27], [18, 0, -36, 270], [-818, -28, 0, 14], [0, -144, 599, -144], [0, -119, 467, -119], [-282, -57, 141, 57], [215, 180, -215, -360], [450, 0, -900, -162], [429, -135, 429, 0], [894, -306, -447, 306], [574, 96, 0, -48], [0, 83, -251, 83], [-635, 184, 635, -368], [-204, 0, 408, -392], [-1144, -68, 0, 34], [-486, -185, 243, 185], [230, -100, 0, 50], [-438, 132, 219, -132], [168, 0, -336, 212], [735, 68, 735, 0], [-1157, -2, 1157, 4], [0, 20, -61, 20], [438, -120, -219, 120], [-60, 0, 120, 112], [621, 68, 621, 0], [0, 173, 875, 173], [-553, 80, 553, -160], [0, 24, 517, 24], [-276, 0, 552, -140], [-78, 0, 156, -278], [-1430, 92, 0, -46], [471, 135, 471, 0], [942, 0, -471, 0], [97, -109, -97, 218], [-148, 236, 0, -118], [471, -122, 471, 0], [0, 21, -193, 21], [-1110, 75, 555, -75], [189, 365, 189, 0], [0, -144, -923, -144], [161, 93, -161, -186], [1818, -254, -909, 254], [-12, 0, 24, 332], [723, -206, 723, 0], [-715, 50, 715, -100], [395, -99, -395, 198], [-1455, -18, -1455, 0], [214, -660, 0, 330], [-441, -5, -441, 0], [0, -148, 731, -148], [-1314, 88, 657, -88], [-84, 0, 168, -388], [0, -143, 161, -143], [-1662, -1, 831, 1], [1343, -32, -1343, 64], [-214, -372, 0, 186], [-1351, 165, 1351, -330], [507, -132, 507, 0], [0, -181, 379, -181], [-102, 301, 51, -301], [-5, 130, 5, -260], [450, 0, -900, -246], [963, -149, 963, 0], [168, 0, -336, 296], [-1722, 255, 861, -255], [-286, -204, 0, 102], [251, -291, -251, 582], [-236, 132, 0, -66], [-435, -262, -435, 0], [762, 133, -381, -133], [730, 596, 0, -298], [1173, 147, 1173, 0], [-1925, 51, 1925, -102], [1008, 0, -2016, 324], [233, 30, -233, -60], [-90, 0, 180, 414], [975, -119, 975, 0], [0, 252, 301, 252], [-1710, 284, 855, -284], [726, 0, -1452, -208], [1528, 108, 0, -54], [0, -297, -689, -297], [-792, 0, 1584, 108], [-2248, 36, 0, -18], [0, -43, -2599, -43], [54, -509, -27, 509], [-1621, 132, 1621, -264], [724, -500, 0, 250], [-15, -174, -15, 0], [0, 267, 319, 267], [822, 0, -1644, -170], [990, 268, -495, -268], [199, -141, -199, 282], [783, -22, 783, 0], [1037, -290, -1037, 580], [1058, 452, 0, -226], [0, -156, -203, -156], [791, 273, -791, -546], [1675, 150, -1675, -300], [358, -348, 0, 174], [1119, -129, 1119, 0], [0, 318, -427, 318], [560, 300, 0, -150], [258, 387, -129, -387], [-1551, -195, -1551, 0], [-2161, 117, 2161, -234], [-1578, -323, 789, 323], [0, 120, 707, 120], [-1583, -69, 1583, 138], [-450, 0, 900, -48], [332, 580, 0, -290], [-1527, 136, -1527, 0], [0, 105, -2569, 105], [576, 0, -1152, -96], [2502, 206, -1251, -206], [-42, 0, 84, -368], [-93, -52, -93, 0], [693, -293, 693, 0], [0, 36, -1417, 36], [597, 196, 597, 0], [0, -91, -2887, -91], [462, 270, -231, -270], [-108, 0, 216, 168], [0, -29, -1055, -29], [373, 160, -373, -320], [522, 0, -1044, -246], [2770, -60, 0, 30]];
    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_c();".
// 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_c(: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_c();".
// 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_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1728_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![27, 9, 1]>,<7,R![2809, -106, 57, 2, 1]>],Snew);
    return Vf;
end function;