
// Make newform 546.6.a.f in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_546_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_546_6_a_f();".
// 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_546_6_a_f();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_546_a();"
function MakeCharacter_546_a()
    N := 546;
    order := 1;
    char_gens := [365, 157, 379];
    v := [1, 1, 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;

function MakeCharacter_546_a_Hecke(Kf)
    return MakeCharacter_546_a();
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 := 6;
    raw_aps := [[4], [9], [-54], [49], [-192], [169], [-1422], [1748], [-3792], [-954], [-568], [7886], [-14802], [4964], [-18948], [-426], [34872], [-25618], [-67060], [-28428], [-22894], [-1408], [-17304], [-93690], [16826], [-129090], [-5536], [-201996], [32510], [-36918], [-169792], [177660], [141582], [282356], [203682], [114368], [48734], [-18124], [261300], [550710], [-264444], [-31930], [-210240], [-488062], [-165558], [-800152], [-382060], [-303016], [383376], [226070], [1163874], [-661332], [-662854], [-221100], [1129722], [1015440], [1392822], [-1487392], [195782], [-1503762], [365660], [1879362], [1187372], [-539808], [3305354], [1926402], [-967612], [-763966], [-3328932], [2027606], [2943366], [4254564], [-3307864], [2863574], [1413596], [-4391484], [-1338858], [3389942], [-2846946], [811970], [4411596], [5621702], [3354420], [-691486], [7771280], [-1399404], [246846], [-2559118], [-8340366], [-3170656], [7534668], [-3180684], [2092448], [1213140], [-63316], [1060320], [-3461358], [10595634], [2498852], [-9361474], [10039532], [3857178], [1620564], [-4099758], [-826660], [-9546622], [8658288], [-10780962], [-7083600], [15930698], [5972120], [-877426], [-9605682], [818276], [-2739472], [-13938270], [-3927700], [-18507192], [-5057706], [-10381116], [-14068018], [19227938], [-9598314], [-18847152], [-19646932], [-4956642], [-8402602], [7195560], [4225928], [-7644034], [-26271844], [-24631404], [19630520], [-8871514], [19798302], [-8148286], [-7691454], [-24983668], [6193278], [-5090766], [2110988], [-29226174], [10871216], [7790112], [-14952274], [29408316], [25945358], [-27198126], [15564860], [36126372], [-6969898], [-17308038], [-19766572], [500856], [37090508], [40415520], [-12553432], [-8471466], [-21310918], [-27062934], [-23446608], [-32656686], [-47479024], [14748], [-41601186], [52848828], [1097336], [36302486], [62015834], [46099710], [-32800968], [65863286], [-24224352], [-31086670], [-49657240], [28901178], [-21715276], [-36983406], [-58610752], [38457230], [42200528], [60528324], [-61008778], [-32208378], [72425052], [42210246], [9366446], [-30873964], [-42856606], [30409740], [31331954], [23458704], [61729004], [6810330], [-63036684], [3201282], [-76333438], [-5661202], [-7595106], [36802632], [39669810], [-48312040], [57779558], [-39115246], [2941080], [-47776578], [59887352], [-43858212], [-45155898], [-63107956], [-96160606], [-85414602], [37866776], [-61820784], [31135164], [6957338], [-6613552], [-94444926], [4811940], [-62217030], [43913366], [-41151592], [-16712802], [47747864], [106879836], [82811318], [-4896006], [109199016], [97044152], [2007960], [-134657794], [10455212], [34869968], [30908010], [48267140], [-164211444], [106041770], [-79104510], [54255996], [42815400], [-31786968], [9880796], [-26299432], [-149054362], [-127518066], [45669120], [55432064], [-94878720], [52949708], [177243576], [99018062], [37106982], [-188941524], [-17789926], [168259590], [34463832], [-10710970], [-152305348], [91083798], [-126778390], [173086736], [71200548], [-173038618], [-148680178], [-3579618], [-160762828], [-154876830], [11159886], [50919380], [-45391458], [85925294], [63281324], [-178302046], [-4731256], [102284306], [63437960], [-103830072], [65590502], [104710826], [-120805380], [-69472656], [-91253800], [145947288], [71535758], [15613172], [39042336], [133352354], [274613154], [150944288], [-7548918], [281883102], [-60575148], [-85509498], [248998968], [128557742], [182111934], [-110369848], [13517910], [104304636], [-54889060], [24507242], [-48331518], [225102440], [59493156], [-56519164], [-126596230], [234448812], [-363559618], [-235869204], [-102711850], [-26356248], [121907226], [163449114], [-26947468], [310863972], [-254692438], [298835520], [99643908], [157262810], [-57966126], [-343998292], [230781194], [-191408082], [-422525464], [38426166], [157638530], [271340372], [-171031756], [192853344], [-6670866], [-112125442], [385887702], [255780032], [-433348440], [-333168532], [-103486896], [238187990], [-226558506], [397670066], [-388310632], [288130334], [377299986], [-237782910], [-350996080], [-244171422], [-56595276], [189669782], [214285676], [-156088404], [-117480666], [33367868], [56690642], [45076746], [-163451920], [-403600618], [-108928062], [206049180], [153813240], [-107534790], [96208284], [-313974130], [-294741510], [31421208], [-528474120], [34448060], [-263990326], [224934738], [119757728], [-309365062], [544947276], [223987124], [50098860], [-123064026], [37608200], [-530141650], [30618552], [155219232], [169717250], [-466480686], [608204858], [94978026], [-124104834], [290707544], [-396088146], [-220028572], [-217410180], [-32604592], [216071846], [-90745972], [705865896], [-36519430], [-652142094], [-486380928], [335319476], [595918692], [-18445654], [-665017216], [-296391894], [405527084], [677942586], [-490130650], [129479202], [296591552], [-459977994], [248895450], [-468990808], [421511486], [622412862], [-454755268], [-292187832], [138943634], [754787766], [-101961636], [678409340], [-844733926], [93572670], [-160836852], [-332492728], [263078862], [-397222512], [-571364634], [683318702], [491250780], [-765708060], [558068906], [-141031254], [-55075044], [425957094], [346253852], [948147096]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_546_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_546_6_a_f();".
// 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_546_6_a_f(:prec:=1)
    chi := MakeCharacter_546_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_546_6_a_f();".
// 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_546_6_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_546_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![54, 1]>,<11,R![192, 1]>],Snew);
    return Vf;
end function;