
// Make newform 340.2.o.a in Magma, downloaded from the LMFDB on 23 December 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_340_o();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_340_o_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_340_2_o_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_340_2_o_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 := [121, -396, 504, -484, 391, -144, 114, -72, 9, -16, 6, 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, 0, 0], [229900141235, -14415761207, 46892612187, -10963179920, -11073903716, -11375060157, -11213095538, -2154195286, 7020348, 1781832384, 452648926, 237029484], [-730333528452, -1434507604477, 1609214152887, -1376879728680, 843300294187, -223649959859, 445221044856, -13653681406, 46585030124, -49427698226, -7586462510, -6748724960], [-291050856503, 575293488258, -405063970212, 541727327456, -176541913276, 100961817419, -121943505218, 1569096424, -18940257967, 10971218980, 1206883623, 1681562281], [-392384744796, 686426960951, -607285810857, 653017838202, -205296597965, 155956130923, -133897993651, 6206244710, -25605083005, 11535649657, 702587919, 1877462551], [-2812980959800, 6221724061759, -4037516195405, 9351939602630, -2191973179656, 1046088013009, -2325141152849, -138478128814, -272711613374, 222519008952, 37654083852, 32219944720], [-385736208, 696299706, -584723216, 739653400, -218703357, 144512890, -166696279, 945970, -25839746, 15435730, 1834921, 2354972], [-12607579566516, 25065472715263, -17959072699491, 23728481266737, -7543561488176, 4299759622066, -5345644673517, 68761161218, -801822345473, 490247008135, 56161613079, 73257346909], [17037121470248, -27447383123964, 23215477313116, -28201658254877, 7881505857241, -6020317331675, 6025893557670, -30608455483, 1047034493211, -527061304371, -52566459528, -84496178552], [-17011911372159, 29532601568435, -25834105522797, 28244932267173, -8853691313006, 6451651885166, -5986089291485, 189424835950, -1072415229638, 532895178107, 45952103197, 85003027341], [13315455160535, -27108522841694, 21530851893947, -27716108570291, 8215251223637, -5279301694343, 6242542078232, -6562149437, 981046406518, -564718779799, -64707732221, -86757664220], [-22714181280351, 42015081262731, -34331893394458, 41084119438424, -13207313948854, 8412241513732, -9025207727477, 234253438182, -1466105836771, 831492598779, 85006522887, 127836924130]];
    Rf_basisdens := [1, 91818516077, 1010003676847, 59411980991, 59411980991, 1010003676847, 49800487, 1010003676847, 1010003676847, 1010003676847, 1010003676847, 1010003676847];
    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_340_o();"
function MakeCharacter_340_o()
    N := 340;
    order := 4;
    char_gens := [171, 137, 241];
    v := [4, 4, 3];
    // 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_340_o_Hecke();"
