
// Make newform 1024.4.a.d in Magma, downloaded from the LMFDB on 04 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1024_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_1024_4_a_d();".
// 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_1024_4_a_d();".
// 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 := [-2, 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_1024_a();"
function MakeCharacter_1024_a()
    N := 1024;
    order := 1;
    char_gens := [1023, 5];
    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_1024_a_Hecke(Kf)
    return MakeCharacter_1024_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, 0], [0, 4], [0, -1], [8, 0], [0, 4], [0, -3], [-24, 0], [0, -92], [-184, 0], [0, 69], [176, 0], [0, -215], [200, 0], [0, -100], [208, 0], [0, 263], [0, -108], [0, 339], [0, -484], [-936, 0], [406, 0], [768, 0], [0, 364], [-1306, 0], [-472, 0], [0, -935], [952, 0], [0, 580], [0, 299], [-1298, 0], [-1744, 0], [0, 1036], [-1400, 0], [0, 1260], [0, -1583], [-1304, 0], [0, 1635], [0, 1524], [3848, 0], [0, 2125], [0, 796], [0, -1503], [-3696, 0], [-2680, 0], [0, 751], [-2680, 0], [0, 2476], [2544, 0], [0, -2652], [0, 2447], [-3914, 0], [2272, 0], [2280, 0], [0, -2236], [-702, 0], [-7528, 0], [0, -933], [8272, 0], [0, -4377], [1818, 0], [0, -740], [0, 1327], [0, -2604], [-7688, 0], [680, 0], [0, -6203], [0, 6100], [5298, 0], [0, -860], [0, 7477], [7646, 0], [2456, 0], [1744, 0], [0, 7959], [0, -2748], [7296, 0], [0, -791], [0, -10197], [-4728, 0], [-6424, 0], [0, 6252], [0, 1791], [9952, 0], [9928, 0], [7288, 0], [0, 12300], [4808, 0], [14792, 0], [0, -6725], [-11200, 0], [0, 11620], [-8240, 0], [-11544, 0], [0, 8956], [0, 10172], [8200, 0], [0, -11453], [-600, 0], [0, -12444], [0, 11667], [0, 6492], [0, 2941], [0, -8924], [-4824, 0], [0, 6540], [-7938, 0], [0, 9044], [-15346, 0], [2744, 0], [6374, 0], [-9984, 0], [0, 4351], [-10486, 0], [0, -1116], [-26680, 0], [-2648, 0], [0, 16764], [18264, 0], [0, 4411], [0, -22228], [0, -22015], [-14072, 0], [0, 13833], [0, -20436], [0, 972], [0, -25805], [0, 4137], [864, 0], [-5416, 0], [0, 15077], [0, -26700], [-18840, 0], [24640, 0], [0, -8303], [4968, 0], [-34808, 0], [0, -1719], [0, -13092], [0, -1469], [-24056, 0], [0, 8980], [0, 10839], [37528, 0], [0, 1948], [0, 25475], [6600, 0], [0, -23449], [-15480, 0], [0, 22596], [-1392, 0], [0, 2875], [-49518, 0], [0, -3292], [33720, 0], [0, -12020], [18128, 0], [13640, 0], [-49848, 0], [-4490, 0], [0, -757], [0, -13324], [-25272, 0], [20008, 0], [0, 37372], [-53912, 0], [-27592, 0], [3104, 0], [0, -22839], [23848, 0], [0, 15969], [0, -6932], [0, 7765], [-13352, 0], [36714, 0], [-7472, 0], [-59750, 0], [0, -22108], [0, -13511], [64936, 0], [0, -21075], [-37664, 0], [0, 29924], [0, -27809], [-31446, 0], [34032, 0], [0, 21521], [0, 2915], [0, -2172], [62312, 0], [-6592, 0], [46974, 0], [0, -29348], [0, 5588], [0, -32987], [0, -48684], [-23094, 0], [15598, 0], [0, 7075], [-4158, 0], [-54408, 0], [0, -51043], [40736, 0], [0, -6825], [12770, 0], [0, 308], [0, -22635], [-45344, 0], [0, -45116], [-17194, 0], [0, -5964], [24232, 0], [0, 51809], [-47560, 0], [0, -41764], [33768, 0], [-70072, 0], [1824, 0], [-50552, 0], [1496, 0], [0, -53595], [0, 10031], [-8120, 0], [-53016, 0], [41056, 0], [0, -57100], [0, -3273], [82086, 0], [-58400, 0], [65880, 0], [0, -516], [0, 45307], [0, -48012], [104160, 0], [-13014, 0], [0, 41708], [-36576, 0], [45960, 0], [0, 38337], [0, -61204], [57576, 0], [0, 23580], [0, 56972], [82712, 0], [0, -5043], [36594, 0], [67992, 0], [-64048, 0], [0, 27388], [0, -13028], [24960, 0], [0, 2421], [-50370, 0], [1880, 0], [-3946, 0], [0, 45611], [0, 29356], [0, 64881], [0, 6660], [0, 23465], [96392, 0], [-24128, 0], [0, -18604], [0, -24071], [0, -72747], [-83544, 0], [0, -40596], [0, -89941], [-75322, 0], [0, -87036], [0, -39399], [0, 66237], [0, 8244], [51418, 0], [-104960, 0], [-76846, 0], [101480, 0], [0, 81100], [0, 65859], [7510, 0], [0, 64596], [-36624, 0], [-129896, 0], [47944, 0], [0, -26305], [0, -85204], [-74608, 0], [-134872, 0], [0, 82055], [-43608, 0], [-52962, 0], [0, -58149], [0, -63100], [-69882, 0], [0, 53060], [0, 117307], [0, 111123], [23120, 0], [0, -79567], [0, -26740], [0, 24220], [-64920, 0], [0, 8765], [-103024, 0], [0, -111028], [0, -36580], [123976, 0], [0, -18012], [0, 13933], [-21384, 0], [0, -127479], [70128, 0], [0, 61537], [-171064, 0], [0, -53460], [-50328, 0], [-31242, 0], [0, 31564], [156768, 0], [128322, 0], [-11122, 0], [0, -95748], [-71800, 0], [0, -24395], [-25376, 0], [83784, 0], [-137080, 0], [0, 39316], [0, 72828], [-72528, 0], [0, -6353], [0, 55613], [0, -1755], [-6592, 0], [0, -79268], [0, -63348], [0, 53204], [0, -27131], [67934, 0], [-91382, 0], [-72016, 0], [0, -7135], [179304, 0], [0, 52969], [-140808, 0], [0, 41459], [0, -91932], [0, 20191], [0, 134500], [-212224, 0], [0, -130191], [0, 55764], [-49496, 0], [0, 509], [128960, 0], [0, -15727], [168538, 0], [-20864, 0], [0, -108116], [-120472, 0], [1528, 0], [0, 77721], [-215466, 0], [35184, 0], [0, 131156], [0, -61852], [-98358, 0], [0, -45411], [34232, 0], [148424, 0], [0, 82204], [0, 116868], [-117776, 0], [0, -140297], [58312, 0], [0, -49851], [0, 101884], [14480, 0], [-350, 0], [222088, 0], [33480, 0], [0, -82939], [79624, 0], [-144216, 0], [58722, 0], [0, 146340], [-48968, 0], [-51280, 0], [0, 89799], [0, 22612], [-146016, 0], [15422, 0], [0, 101577], [0, 170340], [0, -47300], [-148968, 0], [2472, 0], [156768, 0], [-158360, 0], [0, -115412], [0, 185335], [0, -45387], [145672, 0], [73440, 0], [136104, 0], [0, -81505], [155768, 0], [0, -128227], [19080, 0], [0, 33468], [0, -48404], [183346, 0], [0, 8119], [0, -48700], [0, -95148], [-2102, 0], [0, 60797], [101792, 0], [-306696, 0], [136, 0], [-100360, 0], [0, 74867], [0, 154585], [-33936, 0], [0, 114452], [218024, 0], [0, -48755], [0, 17732], [108070, 0], [0, 13644], [-106056, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1024_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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