
// Make newform 6664.2.a.h in Magma, downloaded from the LMFDB on 03 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6664_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_6664_2_a_h();".
// 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_6664_2_a_h();".
// 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
        poly := [-1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_6664_a();"
function MakeCharacter_6664_a()
    N := 6664;
    order := 1;
    char_gens := [4999, 3333, 4217, 785];
    v := [1, 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_6664_a_Hecke(Kf)
    return MakeCharacter_6664_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]];
    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, 0], [0, 1], [1, 1], [0, 0], [-2, 4], [2, -4], [-1, 0], [0, 4], [-8, 2], [2, 0], [1, 5], [4, 0], [2, 5], [-2, -5], [8, 2], [5, -5], [4, 0], [-6, -1], [3, 5], [0, 0], [6, -9], [-8, 10], [8, 2], [8, -6], [-7, 13], [4, 8], [4, 6], [-10, -4], [10, 0], [4, -6], [13, -5], [8, -4], [-8, -5], [5, -11], [4, 3], [-10, 7], [-8, -4], [0, -8], [4, -7], [18, 3], [6, -15], [-6, 16], [3, -7], [-10, 4], [24, -4], [-16, 5], [-4, 18], [0, 8], [5, -3], [-26, 4], [6, 0], [-2, 7], [5, -11], [12, -16], [18, -22], [12, -24], [-6, 0], [4, -2], [4, -14], [3, -3], [-17, 19], [2, -10], [18, -2], [-5, -17], [21, -15], [2, 18], [-11, 3], [4, 0], [6, -20], [2, 10], [26, -12], [-23, 11], [-12, -7], [-24, 5], [14, -28], [18, -20], [-11, 7], [25, -19], [14, -8], [-4, 26], [-25, 23], [8, 13], [-22, 14], [14, -6], [8, -9], [-36, 4], [-12, -4], [-17, -7], [-10, -10], [22, -21], [-6, -10], [-28, 3], [-2, -2], [-17, 17], [-6, 10], [9, -19], [24, 0], [14, 3], [-8, -6], [40, -6], [0, 24], [-2, 12], [26, -20], [20, -29], [-4, -4], [16, -8], [-2, -6], [24, -12], [2, 21], [30, 0], [-9, 7], [-11, -13], [-26, 24], [-24, -4], [7, 5], [14, -2], [-4, -9], [42, -12], [6, -16], [11, -7], [14, -28], [22, 6], [34, -20], [-4, -8], [0, -3], [10, 0], [14, 12], [0, -7], [30, -10], [-38, 22], [34, -11], [10, -2], [-8, 0], [16, -7], [-14, 42], [4, -36], [-44, 10], [16, 4], [-20, 18], [16, 0], [12, 11], [-8, -12], [-38, 22], [-10, 2], [6, -32], [12, 4], [-14, 32], [-21, 15], [32, -6], [-7, 15], [48, -12], [-29, -17], [-15, 35], [-19, 21], [6, -28], [-10, 4], [-2, 29], [-25, 39], [-48, 0], [-10, 19], [18, 24], [-3, -9], [-22, -9], [2, -12], [-25, 17], [1, 17], [38, -12], [50, -9], [-16, -8], [6, 21], [-24, -12], [16, -16], [24, -38], [-41, 1], [18, -12], [-47, 5], [-16, -18], [-26, 26], [-10, -16], [-34, 6], [4, -16], [-40, 21], [-16, 14], [-26, -2], [25, -17], [16, -14], [8, 12], [-30, 2], [26, -12], [12, -8], [-10, -13], [-32, -8], [-32, 0], [-4, -12], [-22, 44], [-17, -1], [20, -38], [2, -30], [-22, 53], [14, -12], [-18, 4], [45, -15], [8, 30], [20, 14], [-34, 34], [54, 0], [-54, 12], [62, -3], [-4, 10], [14, -52], [6, -38], [-10, -4], [-12, -4], [30, -38], [0, -15], [-34, 21], [-44, -16], [16, -18], [26, -10], [-13, 5], [-35, 35], [-24, 5], [36, -19], [-20, 6], [-8, -10], [-17, -11], [50, -8], [22, -6], [16, 12], [38, 18], [-20, 15], [48, 4], [-28, -12], [16, 19], [-42, 12], [-52, 38], [-14, -16], [-16, 36], [12, 2], [20, -34], [-26, 24], [-18, -8], [34, 10], [-4, 15], [-37, -5], [1, 17], [-12, -13], [-12, -5], [-37, -7], [34, -39], [-48, 31], [-37, -1], [13, -53], [47, -41], [26, -24], [58, -17], [18, -42], [19, -29], [-2, 31], [0, -16], [4, -24], [-49, -7], [11, 35], [65, -19], [70, -5], [-46, -4], [17, -49], [36, -12], [-50, 42], [-52, -11], [-37, 19], [52, -40], [6, -10], [-19, -9], [48, -30], [8, 32], [-14, -7], [29, -49], [-20, -23], [-30, 14], [-22, 12], [-30, 66], [-12, -4], [-14, -22], [-57, -17], [6, -19], [-14, 10], [-40, 50], [2, 30], [-36, 42], [36, -72], [-35, 53], [10, -48], [-21, 31], [-48, -2], [22, 27], [66, -28], [-8, -30], [21, 21], [-48, 28], [52, -20], [-10, 48], [50, -20], [56, -12], [-30, -18], [30, -34], [35, -27], [-18, 11], [25, -9], [-32, -5], [-15, -35], [-32, -14], [40, -34], [-26, 19], [18, 37], [4, 2], [-8, -20], [20, -12], [22, -5], [-30, 72], [-8, -15], [16, 22], [-54, 36], [69, 7], [-36, 2], [-14, 2], [-8, 0], [41, -59], [50, -4], [-22, 14], [-2, 30], [-62, 6], [-8, 41], [6, -32], [-22, -22], [52, 0], [36, -49], [-58, 23], [26, -5], [70, 0], [-60, 21], [11, -35], [-58, 4], [81, -17], [40, -5], [-34, 24], [-71, 9], [65, -37], [-7, -31], [14, 7], [28, -14], [14, 16], [20, -21], [27, -49], [-29, -3], [-36, 46], [46, -34], [48, 19], [60, -56], [72, -16], [52, 2], [-31, 41], [1, -25], [50, -44], [4, -56], [-45, -15], [82, 7], [42, -21], [36, -52], [42, -35], [-52, -25], [4, -10], [4, 29], [-48, 62], [-28, 14], [60, -17], [0, -12], [34, -10], [44, -40], [-28, 18], [-6, -33], [38, 28], [38, 12], [52, 2], [56, -37], [4, -40], [-28, 16], [-32, 62], [14, 44], [42, -26], [-48, 8], [38, -16], [49, -21], [58, -50], [23, 25], [-74, -14], [48, 12], [-40, 46], [12, -38], [-13, -7], [-18, -24], [29, 15], [-18, 65], [-26, 35], [-72, 14], [-24, 68], [75, -11], [64, -7], [-42, 60], [-20, 45], [53, -19], [-51, -21], [-51, -5], [12, -60], [-4, -23], [-2, -39], [10, -6], [43, -45], [49, -71], [16, 40], [52, -21], [-2, -15], [-26, 34], [40, 30], [-46, 64], [-48, 32], [42, -2], [14, 36], [48, -66], [-35, 29]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6664_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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