
// Make newform 3450.2.a.q in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3450_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_3450_2_a_q();".
// 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_3450_2_a_q();".
// 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_3450_a();"
function MakeCharacter_3450_a()
    N := 3450;
    order := 1;
    char_gens := [1151, 277, 1201];
    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_3450_a_Hecke(Kf)
    return MakeCharacter_3450_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], [0], [0], [4], [2], [-2], [0], [-1], [6], [0], [-2], [2], [12], [-8], [2], [0], [6], [-12], [12], [6], [4], [4], [-6], [10], [6], [-8], [4], [-2], [6], [16], [0], [14], [4], [-10], [-16], [14], [20], [-8], [14], [-8], [6], [16], [6], [22], [-4], [4], [-16], [12], [14], [26], [-12], [-22], [12], [-22], [-8], [-18], [-8], [-14], [10], [-28], [18], [-12], [-12], [-22], [6], [-12], [-22], [-12], [-2], [10], [0], [-16], [-10], [8], [-24], [6], [10], [18], [10], [-12], [-2], [32], [26], [0], [-36], [-14], [2], [30], [-8], [-36], [-24], [-16], [-32], [12], [0], [-26], [-22], [20], [-34], [36], [26], [20], [42], [-40], [-10], [-28], [42], [44], [26], [8], [-10], [-18], [-8], [-20], [18], [-28], [-16], [30], [12], [30], [-34], [-6], [-44], [-44], [-2], [-50], [-4], [-32], [14], [-20], [-16], [-44], [-50], [42], [10], [-6], [4], [-38], [18], [-28], [-2], [-16], [20], [-2], [56], [-30], [-6], [-52], [0], [-14], [2], [-12], [-8], [20], [40], [36], [-30], [-22], [-50], [-12], [6], [32], [-36], [-26], [-56], [16], [26], [-6], [14], [36], [30], [0], [26], [-24], [10], [-52], [38], [8], [46], [16], [36], [-62], [42], [40], [6], [2], [44], [26], [4], [-10], [52], [-64], [6], [4], [-42], [-22], [54], [14], [-24], [-2], [-40], [18], [26], [12], [62], [-4], [-44], [66], [20], [-2], [30], [-32], [-28], [12], [18], [8], [18], [56], [-2], [6], [68], [26], [64], [-12], [-10], [-50], [44], [40], [-24], [42], [-56], [44], [-14], [52], [72], [-46], [66], [-56], [-20], [-12], [44], [-64], [-66], [-62], [-76], [-64], [-36], [16], [-24], [22], [18], [32], [26], [30], [-48], [54], [20], [-18], [-2], [-16], [-76], [-10], [-2], [42], [80], [-34], [58], [12], [-82], [-50], [-20], [-70], [44], [-18], [-80], [12], [-82], [2], [76], [-72], [68], [32], [54], [68], [12], [18], [34], [-32], [-78], [-50], [-20], [18], [-60], [-46], [30], [20], [30], [-88], [-52], [-86], [-22], [60], [-36], [40], [22], [68], [70], [48], [34], [-16], [-58], [34], [44], [-8], [34], [-24], [4], [2], [82], [16], [-38], [70], [0], [-42], [74], [8], [-76], [8], [2], [62], [14], [16], [-60], [24], [28], [-82], [6], [-22], [8], [-14], [6], [-74], [-36], [-14], [32], [-58], [52], [-64], [-78], [20], [-42], [-26], [24], [30], [-14], [-32], [28], [66], [48], [-50], [-6], [56], [-44], [-12], [94], [30], [-8], [50], [32], [20], [-24], [-10], [-52], [-30], [-24], [-72], [90], [50], [62], [-98], [30], [32], [66], [-16], [-32], [-88], [18], [52], [-72], [50], [14], [-40], [-20], [-72], [58], [92], [-38], [-32], [-2], [-66], [-94], [72], [-42], [-10], [88], [54], [-94], [28], [16], [62], [-98], [-68], [-8], [2], [-18], [4], [-56], [-26], [-96], [-34], [-10], [-8], [72], [30], [-58], [68], [42], [68], [-60]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3450_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3450_2_a_q();".
// 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_3450_2_a_q(:prec:=1)
    chi := MakeCharacter_3450_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_3450_2_a_q();".
// 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_3450_2_a_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3450_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![0, 1]>,<11,R![-4, 1]>,<13,R![-2, 1]>,<17,R![2, 1]>],Snew);
    return Vf;
end function;