
// Make newform 1521.4.a.q in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1521_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_1521_4_a_q();".
// 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_1521_4_a_q();".
// 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 := [-3, 0, 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_1521_a();"
function MakeCharacter_1521_a()
    N := 1521;
    order := 1;
    char_gens := [677, 847];
    v := [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;

function MakeCharacter_1521_a_Hecke(Kf)
    return MakeCharacter_1521_a();
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, 2], [0, 0], [0, 8], [0, 13], [0, -13], [0, 0], [27, 0], [0, 51], [57, 0], [69, 0], [0, 42], [0, 23], [0, 227], [85, 0], [0, 198], [-426, 0], [0, 11], [-17, 0], [0, 95], [0, 337], [0, 580], [-1244, 0], [0, -246], [0, 177], [0, -713], [1959, 0], [-1856, 0], [255, 0], [0, 352], [-411, 0], [2243, 0], [372, 0], [0, 687], [-2545, 0], [0, 753], [0, -50], [-1534, 0], [0, -943], [0, 939], [-873, 0], [-1287, 0], [-2, 0], [2841, 0], [0, 2451], [0, -1589], [1685, 0], [1681, 0], [0, -2365], [0, 253], [0, 104], [-5778, 0], [0, -1074], [0, 1189], [4491, 0], [5451, 0], [783, 0], [5085, 0], [0, 765], [3421, 0], [0, -468], [-7177, 0], [0, -5377], [0, -2758], [6192, 0], [-770, 0], [0, 4652], [0, 3047], [-8278, 0], [-6867, 0], [0, -7017], [0, -3353], [0, -774], [3665, 0], [5371, 0], [0, -6645], [0, 1397], [-9858, 0], [0, -5035], [0, -4379], [0, -2485], [-5397, 0], [0, -4192], [0, 281], [-12139, 0], [-461, 0], [-12156, 0], [0, 171], [0, 353], [0, -7579], [0, -482], [14496, 0], [0, -5137], [0, -2745], [-1635, 0], [0, 8334], [-12687, 0], [0, 3319], [-6054, 0], [-14803, 0], [0, -12396], [-13516, 0], [0, 1669], [11583, 0], [12879, 0], [11636, 0], [0, -7108], [0, 9033], [0, -14908], [-1668, 0], [13699, 0], [-23173, 0], [0, 9593], [0, 16393], [0, -3606], [0, 12921], [19827, 0], [0, 4879], [2949, 0], [-12039, 0], [-3363, 0], [0, -5865], [18169, 0], [-9042, 0], [0, 7195], [0, -2493], [-18270, 0], [0, 941], [9831, 0], [15464, 0], [0, 7284], [0, 9401], [0, -6239], [13615, 0], [5551, 0], [0, -5821], [0, 17181], [0, 16013], [0, -18215], [1455, 0], [-1659, 0], [0, -2542], [0, 16571], [-15779, 0], [0, 4246], [-17371, 0], [0, -17017], [0, 1680], [-5346, 0], [24244, 0], [0, 18926], [0, 2623], [-20517, 0], [23852, 0], [-38757, 0], [39071, 0], [53040, 0], [367, 0], [0, -17283], [42166, 0], [0, -20220], [0, -1501], [10623, 0], [0, -11662], [2325, 0], [0, -19017], [0, -24654], [-4843, 0], [10943, 0], [0, 4956], [-13422, 0], [0, -26614], [0, -7263], [44535, 0], [0, 27531], [-22424, 0], [-56979, 0], [0, 5025], [0, -15296], [29563, 0], [-18905, 0], [0, 13690], [-38172, 0], [-19646, 0], [0, 948], [0, 155], [-23973, 0], [-36266, 0], [0, -8650], [0, -28873], [0, 11447], [54035, 0], [0, 41049], [68048, 0], [0, -21963], [-73293, 0], [74739, 0], [0, 11840], [-52495, 0], [0, -4468], [42384, 0], [0, 7547], [-11617, 0], [0, -3211], [8750, 0], [0, 20215], [56973, 0], [0, -15370], [-66555, 0], [0, -39293], [38945, 0], [48905, 0], [73074, 0], [47797, 0], [0, -35509], [0, 40965], [0, -12908], [76048, 0], [46377, 0], [0, 29137], [0, -39984], [-55069, 0], [0, -19978], [0, 22808], [0, 6327], [-6579, 0], [1006, 0], [-48735, 0], [77241, 0], [-73015, 0], [0, 4018], [-21911, 0], [97469, 0], [0, -33437], [64410, 0], [-31588, 0], [0, 16366], [0, -1079], [0, -48411], [96663, 0], [24819, 0], [0, -23863], [-67669, 0], [-31301, 0], [0, 14901], [0, 20185], [7680, 0], [0, 36013], [0, 19183], [0, 24091], [47457, 0], [0, 26911], [0, -25881], [0, 59402], [56321, 0], [-47130, 0], [0, 7307], [-9353, 0], [0, -12161], [80298, 0], [0, 3967], [-69376, 0], [75531, 0], [0, 17792], [-9637, 0], [0, 12975], [47995, 0], [0, 26337], [0, -39628], [0, 65477], [12219, 0], [182, 0], [0, -66810], [0, -17029], [-11315, 0], [58855, 0], [0, 43595], [0, -61747], [0, 21496], [0, 74473], [110787, 0], [2475, 0], [0, -77735], [65964, 0], [0, -10791], [0, -17918], [63792, 0], [-3986, 0], [0, -4568], [0, -64211], [-119805, 0], [-104535, 0], [88965, 0], [0, 25463], [0, 53035], [-48949, 0], [-103110, 0], [-151384, 0], [45219, 0], [59895, 0], [0, 51101], [79693, 0], [0, -22476], [75223, 0], [3012, 0], [168455, 0], [0, 10353], [78024, 0], [18290, 0], [0, -58661], [24566, 0], [137289, 0], [0, -46217], [-42054, 0], [-159047, 0], [0, -95365], [-152201, 0], [0, -81903], [0, -109546], [0, 21709], [-15093, 0], [134932, 0], [0, 17500], [110433, 0], [0, 2025], [0, 41744], [99103, 0], [0, -5350], [0, 12039], [128457, 0], [-1119, 0], [0, -113437], [-156210, 0], [-1079, 0], [0, -64445], [0, -34169], [0, 16922], [0, -65375], [0, -30483], [0, 56251], [-132176, 0], [0, -41784], [-207999, 0], [0, -93804], [-153281, 0], [0, 49713], [-114876, 0], [68722, 0], [0, -43223], [0, -19969], [72339, 0], [0, 87546], [0, 63387], [0, 62407], [-42667, 0], [174845, 0], [129930, 0], [0, -33569], [0, 28877], [-159594, 0], [0, 113854], [0, 56895], [26055, 0], [-3729, 0], [0, -88683], [3643, 0], [-62953, 0], [0, -67353], [0, -109531], [109066, 0], [-232551, 0], [226165, 0], [0, -22086], [-198066, 0], [-126311, 0], [185603, 0], [0, -16506], [20559, 0], [0, 51515], [5481, 0], [-109067, 0], [0, 137624], [0, 38659], [0, 76150], [0, -18308], [0, 40301], [0, 18651], [0, -14385], [-197194, 0], [0, 78674], [260685, 0], [0, 103015], [0, -62597], [0, 71422], [201229, 0], [0, 125135], [-57593, 0], [0, -63961], [-73158, 0], [39700, 0], [0, 21373], [0, 111503], [-201093, 0], [0, -91015], [0, 143396], [0, 37879], [6931, 0], [0, 95593], [0, 162317], [0, -18702], [0, -91029], [-133310, 0], [-263319, 0], [82641, 0], [251665, 0], [49669, 0], [-37158, 0], [0, 24497], [-128816, 0], [0, 178573], [97863, 0], [224715, 0], [0, 166128], [0, -94719], [244284, 0], [0, 82105], [0, -139127], [-113724, 0], [0, 15140], [0, -88927], [-109911, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1521_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1521_4_a_q();".
// 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_1521_4_a_q(:prec:=2)
    chi := MakeCharacter_1521_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_1521_4_a_q();".
// 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_1521_4_a_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1521_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-12, 0, 1]>,<5,R![-192, 0, 1]>,<7,R![-507, 0, 1]>],Snew);
    return Vf;
end function;