
// Make newform 405.3.l.a 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_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_405_3_l_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, 0, -1, 0, 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_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, 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]];
    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 := [[-1, 1, 0, 0], [0, 0, 0, 0], [5, 0, 0, 0], [4, -7, 3, 3], [0, 3, -1, 3], [7, -7, 6, 13], [-19, 9, 9, -19], [-9, 0, 18, -15], [-1, 1, -15, -16], [16, 15, -8, -15], [-26, -18, 26, 36], [7, -27, -27, 7], [26, 12, -26, -24], [22, 31, 9, -9], [-4, -14, 18, 18], [-37, -30, 30, 37], [26, 6, 26, 0], [0, -18, 1, -18], [31, 31, -33, 2], [67, -42, 0, 21], [-20, 39, -39, 20], [-46, 57, 23, -57], [-40, -88, -48, 48], [45, 0, -90, -96], [70, -73, 3, 3], [0, 48, -4, 48], [28, -28, -102, -74], [-4, 0, 0, -4], [33, 0, -66, -72], [-40, 40, 57, 17], [-29, -72, -72, -29], [-19, -93, 19, 186], [23, 121, -144, -144], [-38, 120, -38, 0], [41, 48, 41, 0], [0, -36, -122, -36], [103, 103, -144, 41], [-92, -72, 72, 92], [119, 119, 63, -182], [23, -112, -135, 135], [72, 0, -144, -72], [188, -60, 0, 30], [0, -27, 107, -27], [-47, 47, -66, -113], [-1, -153, -153, -1], [-60, 0, 120, -138], [-245, -15, 245, 30], [70, 139, 69, -69], [128, -77, -51, -51], [-38, 75, -38, 0], [-253, -93, 93, 253], [-118, 0, -118, 0], [0, 123, -122, 123], [37, -366, 0, 183], [134, 134, -111, -23], [20, -256, -276, 276], [-159, 0, 318, -84], [-139, 18, 0, -9], [-14, -91, 105, 105], [0, 153, -4, 153], [-8, 8, 282, 274], [-52, 52, -135, -187], [-260, 204, 204, -260], [-196, 150, 196, -300], [217, 25, -192, 192], [-373, 181, 192, 192], [0, -69, 127, -69], [271, 271, -249, -22], [-127, -127, -255, 382], [-64, 222, 32, -222], [-172, 161, 333, -333], [-3, 0, 6, -489], [-296, 32, 264, 264], [-107, 107, -219, -326], [-186, 0, 372, -120], [293, -293, 105, 398], [388, -204, -194, 204], [-242, 153, 153, -242], [-55, -108, 55, 216], [-380, 33, -380, 0], [164, -108, 164, 0], [0, 66, -179, 66], [-80, 636, 0, -318], [217, 273, -273, -217], [-640, 30, 320, -30], [-274, 32, 306, -306], [132, 0, -264, 576], [7, -19, 12, 12], [0, -108, -250, -108], [34, -34, 84, 118], [584, -138, -138, 584], [-386, -213, 193, 213], [28, 132, 132, 28], [-673, -63, 673, 126], [1, -207, 1, 0], [380, 222, -222, -380], [-286, -354, -286, 0], [-458, -60, 0, 30], [-125, 168, -168, 125], [392, -114, 0, 57], [-98, 524, -426, -426], [-268, 597, 597, -268], [68, -68, 48, 116], [-92, -537, 46, 537], [214, -60, -214, 120], [265, 237, 237, 265], [-4, 73, -69, -69], [152, -339, 339, -152], [386, -114, 386, 0], [0, -51, 550, -51], [-41, -41, -525, 566], [379, 414, -414, -379], [239, 239, -198, -41], [440, -42, -220, 42], [-634, -564, 0, 282], [0, -99, -130, -99], [-326, 326, 636, 310], [803, -36, -36, 803], [-319, 319, 405, 86], [-434, 459, 217, -459], [-587, -102, 587, 204], [475, -254, -729, 729], [290, -557, 267, 267], [-64, -102, 102, 64], [0, 15, 103, 15], [-1055, 36, 0, -18], [368, 573, -184, -573], [-177, 0, 354, -51], [-188, 83, 105, 105], [319, -319, 3, 322], [-564, 0, 1128, 42], [-490, 490, 60, -430], [277, -465, -277, 930], [-191, -18, -18, -191], [749, -282, -749, 564], [199, -540, 199, 0], [-211, 531, -531, 211], [-35, -35, 555, -520], [8, 8, -165, 157], [465, 0, -930, 102], [566, -948, 0, 474], [0, 420, -163, 420], [-110, 110, 534, 424], [275, -132, -132, 275], [24, 0, -48, 570], [-1190, -183, 595, 183], [-374, -209, 165, -165], [-13, -200, 213, 213], [238, -594, 238, 0], [263, -78, 78, -263], [1102, 1102, -1125, 23], [442, -1008, 0, 504], [634, -312, 312, -634], [347, 347, 513, -860], [166, -142, -24, -24], [0, -669, -499, -669], [351, 0, -702, -651], [688, -675, -344, 675], [166, -957, -957, 166], [-520, 315, 520, -630], [-646, 367, 279, 279], [-382, 219, -219, 382], [-671, -671, 45, 626], [31, 1710, 0, -855], [-745, -745, 267, 478], [-154, -760, -606, 606], [251, 222, 0, -111], [-941, 743, 198, 198]];
    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_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_405_3_l_a(:prec:=4)
    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_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_405_3_l_a( : 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, 2, 5, 4, 1]>],Snew);
    return Vf;
end function;