
// Make newform 156.3.d.a in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_156_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_156_d_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_156_3_d_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_156_3_d_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 := [3792, 1696, 700, 252, 97, 10, 15, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [13836, 736, 3758, -553, 516, -95, 28, -1], [-1560, -1972, -1676, 511, -408, 11, -4, -5], [-6204, -1144, -272, -263, -156, 11, 14, -5], [102, 280, -139, -7, 18, -14, 4, -1], [132, 2444, -494, 109, -138, 179, -52, 7], [13560, 9224, 718, 2005, -228, 263, -40, 13]];
    Rf_basisdens := [1, 1, 2916, 2916, 2916, 486, 2916, 2916];
    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_156_d();"
function MakeCharacter_156_d()
    N := 156;
    order := 2;
    char_gens := [79, 53, 145];
    v := [2, 1, 2];
    // 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_156_d_Hecke();"
function MakeCharacter_156_d_Hecke(Kf)
    N := 156;
    order := 2;
    char_gens := [79, 53, 145];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 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+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], [1, -1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [-1, 0, 1, 1, 1, 0, 1, 0], [1, 0, -1, 0, -1, 0, 1, 0], [0, 0, 0, 0, 0, -1, 0, 0], [-1, 0, 1, 2, -2, 0, 2, -3], [-2, -4, 0, -2, -2, 4, -2, 2], [-2, 0, 2, 2, 2, 0, -2, 0], [2, -4, -2, 2, 2, 0, -2, 2], [12, 0, 0, 0, 0, 10, 0, 0], [-11, -4, 3, 1, 1, 4, 1, 2], [1, -8, -1, -4, 1, 0, -1, -2], [7, 10, -7, -2, -2, 6, -2, -5], [-4, 8, 4, -3, 2, 0, -2, 2], [-4, 12, 4, 2, -2, 0, 2, 0], [-3, 8, 3, -6, 1, 0, -1, 2], [-12, -4, 0, -2, -2, -6, -2, 2], [-50, 8, -4, 0, 0, 4, 0, -4], [6, 0, -6, -7, 0, 0, 0, 6], [0, -4, 2, 0, 0, 0, 0, 2], [42, -4, 2, 0, 0, 6, 0, 2], [-9, 24, 9, 0, -3, 0, 3, 0], [-1, 0, 1, 8, 7, 0, -7, 6], [52, 0, -2, -2, -2, -28, -2, 0], [-4, 4, 4, 20, -6, 0, 6, -8], [-20, 16, 2, 10, 10, 0, 10, -8], [-4, -4, 4, 20, -10, 0, 10, -16], [51, -4, 9, 7, 7, -12, 7, 2], [0, -8, 0, -16, -4, 0, 4, -8], [44, -16, 4, -4, -4, 2, -4, 8], [-5, 28, 5, 12, 4, 0, -4, 13], [3, 32, -3, -10, 1, 0, -1, 20], [55, -6, -7, -10, -10, 14, -10, 3], [-21, 24, 21, 6, 3, 0, -3, -6], [-7, -24, 11, -1, -1, 0, -1, 12], [20, -36, 18, 0, 0, -12, 0, 18], [70, 16, -8, 0, 0, 0, 0, -8], [1, 8, -1, 6, -15, 0, 15, -10], [20, -20, -20, -26, -12, 0, 12, -2], [17, 4, -17, -36, 6, 0, -6, 25], [24, 16, -26, -18, -18, 8, -18, -8], [-14, 8, 14, 14, 12, 0, -12, 2], [-4, 8, -30, -26, -26, 32, -26, -4], [8, -56, -8, 9, 12, 0, -12, -8], [-144, 16, 8, 16, 16, 2, 16, -8], [-109, 10, 15, 20, 20, 40, 20, -5], [-219, 16, -29, -21, -21, -4, -21, -8], [9, -16, -9, 32, -11, 0, 11, -10], [-85, 4, -7, -5, -5, 4, -5, -2], [9, -64, -9, -14, 10, 0, -10, -13], [10, 32, -10, -15, 0, 0, 0, 26], [54, -36, 4, -14, -14, -20, -14, 18], [14, -52, -14, 4, -16, 0, 16, -28], [5, -8, -5, 18, -6, 0, 6, -5], [4, -8, -4, 20, 22, 0, -22, 22], [16, -36, -16, 16, -4, 0, 4, -6], [-107, -16, 11, 3, 3, 52, 3, 8], [-58, -8, 32, 28, 28, -68, 28, 4], [-13, -8, 13, -4, 15, 0, -15, -2], [140, -20, 40, 30, 30, 10, 30, 10], [10, 48, -10, 9, -10, 0, 10, 24], [-96, -20, 26, 16, 16, -4, 16, 10], [26, 16, -26, 0, -36, 0, 36, -2], [-37, -2, 3, 2, 2, -56, 2, 1], [3, 0, -3, 54, -9, 0, 9, -6], [180, 8, 14, 18, 18, -24, 18, -4], [215, 22, -21, -10, -10, 28, -10, -11], [-21, 60, 21, 48, 0, 0, 0, 9], [-167, 36, -13, 5, 5, -64, 5, -18], [-21, 64, 21, 48, -19, 0, 19, -8], [-15, 48, 15, -18, 9, 0, -9, 18], [-122, 12, 16, 22, 22, -40, 22, -6], [-82, -32, 18, 2, 2, 70, 2, 16], [-270, 40, -36, -16, -16, -16, -16, -20], [-10, -32, 10, -63, 36, 0, -36, 10], [-12, 28, 12, -80, 2, 0, -2, 4], [-28, -56, 36, 8, 8, -14, 8, 28], [19, 8, -19, -54, -27, 0, 27, -4], [342, -24, 0, -12, -12, 4, -12, 12], [-49, 92, 49, -68, 8, 0, -8, 5], [19, 76, -31, 7, 7, -12, 7, -38], [18, 16, -18, -35, 32, 0, -32, 58], [99, -26, 39, 26, 26, 92, 26, 13], [408, 28, -22, -8, -8, -40, -8, -14], [39, -76, -39, -28, 4, 0, -4, 5], [-35, 40, 35, 16, 7, 0, -7, -8], [-30, -92, 36, -10, -10, -88, -10, 46], [16, 80, -16, -3, -12, 0, 12, 44], [-224, -56, -28, -56, -56, 90, -56, 28], [-50, 68, 50, 76, 24, 0, -24, 8], [10, -8, -10, -1, -32, 0, 32, -26], [196, 24, -8, 4, 4, -118, 4, -12], [-13, -12, 13, -20, -2, 0, 2, -21], [-182, 84, -40, 2, 2, -64, 2, -42], [4, 104, -4, 20, -30, 0, 30, 26], [-37, 120, 37, 46, 19, 0, -19, 42], [25, -120, -25, 34, 8, 0, -8, -27], [-150, -24, -8, -20, -20, -128, -20, 12], [-293, 52, 1, 27, 27, 84, 27, -26], [-55, -2, -29, -30, -30, -134, -30, 1], [-22, 64, 22, -51, -18, 0, 18, -8], [-49, 36, 49, 72, -26, 0, 26, -57], [61, -80, -61, 22, -2, 0, 2, 19], [429, -66, 85, 52, 52, 16, 52, 33], [-24, 68, 10, 44, 44, -48, 44, -34], [-51, 80, 51, 30, 19, 0, -19, 8], [77, -160, -77, 20, -13, 0, 13, -16], [-14, -104, 14, 64, 22, 0, -22, -44], [-159, -54, -39, -66, -66, 100, -66, 27], [568, 12, 6, 12, 12, -52, 12, -6], [-304, -136, 12, -56, -56, 110, -56, 68], [23, -72, -23, -38, 19, 0, -19, 6], [-518, -52, 44, 18, 18, -164, 18, 26], [-127, 80, -17, 23, 23, 68, 23, -40], [-78, 184, 78, -72, 14, 0, -14, 28], [234, 32, -100, -84, -84, 24, -84, -16], [24, 8, -24, 40, -44, 0, 44, -16], [-6, -100, 6, -40, 64, 0, -64, 8], [-32, 140, 32, -84, -6, 0, 6, 32], [-448, 56, -60, -32, -32, 14, -32, -28], [-29, -50, -31, -56, -56, 6, -56, 25], [-14, -20, 14, -106, -26, 0, 26, -50], [81, -200, -81, -72, -13, 0, 13, -32], [208, 52, -34, -8, -8, 24, -8, -26], [-106, 212, 106, 10, -4, 0, 4, -4], [148, 80, -32, 8, 8, 118, 8, -40], [42, -8, -42, -70, 38, 0, -38, 76], [214, -124, 38, -24, -24, -2, -24, 62], [-323, -60, -17, -47, -47, 32, -47, 30], [306, 108, 4, 58, 58, -44, 58, -54], [2, 72, -2, -83, -56, 0, 56, -18], [170, -64, 68, 36, 36, 212, 36, 32], [-280, 168, -64, 20, 20, 66, 20, -84], [-43, 32, 43, 70, -7, 0, 7, -34], [184, -36, 102, 84, 84, -52, 84, 18], [-42, 112, 42, 35, 26, 0, -26, 40], [-574, -76, 12, -26, -26, -52, -26, 38], [2, 164, -2, -68, 2, 0, -2, 86], [39, -40, -39, 26, 28, 0, -28, 47], [-76, -116, 42, -16, -16, -32, -16, 58], [16, 24, -16, -79, 56, 0, -56, 84], [-144, -44, -14, -36, -36, -64, -36, 22], [-77, 24, 77, 136, 11, 0, -11, -54], [48, -112, 48, -8, -8, -30, -8, 56], [-19, -104, 19, 94, -3, 0, 3, -74], [-473, 28, 29, 43, 43, 84, 43, -14], [18, -136, -18, 40, -26, 0, 26, -76], [-160, -52, 144, 118, 118, -154, 118, 26], [-56, 8, 56, -1, -66, 0, 66, -118], [533, 4, 35, 37, 37, -116, 37, -2], [53, -168, -53, 26, -26, 0, 26, -57], [-185, 106, -73, -20, -20, -108, -20, -53], [-40, 96, 40, -32, -50, 0, 50, -42], [511, -46, -33, -56, -56, 76, -56, 23], [-6, -104, 6, -158, 20, 0, -20, -38], [302, -208, 74, -30, -30, 62, -30, 104], [-37, -64, 37, 154, -25, 0, 25, -94], [336, 4, -8, -6, -6, 38, -6, -2], [-2, 72, 2, -141, -10, 0, 10, 24], [-15, 152, 15, 32, -53, 0, 53, 8], [-21, 64, 21, 98, -34, 0, 34, -23], [-179, 192, -77, 19, 19, -52, 19, -96], [37, -20, -37, -136, 16, 0, -16, 43], [21, 96, -21, -138, 99, 0, -99, 168], [0, 88, 0, -127, -22, 0, 22, 22], [-418, -24, -4, -16, -16, -120, -16, 12], [60, 136, -58, 10, 10, -100, 10, -68]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_156_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_156_3_d_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_156_3_d_a(:prec:=8)
    chi := MakeCharacter_156_d();
    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_156_3_d_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_156_3_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_156_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;