
// Make newform 2450.2.a.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2450_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_2450_2_a_b();".
// 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_2450_2_a_b();".
// 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_2450_a();"
function MakeCharacter_2450_a()
    N := 2450;
    order := 1;
    char_gens := [1177, 101];
    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_2450_a_Hecke(Kf)
    return MakeCharacter_2450_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], [-3], [0], [0], [-2], [0], [4], [-6], [-3], [9], [-4], [4], [-7], [5], [-8], [2], [10], [1], [9], [2], [4], [10], [7], [1], [-14], [3], [-1], [-3], [-9], [-2], [-16], [8], [-12], [-14], [3], [-16], [-10], [4], [21], [-8], [12], [7], [-18], [-26], [-2], [-4], [-26], [-28], [4], [22], [-24], [16], [10], [0], [-8], [-5], [3], [-6], [-12], [2], [4], [-28], [-7], [-18], [-8], [32], [-32], [26], [-19], [-35], [18], [-4], [11], [4], [30], [-15], [26], [-22], [31], [3], [0], [-19], [-30], [-14], [-20], [-31], [-33], [32], [-14], [19], [13], [24], [16], [-12], [-18], [21], [1], [38], [20], [3], [33], [2], [-17], [-18], [-30], [-10], [28], [6], [12], [42], [-1], [-12], [-44], [-46], [2], [5], [-28], [11], [4], [-26], [-11], [-16], [48], [37], [22], [-47], [-11], [-6], [21], [-22], [-2], [-9], [-4], [-16], [-6], [-14], [-24], [-31], [0], [-51], [-28], [-18], [-19], [19], [-46], [14], [-14], [18], [-48], [11], [-38], [-7], [12], [-29], [-5], [30], [38], [43], [28], [-46], [25], [12], [-37], [-48], [30], [-17], [40], [46], [37], [62], [-16], [35], [24], [-54], [-4], [49], [-26], [-30], [49], [43], [25], [0], [-2], [34], [-12], [45], [-40], [4], [31], [-18], [-36], [-16], [10], [-55], [-12], [-66], [-23], [-10], [-42], [-8], [61], [56], [6], [45], [0], [-38], [-34], [60], [9], [-4], [-66], [21], [47], [-29], [-18], [50], [-19], [3], [32], [-72], [17], [14], [-39], [-24], [7], [23], [-64], [26], [41], [-4], [-40], [66], [58], [33], [-7], [-45], [-41], [4], [2], [70], [44], [8], [-24], [-11], [0], [-6], [-77], [-60], [-16], [40], [-16], [15], [-33], [-21], [60], [10], [47], [11], [42], [6], [51], [5], [10], [28], [-24], [50], [30], [14], [5], [-12], [15], [72], [46], [24], [-14], [-8], [-80], [5], [45], [22], [-31], [-16], [0], [-21], [55], [-60], [2], [-72], [-4], [-42], [33], [25], [32], [-70], [54], [-81], [22], [14], [-6], [28], [48], [-38], [-2], [-51], [-4], [-72], [-68], [-63], [24], [4], [-57], [54], [31], [37], [33], [17], [0], [54], [42], [23], [52], [32], [-7], [-51], [-12], [-55], [80], [27], [32], [-30], [-46], [72], [70], [-3], [-58], [-63], [58], [6], [-35], [-8], [44], [68], [-21], [-82], [18], [58], [45], [53], [-84], [48], [-34], [-12], [9], [-17], [45], [0], [36], [10], [-24], [33], [-2], [-34], [-47], [52], [11], [-52], [56], [-40], [37], [54], [-20], [11], [-54], [-88], [-52], [38], [30], [4], [-90], [42], [-53], [-16], [33], [72], [0], [-45], [-30], [4], [25], [-91], [-82], [-64], [40], [-36], [-74], [2], [-74], [49], [-12], [-51], [15], [-66], [-73], [48], [31], [-62], [-68], [-33], [-3], [-48], [-50], [18], [12], [-32], [82], [106], [-18], [-24], [42], [-29], [19], [-8], [-93], [56], [-42], [-10], [4], [-33], [-32], [-46]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2450_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2450_2_a_b();".
// 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_2450_2_a_b(:prec:=1)
    chi := MakeCharacter_2450_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_2450_2_a_b();".
// 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_2450_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2450_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![3, 1]>,<11,R![2, 1]>,<13,R![0, 1]>,<17,R![-4, 1]>,<19,R![6, 1]>,<23,R![3, 1]>,<37,R![-4, 1]>],Snew);
    return Vf;
end function;