
// Make newform 384.3.e.d in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_384_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_384_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_384_3_e_d();".
// 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_384_3_e_d();".
// 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 := [81, 0, 170, 0, 99, 0, 18, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [63, 6, 70, 0, 16, 0, 1, 0], [18, 0, 11, 0, 1, 0, 0, 0], [0, 193, 0, 159, 0, 33, 0, 2], [0, 178, 0, 180, 0, 36, 0, 2], [-27, -57, -27, -70, -3, -16, 0, -1], [-61, 6, -70, 0, -16, 0, -1, 0], [-27, 63, -27, 70, -3, 16, 0, 1]];
    Rf_basisdens := [1, 6, 1, 9, 9, 3, 2, 3];
    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_384_e();"
function MakeCharacter_384_e()
    N := 384;
    order := 2;
    char_gens := [127, 133, 257];
    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_384_e_Hecke();"
function MakeCharacter_384_e_Hecke(Kf)
    N := 384;
    order := 2;
    char_gens := [127, 133, 257];
    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], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, -1], [1, -1, 0, 0, 0, -1, 1, -1], [1, -3, 0, -2, -2, -1, -1, 1], [-3, 1, -2, 0, 0, -2, 1, -2], [0, 0, 0, 4, 0, 2, 0, -2], [-2, 6, 0, 3, 0, 2, 2, -2], [-9, 4, -1, 0, 0, -2, 0, -2], [-1, 9, -4, 0, 0, -3, -1, -3], [-2, 6, 0, -6, -6, -2, 2, 2], [17, 1, 2, 0, 0, -2, 1, -2], [-2, 6, 0, 0, -2, 2, 2, -2], [4, -12, 0, 1, -4, 4, -4, -4], [-1, 3, 0, 0, 8, 0, 1, 0], [-13, -11, 4, 0, 0, 1, 3, 1], [-37, 1, 8, 0, 0, 4, -3, 4], [-2, 6, 0, 4, -10, -4, 2, 4], [-2, -12, 4, 0, 0, 0, 4, 0], [-41, -16, 3, 0, 0, 2, 4, 2], [2, -6, 0, -8, 5, 1, -2, -1], [-5, 15, 0, 16, -8, 1, 5, -1], [5, 13, -2, 0, 0, -5, -1, -5], [2, -6, 0, -3, -12, -10, -2, 10], [69, -12, -7, 0, 0, 0, 4, 0], [1, -3, 0, -8, 6, -6, -1, 6], [27, -11, -12, 0, 0, 1, 3, 1], [2, -6, 0, 0, -8, 6, -2, -6], [-69, 28, 3, 0, 0, 10, -16, 10], [3, -9, 0, -24, 6, -6, -3, 6], [0, 0, 0, -10, 6, 12, 0, -12], [115, -7, 16, 0, 0, 8, -3, 8], [0, 0, 0, -11, -16, 0, 0, 0], [89, 12, 13, 0, 0, 12, -12, 12], [-71, 7, 0, 0, 0, 7, -7, 7], [-71, -7, -14, 0, 0, -10, 9, -10], [-4, 12, 0, -20, -12, 2, 4, -2], [4, -12, 0, 3, 12, 4, -4, -4], [-7, 21, 0, 16, 8, -8, 7, 8], [37, -29, -8, 0, 0, -5, 13, -5], [14, -42, 0, 24, 14, -2, -14, 2], [-14, -24, 12, 0, 0, 12, 0, 12], [-6, 18, 0, -7, 0, 6, 6, -6], [81, -20, 21, 0, 0, 4, 4, 4], [-177, 25, 4, 0, 0, 4, -11, 4], [-57, 64, 3, 0, 0, -14, -12, -14], [-2, 6, 0, -8, -19, 9, 2, -9], [-67, -21, 24, 0, 0, 3, 5, 3], [11, -33, 0, -36, -12, -23, -11, 23], [-22, 66, 0, 16, 2, 6, 22, -6], [46, -52, 36, 0, 0, 8, 12, 8], [-10, 30, 0, -24, 3, 7, 10, -7], [-2, 6, 0, -20, -4, 2, 2, -2], [-14, 42, 0, 20, 58, 16, 14, -16], [8, -24, 0, 15, 8, 8, -8, -8], [-73, -64, 19, 0, 0, 2, 20, 2], [23, 1, -36, 0, 0, -11, 7, -11], [13, -39, 0, 4, 44, -1, -13, 1], [167, 49, 12, 0, 0, -8, -11, -8], [-10, 30, 0, 41, 24, 26, 10, -26], [-1, -39, 28, 0, 0, 0, 13, 0], [10, -30, 0, -4, -6, -12, -10, 12], [-49, 3, -18, 0, 0, 9, -7, 9], [-4, 12, 0, -13, 64, 4, 4, -4], [11, -7, -32, 0, 0, -4, 5, -4], [-91, 21, -38, 0, 0, -9, -1, -9], [14, -42, 0, -8, -13, -17, -14, 17], [-279, -1, -16, 0, 0, -1, 1, -1], [-4, 12, 0, 8, 64, -12, 4, 12], [-6, 18, 0, 36, 2, 0, 6, 0], [-9, -68, 7, 0, 0, 22, 8, 22], [169, 23, -16, 0, 0, -25, 9, -25], [-75, -71, 22, 0, 0, 10, 17, 10], [14, -42, 0, 24, 6, -10, -14, 10], [24, -72, 0, -67, 36, 0, -24, 0], [279, -71, -20, 0, 0, 13, 15, 13], [-9, 27, 0, 74, 10, -7, 9, 7], [102, -28, 16, 0, 0, -28, 28, -28], [-20, 60, 0, -32, 19, -29, 20, 29], [-79, -25, 0, 0, 0, 23, -7, 23], [-6, 18, 0, -80, -30, -42, 6, 42], [101, 29, 14, 0, 0, 11, -17, 11], [-103, -12, 37, 0, 0, 36, -20, 36], [-24, 72, 0, 64, -67, 13, 24, -13], [17, -51, 0, 26, 2, 23, -17, -23], [-86, -80, 8, 0, 0, -8, 32, -8], [6, -18, 0, -41, 40, -22, -6, 22], [-93, -12, 3, 0, 0, -6, 8, -6], [36, -108, 0, -48, -17, -9, -36, 9], [4, -12, 0, 32, 28, -44, -4, 44], [-299, -16, -35, 0, 0, 20, -8, 20], [15, -45, 0, 0, -4, -20, -15, 20], [-105, -71, -36, 0, 0, -20, 37, -20], [6, -18, 0, 12, 14, 40, -6, -40], [-16, 48, 0, -89, -68, -8, 16, 8], [0, 0, 0, 94, 6, 44, 0, -44], [-383, -15, 10, 0, 0, -6, 9, -6], [-425, -15, 44, 0, 0, 21, -9, 21], [273, -87, 66, 0, 0, 18, 17, 18], [-42, 126, 0, -5, -36, -14, 42, 14], [-36, 108, 0, 0, 63, 15, 36, -15], [-20, 60, 0, -86, 18, -24, 20, 24], [135, -111, -4, 0, 0, 0, 37, 0], [-15, 73, 66, 0, 0, 19, -37, 19], [-11, 33, 0, 72, -46, -10, 11, 10], [24, -72, 0, -68, -52, -32, -24, 32], [-8, 24, 0, -28, -80, 18, 8, -18], [134, 44, -12, 0, 0, 8, -20, 8], [-157, -52, 107, 0, 0, 50, -16, 50], [-265, 105, -68, 0, 0, -51, -1, -51], [-19, 57, 0, 8, -72, -9, 19, 9], [-397, 57, 8, 0, 0, 12, -27, 12], [-443, -8, -59, 0, 0, -20, 16, -20], [-17, 51, 0, 30, 78, 9, 17, -9], [1, 89, -14, 0, 0, -70, 17, -70], [4, -12, 0, 28, -76, -38, -4, 38], [-26, 78, 0, 143, 16, 58, 26, -58], [17, -51, 0, 64, 8, 24, -17, -24], [319, -127, -4, 0, 0, 5, 39, 5], [49, -47, 6, 0, 0, 55, -21, 55], [14, -42, 0, 45, 52, -22, -14, 22], [-14, 42, 0, 72, 99, 39, 14, -39], [197, -127, -18, 0, 0, 26, 25, 26], [42, -126, 0, -65, -36, 30, -42, -30], [-17, 41, -4, 0, 0, -67, 31, -67], [28, -84, 0, 8, -116, -24, -28, 24], [-379, -36, -15, 0, 0, -24, 28, -24], [-451, 11, -8, 0, 0, -61, 37, -61], [563, -47, -104, 0, 0, -32, 37, -32], [-8, 24, 0, 156, -16, 46, 8, -46], [-285, 144, 55, 0, 0, 30, -68, 30], [281, -17, 64, 0, 0, 31, -15, 31], [30, -90, 0, 70, -26, 54, -30, -54], [-58, -76, -92, 0, 0, -16, 36, -16], [-38, 114, 0, -11, -28, -2, 38, 2], [-299, 81, -66, 0, 0, 18, -39, 18], [14, -42, 0, 31, -124, 10, -14, -10], [-14, 42, 0, 82, -94, 10, 14, -10], [-367, -47, 154, 0, 0, 58, -23, 58], [-50, 150, 0, 17, 84, 10, 50, -10], [21, -24, 133, 0, 0, 60, -32, 60], [-43, 129, 0, -72, 22, -38, 43, 38], [-551, 39, -32, 0, 0, 39, -39, 39], [50, -150, 0, -44, 2, 56, -50, -56], [-3, -77, 88, 0, 0, -5, 29, -5], [0, 0, 0, 66, 114, 36, 0, -36], [517, 73, -26, 0, 0, 34, -47, 34], [-82, 246, 0, 136, 78, 54, 82, -54], [549, -45, -40, 0, 0, -21, 29, -21], [-82, 246, 0, -16, -16, 58, 82, -58], [717, -127, 46, 0, 0, -34, 65, -34], [-10, 30, 0, -84, -138, -64, 10, 64], [-303, -95, 34, 0, 0, 46, 1, 46], [70, -210, 0, -176, 86, -30, -70, 30], [-515, -128, 5, 0, 0, -20, 56, -20], [3, -9, 0, 102, 78, 69, -3, -69], [183, 35, 26, 0, 0, -19, 1, -19], [90, -270, 0, -129, -136, -74, -90, 74], [-37, 111, 0, -72, -110, 38, 37, -38], [16, -48, 0, -14, -38, -60, -16, 60], [821, 40, -59, 0, 0, -20, 0, -20], [6, -18, 0, -24, -97, -109, -6, 109], [25, -75, 0, 230, -2, 71, -25, -71], [-2, 6, 0, -4, 78, -32, 2, 32], [283, -200, 23, 0, 0, 94, 4, 94], [243, -27, 132, 0, 0, 81, -45, 81]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_384_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_384_3_e_d();".
// 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_384_3_e_d(:prec:=8)
    chi := MakeCharacter_384_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(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_384_3_e_d();".
// 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_384_3_e_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_384_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<7,R![-176, 368, -108, -4, 1]>,<13,R![7824, 256, -360, 0, 1]>],Snew);
    return Vf;
end function;