
// Make newform 1521.2.a.a in Magma, downloaded from the LMFDB on 24 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_2_a_a();".
// 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_2_a_a();".
// 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 := 2;
    raw_aps := [[-1], [0], [1], [2], [2], [0], [7], [-6], [6], [1], [4], [1], [-9], [6], [-6], [9], [0], [1], [-2], [-6], [11], [-4], [14], [14], [-2], [-3], [6], [6], [-2], [15], [20], [8], [3], [12], [-3], [-2], [-3], [-4], [-16], [6], [2], [-7], [4], [-9], [-6], [14], [-8], [16], [14], [-22], [-10], [-30], [7], [-12], [7], [30], [14], [0], [-31], [19], [-18], [9], [14], [-18], [6], [25], [-4], [-33], [-18], [-26], [11], [18], [10], [-11], [-36], [-8], [-19], [-34], [-1], [7], [-16], [-19], [30], [19], [14], [4], [-34], [-13], [-19], [-26], [-6], [-24], [18], [-6], [24], [2], [-7], [3], [14], [45], [-26], [9], [0], [-22], [26], [11], [-16], [-13], [16], [-5], [8], [-23], [-13], [-24], [20], [31], [-16], [32], [6], [8], [45], [-29], [-34], [24], [42], [-2], [-11], [48], [14], [-15], [-16], [36], [-34], [-50], [-50], [30], [14], [28], [2], [-33], [-28], [-50], [-24], [-16], [17], [-12], [21], [31], [34], [-10], [17], [-37], [16], [0], [12], [40], [24], [27], [-49], [38], [48], [-6], [2], [-36], [-33], [-4], [-2], [-35], [30], [-27], [-54], [-23], [4], [-45], [-24], [-33], [6], [9], [16], [-42], [38], [0], [-19], [-6], [-26], [-63], [-2], [-52], [-13], [-42], [43], [-34], [10], [-14], [-18], [-66], [19], [-11], [7], [36], [-39], [-16], [-35], [11], [-30], [25], [-50], [-52], [43], [-56], [-13], [-42], [-22], [62], [56], [-1], [-16], [7], [-36], [54], [29], [28], [-73], [52], [54], [-47], [-21], [52], [-20], [4], [-51], [-4], [20], [-33], [-56], [-60], [-34], [-43], [64], [-58], [2], [-14], [14], [57], [51], [36], [-44], [60], [74], [-6], [62], [-14], [72], [-5], [66], [-10], [-10], [-46], [-42], [-41], [12], [-76], [-31], [-47], [-21], [22], [-62], [-21], [-22], [17], [41], [12], [31], [-4], [54], [70], [-52], [61], [31], [6], [18], [-16], [-28], [-19], [-22], [74], [19], [33], [14], [-5], [30], [-42], [27], [72], [-50], [17], [48], [-79], [28], [2], [71], [-31], [-86], [-44], [38], [-57], [58], [49], [-36], [-39], [34], [30], [11], [82], [22], [35], [-54], [2], [-2], [15], [-10], [10], [9], [-78], [30], [-21], [-46], [-62], [62], [-27], [-55], [-75], [-26], [-14], [82], [14], [-71], [70], [47], [-92], [54], [-13], [-35], [62], [46], [-30], [25], [-32], [-2], [62], [6], [54], [-27], [14], [-55], [7], [-50], [84], [39], [26], [-15], [63], [72], [-36], [-32], [22], [30], [88], [51], [-60], [-90], [-10], [33], [66], [-39], [66], [78], [-14], [83], [-9], [-18], [15], [2], [30], [-44], [74], [-66], [102], [80], [-68], [-85], [49], [58], [-70], [-80], [46], [38], [42], [-68], [-75], [50], [6], [64], [-69], [49], [-34], [82], [43], [18], [0], [-85], [97], [-90], [52], [27], [-27], [4], [-10], [-30], [-92], [65], [69], [106], [70], [86], [77], [-108], [75], [-70], [84]];
    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_2_a_a();".
// 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_2_a_a(:prec:=1)
    chi := MakeCharacter_1521_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    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_2_a_a();".
// 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_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1521_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![1, 1]>,<5,R![-1, 1]>,<7,R![-2, 1]>],Snew);
    return Vf;
end function;