
// Make newform 121.3.d.d in Magma, downloaded from the LMFDB on 02 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_121_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_121_d_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_121_3_d_d();".
// 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_121_3_d_d();".
// 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, -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_121_d();"
function MakeCharacter_121_d()
    N := 121;
    order := 10;
    char_gens := [2];
    v := [7];
    // 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_121_d_Hecke();"
function MakeCharacter_121_d_Hecke(Kf)
    N := 121;
    order := 10;
    char_gens := [2];
    char_values := [[0, 1, 0, 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, 2, -2, 1], [-2, 3, -3, 2], [0, 0, 4, 0], [-6, 4, -2, 8], [0, 0, 0, 0], [2, 6, -4, 2], [0, -13, 0, 13], [-5, 2, 2, -5], [-4, 0, 2, -2], [18, -10, 2, -20], [-18, 18, 0, -4], [36, -18, 36, 0], [16, 27, 27, 16], [18, -36, 17, -19], [-26, 24, -24, 26], [30, -30, 0, 30], [15, -22, 15, 0], [-4, -14, -2, 18], [-49, 0, 17, -17], [0, 62, -24, 62], [-77, 17, 43, 34], [12, -40, 52, -26], [-90, 47, -45, 43], [72, 0, 83, -83], [-67, 67, 0, 36], [60, 56, 4, -2], [-56, -2, -56, 0], [22, 31, 31, 22], [-30, 60, -98, -38], [19, -36, 36, -19], [104, 24, 52, -128], [-1, 2, 73, 75], [0, -19, 47, -19], [116, -124, 132, -248], [172, -168, 86, -4], [86, -106, -106, 86], [102, 132, -132, -102], [109, -109, 0, -107], [-194, 142, -336, 168], [-58, 62, 62, -58], [-159, 62, -62, 159], [0, 50, -42, 50], [-100, 42, -100, 0], [-184, 216, -92, -32], [-104, 208, -20, 188], [86, 0, 148, -148], [-109, 191, -300, 150], [-224, 156, -156, 224], [-151, 106, -61, 212], [-78, 78, 0, -8], [181, -131, 312, -156], [-116, -94, -94, -116], [-9, 18, -157, -139], [196, -196, 0, 110], [193, 91, 193, 0], [46, -92, 52, -40], [0, -288, 56, -288], [122, -122, 122, -244], [-132, -284, 152, -76], [146, -81, 73, -65], [100, 160, 160, 100], [108, 86, -280, 172], [-46, 92, 113, 205], [308, 92, -92, -308], [0, -321, 188, -321], [-118, 118, 0, 444], [254, 0, -73, 73], [95, -32, -31, -64], [180, -155, 90, -25], [-48, 120, 120, -48], [-77, 0, -69, 69], [-222, 284, -346, 568], [312, -176, 312, 0], [276, -552, 334, -218], [0, 269, -115, 269], [-530, 530, 0, 220], [148, -576, 148, 0], [82, 0, 54, -54], [0, 155, -187, 155], [372, 108, 264, -132], [38, 0, 175, -175], [-408, 108, -108, 408], [-42, -302, 260, -130], [-213, -276, -213, 0], [100, -200, -194, -394], [98, 163, -163, -98], [355, -355, 0, -183], [-170, 5, -85, 165], [-234, 468, -144, 324], [240, 0, -270, 270], [0, -160, 270, -160], [660, -376, 330, -284], [506, -304, 304, -506], [175, -317, 459, -634], [-27, 509, -27, 0], [-352, -24, -24, -352], [300, -396, 396, -300], [231, 324, 231, 0], [444, -265, 222, -179], [86, -178, 264, -132], [117, 318, 318, 117], [478, -260, 42, -520], [497, 765, -268, 134], [17, 112, 112, 17], [-400, 800, -544, 256], [0, 219, -212, 219], [-177, 526, -177, 0], [-11, 22, 297, 319], [0, 336, -240, 336], [229, 119, -467, 238], [646, -510, 1156, -578], [4, -630, -630, 4], [221, 0, -183, 183], [601, -658, 658, -601], [-710, 458, -710, 0], [-716, 625, -625, 716], [0, 127, 739, 127], [-452, 452, 0, 696], [80, -370, 80, 0], [267, -534, 925, 391], [116, 0, 28, -28], [-673, 361, -1034, 517], [-900, 664, -450, 236], [846, 0, 672, -672], [215, -215, 0, 563], [284, 92, 92, 284], [0, 62, -838, 62], [460, -218, 460, 0], [-656, 0, -162, 162], [-172, 308, -444, 616], [-243, 495, -738, 369], [-360, 282, -180, 78], [1296, -1020, 1020, -1296], [326, -326, 0, -1128], [-373, -315, -58, 29], [-456, 912, -372, 540], [-62, 28, -28, 62], [436, -663, 218, 227], [0, -124, -798, -124], [320, -483, 160, 163], [422, 261, 261, 422], [810, -406, 2, -812], [-720, 720, 0, 90], [-101, 273, -374, 187], [366, 224, 366, 0], [-446, 1068, -1068, 446], [-1576, 858, -788, 718], [-68, 136, -767, -631], [67, 0, -667, 667], [0, -604, 572, -604], [-144, -98, -98, -144], [-528, 0, -575, 575], [-822, 803, -803, 822], [-180, -164, 508, -328], [0, -849, -98, -849], [16, -16, 0, -166], [820, 390, 410, -1210], [0, -317, 1448, -317], [447, 577, -130, 65], [-264, -46, -132, 310], [1033, 0, 1211, -1211], [-531, 247, 37, 494], [426, -852, 208, -644], [-218, -622, 622, 218], [28, -28, 0, -174], [-1024, 702, -1024, 0], [-568, 0, -822, 822], [-472, -100, 672, -200]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_121_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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