
// Make newform 768.4.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_768_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_768_4_a_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_768_4_a_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_768_a();"
function MakeCharacter_768_a()
    N := 768;
    order := 1;
    char_gens := [511, 517, 257];
    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_768_a_Hecke(Kf)
    return MakeCharacter_768_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 := 4;
    raw_aps := [[0], [-3], [-8], [12], [12], [20], [62], [-108], [-72], [-128], [204], [-228], [22], [204], [600], [256], [828], [-84], [-348], [456], [-822], [1356], [-108], [938], [1278], [608], [948], [-1044], [1780], [-622], [-204], [-348], [-106], [-1188], [2648], [3420], [60], [228], [4128], [-1352], [1716], [-3692], [-48], [2414], [4304], [-204], [4020], [516], [1428], [-6028], [-2630], [-4416], [4830], [5532], [-254], [-4272], [4544], [-2076], [484], [-406], [8172], [4960], [-6684], [-4992], [-5402], [7856], [-3732], [-5598], [8220], [-11844], [-7006], [-7512], [-5076], [4860], [5964], [432], [8888], [-2676], [13790], [1974], [-4764], [-92], [-10488], [-13138], [-3612], [12972], [5998], [8934], [7448], [4356], [8580], [-12648], [15036], [-15684], [16308], [10344], [-5648], [-19498], [-22596], [-812], [6132], [2792], [6468], [-10522], [1068], [3602], [-17940], [18034], [12264], [12634], [-2796], [13788], [18074], [-5940], [8700], [-16306], [-22668], [11928], [-2552], [2196], [-4260], [-2018], [9256], [29244], [3684], [13456], [-6460], [17160], [11820], [23924], [11796], [-27024], [17340], [27236], [14758], [25774], [-37424], [12804], [-32024], [-38090], [10428], [-7984], [-28788], [-468], [28852], [1944], [-37044], [15046], [-12180], [28752], [31884], [30802], [-32460], [14832], [-6900], [-32832], [-8340], [-39826], [-28550], [-50632], [-18204], [4934], [13284], [-50820], [11038], [-44112], [-56196], [-45588], [-2226], [-6008], [-44244], [2316], [48], [10842], [-31284], [63914], [-58980], [-37000], [-51900], [32556], [-4788], [33108], [23636], [-65542], [-19632], [-48536], [65540], [35028], [-27306], [-76032], [40578], [41820], [37284], [-27160], [72084], [13018], [-34846], [-11932], [-44830], [23808], [-77768], [-53388], [29572], [58962], [56364], [78224], [-1356], [-71532], [5530], [-69060], [-4034], [58696], [-48156], [-86484], [36888], [84134], [88284], [35038], [-24864], [-27368], [8548], [-684], [-81298], [96660], [-36252], [-57012], [-48470], [46200], [50100], [103452], [-3116], [-17580], [-52092], [-37766], [45708], [7896], [-13650], [-50728], [-41076], [-44904], [-61068], [-25620], [16332], [81324], [-22190], [98136], [103788], [101604], [92700], [87312], [-39836], [46594], [111000], [-38118], [65792], [-43308], [52180], [-71748], [-118448], [62966], [99396], [-55404], [10708], [-66132], [-110642], [76932], [73672], [130282], [-18036], [44792], [-51396], [123012], [13770], [29580], [40722], [83796], [51948], [-93876], [100010], [55428], [5688], [84516], [29160], [-100068], [-145236], [-131472], [-141954], [87656], [108876], [-52894], [64840], [52644], [64394], [-122004], [-125988], [8696], [-102972], [-59776], [-10260], [-53868], [27254], [92552], [-13428], [26868], [83004], [60174], [8652], [57620], [-115464], [79212], [22608], [69440], [27118], [111924], [85824], [-52038], [-43500], [-36600], [100338], [-68846], [83316], [126470], [85888], [-51444], [62294], [85902], [-161916], [75612], [130752], [-94856], [-184988], [-129800], [69444], [-148860], [-127620], [77916], [165324], [-160190], [-34902], [-8316], [-154788], [153670], [40448], [-173292], [6544], [-83052], [54164], [53580], [-132792], [146512], [-43260], [-51546], [160208], [76500], [-25764], [-127286], [-36672], [-94500], [-60610], [-187968], [-184260], [-57094], [198960], [66684], [26004], [-39094], [-23936], [118908], [-112682], [8772], [-167652], [114816], [36640], [-41460], [14996], [250260], [57840], [-22430], [-79330], [-27930], [-101960], [-86282], [49596], [-55646], [6756], [180384], [-171876], [44548], [147324], [35880], [-148926], [-30392], [-136212], [183684], [-105744], [44022], [-53724], [157942], [83244], [59992], [86844], [255982], [202692], [44038], [-11936], [36012], [-246908], [177470], [162372], [193572], [-148030], [-186712], [123228], [100932], [-155094], [93472], [269808], [35388], [-137954], [109752], [-134536], [261180], [222672], [198300], [239222], [-116192], [116676], [10666], [-222756], [42168]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_768_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_768_4_a_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_768_4_a_a(:prec:=1)
    chi := MakeCharacter_768_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_768_4_a_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_768_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_768_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![8, 1]>,<7,R![-12, 1]>,<11,R![-12, 1]>,<19,R![108, 1]>],Snew);
    return Vf;
end function;