
// Make newform 441.4.e.k in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_441_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_441_e_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_441_4_e_k();".
// 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_441_4_e_k();".
// 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_441_e();"
function MakeCharacter_441_e()
    N := 441;
    order := 3;
    char_gens := [344, 199];
    v := [3, 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_441_e_Hecke();"
function MakeCharacter_441_e_Hecke(Kf)
    N := 441;
    order := 3;
    char_gens := [344, 199];
    char_values := [[1, 0], [0, -1]];
    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, 2], [0, 0], [0, -7], [0, 0], [-5, 5], [14, 0], [21, -21], [0, 49], [0, -159], [-58, 0], [147, -147], [0, -219], [350, 0], [-124, 0], [0, -525], [303, -303], [105, -105], [0, -413], [-415, 415], [432, 0], [-1113, 1113], [0, 103], [1092, 0], [0, 329], [882, 0], [-1379, 1379], [0, -679], [0, 457], [1125, -1125], [1538, 0], [72, 0], [0, -2149], [-1125, 1125], [-252, 0], [0, -201], [-1619, 1619], [679, -679], [0, 467], [1204, 0], [0, 2821], [-3253, 3253], [-1582, 0], [0, 2557], [397, -397], [-2914, 0], [3339, -3339], [1780, 0], [1400, 0], [2205, -2205], [0, 287], [0, 4587], [-1668, 0], [-3409, 3409], [-4760, 0], [0, 805], [-257, 257], [-3591, 3591], [0, 1393], [-415, 415], [4954, 0], [-4277, 4277], [7742, 0], [7364, 0], [-9975, 9975], [0, -4753], [0, -3477], [0, -3341], [7366, 0], [7415, -7415], [3878, 0], [-1267, 1267], [0, 4685], [-4641, 4641], [0, 8797], [13680, 0], [0, -9765], [1731, -1731], [0, 10983], [0, 6603], [10955, -10955], [6636, 0], [-16630, 0], [4923, -4923], [-8974, 0], [0, -4179], [0, -12927], [2826, 0], [0, -8479], [9338, 0], [-4016, 0], [0, 5859], [-6503, 6503], [16049, -16049], [-8864, 0], [0, 10211], [-1680, 0], [0, 9457], [18081, -18081], [0, 20377], [0, 6193], [-18464, 0], [-9413, 9413], [-3199, 3199], [0, 21583], [-20267, 20267], [13951, -13951], [-20972, 0], [0, 189], [-10281, 10281], [6090, 0], [0, 4949], [15797, -15797], [9378, 0], [-24353, 24353], [-12640, 0], [-1041, 1041], [-9548, 0], [3241, -3241], [0, -8853], [-7044, 0], [-12089, 12089], [982, 0], [0, 30513], [11475, -11475], [0, -28315], [-10614, 0], [0, -10299], [0, -32529], [-29456, 0], [0, 27867], [-19539, 19539], [-1248, 0], [0, -28093], [35954, 0], [0, 861], [-24710, 0], [-16499, 16499], [16471, -16471], [-36470, 0], [35751, -35751], [16492, 0], [0, -41473], [25065, -25065], [-9732, 0], [27755, -27755], [21112, 0], [21238, 0], [35609, -35609], [0, 2177], [0, -32247], [0, -27631], [24402, 0], [-19612, 0], [0, -2261], [23833, -23833], [-31824, 0], [0, 16819], [0, -1799], [-14154, 0], [-12047, 12047], [0, -24379], [52330, 0], [-12416, 0], [0, -36813], [34995, -34995], [14301, -14301], [2665, -2665], [24871, -24871]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_441_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_441_4_e_k();".
// 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_441_4_e_k(:prec:=2)
    chi := MakeCharacter_441_e();
    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_441_4_e_k();".
// 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_441_4_e_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_441_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![4, -2, 1]>,<5,R![49, 7, 1]>,<13,R![-14, 1]>],Snew);
    return Vf;
end function;