
// Make newform 38.3.d.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_38_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_38_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_38_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_38_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 := [4, 0, -2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2, 2];
    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_38_d();"
function MakeCharacter_38_d()
    N := 38;
    order := 6;
    char_gens := [21];
    v := [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_38_d_Hecke();"
function MakeCharacter_38_d_Hecke(Kf)
    N := 38;
    order := 6;
    char_gens := [21];
    char_values := [[0, 0, 1, 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], [1, -1, 1, 0], [1, 0, -1, 0], [2, -4, 0, 2], [-10, 4, 0, -2], [-10, 6, 5, -6], [7, 2, -7, -4], [3, -6, 2, 15], [0, 9, -5, 9], [22, 18, -11, -18], [-18, 0, 36, 0], [-2, 0, 4, -18], [3, -42, 3, 0], [19, -23, -19, 46], [0, -19, -35, -19], [-14, 48, 7, -48], [7, -33, 7, 0], [0, 16, 37, 16], [34, 63, -17, -63], [51, -9, 51, 0], [49, -32, -49, 64], [-21, -21, -21, 0], [-16, -12, 0, 6], [-2, -6, 1, 6], [-81, -6, -81, 0], [0, 30, 67, 30], [56, 0, -112, -12], [56, 0, -112, -12], [15, 30, 15, 0], [-62, 0, 124, -18], [150, 3, -75, -3], [-95, 5, 95, -10], [0, 2, 7, 2], [0, -15, -5, -15], [-77, 46, 77, -92], [-20, 0, 40, 36], [-35, 96, 35, -192], [-142, 12, 0, -6], [-158, -33, 79, 33], [-107, 84, -107, 0], [84, 0, -168, 120], [2, 24, -1, -24], [50, -188, 0, 94], [77, 60, 77, 0], [296, 36, 0, -18], [0, 121, 37, 121], [121, 99, 121, 0], [-11, -9, -11, 0], [-56, 0, 112, 108], [-196, -52, 0, 26], [307, 34, -307, -68], [-184, -92, 0, 46], [14, -90, -7, 90], [0, -51, -365, -51], [-126, -84, 63, 84], [-11, -51, 11, 102], [-41, 234, -41, 0], [-83, 129, 83, -258], [260, -220, 0, 110], [122, 36, -61, -36], [73, -31, -73, 62], [-138, 0, 276, 66], [107, 51, 107, 0], [56, 132, 0, -66], [0, 114, 31, 114], [-330, -42, 165, 42], [56, 0, -112, -276], [-85, 222, -85, 0], [-341, -15, 341, 30], [-268, -260, 0, 130], [80, 324, 0, -162], [49, 81, -49, -162], [0, -51, -359, -51], [144, 0, -288, 276], [104, 0, -208, -204], [-89, -285, -89, 0], [0, -156, 289, -156], [-83, -72, 83, 144], [-49, -258, -49, 0], [-310, 264, 155, -264], [566, -84, 0, 42], [5, -348, 5, 0], [342, 111, -171, -111], [554, 192, -277, -192], [203, 195, 203, 0], [0, -183, 7, -183], [98, 0, -196, -126], [326, 96, 0, -48], [211, -166, -211, 332], [-40, -556, 0, 278], [218, 44, 0, -22], [0, 93, -575, 93], [-12, 0, 24, 336], [-527, -3, 527, 6], [-617, -71, 617, 142], [0, -135, -89, -135], [702, 138, -351, -138], [-376, 0, 752, -168], [378, 123, -189, -123], [0, -102, 643, -102], [-778, 135, 389, -135], [0, 184, -299, 184], [-4, 0, 8, 120], [362, 0, -724, -234], [554, 228, 0, -114], [20, -412, 0, 206], [685, -27, -685, 54], [0, -78, 379, -78], [-274, 111, 137, -111], [-262, 0, 524, 150], [-76, 0, 152, -168], [379, 310, -379, -620], [0, -74, -737, -74], [-874, -204, 0, 102], [0, 1, -83, 1], [57, -420, 57, 0], [7, -203, -7, 406], [-646, 124, 0, -62], [-88, 460, 0, -230], [66, 363, -33, -363], [318, -210, -159, 210], [-190, 0, 380, -414], [-270, 0, 540, 138], [-42, 0, 84, 168], [614, -348, 0, 174], [-233, -126, 233, 252], [0, -84, 697, -84], [-299, 357, 299, -714], [121, 285, -121, -570], [-4, 660, 0, -330], [343, -171, -343, 342], [129, 255, 129, 0], [406, 171, -203, -171], [145, 168, -145, -336], [8, -996, 0, 498], [0, 332, 145, 332], [-598, 36, 299, -36], [540, 0, -1080, -336], [126, 0, -252, 702], [-100, -828, 0, 414], [-278, 159, 139, -159], [0, 22, 151, 22], [0, -483, -59, -483], [-698, -381, 349, 381], [232, 0, -464, -480], [141, 303, 141, 0], [517, -312, -517, 624], [-175, -288, -175, 0], [0, -359, -131, -359], [-126, 0, 252, 168], [87, 354, 87, 0], [-484, -28, 0, 14], [0, 365, -221, 365], [1410, 195, -705, -195], [287, -153, 287, 0], [204, 0, -408, 804], [-382, 132, 0, -66], [-671, -228, 671, 456], [0, -24, 193, -24], [-358, -204, 179, 204], [-725, -39, 725, 78], [-163, 144, -163, 0], [-881, -183, 881, 366], [-517, 231, -517, 0], [506, 0, -1012, -6], [407, -261, 407, 0], [-673, 423, -673, 0], [0, 84, 229, 84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_38_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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