
// Make newform 1521.4.a.p in Magma, downloaded from the LMFDB on 25 December 2025.
// 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_p();".
// 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_p();".
// 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 := [-7, 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 := 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]];
    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, 1], [0, 0], [0, -4], [22, 0], [0, -2], [0, 0], [0, -44], [126, 0], [0, -12], [0, -20], [182, 0], [86, 0], [0, -168], [96, 0], [0, 138], [0, 72], [0, -222], [574, 0], [530, 0], [0, 306], [154, 0], [-460, 0], [0, -122], [0, 544], [-70, 0], [0, 552], [-1428, 0], [0, 612], [338, 0], [0, 636], [-376, 0], [0, 260], [0, 528], [2100, 0], [0, -756], [-3526, 0], [3066, 0], [3442, 0], [0, 1018], [0, -1320], [0, -64], [3374, 0], [0, 448], [1542, 0], [0, 804], [-952, 0], [-1640, 0], [4886, 0], [0, 706], [-5558, 0], [0, 1352], [0, -1110], [602, 0], [0, 1332], [0, -1112], [0, -324], [0, -124], [2814, 0], [-3190, 0], [0, 2312], [-4788, 0], [0, -2532], [406, 0], [0, -3132], [-5586, 0], [0, -3172], [4426, 0], [8370, 0], [0, -2304], [-4354, 0], [0, 1288], [0, -2934], [-7784, 0], [-8510, 0], [-1650, 0], [0, -3274], [0, -916], [1414, 0], [0, -1976], [-5782, 0], [0, 4340], [14194, 0], [0, 1974], [-686, 0], [-1372, 0], [0, -884], [0, 6520], [17866, 0], [0, 796], [13718, 0], [0, -1548], [0, 3294], [-4506, 0], [0, 8172], [786, 0], [0, -796], [0, 3172], [0, -1476], [-17668, 0], [1650, 0], [3796, 0], [0, -780], [0, -9840], [0, 1376], [-19612, 0], [-15722, 0], [0, 1030], [0, 6808], [0, -4604], [5950, 0], [14168, 0], [6326, 0], [0, 3328], [24486, 0], [-22430, 0], [0, -10388], [-16478, 0], [0, 10040], [0, 7668], [0, 248], [14238, 0], [-4874, 0], [0, 8240], [0, -3306], [-294, 0], [0, 5956], [6722, 0], [0, -12], [12824, 0], [29610, 0], [15622, 0], [0, -3246], [29468, 0], [-35030, 0], [0, -8440], [27342, 0], [0, 4492], [-22666, 0], [0, -220], [0, -300], [9478, 0], [0, 1220], [40476, 0], [0, 2710], [27482, 0], [0, 7230], [-31962, 0], [0, 1864], [11704, 0], [0, 862], [1006, 0], [0, -15376], [124, 0], [0, 6264], [-42996, 0], [0, -20096], [8244, 0], [0, 6120], [-18214, 0], [0, 5260], [0, -15270], [0, 8040], [-9578, 0], [0, 16992], [0, 12384], [0, -17930], [-6848, 0], [-5810, 0], [46338, 0], [0, -2880], [0, -7350], [-48202, 0], [0, -1676], [-23382, 0], [9828, 0], [0, -2808], [-28338, 0], [0, -10764], [64204, 0], [-16758, 0], [-3194, 0], [0, 3120], [-8566, 0], [0, 9792], [0, -8974], [0, 22884], [29734, 0], [3262, 0], [54514, 0], [0, -15534], [61138, 0], [0, 7150], [-18356, 0], [0, 2132], [0, -2616], [0, 24288], [-24106, 0], [4006, 0], [0, 14096], [0, 2160], [0, 2052], [64568, 0], [30674, 0], [19250, 0], [0, -5670], [0, -12152], [12950, 0], [0, -7300], [0, 30296], [12460, 0], [-19486, 0], [0, -23064], [9812, 0], [0, -3818], [0, 16366], [-77546, 0], [74996, 0], [0, 27784], [0, 18282], [0, 11244], [22370, 0], [-26026, 0], [0, 4288], [28478, 0], [0, 20340], [-29402, 0], [0, -936], [0, -13652], [-26600, 0], [0, 25442], [13494, 0], [-33964, 0], [30634, 0], [0, -22404], [-41636, 0], [0, 31770], [55930, 0], [0, 15508], [0, -11624], [0, 3296], [0, -13250], [95620, 0], [-27412, 0], [-67842, 0], [0, -3024], [0, 28260], [80318, 0], [0, 25458], [-57962, 0], [0, 9348], [51130, 0], [0, -35920], [0, 5670], [-83846, 0], [0, 11700], [0, 36238], [21758, 0], [66598, 0], [0, 28260], [-77770, 0], [-54360, 0], [0, 49976], [-11942, 0], [-16394, 0], [0, -3744], [-99680, 0], [0, 38156], [0, -22056], [-5546, 0], [0, -38992], [-44842, 0], [125106, 0], [60242, 0], [29972, 0], [-17094, 0], [5642, 0], [0, -36830], [-126586, 0], [77534, 0], [0, -10476], [0, -2976], [-14786, 0], [0, -37936], [-12026, 0], [39382, 0], [0, -20320], [-102482, 0], [0, 14004], [25690, 0], [0, 8488], [0, -29928], [0, -52224], [0, -10056], [0, 17322], [-43958, 0], [0, 17972], [79968, 0], [0, 18860], [0, -58664], [-70322, 0], [148778, 0], [0, -23900], [110980, 0], [0, -31616], [103196, 0], [12714, 0], [0, 58384], [-4550, 0], [0, -46314], [-138462, 0], [0, 58492], [0, 15612], [0, -42436], [96976, 0], [0, -10774], [-99358, 0], [0, -46854], [0, -52274], [-98350, 0], [0, -33684], [33320, 0], [86438, 0], [0, 46968], [-104718, 0], [0, -37848], [-62706, 0], [59050, 0], [18578, 0], [0, 48224], [0, -6108], [-80246, 0], [0, 30636], [66220, 0], [0, 35198], [-97162, 0], [0, -20306], [75710, 0], [0, -14392], [103978, 0], [-165116, 0], [-25650, 0], [0, -54700], [0, 26828], [-175156, 0], [0, -71380], [0, 39120], [-177170, 0], [-58798, 0], [0, 15338], [0, 49916], [-162918, 0], [-124014, 0], [0, -48108], [141512, 0], [-59554, 0], [0, -54020], [0, 5522], [0, -44334], [0, 33120], [0, 12214], [-63114, 0], [0, 80148], [0, 19452], [0, -54462], [-81872, 0], [-21002, 0], [0, -32532], [-3502, 0], [204510, 0], [0, 25044], [214704, 0], [0, 65866], [0, -56832], [20076, 0], [-214086, 0], [0, -32202], [0, -33624], [254702, 0], [0, 45248], [-167118, 0], [0, -40764], [0, 5064], [-151226, 0], [0, 46536], [-240646, 0], [0, -9650], [76050, 0], [-86142, 0], [34774, 0], [0, 39100], [238138, 0], [0, 33380], [0, -65962], [-25816, 0], [0, -79246], [98950, 0], [-111062, 0], [0, 14796], [10768, 0], [0, 98844], [113806, 0], [0, 35196], [22790, 0], [0, 100104], [0, 39596], [159068, 0], [-8710, 0], [0, -736], [-65394, 0], [0, 25638], [151970, 0], [0, 80080], [0, -88416], [-137384, 0], [-246762, 0], [0, -34848], [0, -20830], [-200788, 0], [0, -5400], [0, 29740], [0, 92264], [37926, 0], [0, -31606], [0, 66076], [104734, 0], [0, -73748], [0, -75684], [0, 16776], [84322, 0], [0, -19848]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1521_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1521_4_a_p();".
// 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_p(: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_p();".
// 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_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1521_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-7, 0, 1]>,<5,R![-112, 0, 1]>,<7,R![-22, 1]>],Snew);
    return Vf;
end function;