
// Make newform 14.6.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_14_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_14_6_a_b();".
// 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_14_6_a_b();".
// 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_14_a();"
function MakeCharacter_14_a()
    N := 14;
    order := 1;
    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;

function MakeCharacter_14_a_Hecke(Kf)
    return MakeCharacter_14_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], [8], [10], [-49], [-340], [-294], [1226], [2432], [2000], [-6746], [8856], [9182], [-14574], [8108], [-312], [-14634], [-27656], [34338], [12316], [36920], [-61718], [-64752], [-77056], [-8166], [20650], [186250], [-60064], [47892], [22102], [-245054], [-96696], [134368], [-294662], [314944], [113622], [408208], [293546], [-317116], [141568], [-71222], [485628], [657090], [68304], [352754], [196982], [-1103920], [-103444], [307328], [-891792], [276706], [1479434], [1000344], [1358330], [-177408], [326658], [-34920], [716458], [-953376], [-1847290], [-1996006], [234088], [-2500806], [2342032], [-163064], [1739650], [-1797714], [-2475412], [89154], [938556], [3342682], [-3766062], [-1539344], [-859312], [-976586], [106444], [-2006344], [-684002], [-222870], [1900722], [1777154], [28056], [-2708970], [5538984], [-868294], [-1137672], [1753988], [2416738], [-127430], [-128198], [-4016528], [8672464], [8289464], [-8917696], [-5715372], [125116], [-2771160], [-138534], [-1802814], [9772472], [2455038], [13208148], [7832926], [3579080], [-3395814], [-1476948], [-14996078], [-3292912], [-11790846], [-1526424], [-10014214], [12065952], [5819502], [-4165894], [-8080904], [-8408776], [6297602], [4397624], [6553968], [3796518], [-8826844], [-341270], [4418066], [16385778], [-17539932], [3146384], [-19091882], [990974], [-16901448], [-23430176], [975810], [-6302076], [-6956976], [27453480], [-19688898], [-28207918], [-1380806], [-15434726], [-7101072], [6481818], [16057786], [4847752], [21797606], [32020600], [21900804], [-14599934], [4609472], [-19843726], [-1229598], [33304064], [-23661584], [-23781242], [-14187054], [20928140], [-7985856], [-23186076], [16529928], [12808696], [29731866], [11097002], [37431346], [15090724], [-21574054], [-32946688], [22459944], [-51623646], [-11020176], [32102864], [28177178]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_14_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_14_6_a_b();".
// 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_14_6_a_b(:prec:=1)
    chi := MakeCharacter_14_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(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_14_6_a_b();".
// 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_14_6_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_14_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![-8, 1]>],Snew);
    return Vf;
end function;