
// Make newform 1728.3.g.a in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1728_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1728_g_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_g_a();".
// 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_g_a();".
// 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 := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [1, 1];
    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_g();"
function MakeCharacter_1728_g()
    N := 1728;
    order := 2;
    char_gens := [703, 325, 1217];
    v := [1, 2, 2];
    // 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_g_Hecke();"
function MakeCharacter_1728_g_Hecke(Kf)
    N := 1728;
    order := 2;
    char_gens := [703, 325, 1217];
    char_values := [[-1, 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], [-7, 0], [0, -5], [0, -5], [-20, 0], [-8, 0], [0, -6], [0, -2], [-10, 0], [0, -31], [10, 0], [-50, 0], [0, -10], [0, 50], [47, 0], [0, 20], [64, 0], [0, -50], [0, 0], [-55, 0], [0, 4], [0, -17], [10, 0], [-25, 0], [155, 0], [0, 80], [0, 75], [-134, 0], [-74, 0], [0, -15], [0, 95], [-62, 0], [0, 100], [-115, 0], [0, 25], [-20, 0], [0, -60], [0, 142], [-127, 0], [0, -135], [-56, 0], [0, 20], [65, 0], [-253, 0], [0, -75], [0, -86], [0, -20], [0, 52], [-146, 0], [334, 0], [0, 10], [134, 0], [0, 120], [268, 0], [0, -250], [350, 0], [0, -21], [520, 0], [-440, 0], [0, 190], [218, 0], [0, 120], [0, -170], [485, 0], [-217, 0], [0, -250], [-310, 0], [0, 125], [-74, 0], [394, 0], [0, -330], [0, 325], [40, 0], [0, 396], [0, 208], [-475, 0], [-260, 0], [-740, 0], [659, 0], [0, -340], [496, 0], [0, -330], [-235, 0], [0, -239], [0, -332], [-470, 0], [-325, 0], [-655, 0], [0, 95], [0, -33], [0, -190], [0, 300], [0, 125], [0, -26], [0, -222], [-265, 0], [-380, 0], [0, -360], [532, 0], [0, -520], [89, 0], [0, -175], [100, 0], [0, 196], [-730, 0], [0, 463], [982, 0], [0, 130], [251, 0], [0, -220], [-650, 0], [-758, 0], [0, -100], [0, -69], [910, 0], [0, -20], [0, -528], [-103, 0], [0, -35], [-578, 0], [845, 0], [1154, 0], [0, 108], [0, 284], [215, 0], [532, 0], [0, 660], [0, 715], [-950, 0], [0, 336], [0, -500], [0, 101], [-830, 0], [-560, 0], [-331, 0], [-298, 0], [0, 640], [-1303, 0], [-170, 0], [0, 450], [-1090, 0], [0, 545], [0, 48], [-542, 0], [0, -400], [-290, 0], [-368, 0], [0, 616], [0, 588], [1330, 0], [1060, 0], [0, -870], [0, 412], [0, 770], [0, 890], [0, 525], [-1340, 0], [-1225, 0], [-115, 0], [0, 397], [-44, 0], [0, -145], [0, 195], [346, 0], [0, 458], [0, -609], [-260, 0], [-109, 0], [338, 0], [0, 445], [-1154, 0], [0, -1070], [530, 0], [0, -211], [-50, 0], [0, 26], [485, 0], [0, 975], [-1088, 0], [0, -515], [0, -845], [1510, 0], [-956, 0], [0, 772], [-1495, 0], [-170, 0], [0, -280], [779, 0], [0, -450], [1970, 0], [0, 37], [0, -846], [-415, 0], [0, 612], [1732, 0], [-349, 0], [70, 0], [-1916, 0], [0, -300], [-115, 0], [0, 25], [-680, 0], [-199, 0], [0, 1185], [-754, 0], [0, 600], [0, 983], [70, 0], [0, 400], [2405, 0], [470, 0], [0, -5], [0, 1225], [0, 880], [-979, 0], [0, 860], [-1970, 0], [0, 942], [-1798, 0], [1438, 0], [0, 1376], [160, 0], [0, 600], [0, -973], [1504, 0], [592, 0], [0, -90], [0, 220], [0, -980], [-290, 0], [0, 384], [0, 1100], [-1100, 0], [0, 1430], [0, -412], [-1186, 0], [218, 0], [0, -895], [0, -660], [0, 700], [0, -594], [0, -925], [2176, 0], [-1256, 0], [0, 860], [0, 105], [0, 815], [0, 1150], [0, 1092], [-1460, 0], [640, 0], [0, 1018], [-1891, 0], [-613, 0], [0, -705], [604, 0], [0, -640], [2663, 0], [1685, 0], [0, 1795], [0, 733], [1294, 0], [2320, 0], [3028, 0], [0, -700], [-10, 0], [730, 0], [0, -20], [-2734, 0], [16, 0], [0, -360], [-2455, 0], [0, -1309], [785, 0], [0, -1635], [0, -37], [-1664, 0], [-2449, 0], [0, -260], [0, -500], [0, 1769], [0, 1022], [-3014, 0], [0, 970], [0, -1170], [-505, 0], [-1471, 0], [0, -1796], [-1520, 0], [2810, 0], [0, 1443], [-3476, 0], [0, -895], [1990, 0], [-1870, 0], [0, 49], [-2521, 0], [0, -1785], [0, 890], [-1150, 0], [-2347, 0], [0, -225]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1728_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1728_3_g_a();".
// 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_g_a(:prec:=2)
    chi := MakeCharacter_1728_g();
    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_g_a();".
// 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_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1728_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![7, 1]>,<7,R![75, 0, 1]>],Snew);
    return Vf;
end function;