
// Make newform 2001.2.a.c in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2001_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_2001_2_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_2001_2_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_2001_a();"
function MakeCharacter_2001_a()
    N := 2001;
    order := 1;
    char_gens := [668, 1132, 553];
    v := [1, 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_2001_a_Hecke(Kf)
    return MakeCharacter_2001_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 := [[0], [1], [0], [0], [-4], [3], [3], [-7], [1], [-1], [2], [1], [-6], [-11], [10], [-6], [-11], [14], [0], [-15], [-12], [1], [-2], [7], [-14], [-8], [-8], [-2], [10], [10], [-16], [-16], [15], [0], [20], [11], [-11], [-4], [-9], [1], [3], [-2], [-11], [-18], [13], [18], [-18], [-7], [14], [23], [-21], [15], [-18], [25], [9], [28], [24], [-20], [19], [24], [-10], [9], [26], [8], [-14], [-14], [-18], [-23], [17], [19], [10], [-3], [-4], [-6], [-1], [-30], [-15], [18], [-34], [-8], [0], [17], [22], [-14], [-35], [-6], [0], [4], [-28], [-39], [11], [24], [32], [30], [31], [21], [-1], [20], [28], [44], [23], [26], [-7], [6], [-18], [-26], [-15], [-9], [-38], [0], [-20], [-2], [34], [29], [34], [17], [-36], [-7], [-14], [-19], [-10], [37], [-38], [44], [-20], [6], [22], [-33], [16], [-14], [-2], [-3], [-51], [-29], [35], [-45], [26], [-38], [-30], [-30], [12], [43], [-24], [-43], [-52], [28], [14], [18], [-46], [4], [23], [11], [-13], [-48], [-5], [-31], [58], [-27], [14], [-12], [48], [-42], [-42], [-40], [-52], [-27], [-32], [4], [-38], [20], [-42], [-5], [-22], [-17], [24], [20], [5], [52], [-16], [-24], [36], [-51], [-26], [27], [-36], [24], [8], [-64], [-48], [40], [-55], [-56], [52], [4], [-3], [36], [22], [16], [12], [-43], [43], [7], [10], [-35], [-49], [-39], [25], [-27], [63], [70], [66], [5], [39], [39], [-6], [22], [-1], [-12], [68], [-58], [-48], [-28], [12], [5], [37], [56], [-21], [42], [15], [-60], [38], [52], [19], [-30], [12], [-16], [15], [-51], [-67], [36], [21], [-47], [-5], [36], [18], [-33], [8], [30], [0], [-71], [62], [24], [17], [-2], [16], [-60], [26], [-28], [-69], [-1], [64], [-15], [29], [26], [-14], [43], [-69], [-23], [76], [42], [-69], [-2], [-36], [5], [-18], [56], [60], [2], [-58], [48], [48], [22], [-68], [50], [-38], [-30], [-1], [-57], [16], [-9], [48], [-34], [-13], [61], [-24], [38], [-51], [41], [-60], [42], [-23], [0], [-72], [18], [60], [33], [-44], [46], [12], [-53], [40], [10], [16], [9], [0], [8], [-60], [44], [-34], [-10], [-60], [84], [67], [-10], [72], [-34], [80], [1], [0], [-8], [-38], [79], [25], [-4], [-69], [12], [12], [54], [14], [60], [78], [62], [-76], [40], [46], [-64], [-62], [-18], [72], [-55], [-19], [5], [-58], [58], [22], [-36], [19], [-50], [38], [-21], [-38], [6], [50], [-60], [32], [44], [32], [24], [-65], [-40], [25], [-51], [23], [-30], [13], [80], [-32], [97], [76], [9], [-55], [40], [38], [50], [-74], [-67], [-6], [-82], [61], [-63], [-97], [70], [-70], [22], [-3], [-68], [-30], [67], [-44], [0], [46], [-96], [-75], [52], [50], [55], [-54], [-48], [-53], [19], [85], [42], [48], [-8], [-65], [-10], [58], [69], [-33], [20], [-56], [51], [-58], [14], [-11], [-10], [-54], [66], [12], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2001_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2001_2_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_2001_2_a_c(:prec:=1)
    chi := MakeCharacter_2001_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_2001_2_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_2001_2_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2001_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![0, 1]>,<5,R![0, 1]>],Snew);
    return Vf;
end function;