
// Make newform 1296.3.e.g in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1296_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1296_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_1296_3_e_g();".
// 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_1296_3_e_g();".
// 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, 0, 0, 3], [0, 12, 0, -3], [-3, 0, 3, 0]];
    Rf_basisdens := [1, 2, 2, 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_1296_e();"
function MakeCharacter_1296_e()
    N := 1296;
    order := 2;
    char_gens := [1135, 325, 1217];
    v := [2, 2, 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_1296_e_Hecke();"
function MakeCharacter_1296_e_Hecke(Kf)
    N := 1296;
    order := 2;
    char_gens := [1135, 325, 1217];
    char_values := [[1, 0, 0, 0], [1, 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+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], [0, 0, 0, 0], [0, 0, 0, 1], [1, 0, 1, 0], [0, 1, 0, -1], [5, 0, -2, 0], [0, 2, 0, 2], [10, 0, 2, 0], [0, -1, 0, 1], [0, 2, 0, 1], [19, 0, -3, 0], [32, 0, 2, 0], [0, -6, 0, 7], [-23, 0, -3, 0], [0, -7, 0, -3], [0, 10, 0, 10], [0, -13, 0, 7], [-31, 0, 6, 0], [-53, 0, -3, 0], [0, -8, 0, 10], [-52, 0, 6, 0], [7, 0, 5, 0], [0, 5, 0, 21], [0, -22, 0, -10], [-7, 0, -14, 0], [0, 36, 0, -1], [37, 0, 9, 0], [0, -16, 0, 20], [-16, 0, -18, 0], [0, 44, 0, -3], [34, 0, 6, 0], [0, 27, 0, 23], [0, -10, 0, 37], [-11, 0, 13, 0], [0, -26, 0, -1], [175, 0, -15, 0], [-109, 0, 12, 0], [58, 0, -26, 0], [0, -29, 0, -33], [0, -20, 0, 3], [0, -28, 0, 32], [140, 0, 14, 0], [0, -3, 0, 1], [125, 0, 30, 0], [0, -28, 0, 8], [-80, 0, -10, 0], [103, 0, -7, 0], [-203, 0, -15, 0], [0, 29, 0, -9], [143, 0, -36, 0], [0, 16, 0, 16], [0, -17, 0, -3], [143, 0, 8, 0], [0, 32, 0, -16], [0, 8, 0, 9], [0, -15, 0, -77], [0, -2, 0, 10], [34, 0, 42, 0], [-49, 0, 0, 0], [0, 38, 0, -35], [145, 0, -27, 0], [0, -32, 0, 29], [-110, 0, 6, 0], [0, -21, 0, -31], [-397, 0, -4, 0], [0, -42, 0, -55], [-245, 0, -31, 0], [335, 0, -4, 0], [0, -53, 0, 69], [107, 0, -28, 0], [0, -34, 0, -35], [0, -84, 0, -12], [-77, 0, -15, 0], [113, 0, 46, 0], [328, 0, 22, 0], [0, 99, 0, -61], [0, 24, 0, -13], [-124, 0, 18, 0], [0, -18, 0, -65], [-385, 0, 8, 0], [0, -65, 0, -51], [269, 0, 2, 0], [0, 52, 0, -92], [344, 0, 18, 0], [139, 0, 57, 0], [0, -163, 0, -27], [0, -150, 0, -18], [401, 0, 42, 0], [0, 26, 0, -23], [283, 0, -31, 0], [0, -172, 0, 20], [0, 41, 0, -93], [190, 0, -22, 0], [0, 83, 0, 115], [-35, 0, 81, 0], [0, -72, 0, -114], [0, 126, 0, -65], [0, 20, 0, 56], [-488, 0, 54, 0], [-274, 0, 32, 0], [-41, 0, 69, 0], [0, -34, 0, -106], [0, 7, 0, -111], [0, 60, 0, -1], [7, 0, -11, 0], [-88, 0, 6, 0], [0, -15, 0, 97], [0, 50, 0, 26], [0, 11, 0, -161], [-751, 0, 6, 0], [745, 0, 61, 0], [56, 0, 78, 0], [0, -54, 0, 7], [175, 0, -3, 0], [58, 0, -14, 0], [0, 8, 0, -53], [-593, 0, -27, 0], [0, 84, 0, -12], [0, 28, 0, 125], [0, 93, 0, 7], [395, 0, 84, 0], [137, 0, 58, 0], [0, -96, 0, 11], [0, 192, 0, -60], [-707, 0, 45, 0], [0, 34, 0, -122], [-349, 0, 112, 0], [0, 48, 0, -60], [643, 0, -27, 0], [-55, 0, -90, 0], [-272, 0, -90, 0], [0, -125, 0, 99], [241, 0, -143, 0], [866, 0, -24, 0], [0, 74, 0, 123], [-157, 0, -68, 0], [0, -30, 0, 78], [-359, 0, -63, 0], [0, -286, 0, -21], [0, -58, 0, 2], [-74, 0, 90, 0], [0, -258, 0, 29], [1579, 0, 5, 0], [0, -44, 0, 148], [980, 0, -14, 0], [0, 257, 0, 9], [77, 0, -54, 0], [0, -14, 0, 165], [-245, 0, 97, 0], [0, -92, 0, -176], [317, 0, -94, 0], [0, 154, 0, -170], [838, 0, 74, 0], [0, 171, 0, 29], [-335, 0, -115, 0], [0, -259, 0, -33], [-38, 0, -174, 0], [0, 34, 0, 93], [896, 0, -58, 0], [0, -152, 0, -55], [0, -91, 0, 75], [0, 30, 0, 114], [-629, 0, 81, 0], [0, 192, 0, 30], [0, 364, 0, 5], [0, 189, 0, -5], [-650, 0, -30, 0], [275, 0, 132, 0], [-1204, 0, -54, 0], [0, -90, 0, 163], [0, -71, 0, 173], [-307, 0, 90, 0], [0, 123, 0, -43], [89, 0, -166, 0], [769, 0, -163, 0], [0, 4, 0, -91], [1453, 0, -27, 0], [0, -270, 0, -6], [-818, 0, 14, 0], [599, 0, 144, 0], [-467, 0, -119, 0], [0, 57, 0, -47], [215, 0, -180, 0], [0, -162, 0, 150], [0, 135, 0, 143], [0, 306, 0, 149], [-574, 0, 48, 0], [-251, 0, -83, 0], [635, 0, 184, 0], [0, -392, 0, -68], [-1144, 0, 34, 0], [0, 185, 0, -81], [-230, 0, -50, 0], [0, -132, 0, -73], [0, -212, 0, -56], [0, -68, 0, 245], [1157, 0, -2, 0], [-61, 0, -20, 0], [0, -120, 0, -73], [0, 112, 0, -20], [0, 68, 0, -207], [-875, 0, 173, 0], [-553, 0, -80, 0], [-517, 0, 24, 0], [0, 140, 0, 92], [0, 278, 0, 26], [-1430, 0, -46, 0], [0, 135, 0, -157], [0, 0, 0, -157], [97, 0, 109, 0], [-148, 0, -118, 0], [0, -122, 0, -157], [193, 0, 21, 0], [0, -75, 0, -185], [0, -365, 0, 63], [923, 0, -144, 0], [-161, 0, 93, 0], [0, -254, 0, -303], [0, 332, 0, -4], [0, -206, 0, -241], [-715, 0, -50, 0], [-395, 0, -99, 0], [0, 18, 0, -485], [214, 0, 330, 0], [0, -5, 0, 147], [731, 0, 148, 0], [0, 88, 0, 219], [0, -388, 0, -28], [-161, 0, -143, 0], [0, 1, 0, -277], [1343, 0, 32, 0], [214, 0, -186, 0], [1351, 0, 165, 0], [0, 132, 0, 169], [379, 0, 181, 0], [0, 301, 0, 17], [5, 0, 130, 0], [0, 246, 0, -150], [0, -149, 0, -321], [0, 296, 0, 56], [0, -255, 0, -287], [286, 0, -102, 0], [-251, 0, -291, 0], [236, 0, 66, 0], [0, 262, 0, -145], [0, 133, 0, -127], [730, 0, -298, 0], [0, 147, 0, -391], [-1925, 0, -51, 0], [0, 324, 0, 336], [233, 0, -30, 0], [0, 414, 0, -30], [0, 119, 0, 325], [-301, 0, 252, 0], [0, -284, 0, -285], [0, 208, 0, -242], [-1528, 0, 54, 0], [-689, 0, 297, 0], [0, -108, 0, 264], [-2248, 0, -18, 0], [2599, 0, -43, 0], [0, 509, 0, 9], [-1621, 0, -132, 0], [-724, 0, -250, 0], [0, 174, 0, -5], [319, 0, -267, 0], [0, 170, 0, -274], [0, 268, 0, -165], [199, 0, 141, 0], [0, -22, 0, -261], [1037, 0, 290, 0], [-1058, 0, 226, 0], [203, 0, -156, 0], [-791, 0, 273, 0], [-1675, 0, 150, 0], [358, 0, 174, 0], [0, -129, 0, -373], [-427, 0, -318, 0], [560, 0, -150, 0], [0, -387, 0, 43], [0, 195, 0, -517], [2161, 0, 117, 0], [0, -323, 0, 263], [707, 0, -120, 0], [-1583, 0, 69, 0], [0, -48, 0, -150], [332, 0, -290, 0], [0, 136, 0, 509], [2569, 0, 105, 0], [0, -96, 0, 192], [0, -206, 0, 417], [0, 368, 0, 14], [0, 52, 0, -31], [0, -293, 0, -231], [-1417, 0, -36, 0], [0, 196, 0, -199], [2887, 0, -91, 0], [0, -270, 0, 77], [0, -168, 0, 36], [-1055, 0, 29, 0], [-373, 0, 160, 0], [0, 246, 0, -174], [2770, 0, 30, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1296_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1296_3_e_g();".
// 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_1296_3_e_g(:prec:=4)
    chi := MakeCharacter_1296_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1296_3_e_g();".
// 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_1296_3_e_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1296_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![27, 0, 1]>,<7,R![-53, -2, 1]>],Snew);
    return Vf;
end function;