
// Make newform 12.17.c.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_12_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_12_c_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_12_17_c_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_12_17_c_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_12_c();"
function MakeCharacter_12_c()
    N := 12;
    order := 2;
    char_gens := [7, 5];
    v := [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_12_c_Hecke();"
function MakeCharacter_12_c_Hecke(Kf)
    N := 12;
    order := 2;
    char_gens := [7, 5];
    char_values := [[1], [-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 := 17;
    raw_aps := [[0], [6561], [0], [4743554], [0], [-349391806], [0], [32868566594], [0], [0], [1568167997954], [-6771424503358], [0], [-11180981621566], [0], [0], [0], [184288715234114], [-782743712096446], [0], [-1351474503392638], [-2677497415399678], [0], [0], [15621585304991234], [0], [-23359691404874878], [0], [1431198471131714], [0], [25099381669800962], [0], [0], [224584841911930562], [0], [-467535426934962046], [-736981868925544126], [375525813721157954], [0], [0], [0], [-1195744941970782526], [0], [1563275229680028674], [0], [-1909480908969003646], [-7823845340629714366], [10116369370357458434], [0], [13590809847805973954], [0], [0], [8218252573806266114], [0], [0], [0], [0], [-49258703161402164478], [-14145239213080120126], [0], [25246032301864362434], [0], [41720443873848654914], [0], [-47335136488972787326], [0], [-24127467317789363518], [331325751308639231234], [0], [435998969790095611202], [0], [0], [-67438772179771607806], [-724927704098029247806], [-839225623222278585406], [0], [0], [-985697241089713288126], [0], [1209195665651193743234], [0], [-1838958823870098607678], [0], [1775567448802400723714], [1803944704170366068354], [0], [0], [-3061893155976088175998], [0], [507862599627719888642], [0], [0], [1952770173733971399554], [0], [7521955444512626475074], [0], [0], [0], [-10596490010079448865086], [-10740672643991969926846], [-12207287924645337343678], [0], [0], [0], [15400802330757797533634], [-763731850516242336766], [0], [0], [0], [-30166449562814088678526], [33891247073297316441602], [-39626570233290675524158], [0], [7757406524830103931074], [-10237341571969430529406], [0], [-22892310182699912414398], [0], [0], [0], [53856765240413897223362], [-31757754925615354160638], [0], [0], [-79683143585340949556158], [0], [110134779044990638037954], [0], [-141302577625530522377086], [166637726616923063618882], [11897122315584994954562], [0], [84743025672456221932802], [-4290741837332192601406], [0], [204979226287175703539714], [0], [161866304840564166399554], [0], [0], [-366385048881021463987006], [0], [322416275259417383693954], [0], [-276417610293741197350078], [0], [-182397910304112332543806], [0], [51519801692832185327042], [0], [-655437898108258770345406], [0], [-410931442687260511086526], [0], [535808539730359371345602], [0], [-721029034490881638434686], [0], [881756864252095415056514], [0], [0], [0], [767665327831826966455682], [0], [0], [0], [1828473137412561470510594], [-1614486158284655790742078]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_12_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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