
// Make newform 3440.2.a.j in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3440_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_3440_2_a_j();".
// 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_3440_2_a_j();".
// 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_3440_a();"
function MakeCharacter_3440_a()
    N := 3440;
    order := 1;
    char_gens := [431, 2581, 1377, 1121];
    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_3440_a_Hecke(Kf)
    return MakeCharacter_3440_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, 0], [0, 1], [1, 0], [-1, 0], [-2, -1], [1, 2], [0, -1], [1, 0], [-2, -5], [-3, -3], [-1, -1], [-2, -3], [1, 2], [-1, 0], [0, 5], [0, 4], [4, 2], [-7, -5], [-3, -3], [-2, 7], [-5, 3], [7, -1], [2, 4], [-2, 5], [-2, -11], [0, -1], [2, 2], [-9, -5], [-8, 2], [-5, -7], [-12, -6], [-6, 0], [11, 5], [2, -3], [5, 11], [-2, 6], [6, -3], [-8, 4], [2, 11], [9, 6], [-9, 6], [-8, 5], [-10, 7], [4, -2], [11, -10], [10, 7], [-4, -4], [-2, 10], [-12, -2], [-2, -2], [0, -6], [-13, -1], [-20, 1], [-6, -12], [-1, -7], [-9, 6], [12, 2], [1, 7], [8, 6], [-7, -4], [-23, -3], [-6, -12], [13, 5], [-3, -5], [10, 8], [15, 10], [14, -8], [-14, -4], [18, -3], [-2, -18], [-14, 13], [5, 23], [20, -8], [2, -9], [10, 10], [-3, -8], [18, -2], [-12, 6], [7, 22], [-6, 15], [-7, -14], [-3, -3], [-18, -12], [-29, 5], [4, -8], [17, 5], [18, -3], [-22, 6], [-8, 5], [-19, 8], [28, 5], [-12, -2], [12, -15], [6, 16], [9, -18], [26, -4], [-12, 6], [-4, 5], [2, 9], [22, 10], [12, 0], [39, -2], [9, 5], [3, -14], [-33, 0], [-7, -1], [12, -11], [-3, 3], [-24, -10], [-28, 0], [-6, 0], [41, 2], [12, 20], [10, 7], [-16, -21], [6, -26], [-27, 9], [-25, -14], [-8, -1], [-6, 13], [-2, -19], [19, -1], [18, -18], [12, 6], [-10, 6], [24, 9], [14, 15], [14, -16], [14, 0], [32, 3], [-25, -12], [9, 8], [-4, -9], [-6, -30], [-42, 2], [21, 0], [-18, 5], [-27, 3], [-9, 24], [-9, 14], [17, -22], [-6, 10], [-22, -6], [11, -23], [-1, -3], [10, -29], [-2, 24], [40, -5], [25, 6], [38, -2], [4, 16], [-21, -14], [-5, 15], [25, -22], [-5, 7], [2, -13], [25, -7], [6, 14], [8, 30], [-46, 5], [-21, 27], [-19, -7], [-32, 3], [-14, 5], [-10, 5], [33, 4], [4, 32], [40, 2], [-14, -32], [-3, -32], [18, 32], [29, 23], [36, 8], [-24, -9], [-32, 0], [7, -28], [-41, -8], [-7, -17], [-8, -16], [11, 17], [1, 36], [12, -18], [28, 10], [-14, -26], [27, -20], [-23, -5], [-2, 14], [-50, -8], [27, 6], [-30, -17], [-18, -6], [-6, 27], [-32, 12], [-21, -5], [-36, -13], [-13, 29], [5, -28], [-3, -12], [36, 2], [5, -20], [0, -24], [24, 27], [-2, 33], [18, -21], [13, -16], [28, 20], [6, -39], [-1, 11], [4, 25], [-22, -8], [-13, 31], [-16, -25], [-2, -14], [-21, -23], [-16, -17], [-23, -2], [-11, -12], [-6, 1], [42, 6], [21, -18], [-22, 6], [17, 27], [-42, -6], [34, -10], [0, 14], [-8, -27], [30, 20], [-28, -11], [5, -20], [-21, 6], [16, 1], [10, 28], [-27, 27], [34, -26], [-4, 34], [-38, -19], [-12, -36], [33, -10], [-9, -28], [53, 1], [-18, 16], [58, 2], [-28, 8], [-16, 39], [-3, -13], [-7, -23], [40, -4], [-42, 1], [6, -6], [-48, -8], [16, 14], [27, -14], [16, 28], [20, 33], [30, 3], [36, 8], [-28, -10], [-7, 1], [-30, -32], [-50, 0], [-17, 18], [-2, -14], [-6, 36], [5, -32], [-9, -5], [11, 6], [-61, 7], [4, -34], [-30, -21], [-12, 22], [2, -1], [52, -19], [-7, 13], [30, 3], [-42, -21], [-16, -20], [9, -25], [10, -18], [-52, -12], [25, -22], [-60, -14], [-31, 25], [-42, 7], [7, -21], [8, -28], [-38, 14], [-42, -24], [-40, 7], [-40, 12], [42, 0], [18, -30], [-53, -7], [2, -2], [-7, -56], [41, -6], [-24, -35], [20, -46], [35, 28], [-18, 4], [-47, -3], [32, 17], [0, 8], [-41, 19], [-56, -10], [-29, 18], [-67, 9], [-23, -1], [-1, -23], [-56, -22], [-58, 4], [-6, 0], [-53, -11], [60, -12], [22, 12], [48, 9], [35, 6], [-12, 11], [18, 4], [36, 30], [-6, -9], [12, -3], [16, -30], [15, 51], [-38, -9], [-13, 41], [16, 20], [-9, -12], [-13, -21], [-56, -22], [54, 20], [62, 20], [3, 38], [-34, -5], [-4, 14], [-10, 39], [12, -18], [-31, 39], [66, -6], [-48, 9], [-57, -18], [-16, 42], [9, -35], [-1, 19], [8, 27], [15, -4], [-48, 6], [83, -3], [20, 4], [-36, 26], [-36, 0], [52, -10], [-17, -9], [-30, -43], [3, 42], [-20, -11], [-30, -18], [-6, 28], [-11, -14], [6, 20], [30, 43], [-8, 15], [-18, -34], [24, -12], [33, -12], [-4, 28], [-9, -3], [22, -47], [-4, 9], [48, -6], [-9, -36], [13, -18], [-30, 30], [-60, 2], [53, -19], [-32, 0], [-30, 18], [33, -21], [-28, 13], [6, 0], [13, -9], [-54, 3], [-6, -23], [-18, -39], [-26, 22], [-20, -33], [16, 5], [2, 21], [17, -44], [9, 3], [16, 46], [33, -36], [54, -30], [69, -6], [-17, -15], [68, 2], [-66, -9], [23, 5], [22, -5], [-46, 38], [39, 19], [-46, 36], [-12, -9], [-48, -27], [-60, -20], [-51, 23], [24, -24], [52, 3], [-11, 58], [4, 23], [6, -32], [-6, -24], [60, -5], [-2, -7], [-8, -3], [-19, -21], [-4, -62], [9, 17], [-66, 9], [84, 7], [30, 44], [-27, -1], [-4, -34], [45, -18], [-18, 6], [-18, 6], [-78, -3], [6, -59], [0, 24], [-28, -48], [-6, 24]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3440_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3440_2_a_j();".
// 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_3440_2_a_j(:prec:=2)
    chi := MakeCharacter_3440_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_3440_2_a_j();".
// 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_3440_2_a_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3440_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-2, 0, 1]>,<7,R![1, 1]>],Snew);
    return Vf;
end function;