
// Make newform 90.3.g.d in Magma, downloaded from the LMFDB on 19 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_90_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_90_g_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_90_3_g_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_90_3_g_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 := [9, 0, 0, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 0, 1, 0], [0, 6, 0, 2], [0, 6, 0, -2]];
    Rf_basisdens := [1, 3, 3, 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_90_g();"
function MakeCharacter_90_g()
    N := 90;
    order := 4;
    char_gens := [11, 37];
    v := [4, 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_90_g_Hecke();"
function MakeCharacter_90_g_Hecke(Kf)
    N := 90;
    order := 4;
    char_gens := [11, 37];
    char_values := [[1, 0, 0, 0], [0, 1, 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 := [[-1, -1, 0, 0], [0, 0, 0, 0], [0, 1, 0, -1], [-4, -4, -1, -1], [4, 0, 0, -2], [3, -3, 2, -2], [-11, -11, 1, 1], [0, 16, -2, 0], [4, -4, -3, 3], [0, 16, -2, 0], [-20, 0, 0, 4], [-27, -27, 0, 0], [-8, 0, 0, 2], [12, -12, -5, 5], [-24, -24, -3, -3], [-25, 25, 9, -9], [0, -20, 0, 0], [-24, 0, 0, -8], [-4, -4, 9, 9], [-16, 0, 0, -2], [-47, 47, 2, -2], [0, -12, -26, 0], [-48, 48, -7, 7], [0, -88, 6, 0], [33, 33, -10, -10], [50, 0, 0, 16], [-8, 8, 5, -5], [-20, -20, -1, -1], [0, -130, 0, 0], [-43, 43, -7, 7], [-16, -16, -7, -7], [124, 0, 0, -2], [-21, -21, 1, 1], [0, -64, -2, 0], [0, -114, -32, 0], [100, 0, 0, -16], [123, 123, 0, 0], [-88, 88, 5, -5], [-64, -64, -23, -23], [-89, 89, 11, -11], [0, -4, 28, 0], [98, 0, 0, 8], [296, 0, 0, -6], [39, -39, -16, 16], [183, 183, 13, 13], [0, -12, 34, 0], [-64, 0, 0, 52], [96, -96, 23, -23], [-100, -100, 29, 29], [0, 120, -60, 0], [117, -117, -37, 37], [0, 96, -12, 0], [-128, 0, 0, 60], [-324, 0, 0, 14], [197, 197, 15, 15], [-16, 16, -3, 3], [0, -194, 48, 0], [-132, 0, 0, -72], [3, 3, 10, 10], [120, 0, 0, -74], [188, -188, 17, -17], [105, -105, 9, -9], [-216, -216, 3, 3], [-256, 0, 0, -62], [63, -63, 32, -32], [-15, -15, -71, -71], [112, 0, 0, -20], [-39, -39, -26, -26], [-188, -188, 5, 5], [0, 280, 0, 0], [-237, 237, 5, -5], [0, 104, -108, 0], [-292, -292, -5, -5], [-83, 83, -60, 60], [0, 256, 18, 0], [444, -444, 7, -7], [0, 66, 48, 0], [205, 205, 56, 56], [-258, 0, 0, 32], [0, -174, 88, 0], [0, -180, -40, 0], [296, 0, 0, -28], [64, 0, 0, 18], [137, -137, 80, -80], [0, -172, -26, 0], [-228, 228, -17, 17], [0, -494, -72, 0], [-9, -9, -6, -6], [274, 0, 0, -72], [-388, 388, 15, -15], [-32, -32, 3, 3], [0, 424, 72, 0], [-72, -72, -75, -75], [332, 0, 0, -118], [0, -784, 38, 0], [120, -120, -81, 81], [0, -224, 138, 0], [-270, 0, 0, -104], [96, -96, -17, 17], [46, 0, 0, -88], [108, 108, -125, -125], [135, 135, -1, -1], [-564, 564, 21, -21], [0, -478, -104, 0], [296, 0, 0, 32], [463, 463, 0, 0], [-420, -420, 9, 9], [139, -139, -53, 53], [0, -176, -68, 0], [80, 0, 0, -96], [156, 156, 69, 69], [-357, 357, -88, 88], [21, 21, 77, 77], [0, -456, -78, 0], [52, 0, 0, 20], [584, 0, 0, -42], [-408, 408, -25, 25], [248, 248, 23, 23], [-249, 249, 61, -61], [0, 92, -24, 0], [-472, 0, 0, -72], [217, -217, -140, 140], [-17, -17, -137, -137], [-144, 144, -29, 29], [288, 0, 0, -92], [-368, 0, 0, 22], [0, 328, -36, 0], [0, 400, 180, 0], [-72, -72, 145, 145], [469, -469, 14, -14], [0, 608, -46, 0], [428, -428, 115, -115], [-924, 0, 0, 72], [-477, -477, -90, -90], [-50, 0, 0, 136], [0, 272, -24, 0], [297, -297, 23, -23], [-584, -584, -41, -41], [-39, -39, 107, 107], [0, 584, -38, 0], [-496, 0, 0, -104], [-320, 0, 0, 86], [-64, 64, -27, 27], [192, 192, 35, 35], [0, 578, 64, 0], [0, 176, 108, 0], [235, -235, -44, 44], [-333, -333, 55, 55], [0, -184, -262, 0], [-200, 200, -101, 101], [603, 603, -100, -100], [616, 0, 0, 154], [592, -592, 85, -85], [576, 576, -13, -13], [-908, -908, -3, -3], [-1192, 0, 0, -30], [0, 284, 222, 0], [0, 526, -152, 0], [-1049, -1049, -16, -16], [510, 0, 0, 16], [824, 824, 21, 21], [-267, 267, 205, -205], [856, 856, -21, -21], [-148, 0, 0, -338], [75, 75, -61, -61], [1148, -1148, 35, -35], [508, 0, 0, 168], [931, 931, -16, -16]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_90_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_90_3_g_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_90_3_g_d(:prec:=4)
    chi := MakeCharacter_90_g();
    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_90_3_g_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_90_3_g_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_90_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<7,R![256, -256, 128, 16, 1]>,<11,R![-80, -8, 1]>],Snew);
    return Vf;
end function;