
// Make newform 1840.2.a.b in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1840_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_1840_2_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_1840_2_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_1840_a();"
function MakeCharacter_1840_a()
    N := 1840;
    order := 1;
    char_gens := [1151, 1381, 737, 1201];
    v := [1, 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_1840_a_Hecke(Kf)
    return MakeCharacter_1840_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 := 2;
    raw_aps := [[0], [-1], [-1], [2], [0], [1], [-4], [4], [-1], [-3], [1], [-8], [-5], [6], [-9], [2], [0], [0], [-4], [-3], [7], [-4], [-8], [-14], [-14], [2], [-8], [-4], [2], [-6], [-7], [-3], [-12], [-1], [18], [13], [-2], [-11], [-24], [14], [-9], [-12], [10], [-9], [-13], [20], [8], [-16], [-10], [10], [21], [21], [22], [12], [-3], [-16], [25], [28], [3], [6], [10], [-16], [4], [9], [-4], [26], [19], [-10], [-4], [-15], [3], [-4], [36], [6], [16], [-2], [-24], [-7], [2], [-19], [6], [-6], [18], [2], [19], [-5], [-18], [-40], [-3], [20], [-18], [4], [5], [27], [-19], [30], [-19], [-28], [38], [17], [1], [34], [24], [-38], [-24], [23], [-29], [26], [-24], [45], [40], [-4], [-28], [-4], [-26], [6], [2], [-21], [-35], [36], [-2], [-43], [26], [9], [-32], [18], [22], [-36], [26], [-28], [-37], [8], [-2], [-40], [5], [-10], [18], [-32], [-24], [-46], [-39], [34], [3], [-28], [14], [18], [14], [-7], [45], [-11], [2], [56], [-4], [3], [28], [-8], [-46], [-45], [36], [48], [-45], [-6], [-23], [-50], [0], [30], [-8], [10], [-12], [-51], [4], [13], [-20], [16], [47], [-42], [51], [-5], [8], [52], [-19], [-66], [-25], [46], [34], [20], [-30], [0], [9], [-32], [49], [56], [-18], [-62], [2], [-66], [64], [-48], [28], [-39], [-12], [16], [-33], [44], [32], [51], [20], [-45], [-9], [-49], [-11], [-5], [-42], [2], [36], [10], [-5], [-27], [-18], [-51], [-29], [-24], [9], [34], [-9], [55], [36], [-9], [-38], [20], [-46], [50], [38], [-22], [-4], [34], [52], [8], [37], [-17], [72], [17], [-49], [71], [-6], [41], [-13], [30], [50], [54], [24], [6], [18], [22], [-18], [-53], [-48], [70], [-17], [47], [52], [18], [30], [10], [18], [-60], [-56], [-10], [-52], [55], [45], [-30], [66], [54], [-13], [-41], [28], [62], [62], [64], [9], [-82], [-12], [10], [-72], [24], [82], [28], [-12], [-30], [-30], [-40], [26], [-48], [-29], [54], [4], [61], [3], [-23], [62], [44], [24], [-43], [-52], [-19], [28], [-56], [74], [-70], [-1], [72], [36], [-51], [52], [-14], [-65], [5], [28], [-89], [38], [2], [-21], [-64], [-18], [-56], [6], [21], [48], [72], [-79], [-51], [-85], [-63], [-68], [-15], [-82], [6], [-1], [-2], [-67], [-30], [10], [-62], [34], [68], [-25], [80], [-42], [78], [-67], [-45], [-58], [-2], [52], [46], [20], [58], [-73], [56], [-46], [-7], [-23], [-26], [33], [-65], [33], [-56], [-46], [63], [68], [3], [36], [56], [-83], [-57], [-68], [-38], [-76], [1], [-62], [-56], [48], [-55], [-2], [45], [87], [-46], [-74], [4], [-44], [10], [-95], [76], [6], [-22], [38], [-86], [56], [82], [-1], [-85], [-74], [44], [15], [-87], [-44], [-15], [-22], [68], [77], [21], [-84], [-14], [-20], [57], [-64], [24], [58], [98], [-50], [22], [-96], [60], [-61], [65], [32], [-66], [-7], [81]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1840_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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