function MakeCharacter_340_o_Hecke(Kf)
    N := 340;
    order := 4;
    char_gens := [171, 137, 241];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 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 := 2;
    raw_aps := [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, -1], [-2, 0, 1, 1, -1, 1, 2, 0, 2, 2, 0, -1], [2, 1, 1, 1, -1, 0, 0, 0, -1, 0, 1, 0], [1, -2, -2, 0, 2, 1, 0, 1, -1, -1, 0, 0], [0, 0, 0, 0, 0, 2, -2, 0, 0, 2, 0, 0], [1, -3, -1, -1, 1, 2, -1, 0, 1, 1, 0, 1], [1, -3, -1, -3, 1, -2, 1, 2, 0, -2, 0, -1], [-1, 2, -1, 5, 1, 3, -1, -1, 0, 3, 0, -1], [-1, -2, -1, 3, 1, -1, -1, 0, 0, -1, 2, -1], [0, 2, 2, 2, 0, 0, 0, 0, -2, 4, 0, -2], [0, 0, 0, -2, -2, -2, 0, 1, 0, -1, 0, -1], [0, -3, 1, -1, 1, 0, 0, 2, 1, -2, -1, 0], [0, 0, 0, 0, 0, -2, 0, -1, -1, -3, -1, 0], [0, 0, 0, 4, 4, -2, 0, 0, 0, -2, 0, 0], [-2, 3, 1, 1, -1, -2, 2, 0, 0, 2, 0, -1], [6, -3, -5, -7, 7, 0, 0, 2, -3, -2, 3, 0], [2, 1, 4, 2, -4, -3, 2, 1, 0, -3, -2, 4], [-2, -1, -1, -3, 1, 0, -2, -2, 0, 0, -4, -1], [-1, 1, 2, 2, -6, 1, 1, 0, -2, 0, 0, -2], [0, 0, 0, -4, -4, -4, -2, 0, -1, -4, -1, 5], [-6, -3, -3, -4, 4, 0, 0, -1, 1, 1, -1, 0], [-3, 0, -1, -5, 1, 1, -3, 0, 0, 1, 2, -1], [-4, 3, 3, -2, 2, 0, 0, -3, 3, 3, -3, 0], [4, -3, -7, -1, 1, 0, 0, 3, -2, -3, 2, 0], [-5, -2, 2, -2, -2, -4, -5, -1, 0, -4, -3, 2], [4, -4, -2, -2, 4, 2, -4, 0, -4, -8, 0, 2], [-4, -1, 3, 3, -5, 4, 4, 0, 2, 6, 0, -3], [0, 0, 0, 0, 0, 2, 4, 1, 4, 3, 4, 3], [-3, 1, 2, -2, -2, -1, -3, -1, 0, -1, -8, 2], [-2, -1, -1, -1, 1, 0, 0, 0, 1, 0, -1, 0], [2, 1, 0, 6, 0, 1, 2, -5, 0, 1, 0, 0], [6, -2, 2, 4, -4, 0, 0, 3, 0, -3, 0, 0], [0, 0, 0, 2, 2, 2, 6, 0, 6, 2, 6, -2], [10, -4, -4, -8, 8, 0, 0, 1, -1, -1, 1, 0], [0, -2, -3, -3, -7, -1, 0, 0, 1, -4, 0, 3], [6, 0, -1, 5, 1, 1, 6, 3, 0, 1, 3, -1], [-1, 2, 1, 13, -1, 1, -1, -2, 0, 1, -2, 1], [0, 0, 0, -8, -8, -2, 6, 1, -2, -1, -2, 2], [3, 1, 5, 5, -7, 4, -3, 0, 2, 2, 0, -5], [0, -10, -2, -6, 6, 0, 0, 7, 0, -7, 0, 0], [-6, 6, 2, 2, -4, -4, 6, 0, 8, 2, 0, -2], [5, -1, 6, 6, 6, 7, -5, 0, 4, 7, 0, -6], [5, 7, 0, 4, 0, 7, 5, -3, 0, 7, 2, 0], [1, 0, -5, -5, -1, -5, -1, 0, -4, -2, 0, 5], [0, 0, 0, -4, -4, 6, -16, -6, -3, 0, -3, -1], [-10, 0, -1, -1, 3, -1, 10, 0, -1, 0, 0, 1], [0, 0, 0, -1, -1, 0, -6, -5, -5, -5, -5, 9], [3, 1, 2, -10, -2, -1, 3, -6, 0, -1, -4, 2], [-4, 0, 4, 0, 0, 0, 0, -6, 8, 6, -8, 0], [2, -1, 1, 5, -1, -2, 2, 10, 0, -2, 2, 1], [2, -2, -6, -2, 2, 0, 0, -1, -4, 1, 4, 0], [0, 0, 0, 2, 2, 2, 12, -2, 1, 0, 1, -1], [0, 0, 0, 2, 2, -6, 4, 1, -2, -5, -2, 3], [-6, 2, 4, 2, -4, -2, -6, 0, 0, -2, 0, 4], [2, -6, -2, -14, 14, 0, 0, 6, -2, -6, 2, 0], [-2, 5, 1, -1, -1, 4, -2, -4, 0, 4, -2, 1], [0, 0, 0, -6, -6, -4, 2, -3, 0, -7, 0, 4], [2, 5, -4, -4, -14, -9, -2, 0, -5, -6, 0, 4], [0, 4, -2, 0, 0, 0, 0, 2, -4, -2, 4, 0], [2, 3, 1, 5, -5, 0, 0, -1, -4, 1, 4, 0], [-4, -3, 2, -8, -2, -5, -4, 1, 0, -5, 2, 2], [-4, -1, -3, -3, 1, -2, 4, 0, 8, 8, 0, 3], [-2, 6, 2, 2, -12, -4, 2, 0, 4, 6, 0, -2], [0, 0, 0, -2, -2, -10, -10, -2, -6, -12, -6, 2], [1, -2, 1, 13, -1, -3, 1, -2, 0, -3, -10, 1], [0, 1, -4, -4, 2, -5, 0, 0, 5, -6, 0, 4], [0, 0, 0, -4, -4, -4, -2, -5, -6, -9, -6, 0], [10, 6, -2, 8, -8, 0, 0, -2, -4, 2, 4, 0], [0, 0, 0, -2, -2, -2, 6, 11, 2, 9, 2, -2], [4, 10, 7, 7, -5, -3, -4, 0, -1, 2, 0, -7], [-12, -1, 5, 5, -5, 0, 0, -6, 3, 6, -3, 0], [7, 2, 7, 5, -7, -5, 7, 3, 0, -5, 2, 7], [0, 0, 0, 6, 6, 4, 6, -2, 3, 2, 3, -3], [0, 0, 0, 1, 1, 12, -20, -6, -1, 6, -1, -9], [-12, 0, 4, 4, -2, 4, 12, 0, 2, 10, 0, -4], [3, -1, 5, 5, 9, 6, -3, 0, 0, 2, 0, -5], [-14, 6, 10, 0, 0, 0, 0, -8, 2, 8, -2, 0], [3, 6, 3, 3, -5, -3, -3, 0, -6, -8, 0, -3], [4, -13, -5, 8, -8, 0, 0, 6, 1, -6, -1, 0], [9, -5, -4, -4, -8, 1, -9, 0, -8, -8, 0, 4], [0, 0, 0, 4, 4, 12, 14, 4, 11, 16, 11, -5], [-2, 6, 5, 13, -5, 1, -2, -11, 0, 1, 0, 5], [6, -3, -1, 5, -5, 0, 0, 3, -6, -3, 6, 0], [2, 3, -1, -1, -15, -4, -2, 0, -4, -12, 0, 1], [0, 0, 0, 6, 6, 8, 6, -4, -3, 4, -3, 1], [0, 0, 0, -4, -4, -4, -10, -5, -2, -9, -2, 4], [-8, 1, 1, -5, 5, 0, 0, 5, 2, -5, -2, 0], [0, 0, 0, 6, 6, 0, -14, 5, 0, 5, 0, -1], [1, 2, -1, 5, 1, 3, 1, -3, 0, 3, -10, -1], [2, 0, 1, 1, -11, 1, -2, 0, 11, 6, 0, -1], [0, 0, 0, 8, 8, 6, 12, 5, 4, 11, 4, -2], [11, -2, 3, 3, 7, 5, -11, 0, 2, 2, 0, -3], [2, 1, 6, 6, -12, 5, -2, 0, 1, -2, 0, -6], [4, -2, 2, -4, 4, 0, 0, 12, -4, -12, 4, 0], [11, 1, -3, -3, 1, -4, -11, 0, -16, -14, 0, 3], [2, 1, 3, 3, -3, 0, 0, 2, 3, -2, -3, 0], [-13, 6, 4, 20, -4, 2, -13, 2, 0, 2, -6, 4], [-7, -9, -5, -9, 5, -4, -7, 15, 0, -4, 1, -5], [10, 3, 3, 1, -1, 0, 0, -4, -9, 4, 9, 0], [0, 0, 0, 6, 6, 6, -4, -8, 1, -2, 1, -11], [0, 0, 0, 6, 6, -8, 6, 6, -2, -2, -2, -12], [-9, 7, 8, 8, 4, 1, 9, 0, 8, 10, 0, -8], [16, -9, 9, 7, -7, 0, 0, 6, 1, -6, -1, 0], [0, 0, 0, -2, -2, 10, -20, -6, 3, 4, 3, -3], [0, 0, 0, -14, -14, 2, 8, 5, 5, 7, 5, 2], [-28, 2, 6, 10, -10, 0, 0, -7, 4, 7, -4, 0], [-2, 7, -5, -5, -11, -12, 2, 0, 2, -6, 0, 5], [-17, 3, 3, 7, -3, 0, -17, -1, 0, 0, -3, 3], [8, 0, 2, 2, -2, 0, 0, 6, -2, -6, 2, 0], [4, -13, -11, -13, 11, -2, 4, 8, 0, -2, 4, -11], [6, -11, -10, -10, 4, 1, -6, 0, -16, -12, 0, 10], [0, 0, 0, -6, -6, 6, 6, 0, 2, 6, 2, -6], [-16, 7, -1, -5, 1, 8, -16, -14, 0, 8, -8, -1], [-3, -8, -8, 0, 8, 0, -3, 13, 0, 0, 5, -8], [0, 5, 1, -7, 7, 0, 0, 0, 3, 0, -3, 0], [-3, 6, -3, -3, -5, -9, 3, 0, -4, -1, 0, 3], [14, 0, 4, -10, 10, 0, 0, 4, 0, -4, 0, 0], [0, 0, 0, 0, 0, -12, -2, 9, -4, -3, -4, -6], [-4, 1, -5, -5, 19, -6, 4, 0, -8, -2, 0, 5], [-2, -1, -7, -1, 7, 6, -2, -6, 0, 6, -4, -7], [-11, 9, 1, -7, -1, 8, -11, -5, 0, 8, -1, 1], [-8, 4, 7, 7, 1, 3, 8, 0, 6, 20, 0, -7], [-14, -3, -7, 8, -8, 0, 0, -10, -1, 10, 1, 0], [9, 2, 10, -10, -10, -8, 9, 4, 0, -8, -12, 10], [-10, 0, -1, 7, 1, 1, -10, 1, 0, 1, -4, -1], [-14, 11, -3, -5, 5, 0, 0, -3, -6, 3, 6, 0], [0, 0, 0, -10, -10, -22, -8, 4, -8, -18, -8, 8], [0, 0, 0, -16, -16, 6, 4, -6, 0, 0, 0, 2], [3, 1, 5, 5, -5, -4, 3, 7, 0, -4, -9, 5], [-16, 1, 0, 2, 0, 1, -16, -19, 0, 1, -10, 0], [0, 0, 0, -4, -4, -2, -4, -8, 5, -10, 5, 11], [-8, 13, 13, 2, -2, 0, 0, -3, 5, 3, -5, 0], [-14, -5, 3, -12, 12, 0, 0, 3, -3, -3, 3, 0], [0, 0, 0, -4, -4, -6, -4, -8, -1, -14, -1, 7], [-4, -3, -4, 22, 4, 1, -4, 3, 0, 1, 11, -4], [0, 0, 0, -12, -12, -12, 18, -4, 0, -16, 0, 0], [1, 8, -6, -6, 2, -14, -1, 0, -14, -10, 0, 6], [14, 13, 2, 28, -2, 11, 14, -9, 0, 11, 6, 2], [4, 9, 11, -5, -11, -2, 4, -4, 0, -2, -2, 11], [-5, 6, 6, 6, 10, 0, 5, 0, -5, 1, 0, -6], [13, -4, 0, 0, 16, 4, -13, 0, -5, -13, 0, 0], [10, -10, 2, 4, -4, 0, 0, 0, -6, 0, 6, 0], [10, 3, 0, 0, -14, -3, -10, 0, 8, 2, 0, 0], [9, 2, -5, -13, 5, 7, 9, 10, 0, 7, 4, -5], [-2, -5, -5, -5, -1, 0, 2, 0, -2, -4, 0, 5], [0, 0, 0, -14, -14, -6, 4, -9, -4, -15, -4, 18], [2, -9, 1, -21, 21, 0, 0, -6, 7, 6, -7, 0], [2, -3, -5, -5, 35, -2, -2, 0, 2, -4, 0, 5], [2, 7, -9, 3, 9, 16, 2, -4, 0, 16, 14, -9], [-10, -9, 1, 5, -5, 0, 0, 5, 14, -5, -14, 0], [-3, -8, 6, -18, -6, -14, -3, 11, 0, -14, 1, 6], [-14, -12, -7, -7, -3, 5, 14, 0, -1, -6, 0, 7], [-20, 8, 5, 5, -13, -3, 20, 0, 14, 0, 0, -5], [-4, 6, 2, 10, -10, 0, 0, 3, 2, -3, -2, 0], [-20, -6, -2, -2, 8, 4, 20, 0, 10, 12, 0, 2], [0, 0, 0, -4, -4, 10, 12, -3, 1, 7, 1, -4], [-5, 4, 12, 12, 16, 8, 5, 0, 4, 14, 0, -12], [-10, 6, 11, 5, -11, -5, -10, 5, 0, -5, 9, 11], [-6, -5, -1, -7, 7, 0, 0, 8, 5, -8, -5, 0], [0, 0, 0, 14, 14, -2, 28, 5, 2, 3, 2, -9], [0, 0, 0, 28, 28, 26, 6, 3, 10, 29, 10, -16], [0, 0, 0, -2, -2, 16, -26, -9, 1, 7, 1, -4], [-6, 7, -2, 4, 2, 9, -6, -7, 0, 9, 7, -2], [6, -2, -3, 13, 3, 1, 6, 5, 0, 1, 10, -3], [-3, -8, 3, 3, 9, 11, 3, 0, 22, 21, 0, -3]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_340_o_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_340_2_o_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_340_2_o_a(:prec:=12)
    chi := MakeCharacter_340_o();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    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_340_2_o_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_340_2_o_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_340_o();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;