
// Make newform 1521.4.a.c in Magma, downloaded from the LMFDB on 19 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_c();".
// 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_c();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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 := [[-3], [0], [9], [2], [-30], [0], [111], [-46], [6], [105], [-100], [17], [231], [-514], [162], [-639], [-600], [233], [926], [930], [-253], [-1324], [-810], [-498], [1358], [357], [1118], [-714], [2006], [1119], [-604], [1584], [-717], [-820], [1749], [-370], [-2611], [-1636], [-264], [-1410], [474], [2249], [-3444], [-4273], [1986], [-2386], [-1600], [-3832], [1398], [4466], [1638], [594], [2303], [-6324], [-7833], [3030], [534], [-3688], [1865], [-2997], [-4114], [4665], [1502], [-2106], [-3898], [-9351], [-9172], [-11089], [-9762], [-8290], [-12405], [1098], [-5734], [-8971], [7244], [6312], [-3627], [-3898], [5703], [6311], [2328], [2045], [-5034], [4283], [-1306], [5796], [-2706], [-829], [5493], [-15346], [9594], [12840], [-14086], [-11694], [-3688], [4746], [14505], [-5085], [-10882], [-4699], [8270], [22785], [11928], [7962], [20618], [-3493], [-10416], [-2061], [-12456], [-781], [19304], [12041], [-9717], [-21040], [-5068], [-10185], [25928], [-23160], [-16626], [14808], [4853], [-16165], [25686], [19056], [-16390], [27846], [-12283], [-25512], [6110], [-27127], [-880], [21876], [11798], [-8074], [-19554], [14030], [-36042], [28628], [37434], [-37569], [5516], [-8778], [-3088], [-13176], [-2359], [2676], [2477], [17199], [24338], [-25146], [18089], [15099], [33488], [39768], [32156], [-11520], [4952], [-8781], [50039], [50670], [-42384], [50538], [-6886], [-9060], [28311], [-4284], [-2458], [24101], [-40930], [-4563], [-52758], [-35215], [4908], [-21925], [-25480], [-24153], [22598], [27657], [-59416], [28430], [20222], [-29952], [-38179], [-60246], [41622], [-22119], [-42874], [-44092], [-71821], [-55122], [-46645], [9822], [2], [19626], [-42594], [43998], [14915], [3269], [-40905], [7668], [9345], [-19384], [59933], [54251], [-46830], [44097], [35702], [-19356], [-17085], [10016], [-26773], [-38898], [-5734], [30054], [56856], [72287], [-74800], [66639], [17196], [42750], [-48283], [96740], [-5841], [-3556], [-81426], [3185], [-9021], [-85812], [-46924], [19116], [100133], [-68212], [-66076], [36855], [38144], [-24444], [55694], [-29163], [24096], [73230], [103074], [52514], [-6202], [56657], [32403], [-52884], [62948], [4884], [-13150], [52890], [119942], [-26094], [21456], [-22717], [57978], [-43890], [45278], [54434], [41886], [-6793], [33956], [-15924], [-68311], [-113335], [31107], [114542], [33690], [-16605], [-18718], [110793], [118001], [101924], [17255], [117188], [1286], [91718], [46092], [16517], [68759], [-8898], [97098], [-63592], [-1692], [19685], [52514], [75114], [-98629], [-71127], [83150], [16491], [51702], [120342], [-131277], [-86712], [-139114], [60489], [-66376], [167433], [56148], [101690], [-49849], [12681], [-127510], [56340], [-54250], [-72433], [-170118], [44657], [109716], [39689], [-105702], [66102], [22995], [-51598], [-102858], [38843], [-116070], [134442], [25118], [54879], [-148642], [-94246], [-95991], [-21862], [-13410], [-27613], [-143110], [43634], [-100146], [115701], [-182503], [135549], [203150], [-145446], [-83782], [151758], [159785], [-53850], [-66697], [-30628], [-173026], [12819], [-218043], [219590], [-75354], [28314], [59633], [-167776], [-168978], [76278], [133982], [-144010], [14037], [-22042], [127745], [168543], [172326], [-21300], [-38961], [171882], [169721], [-82713], [21048], [-59556], [-114328], [31862], [-24234], [-13024], [187811], [142572], [-63226], [-186570], [-639], [-95590], [-94303], [-239958], [-83778], [-59950], [-261429], [148583], [-121050], [107391], [-156214], [-233874], [-47500], [-40278], [87158], [494], [-22408], [243660], [139403], [36969], [-100926], [73826], [-9024], [114542], [101078], [230490], [-234700], [-225723], [-60886], [14982], [-104464], [182331], [130089], [144494], [-71926], [97923], [-26422], [167280], [274859], [178881], [8958], [-150508], [83987], [-265995], [246396], [224174], [-3774], [-109284], [37329], [-162379], [209778], [-33282], [252710], [-133971], [-218556], [-69021], [-228070], [-148524]];
    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_c();".
// 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_c(:prec:=1)
    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_c();".
// 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_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1521_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![3, 1]>,<5,R![-9, 1]>,<7,R![-2, 1]>],Snew);
    return Vf;
end function;