
// Make newform 1003.2.a.c in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1003_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_1003_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_1003_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_1003_a();"
function MakeCharacter_1003_a()
    N := 1003;
    order := 1;
    char_gens := [768, 120];
    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_1003_a_Hecke(Kf)
    return MakeCharacter_1003_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], [1], [1], [3], [4], [0], [1], [7], [2], [3], [4], [0], [-3], [4], [6], [-9], [-1], [-10], [-4], [-4], [-2], [5], [16], [0], [-8], [-8], [-16], [9], [-10], [-14], [15], [-18], [11], [4], [-10], [8], [2], [-16], [15], [-24], [-14], [-5], [2], [-15], [2], [-9], [12], [-24], [14], [26], [8], [21], [7], [-15], [-19], [-11], [-2], [11], [13], [15], [6], [-3], [-31], [7], [22], [2], [-25], [30], [12], [-14], [24], [3], [-8], [-14], [19], [-8], [34], [-22], [-24], [-4], [-4], [-20], [8], [-41], [8], [24], [3], [32], [10], [30], [6], [32], [-1], [33], [-23], [16], [18], [26], [33], [10], [-12], [3], [18], [6], [-4], [-23], [12], [-27], [-37], [4], [23], [8], [-21], [-29], [-28], [34], [-19], [-15], [13], [48], [-29], [-20], [38], [12], [-2], [16], [11], [0], [-8], [26], [-12], [24], [26], [-43], [21], [10], [18], [4], [36], [-50], [-8], [18], [-30], [-36], [-31], [-50], [-37], [-54], [26], [34], [-33], [-24], [-35], [42], [-43], [-37], [-20], [-28], [2], [24], [-35], [-26], [-10], [61], [2], [-10], [4], [31], [-32], [14], [24], [-2], [43], [-18], [-13], [19], [-45], [-48], [16], [-29], [47], [-47], [16], [-37], [-24], [-36], [-48], [46], [-32], [-48], [50], [-20], [46], [-34], [13], [14], [-22], [62], [42], [56], [30], [37], [10], [20], [9], [8], [0], [48], [62], [-58], [-20], [43], [41], [69], [-17], [-22], [-44], [2], [50], [-26], [-16], [24], [-62], [-9], [-50], [38], [21], [-16], [48], [37], [-10], [60], [30], [20], [4], [33], [50], [-10], [-22], [24], [-59], [32], [-63], [5], [3], [-52], [74], [6], [-67], [-33], [65], [18], [12], [51], [-47], [4], [23], [-54], [48], [-2], [-56], [15], [49], [34], [3], [-58], [1], [-54], [-35], [1], [-30], [12], [44], [41], [-33], [58], [-40], [39], [-24], [17], [4], [56], [-38], [-44], [52], [34], [-32], [77], [12], [-61], [26], [72], [-46], [18], [59], [84], [80], [-15], [24], [64], [-19], [42], [58], [8], [61], [-32], [-39], [38], [-24], [64], [37], [1], [-45], [10], [-7], [-46], [-48], [75], [-2], [18], [-72], [4], [71], [63], [-81], [48], [-86], [4], [42], [66], [-90], [90], [-10], [53], [40], [39], [-15], [50], [-26], [83], [-6], [12], [-48], [-68], [-36], [12], [56], [-3], [-42], [-22], [-6], [79], [-59], [92], [-34], [-76], [-18], [77], [-73], [-3], [82], [69], [-25], [-42], [40], [30], [82], [33], [-64], [-20], [24], [26], [-90], [62], [13], [60], [-70], [14], [-30], [-23], [-21], [28], [-68], [12], [-21], [24], [-47], [-42], [52], [64], [-26], [-68], [-42], [42], [12], [14], [53], [-77], [55], [81], [10], [-6], [-97], [-42], [25], [40], [-86], [-21], [90], [68], [29], [18], [62], [-85], [-33], [-69], [-44], [46], [-72], [43], [-36], [70], [48], [27], [-3], [37], [-24], [-54], [-51], [-60]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1003_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1003_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_1003_2_a_c(:prec:=1)
    chi := MakeCharacter_1003_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_1003_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_1003_2_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1003_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, 1]>,<3,R![-1, 1]>],Snew);
    return Vf;
end function;