
// Make newform 4.33.b.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_4_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_4_33_b_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_4_33_b_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_4_b();"
function MakeCharacter_4_b()
    N := 4;
    order := 2;
    char_gens := [3];
    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_4_b_Hecke();"
function MakeCharacter_4_b_Hecke(Kf)
    N := 4;
    order := 2;
    char_gens := [3];
    char_values := [[-1]];
    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 := 33;
    raw_aps := [[65536], [0], [-196496109694], [0], [0], [1330087744899070082], [1427124567881986562], [0], [0], [462878764200641031680642], [0], [13364603395730595798238082], [-117479780930606773712920318], [0], [0], [-6730923570418671225390264958], [0], [-71388302423745245269146679678], [0], [0], [606523506722898114294021899522], [0], [0], [17408498582555430732193248126722], [84097663609016849910038850800642], [-234247817175429022462951664899198], [0], [0], [-790238662302503579953621796277118], [-750076407471570370769827525624318], [0], [0], [5062041290659234321781902254132482], [0], [90757190217096164556376594639497602], [0], [-262580156527350260449287776172604798], [0], [0], [220864197019578127494450312358485122], [0], [-296252683564808121903289418716160638], [0], [-2751932054668484521166551373456025598], [-6716649477402525947686407226082424958], [0], [0], [0], [0], [-51882265766012773241120341581022200958], [55162662409781062728182500218320943362], [0], [-120816341270142484500950930732324958718], [0], [-299731661033838759906903195118965350398], [0], [-800999065341183847193169218657493690238], [0], [2027061692567374759768328510859657640322], [-2914737474817188000934431070155864080638], [0], [-4850452445038569467726222620827817679998], [0], [0], [4877290117081602115544431439664797786882], [-16168603009122800017908269687553302010238], [0], [-42740379357310048562100229121476141278718], [0], [-70589982055238197231885664278212504585598], [7227124160361568739057969340924722211842], [0], [0], [215143449085469206967397225251376028456322], [0], [0], [-143255347738161002546386929034795216126078], [-711797309800980972939527462011938040076158], [-24914782380988742747819006974221069196798], [63849189919691997319777464109026936180482], [0], [878281665448253577368352343536728772359042], [0], [2084108992434155370650993100502336080668162], [0], [0], [-1201469911697332536054067069178428883404798], [6995687314456786594072904293993567213958402], [-8148625557994848118046226902257984164343678], [0], [0], [0], [0], [0], [0], [0], [26239230365937471170557317403832727711473282], [-54672182468985954386425961018203728452616958], [0], [-9062140921074240272998590777378963759861118], [0], [17053168020875424102838646699221682204265602], [0], [221864537657110045646257985308663098210927362], [0], [71241078862913165714964584634294747893483522], [0], [-131027625757046074105497970838402101179532798], [0], [555349761911365576557901877627362931814393602], [0], [485424068695187127957441620680680088534903682], [-810732663114422705413031556058357151383607038], [0], [0], [579277525232480659756959565038830690719764482], [0], [0], [-434490694917199846184715016387487370958911358], [0], [826814357767318382112404935566952746083445122], [-3362844266097000723855197497478877465454586878], [1301081925627505787972258076709648567708341122], [0], [0], [6659744682570649779442190357586730031967196802], [7790161285143618406884568146085308912267511682], [0], [0], [-9917827219309005238702001620573903316926535038], [0], [0], [0], [-7575088066275998912065445532057085576025078398], [17256576564987047683447840525138460538475518722], [-5457359586167307837013802963107729295124848638], [-19202687205826718609232557869342757906276503678], [0], [51474448665932414495752876842353017545935360642], [54463570805648734077014542611614299680662503682], [0], [-68973909514607558059013462606994577200473477758], [0], [0], [34592178969234992431201299814224803896467891842], [0], [-115846418317055053848840356448683765286493445758], [-54271184564527704306296817443698262763663571198], [0], [0], [237582428065616364204767518923008744569599726722], [212610257150282952092697457534911995833325084162], [0], [0], [0], [0], [0], [-376519509124480398673031110222369838613712604158], [-597227294797147290161301335515899988442770763518], [-273113222906374330809348984895296481272865768318], [0], [209005486092211012229082693680993934027970179842], [0], [0], [-785108457700387199837635569115322198097163015678], [0], [0], [1880153118396337029783887500664146404882931309442]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4_33_b_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_4_33_b_a(:prec:=1)
    chi := MakeCharacter_4_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 33));
    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_4_33_b_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_4_33_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,33,sign)));
    Vf := Kernel([<3,R![0, 1]>],Snew);
    return Vf;
end function;