
// Make newform 3042.3.c.e in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3042_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_3042_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_3042_3_c_e();".
// 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_3042_3_c_e();".
// 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
        poly := [2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_3042_c();"
function MakeCharacter_3042_c()
    N := 3042;
    order := 2;
    char_gens := [677, 847];
    v := [1, 2];
    // 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_3042_c_Hecke();"
function MakeCharacter_3042_c_Hecke(Kf)
    N := 3042;
    order := 2;
    char_gens := [677, 847];
    char_values := [[-1, 0], [1, 0]];
    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 := 3;
    raw_aps := [[0, -1], [0, 0], [0, 3], [4, 0], [0, -12], [0, 0], [0, -9], [16, 0], [0, -12], [0, 3], [-44, 0], [34, 0], [0, -33], [-40, 0], [0, 60], [0, 27], [0, -24], [50, 0], [-8, 0], [0, 36], [16, 0], [-76, 0], [0, -84], [0, -9], [-176, 0], [0, 21], [-28, 0], [0, 0], [-56, 0], [0, -111], [92, 0], [0, -120], [0, -111], [152, 0], [0, -195], [148, 0], [-82, 0], [-56, 0], [0, -24], [0, -123], [0, -144], [-232, 0], [0, -24], [-206, 0], [0, -117], [20, 0], [296, 0], [436, 0], [0, -12], [-8, 0], [0, -9], [0, -96], [-32, 0], [0, 36], [0, -129], [0, 264], [0, -243], [-380, 0], [-328, 0], [0, -201], [-208, 0], [0, 309], [520, 0], [0, -264], [-94, 0], [0, -237], [-536, 0], [-208, 0], [0, 204], [238, 0], [0, 159], [0, 396], [284, 0], [-190, 0], [160, 0], [0, 192], [0, -285], [-146, 0], [0, -231], [-368, 0], [0, 276], [40, 0], [0, 108], [542, 0], [-4, 0], [0, 228], [0, -153], [400, 0], [0, 213], [604, 0], [0, 252], [0, -372], [-596, 0], [0, -192], [-224, 0], [0, 612], [0, -339], [0, -369], [-736, 0], [808, 0], [536, 0], [0, 117], [0, -228], [0, 111], [368, 0], [142, 0], [0, -264], [0, -783], [0, 564], [158, 0], [332, 0], [-578, 0], [0, 39], [-896, 0], [-20, 0], [0, 183], [-728, 0], [0, -324], [0, 213], [0, -744], [-62, 0], [-670, 0], [0, -915], [0, 396], [40, 0], [0, 675], [-968, 0], [0, 828], [-508, 0], [1144, 0], [304, 0], [0, -600], [188, 0], [-1240, 0], [0, 111], [910, 0], [0, 981], [1360, 0], [0, -75], [0, -783], [160, 0], [0, -309], [332, 0], [0, -72], [632, 0], [0, -516], [-446, 0], [0, -303], [728, 0], [0, 648], [910, 0], [0, -657], [1064, 0], [0, -984], [-1768, 0], [0, -168], [380, 0], [0, -471], [-178, 0], [0, 309], [0, -1272], [0, 927], [-1700, 0], [0, 324], [0, -537], [0, 744], [-772, 0], [194, 0], [-560, 0], [0, 1005], [0, 816], [946, 0], [0, -192], [544, 0], [1412, 0], [0, -1335], [832, 0], [0, 909], [-196, 0], [878, 0], [-1508, 0], [0, 12], [1736, 0], [0, -153], [0, -528], [0, 813], [776, 0], [1552, 0], [-512, 0], [0, -72], [1280, 0], [0, 1104], [-1168, 0], [0, 1653], [0, 648], [0, -57], [46, 0], [-1048, 0], [0, -273], [0, -720], [0, 939], [-148, 0], [-728, 0], [1616, 0], [0, 684], [0, -99], [1708, 0], [0, -588], [0, -1047], [-1000, 0], [-430, 0], [0, 861], [428, 0], [0, 96], [0, 444], [1426, 0], [-1900, 0], [0, -231], [0, 576], [0, -933], [1880, 0], [-620, 0], [0, 57], [1852, 0], [0, -660], [-2722, 0], [0, -105], [0, 1548], [2012, 0], [0, 1032], [-1000, 0], [1784, 0], [748, 0], [0, 1959], [1544, 0], [0, 1860], [496, 0], [0, 477], [0, -1236], [0, 1404], [0, 852], [632, 0], [-1468, 0], [-2126, 0], [0, -1599], [0, 1020], [-164, 0], [0, -1320], [-440, 0], [0, 1368], [-1880, 0], [0, 1665], [0, 372], [-1120, 0], [0, -195], [0, 216], [-808, 0], [544, 0], [0, 1179], [2194, 0], [1364, 0], [0, 912], [136, 0], [686, 0], [0, 903], [-184, 0], [0, 315], [0, -2343], [-944, 0], [0, 51], [-1774, 0], [-2576, 0], [-896, 0], [908, 0], [1376, 0], [1780, 0], [0, -1212], [1678, 0], [2878, 0], [0, 624], [0, -1668], [-1484, 0], [0, -1716], [3262, 0], [496, 0], [0, -1152], [128, 0], [0, -627], [-3308, 0], [0, 513], [0, 309], [0, 2232], [0, 471], [0, 1776], [2606, 0], [0, 699], [-1732, 0], [0, -1563], [0, 252], [1552, 0], [-514, 0], [0, 837], [-700, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3042_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3042_3_c_e();".
// 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_3042_3_c_e(:prec:=2)
    chi := MakeCharacter_3042_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_3042_3_c_e();".
// 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_3042_3_c_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3042_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![18, 0, 1]>,<7,R![-4, 1]>],Snew);
    return Vf;
end function;