
// Make newform 405.3.l.f in Magma, downloaded from the LMFDB on 05 February 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_405_l();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_405_l_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_405_3_l_f();".
// 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_405_3_l_f();".
// 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, 0, 0, 0, -1, 0, 0, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 0, 1], [0, 2, 0, 0, 0, -1, 0, 0], [0, 0, 0, 2, 0, 0, 0, -1]];
    Rf_basisdens := [1, 1, 1, 1, 1, 1, 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_405_l();"
function MakeCharacter_405_l()
    N := 405;
    order := 12;
    char_gens := [326, 82];
    v := [4, 9];
    // 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_405_l_Hecke();"
function MakeCharacter_405_l_Hecke(Kf)
    N := 405;
    order := 12;
    char_gens := [326, 82];
    char_values := [[-1, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, -1, 0, 0, 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]];
    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, -1, -1, 0, 1, 0, 0, -1], [0, 0, 0, 0, 0, 0, 0, 0], [-1, -3, 1, 0, 0, -2, 1, 0], [0, -1, -1, 0, 1, 0, 0, 2], [0, 0, 4, -3, 0, 0, 3, -3], [8, 8, -8, 0, 0, 0, 2, 0], [10, 0, 0, 0, -10, 6, 0, -6], [0, 0, 0, 6, 6, 6, 0, -6], [14, 14, -14, 0, 0, 0, 2, 0], [0, 18, 0, 7, -18, 0, -7, -7], [4, 0, -4, 0, 0, -6, 6, 0], [16, 0, 0, 0, -16, -18, 0, 18], [-14, 0, 14, 0, 0, 6, -6, 0], [0, -2, 2, -20, 2, 0, 20, 0], [0, 32, 32, 0, -32, 0, 0, -10], [-14, 0, 0, -12, -14, 0, 0, 0], [0, -36, 0, 0, 0, -31, -31, 0], [0, 0, -50, -18, 0, 0, 18, -18], [50, -50, -50, 0, 0, -4, 0, 0], [68, 0, 0, 0, 0, 0, 0, 0], [19, 0, 0, -48, 19, 0, 0, 0], [0, 0, 0, 10, 0, 0, -10, -10], [0, 4, -4, -14, -4, 0, 14, 0], [0, 0, 0, -36, -6, -36, 0, 36], [0, 5, 5, 0, -5, 0, 0, -16], [0, 0, 34, 57, 0, 0, -57, 57], [77, 77, -77, 0, 0, 0, -10, 0], [-32, 0, 0, 0, 32, -30, 0, 30], [0, 0, 0, 6, -54, 6, 0, -6], [56, 56, -56, 0, 0, 0, -34, 0], [25, 0, 0, 0, -25, 114, 0, -114], [40, 0, -40, 0, 0, 27, -27, 0], [0, -76, -76, 0, 76, 0, 0, 74], [0, -54, 0, 0, 0, 56, 56, 0], [0, -102, 0, 0, 0, -7, -7, 0], [0, 0, -2, 60, 0, 0, -60, 60], [-34, 34, 34, 0, 0, 14, 0, 0], [160, 0, 0, 24, 160, 0, 0, 0], [26, -26, -26, 0, 0, -58, 0, 0], [0, -56, 56, -74, 56, 0, 74, 0], [0, 0, 0, -75, 0, -75, 0, 75], [-154, 0, 0, 54, 0, -54, 0, 54], [0, 0, 4, -18, 0, 0, 18, -18], [89, 89, -89, 0, 0, 0, -136, 0], [82, 0, 0, 0, -82, 12, 0, -12], [0, 0, 0, -12, -222, -12, 0, 12], [-206, 0, 206, 0, 0, 24, -24, 0], [0, 67, -67, 82, -67, 0, -82, 0], [0, 116, 116, 0, -116, 0, 0, -112], [0, -234, 0, 0, 0, -4, -4, 0], [22, 0, 0, -150, 22, 0, 0, 0], [0, 336, 0, 0, 0, -4, -4, 0], [0, 0, 16, 48, 0, 0, -48, 48], [368, 0, 0, 15, 0, -15, 0, 15], [-28, 28, 28, 0, 0, 50, 0, 0], [0, 310, -310, -26, -310, 0, 26, 0], [0, 0, 0, 99, -246, 99, 0, -99], [14, 0, 0, -48, 0, 48, 0, -48], [0, 92, 92, 0, -92, 0, 0, 158], [0, 0, 358, 6, 0, 0, -6, 6], [38, 38, -38, 0, 0, 0, 32, 0], [-70, -70, 70, 0, 0, 0, -136, 0], [22, 0, 0, 0, -22, -120, 0, 120], [-116, 0, 116, 0, 0, -192, 192, 0], [0, 25, -25, -104, -25, 0, 104, 0], [0, -268, -268, 0, 268, 0, 0, -130], [0, 0, -122, -150, 0, 0, 150, -150], [-253, 253, 253, 0, 0, 32, 0, 0], [80, -80, -80, 0, 0, -196, 0, 0], [0, -138, 0, 178, 138, 0, -178, -178], [0, -122, 122, 118, 122, 0, -118, 0], [0, 0, 0, -54, -84, -54, 0, 54], [0, 47, 47, 0, -47, 0, 0, 158], [122, 122, -122, 0, 0, 0, 170, 0], [0, 0, 0, 0, 210, 0, 0, 0], [80, 80, -80, 0, 0, 0, 74, 0], [0, 6, 0, -221, -6, 0, 221, 221], [82, 0, 0, 0, -82, 30, 0, -30], [-50, 0, 50, 0, 0, 192, -192, 0], [0, -84, 0, 0, 0, 176, 176, 0], [0, 336, 0, 0, 0, 101, 101, 0], [0, 0, 22, -96, 0, 0, 96, -96], [-496, 0, 0, 126, 0, -126, 0, 126], [-95, 0, 0, 204, -95, 0, 0, 0], [0, -90, 0, -140, 90, 0, 140, 140], [0, 88, -88, -272, -88, 0, 272, 0], [0, 0, 0, -18, 342, -18, 0, 18], [0, -31, -31, 0, 31, 0, 0, -208], [0, 0, -98, 255, 0, 0, -255, 255], [-103, -103, 103, 0, 0, 0, -190, 0], [-452, 0, 0, 0, 452, 30, 0, -30], [0, 60, 0, 100, -60, 0, -100, -100], [-275, 0, 0, 0, 275, -126, 0, 126], [-260, 0, 260, 0, 0, -63, 63, 0], [0, -186, 0, 0, 0, -226, -226, 0], [208, 0, 0, 162, 208, 0, 0, 0], [0, -486, 0, 0, 0, 119, 119, 0], [134, 0, 0, 186, 0, -186, 0, 186], [-362, 0, 0, 180, -362, 0, 0, 0], [446, 0, 0, -6, 0, 6, 0, -6], [0, 554, 554, 0, -554, 0, 0, -88], [-170, 0, 0, 0, 170, 36, 0, -36], [236, 236, -236, 0, 0, 0, 356, 0], [0, -786, 0, 76, 786, 0, -76, -76], [586, 0, -586, 0, 0, -138, 138, 0], [-395, 0, 0, 0, 395, 84, 0, -84], [0, 452, 452, 0, -452, 0, 0, -130], [-488, 0, 0, 30, -488, 0, 0, 0], [0, 168, 0, 0, 0, -82, -82, 0], [0, 0, -374, 228, 0, 0, -228, 228], [-325, 325, 325, 0, 0, -154, 0, 0], [-314, 0, 0, 246, -314, 0, 0, 0], [74, -74, -74, 0, 0, 386, 0, 0], [0, -486, 0, 136, 486, 0, -136, -136], [32, 0, 0, 30, 0, -30, 0, 30], [0, 0, 106, 24, 0, 0, -24, 24], [152, 152, -152, 0, 0, 0, 500, 0], [-20, 0, 0, 0, 20, 246, 0, -246], [-40, -40, 40, 0, 0, 0, -106, 0], [0, 1020, 0, 25, -1020, 0, -25, -25], [118, 0, -118, 0, 0, 210, -210, 0], [0, 199, -199, 28, -199, 0, -28, 0], [0, 248, 248, 0, -248, 0, 0, 242], [-500, 0, 0, -234, -500, 0, 0, 0], [0, 0, -254, -42, 0, 0, 42, -42], [-70, 0, 0, -333, 0, 333, 0, -333], [0, 6, 0, -296, -6, 0, 296, 296], [0, 0, 0, 252, 12, 252, 0, -252], [0, 131, 131, 0, -131, 0, 0, -94], [332, 332, -332, 0, 0, 0, 50, 0], [0, 0, 0, -66, 354, -66, 0, 66], [188, 188, -188, 0, 0, 0, -190, 0], [316, 0, -316, 0, 0, 222, -222, 0], [-596, 0, 0, 0, 596, -294, 0, 294], [-602, 0, 602, 0, 0, 114, -114, 0], [0, -258, 0, 0, 0, -388, -388, 0], [220, 0, 0, 486, 220, 0, 0, 0], [-862, 862, 862, 0, 0, -40, 0, 0], [152, -152, -152, 0, 0, -130, 0, 0], [0, 0, 0, 0, -150, 0, 0, 0], [602, 0, 0, -300, 0, 300, 0, -300], [0, 0, 238, 111, 0, 0, -111, 111], [-391, -391, 391, 0, 0, 0, 74, 0], [340, 0, 0, 0, -340, -564, 0, 564], [0, 0, 0, -150, 330, -150, 0, 150], [0, -540, 0, 250, 540, 0, -250, -250], [0, 310, -310, -314, -310, 0, 314, 0], [0, 62, 62, 0, -62, 0, 0, 290], [0, -150, 0, 0, 0, 260, 260, 0], [742, 0, 0, -270, 742, 0, 0, 0], [-286, 286, 286, 0, 0, 38, 0, 0], [-298, 0, 0, -96, 0, 96, 0, -96], [-452, 0, 0, -240, -452, 0, 0, 0], [-178, 178, 178, 0, 0, 170, 0, 0], [0, 188, 188, 0, -188, 0, 0, 620], [0, 0, -488, -270, 0, 0, 270, -270], [0, 0, 0, 150, -780, 150, 0, -150], [0, -186, 0, -14, 186, 0, 14, 14], [133, 0, 0, 0, -133, -432, 0, 432], [-674, 0, 674, 0, 0, 351, -351, 0], [0, 164, 164, 0, -164, 0, 0, 824], [388, 0, 0, -198, 388, 0, 0, 0], [-385, 385, 385, 0, 0, 626, 0, 0], [464, 0, 0, -219, 0, 219, 0, -219], [650, -650, -650, 0, 0, -46, 0, 0], [0, 676, -676, 442, -676, 0, -442, 0], [-838, 0, 0, -120, 0, 120, 0, -120], [0, 716, 716, 0, -716, 0, 0, 326]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_405_l_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_405_3_l_f();".
// 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_405_3_l_f(:prec:=8)
    chi := MakeCharacter_405_l();
    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_405_3_l_f();".
// 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_405_3_l_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_405_l();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<2,R![1, -4, 8, -40, 79, 40, 8, 4, 1]>],Snew);
    return Vf;
end function;