
// Make newform 304.3.r.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_304_r();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_304_r_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_304_3_r_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_304_3_r_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 := [4, 0, -2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2, 2];
    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_304_r();"
function MakeCharacter_304_r()
    N := 304;
    order := 6;
    char_gens := [191, 229, 97];
    v := [6, 6, 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_304_r_Hecke();"
function MakeCharacter_304_r_Hecke(Kf)
    N := 304;
    order := 6;
    char_gens := [191, 229, 97];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 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 := 3;
    raw_aps := [[0, 0, 0, 0], [-2, 1, 1, -1], [0, 0, 1, 0], [-2, 4, 0, -2], [10, -4, 0, 2], [-5, 6, -5, 0], [0, 2, 7, 2], [-5, 6, 2, 9], [5, -9, -5, 18], [11, 18, 11, 0], [-18, 0, 36, 0], [2, 0, -4, 18], [6, -42, -3, 42], [0, 23, -19, 23], [35, 19, -35, -38], [-7, 48, -7, 0], [-14, 33, 7, -33], [37, 16, -37, -32], [-17, -63, -17, 0], [-102, 9, 51, -9], [0, -32, 49, -32], [42, 21, -21, -21], [16, 12, 0, -6], [-1, -6, -1, 0], [-162, -6, 81, 6], [67, 30, -67, -60], [56, 0, -112, -12], [56, 0, -112, -12], [30, 30, -15, -30], [62, 0, -124, 18], [-75, -3, -75, 0], [0, -5, 95, -5], [7, 2, -7, -4], [5, 15, -5, -30], [0, 46, -77, 46], [-20, 0, 40, 36], [0, 96, -35, 96], [142, -12, 0, 6], [79, 33, 79, 0], [-214, 84, 107, -84], [84, 0, -168, 120], [1, 24, 1, 0], [-50, 188, 0, -94], [154, 60, -77, -60], [296, 36, 0, -18], [-37, -121, 37, 242], [-242, -99, 121, 99], [22, 9, -11, -9], [-56, 0, 112, 108], [-196, -52, 0, 26], [0, 34, 307, 34], [184, 92, 0, -46], [7, -90, 7, 0], [365, 51, -365, -102], [-63, -84, -63, 0], [0, 51, 11, 51], [-82, 234, 41, -234], [0, -129, 83, -129], [260, -220, 0, 110], [61, 36, 61, 0], [0, 31, -73, 31], [138, 0, -276, -66], [-214, -51, 107, 51], [-56, -132, 0, 66], [31, 114, -31, -228], [-165, -42, -165, 0], [56, 0, -112, -276], [-170, 222, 85, -222], [0, 15, 341, 15], [-268, -260, 0, 130], [80, 324, 0, -162], [0, -81, -49, -81], [359, 51, -359, -102], [-144, 0, 288, -276], [104, 0, -208, -204], [178, 285, -89, -285], [289, -156, -289, 312], [0, -72, -83, -72], [-98, -258, 49, 258], [-155, 264, -155, 0], [-566, 84, 0, -42], [10, -348, -5, 348], [-171, -111, -171, 0], [277, 192, 277, 0], [-406, -195, 203, 195], [-7, 183, 7, -366], [-98, 0, 196, 126], [326, 96, 0, -48], [0, -166, 211, -166], [40, 556, 0, -278], [-218, -44, 0, 22], [575, -93, -575, 186], [-12, 0, 24, 336], [0, 3, 527, 3], [0, 71, 617, 71], [89, 135, -89, -270], [351, 138, 351, 0], [376, 0, -752, 168], [-189, -123, -189, 0], [643, -102, -643, 204], [389, -135, 389, 0], [-299, 184, 299, -368], [-4, 0, 8, 120], [-362, 0, 724, 234], [-554, -228, 0, 114], [20, -412, 0, 206], [0, 27, -685, 27], [379, -78, -379, 156], [137, -111, 137, 0], [262, 0, -524, -150], [-76, 0, 152, -168], [0, 310, 379, 310], [-737, -74, 737, 148], [874, 204, 0, -102], [83, -1, -83, 2], [114, -420, -57, 420], [0, 203, -7, 203], [646, -124, 0, 62], [-88, 460, 0, -230], [-33, -363, -33, 0], [159, -210, 159, 0], [190, 0, -380, 414], [270, 0, -540, -138], [-42, 0, 84, 168], [-614, 348, 0, -174], [0, -126, -233, -126], [697, -84, -697, 168], [0, -357, 299, -357], [0, -285, -121, -285], [-4, 660, 0, -330], [0, 171, -343, 171], [-258, -255, 129, 255], [-203, -171, -203, 0], [0, 168, 145, 168], [8, -996, 0, 498], [145, 332, -145, -664], [-299, 36, -299, 0], [540, 0, -1080, -336], [-126, 0, 252, -702], [-100, -828, 0, 414], [139, -159, 139, 0], [151, 22, -151, -44], [59, 483, -59, -966], [349, 381, 349, 0], [-232, 0, 464, 480], [-282, -303, 141, 303], [0, -312, 517, -312], [-350, -288, 175, 288], [131, 359, -131, -718], [-126, 0, 252, 168], [174, 354, -87, -354], [-484, -28, 0, 14], [221, -365, -221, 730], [-705, -195, -705, 0], [-574, 153, 287, -153], [204, 0, -408, 804], [382, -132, 0, 66], [0, -228, -671, -228], [193, -24, -193, 48], [-179, -204, -179, 0], [0, 39, 725, 39], [-326, 144, 163, -144], [0, 183, 881, 183], [1034, -231, -517, 231], [-506, 0, 1012, 6], [-814, 261, 407, -261], [1346, -423, -673, 423], [229, 84, -229, -168]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_304_r_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_304_3_r_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_304_3_r_a(:prec:=4)
    chi := MakeCharacter_304_r();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_304_3_r_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_304_3_r_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_304_r();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![1, 6, 13, 6, 1]>],Snew);
    return Vf;
end function;