
// Make newform 387.3.b.c in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_387_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_387_b_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_387_3_b_c();".
// 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_387_3_b_c();".
// 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 := [214, 0, 121, 0, 20, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, -18, 0, -11, 0, -1], [-22, 0, -11, 0, -1, 0], [46, 0, 15, 0, 1, 0], [0, 50, 0, 15, 0, 1]];
    Rf_basisdens := [1, 1, 4, 4, 4, 4];
    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_387_b();"
function MakeCharacter_387_b()
    N := 387;
    order := 2;
    char_gens := [173, 46];
    v := [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_387_b_Hecke();"
function MakeCharacter_387_b_Hecke(Kf)
    N := 387;
    order := 2;
    char_gens := [173, 46];
    char_values := [[1, 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+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, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, -1, 0, 0, 0], [0, 1, -1, 0, 0, -1], [-7, 0, 0, -3, 1, 0], [5, 0, 0, -5, 5, 0], [5, 0, 0, 0, 5, 0], [0, 5, 0, 0, 0, -5], [15, 0, 0, -5, 10, 0], [0, -10, 0, 0, 0, -5], [-15, 0, 0, -4, 15, 0], [0, -1, 5, 0, 0, 0], [23, 0, 0, -11, -6, 0], [-5, -10, 1, -5, -15, 5], [0, 0, 0, 15, 0, 0], [25, 0, 0, 25, -5, 0], [-12, 0, 0, -26, -16, 0], [0, 5, -5, 0, 0, 15], [-25, 0, 0, -15, -25, 0], [0, 20, 0, 0, 0, 0], [0, -15, -5, 0, 0, 1], [32, 0, 0, -2, 9, 0], [-5, 0, 0, -25, 15, 0], [0, -15, -5, 0, 0, 5], [-45, 0, 0, 5, 50, 0], [-55, 0, 0, -11, -5, 0], [-45, 0, 0, 15, 0, 0], [-100, 0, 0, -30, 0, 0], [-35, 0, 0, 11, -25, 0], [0, 4, 6, 0, 0, 6], [35, 0, 0, -30, -15, 0], [0, 35, -5, 0, 0, 10], [0, 20, -10, 0, 0, -26], [117, 0, 0, 31, 51, 0], [0, -35, 15, 0, 0, 20], [0, -45, -5, 0, 0, -5], [0, 55, 10, 0, 0, 11], [0, -16, 15, 0, 0, -50], [-85, 0, 0, -45, 35, 0], [-30, 0, 0, 0, -70, 0], [0, 10, 15, 0, 0, 0], [52, 0, 0, 56, -34, 0], [0, 40, 10, 0, 0, -10], [35, 0, 0, -75, 70, 0], [-180, 0, 0, 50, 0, 0], [0, 80, -10, 0, 0, 30], [0, -35, 25, 0, 0, 30], [0, -45, -3, 0, 0, 35], [0, 19, -4, 0, 0, 21], [237, 0, 0, 13, 29, 0], [0, 60, 20, 0, 0, 24], [80, 0, 0, 14, 15, 0], [0, 40, 60, 0, 0, 40], [73, 0, 0, 25, -25, 0], [0, -69, 15, 0, 0, 25], [0, -24, 20, 0, 0, 0], [123, 0, 0, 97, 101, 0], [-93, 0, 0, -29, -94, 0], [0, -34, -1, 0, 0, -26], [-57, 0, 0, -68, 31, 0], [-115, 0, 0, 35, 35, 0], [-60, 0, 0, 40, 210, 0], [45, 0, 0, -115, -95, 0], [-247, 0, 0, -51, -131, 0], [0, 51, 29, 0, 0, 29], [235, 0, 0, 75, 85, 0], [0, 95, -15, 0, 0, -75], [65, 0, 0, -30, 205, 0], [0, 26, -35, 0, 0, -100], [0, 20, 30, 0, 0, -5], [-125, 0, 0, -5, -105, 0], [-57, 0, 0, 50, -75, 0], [-440, 0, 0, 25, 20, 0], [0, 1, -6, 0, 0, -31], [-113, 0, 0, -87, -171, 0], [0, 70, 40, 0, 0, -66], [0, -40, 10, 0, 0, 130], [-340, 0, 0, -120, 0, 0], [215, 0, 0, -166, 135, 0], [0, -135, -55, 0, 0, 15], [0, -15, 55, 0, 0, 35], [0, -35, 10, 0, 0, 65], [-147, 0, 0, 14, 139, 0], [0, -161, -15, 0, 0, 25], [177, 0, 0, -62, -221, 0], [-20, 0, 0, -60, -210, 0], [0, 45, -5, 0, 0, 95], [0, -55, 65, 0, 0, -99], [28, 0, 0, 52, -184, 0], [0, -5, 65, 0, 0, 91], [0, -5, 63, 0, 0, -10], [95, 0, 0, -181, -80, 0], [-10, 0, 0, 220, 5, 0], [0, 80, -85, 0, 0, 30], [0, 130, -30, 0, 0, 35], [0, -76, -64, 0, 0, 36], [-535, 0, 0, 19, 135, 0], [0, -45, -95, 0, 0, 5], [0, 34, -90, 0, 0, -75], [367, 0, 0, 93, 69, 0], [205, 0, 0, -165, 85, 0], [75, 0, 0, -155, 15, 0], [-135, 0, 0, -165, 155, 0], [105, 0, 0, -31, 265, 0], [0, 260, 0, 0, 0, -25], [0, 205, -53, 0, 0, -115], [0, 204, 6, 0, 0, 106], [0, -89, 95, 0, 0, -25], [525, 0, 0, 444, 55, 0], [0, -200, -10, 0, 0, -130], [0, 140, -70, 0, 0, 46], [180, 0, 0, 360, -20, 0], [45, 0, 0, 75, 280, 0], [-350, 0, 0, 86, -180, 0], [0, 220, 60, 0, 0, 80], [0, -185, 35, 0, 0, 185], [140, 0, 0, -250, 320, 0], [0, 94, 26, 0, 0, -224], [0, -91, 6, 0, 0, -119], [793, 0, 0, 19, 139, 0], [487, 0, 0, -7, 19, 0], [0, 99, -25, 0, 0, -75], [0, -89, 10, 0, 0, 175], [-695, 0, 0, 35, -305, 0], [0, -195, -65, 0, 0, -5], [-372, 0, 0, -78, 26, 0], [277, 0, 0, -39, -419, 0], [218, 0, 0, -243, 206, 0], [0, 174, 50, 0, 0, 100], [0, -195, 87, 0, 0, -40], [0, -195, -75, 0, 0, -110], [0, -70, -90, 0, 0, 24], [0, -145, -65, 0, 0, -65], [0, 271, -11, 0, 0, 64], [0, -60, 120, 0, 0, 60], [280, 0, 0, 91, 430, 0], [0, 14, -64, 0, 0, 61], [-740, 0, 0, 140, -220, 0], [820, 0, 0, -50, 40, 0], [-592, 0, 0, -21, 154, 0], [0, -10, -45, 0, 0, 280], [-397, 0, 0, 369, -471, 0], [335, 0, 0, -25, -125, 0], [730, 0, 0, 250, -110, 0], [0, -155, 35, 0, 0, 200], [0, -165, -45, 0, 0, 115], [-465, 0, 0, -215, -385, 0], [470, 0, 0, -30, -165, 0], [0, -125, 140, 0, 0, 105], [0, 114, -74, 0, 0, -224], [895, 0, 0, -155, 135, 0], [135, 0, 0, -226, 275, 0], [695, 0, 0, 195, 265, 0], [0, -30, -32, 0, 0, -10], [-35, 0, 0, -555, 75, 0], [0, -295, -85, 0, 0, -105], [-213, 0, 0, -404, 101, 0], [0, -290, 80, 0, 0, 190], [0, 21, -81, 0, 0, 219], [-157, 0, 0, -325, -525, 0], [-325, 0, 0, -115, -185, 0], [0, -121, -29, 0, 0, -179], [-965, 0, 0, 55, -160, 0], [-775, 0, 0, -301, -245, 0], [-840, 0, 0, -415, -70, 0], [0, 351, -95, 0, 0, -175], [0, 65, 105, 0, 0, -215], [0, 64, -50, 0, 0, -75]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_387_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_387_3_b_c();".
// 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_387_3_b_c(:prec:=6)
    chi := MakeCharacter_387_b();
    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_387_3_b_c();".
// 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_387_3_b_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_387_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<2,R![214, 0, 121, 0, 20, 0, 1]>,<11,R![-244, 48, 19, 1]>],Snew);
    return Vf;
end function;