
// Make newform 3675.2.a.p in Magma, downloaded from the LMFDB on 07 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3675_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_3675_2_a_p();".
// 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_3675_2_a_p();".
// 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_3675_a();"
function MakeCharacter_3675_a()
    N := 3675;
    order := 1;
    char_gens := [1226, 1177, 2551];
    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_3675_a_Hecke(Kf)
    return MakeCharacter_3675_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 := [[2], [1], [0], [0], [-6], [-3], [-4], [-1], [4], [-8], [-1], [-7], [6], [-1], [2], [-4], [8], [14], [-7], [6], [1], [-1], [2], [12], [-6], [10], [-19], [-12], [-15], [6], [-5], [-2], [-8], [-21], [-4], [8], [10], [12], [-10], [24], [18], [-13], [10], [9], [-12], [-8], [-20], [-24], [-10], [-13], [-6], [14], [18], [-12], [-18], [-4], [10], [24], [7], [-12], [-7], [-16], [3], [-6], [11], [-20], [-9], [-25], [-16], [-2], [-18], [-24], [19], [-11], [11], [28], [6], [-37], [-12], [-5], [-6], [1], [-2], [-5], [-16], [36], [30], [15], [8], [-3], [22], [4], [13], [-12], [29], [-2], [-10], [-4], [11], [-3], [-36], [10], [-26], [18], [-3], [-29], [12], [-18], [-4], [33], [35], [30], [-18], [-3], [24], [12], [1], [30], [-14], [-12], [-23], [37], [16], [48], [-27], [-44], [-26], [34], [7], [-43], [41], [-6], [29], [22], [12], [49], [18], [-32], [-36], [-42], [48], [54], [-8], [30], [-57], [-24], [-9], [-12], [40], [6], [22], [0], [-7], [10], [-31], [12], [-9], [14], [-29], [0], [-26], [-4], [-55], [52], [22], [32], [-15], [-25], [21], [44], [-40], [-62], [-32], [-1], [51], [40], [-13], [-30], [-40], [17], [7], [44], [27], [28], [-26], [-52], [-38], [-61], [-31], [50], [-43], [-62], [-40], [4], [-48], [36], [-45], [-46], [2], [32], [30], [17], [-2], [-10], [70], [2], [-24], [34], [-20], [12], [-43], [44], [20], [-20], [-24], [1], [-8], [-12], [20], [46], [-59], [-29], [-66], [-16], [18], [-46], [-56], [-14], [-13], [-42], [31], [28], [7], [34], [45], [-34], [27], [-50], [-36], [18], [-50], [-57], [1], [-2], [56], [-44], [-41], [-54], [72], [16], [55], [48], [54], [-13], [-72], [76], [57], [19], [38], [-2], [-31], [16], [11], [-1], [-74], [-25], [30], [-2], [-28], [-2], [-75], [28], [-71], [-73], [18], [28], [50], [-1], [43], [16], [0], [-41], [-6], [-47], [-20], [74], [-21], [30], [35], [-14], [60], [60], [-74], [48], [1], [-78], [47], [40], [6], [-61], [14], [-18], [-68], [-12], [21], [39], [-10], [7], [32], [-39], [-24], [-18], [6], [72], [-32], [47], [-38], [-12], [61], [6], [-5], [53], [-6], [29], [-86], [79], [39], [1], [60], [-78], [1], [-26], [-1], [-66], [83], [-30], [50], [72], [43], [1], [-35], [12], [-16], [37], [-18], [70], [-25], [44], [14], [18], [44], [38], [-6], [32], [7], [-78], [90], [-84], [16], [38], [-90], [-42], [-78], [18], [-19], [26], [-36], [59], [22], [-60], [-13], [18], [-70], [9], [-29], [84], [-48], [-26], [-8], [5], [0], [-40], [-88], [-36], [-28], [84], [-25], [94], [81], [42], [-55], [-14], [0], [-29], [-82], [-58], [-36], [-98], [15], [-10], [-62], [24], [-40], [-82], [-102], [-25], [-54], [68], [17], [60], [82], [-68], [-12], [100], [-53], [-62], [22], [-35], [28], [48], [-24], [83], [28], [-34], [94], [54], [22], [-48], [44], [-36]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3675_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3675_2_a_p();".
// 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_3675_2_a_p(:prec:=1)
    chi := MakeCharacter_3675_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_3675_2_a_p();".
// 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_3675_2_a_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3675_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-2, 1]>,<11,R![6, 1]>,<13,R![3, 1]>],Snew);
    return Vf;
end function;