
// Make newform 1728.3.o.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1728_o();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1728_o_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_o_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_1728_3_o_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 := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_o();"
function MakeCharacter_1728_o()
    N := 1728;
    order := 6;
    char_gens := [703, 325, 1217];
    v := [3, 6, 4];
    // 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_o_Hecke();"
function MakeCharacter_1728_o_Hecke(Kf)
    N := 1728;
    order := 6;
    char_gens := [703, 325, 1217];
    char_values := [[-1, 0], [1, 0], [-1, 1]];
    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], [-4, 4], [4, -2], [14, -7], [-22, 22], [11, 0], [9, -18], [14, 14], [0, -34], [-4, -4], [16, 0], [13, -13], [-58, 29], [4, -2], [52, 0], [31, 31], [0, -16], [67, 67], [0, 0], [-25, 0], [-32, 16], [-40, 20], [2, 0], [0, 43], [0, 20], [14, 14], [-9, 18], [88, 0], [-50, 50], [-126, 252], [112, 112], [0, 169], [-113, -113], [-130, 130], [-140, 70], [-4, 4], [-180, 360], [104, 104], [0, 2], [-108, 216], [-254, 0], [4, -2], [67, -67], [268, 0], [18, -36], [76, 76], [-68, 34], [518, -259], [410, -410], [65, 0], [-22, -22], [0, 223], [-63, 126], [-437, 437], [364, -182], [304, 0], [180, -360], [0, -34], [0, -218], [4, 4], [-202, 202], [63, -126], [158, 158], [0, 79], [0, -502], [-472, 236], [337, -337], [157, 157], [0, 272], [0, -461], [-306, 612], [112, -56], [-346, 346], [189, -378], [-364, -364], [0, 146], [-488, 0], [445, -445], [67, -67], [-356, -356], [0, 272], [234, -468], [-439, 0], [976, -488], [-382, 191], [47, 0], [0, 331], [0, -538], [-328, -328], [369, -738], [-140, 70], [-234, 468], [-419, -419], [301, 301], [504, -1008], [-760, 760], [-745, 0], [324, -648], [520, 0], [446, -223], [934, 0], [409, 409], [0, -695], [-311, -311], [227, 0], [-166, 83], [506, 0], [32, 32], [0, -335], [-364, -364], [340, 0], [391, -391], [14, -7], [252, -504], [0, 421], [-239, -239], [234, -468], [-886, 886], [968, -484], [248, -248], [0, -1154], [0, -1132], [459, -918], [1040, -520], [142, 0], [0, 740], [72, -144], [940, -470], [914, -914], [-207, 414], [230, 230], [644, 644], [-758, 0], [-374, 374], [22, -22], [-1334, 0], [508, 508], [-832, 832], [-493, 0], [-189, 378], [0, 758], [500, 500], [-252, 504], [718, 0], [1048, -524], [0, 146], [0, -146], [49, 49], [-684, 1368], [-1480, 1480], [-142, 0], [-693, 1386], [-364, -364], [-742, 371], [-464, 232], [-450, 900], [0, -758], [-754, 0], [1796, -1796], [122, -61], [-1213, 0], [-202, -202], [-828, 1656], [157, -157], [1624, -812], [144, -288], [0, -412], [-502, 0], [1256, -1256], [1022, -511], [860, -860], [1148, 1148], [0, 1609], [122, 122], [1462, -1462], [-1066, 533], [-326, 0], [-504, 1008], [0, -268], [1228, -614], [1346, -673], [-778, 778], [-439, 0], [662, 662], [0, -736], [-416, 0], [-112, 56], [2047, -2047], [-648, 1296], [1055, 0], [1094, -547], [-243, 486], [0, -124], [81, -162], [535, -535], [697, -697], [0, 1244], [0, -1289], [-558, 1116], [-1102, 1102], [-572, 286], [-1480, 1480], [0, 2365], [441, -882], [-578, 0], [-1332, 2664], [805, 805], [0, 1735], [-437, -437], [-2347, 0], [50, -50], [1948, -974], [-760, 380], [-472, -472], [0, -677], [-4, -4], [0, -83], [198, -396], [176, -176], [2174, -2174], [932, 932], [-2075, 2075], [-198, 396], [-1301, -1301], [0, -2626], [0, 754], [864, -1728], [-1796, 898], [-1264, 632], [1886, -1886], [612, -1224], [302, 302], [283, -283], [2336, -1168], [2380, -1190], [1606, -1606], [-1766, 0], [715, 715], [-1638, 3276], [-1750, 875], [-1188, 2376], [-256, -256], [898, 0], [391, -391], [-752, 376], [1260, -2520], [157, 157], [508, 508], [126, -252], [230, -230], [-187, 0], [-220, -220], [0, -2162], [0, -1366], [873, -1746], [-2936, 0], [-616, 308], [772, 0], [-1087, 0], [-1904, 952], [590, -295], [-1516, 1516], [-1010, 0], [2110, -2110], [914, -457], [-2756, 0], [0, 2743], [139, 139], [2426, -2426], [-724, 724], [1620, -3240], [0, 1105], [1022, 1022], [-2183, 2183], [684, -1368], [-1148, -1148], [0, 1226], [299, 0], [-634, 317], [-850, -850], [-1642, -1642], [3820, -1910], [0, -358], [1309, 1309], [1458, -2916], [794, 0], [2444, -2444], [2632, -1316], [-1087, 0], [0, -2500], [1719, -3438], [823, -823], [1471, 1471], [0, -1996], [1670, -1670], [220, -110], [0, 2990], [684, -1368], [1832, -916], [-2237, 2237], [3022, 0], [1404, -2808]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1728_o_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1728_3_o_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_1728_3_o_b(:prec:=2)
    chi := MakeCharacter_1728_o();
    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_o_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_1728_3_o_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1728_o();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![16, 4, 1]>,<7,R![12, -6, 1]>],Snew);
    return Vf;
end function;