
// Make newform 72.7.e.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_72_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_72_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_72_7_e_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_72_7_e_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 := [2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_72_e();"
function MakeCharacter_72_e()
    N := 72;
    order := 2;
    char_gens := [55, 37, 65];
    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_72_e_Hecke();"
function MakeCharacter_72_e_Hecke(Kf)
    N := 72;
    order := 2;
    char_gens := [55, 37, 65];
    char_values := [[1, 0], [1, 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 := 7;
    raw_aps := [[0, 0], [0, 0], [0, 5], [60, 0], [0, 236], [1192, 0], [0, 2935], [8432, 0], [0, 4532], [0, 23787], [46892, 0], [10926, 0], [0, 51921], [59416, 0], [0, -83004], [0, 58259], [0, -199336], [-339902, 0], [-148024, 0], [0, -291044], [-401552, 0], [79156, 0], [0, -70988], [0, 265785], [663920, 0], [0, -34371], [803236, 0], [0, 1618944], [1512472, 0], [0, -259023], [291676, 0], [0, 1694536], [0, -2065857], [-3194280, 0], [0, -2321189], [-877012, 0], [-1485026, 0], [-6244872, 0], [0, -5039592], [0, -3309795], [0, 5728368], [-2537736, 0], [0, -9204888], [-588850, 0], [0, 9057693], [11081428, 0], [454120, 0], [19774540, 0], [0, 1252140], [7164328, 0], [0, 16378151], [0, -11154464], [-3176736, 0], [0, 18086980], [0, -5461505], [0, -22820728], [0, 9779653], [-5097220, 0], [-20819432, 0], [0, -19606727], [-37236304, 0], [0, -19227549], [-39466440, 0], [0, -9553800], [-29835006, 0], [0, -30067131], [946136, 0], [35945520, 0], [0, 14701740], [24255138, 0], [0, -16761119], [0, 44403252], [54460828, 0], [21449138, 0], [41525984, 0], [0, 720576], [0, 37297307], [33924578, 0], [0, -3669081], [54975408, 0], [0, -28296204], [-122185672, 0], [0, -32446316], [-70053026, 0], [72246204, 0], [0, -95317628], [0, -16575751], [-163670224, 0], [0, -25503853], [-173562844, 0], [0, -118154724], [0, 84409972], [-112186604, 0], [0, -71973696], [101327328, 0], [0, -63681372], [0, 39540027], [0, -26560865], [-17968736, 0], [192941496, 0], [175297496, 0], [0, -181758733], [0, 3349436], [0, 203486495], [-86776080, 0], [287028338, 0], [0, 67072712], [0, -262539217], [0, 238271220], [-212984642, 0], [-222205876, 0], [-256447630, 0], [0, 3552489], [393391744, 0], [-339613868, 0], [0, -217762729], [-332651112, 0], [0, -89108612], [0, -157376787], [0, 53256408], [-123651250, 0], [-474799646, 0], [0, 149348597], [0, -383092204], [-35483752, 0], [0, -368228853], [24893672, 0], [0, -17133892], [145627716, 0], [-96977368, 0], [260556368, 0], [0, -449186728], [181893180, 0], [-252138424, 0], [0, 302910241], [567451250, 0], [0, 530340675], [362103472, 0], [0, -246064147], [0, 500351329], [-504592160, 0], [0, 51967677], [-101113588, 0], [0, 380859016], [1000759384, 0], [0, -94413372], [-981323314, 0], [0, 58038753], [5652504, 0], [0, -323832328], [388790850, 0], [0, -643152881], [-1175575192, 0], [0, -167086424], [-1191167144, 0], [0, -444376360], [628811836, 0], [0, -826408073], [-610905810, 0], [0, -702947469], [0, 505191096], [0, 561501135], [-436986268, 0], [0, 974299236], [0, -773149863], [0, -539807592], [33208828, 0], [1261567474, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_72_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_72_7_e_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_72_7_e_a(:prec:=2)
    chi := MakeCharacter_72_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 7));
    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_72_7_e_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_72_7_e_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_72_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([<5,R![50, 0, 1]>],Snew);
    return Vf;
end function;