
// Make newform 648.4.i.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_648_i();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_648_i_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_648_4_i_g();".
// 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_648_4_i_g();".
// 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 := [1, -1, 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_648_i();"
function MakeCharacter_648_i()
    N := 648;
    order := 3;
    char_gens := [487, 325, 569];
    v := [3, 3, 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_648_i_Hecke();"
function MakeCharacter_648_i_Hecke(Kf)
    N := 648;
    order := 3;
    char_gens := [487, 325, 569];
    char_values := [[1, 0], [1, 0], [0, -1]];
    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 := 4;
    raw_aps := [[0, 0], [0, 0], [0, 1], [9, -9], [-17, 17], [0, 44], [-56, 0], [-94, 0], [0, -50], [-30, 30], [0, 139], [-174, 0], [0, 318], [242, -242], [-630, 630], [-547, 0], [0, -236], [-328, 328], [0, -614], [-296, 0], [433, 0], [56, -56], [-1225, 1225], [-1506, 0], [-1391, 1391], [-1665, 1665], [0, -1468], [-627, 0], [610, 0], [0, 1134], [505, 0], [0, 515], [-6, 6], [0, -636], [0, -2239], [2695, -2695], [0, 1988], [-2580, 0], [0, 1878], [-2163, 2163], [-2181, 0], [-1488, 0], [1764, -1764], [0, 3547], [4941, 0], [-1487, 0], [0, -442], [5096, -5096], [5484, -5484], [0, 3530], [-1630, 0], [0, -1990], [-2214, 2214], [4480, 0], [0, 2204], [1942, -1942], [7606, 0], [4391, 0], [-7000, 7000], [8912, -8912], [0, -3614], [0, -4194], [840, 0], [0, 606], [-2037, 2037], [519, -519], [-2750, 2750], [0, -4410], [0, -10911], [4650, -4650], [9410, -9410], [2130, 0], [-1393, 1393], [0, 2920], [-10780, 0], [0, -3888], [4009, -4009], [860, 0], [0, 1716], [0, 3201], [0, 5700], [10312, -10312], [10354, 0], [-10787, 0], [14715, -14715], [-6628, 6628], [-8122, 0], [6553, -6553], [-4079, 4079], [0, 12685], [-6111, 0], [-11254, 11254], [13936, 0], [0, 6045], [0, 19482], [-19770, 0], [0, -11613], [5500, 0], [-8024, 0], [-9036, 0], [-23456, 23456], [-18721, 0], [0, 1417], [-18596, 18596], [0, 3708], [21494, 0], [-16049, 16049], [4658, 0], [0, 26538], [-1147, 1147], [0, -26048], [-394, 0], [0, -27822], [-21580, 21580], [7855, 0], [-30770, 30770], [0, 18420], [-10448, 0], [0, 19221], [29649, -29649], [0, -4478], [14099, -14099], [14782, -14782], [24620, 0], [-30716, 30716], [-3147, 0], [22660, -22660], [-4828, 0], [3849, -3849], [0, -11738], [-26824, 0], [0, -68], [0, 36039], [18650, 0], [0, 18848], [0, 29215], [-26586, 0], [0, -15520], [0, 7477], [25282, 0], [-35582, 0], [37506, -37506], [0, -12185], [8128, 0], [-39542, 0], [40368, -40368], [20194, -20194], [8664, -8664], [0, 15672], [46076, 0], [0, 28926], [10372, 0], [-15502, 0], [0, -29524], [-27226, 27226], [-53510, 53510], [-443, 0], [27260, -27260], [21075, 0], [0, 22749], [36201, -36201], [4428, 0], [0, -26927], [47961, 0], [0, 16746], [37146, -37146], [42079, 0], [-46492, 46492]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_648_i_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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