
// Make newform 784.4.f.a in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_784_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_784_f_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_784_4_f_a();".
// 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_784_4_f_a();".
// 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;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_784_f();"
function MakeCharacter_784_f()
    N := 784;
    order := 2;
    char_gens := [687, 197, 689];
    v := [1, 2, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_784_f_Hecke();"
function MakeCharacter_784_f_Hecke(Kf)
    N := 784;
    order := 2;
    char_gens := [687, 197, 689];
    char_values := [[-1, 0], [1, 0], [-1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 4;
    raw_aps := [[0, 0], [-7, 0], [0, 9], [0, 0], [0, -7], [0, 40], [0, 19], [119, 0], [0, 77], [210, 0], [301, 0], [-77, 0], [0, -68], [0, -70], [-357, 0], [327, 0], [609, 0], [0, 397], [0, -91], [0, 126], [0, -33], [0, 469], [-588, 0], [0, 425], [0, -180], [0, 1139], [-1001, 0], [0, -1043], [875, 0], [54, 0], [0, 462], [651, 0], [-1869, 0], [-140, 0], [-1185, 0], [0, 161], [0, 1367], [0, -119], [-3444, 0], [0, -1139], [0, -2527], [0, -404], [0, -287], [3211, 0], [-1566, 0], [-259, 0], [0, -1638], [-2800, 0], [1533, 0], [0, 961], [-1533, 0], [0, -1862], [0, 103], [-5208, 0], [0, -2875], [0, -483], [0, -2081], [3367, 0], [4447, 0], [2982, 0], [-259, 0], [0, 852], [-1036, 0], [-3591, 0], [0, 4097], [4083, 0], [0, -2191], [2366, 0], [0, 5005], [0, -40], [0, 219], [0, 6209], [-679, 0], [12395, 0], [0, 6510], [5523, 0], [8043, 0], [0, -8367], [-5229, 0], [0, -3981], [7476, 0], [9298, 0], [0, -4935], [0, -7100], [-4837, 0], [0, 8253], [-9186, 0], [-8017, 0], [0, -2728], [0, -3122], [-7581, 0], [609, 0], [0, -6307], [0, 5054], [0, 5929], [-2520, 0], [0, 1825], [0, 9487], [-7049, 0], [-12161, 0], [0, -5530], [-7005, 0], [13545, 0], [3255, 0], [0, -12719], [0, -8521], [-756, 0], [0, 5605], [0, -6363], [0, -6396], [1883, 0], [3563, 0], [5166, 0], [-23471, 0], [0, 3878], [-13545, 0], [-3220, 0], [-28119, 0], [-19509, 0], [0, -3206], [0, -2217], [-1442, 0], [0, 2641], [0, 7441], [34867, 0], [-25914, 0], [-14917, 0], [-5817, 0], [-26768, 0], [0, 15549], [0, 9177], [0, 6230], [0, -14959], [3458, 0], [0, 4541], [0, -6988], [0, -15349], [-14735, 0], [0, 2336], [-41361, 0], [31444, 0], [-15153, 0], [0, -8379], [0, 17122], [0, 10379], [-38640, 0], [0, 12672], [0, 19591], [18935, 0], [0, 1533], [45479, 0], [0, -10488], [0, -13342], [-43029, 0], [0, 17381], [0, -20202], [0, 28721], [0, 16721], [0, -17064], [0, 25807], [0, -11487], [7758, 0], [0, -24850], [47187, 0], [34923, 0], [42021, 0], [0, -30667], [0, -17281]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_784_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_784_4_f_a();".
// 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_784_4_f_a(:prec:=2)
    chi := MakeCharacter_784_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_784_4_f_a();".
// 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_784_4_f_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_784_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![7, 1]>],Snew);
    return Vf;
end function;