Twist-minimal trace formula for holomorphic cusp forms

We derive an explicit formula for the trace of an arbitrary Hecke operator on spaces of twist-minimal holomorphic cusp forms with arbitrary level and character, and weight at least 2. We show that this formula provides an efficient way of computing Fourier coefficients of basis elements for newform or cusp form spaces. This work was motivated by the development of a twist-minimal trace formula in the non-holomorphic case by Booker, Lee and Strömbergsson, as well as the presentation of a fully generalised trace formula for the holomorphic case by Cohen and Strömberg.


Introduction
Modular forms play a central role in much of modern number theory. Their properties have been crucial in establishing the modularity theorem (in turn proving Fermat's last theorem, see [1]), and the Fourier coefficients of modular forms provide explicit data for problems such as the congruent number problem, representations of integers by quadratic forms, and classifications of Galois representations (see [2] for an overview of several applications).
Cusp forms are of particular interest for many reasons. For example, cusp forms give rise to holomorphic L-functions (which in turn account for all L-functions arising from elliptic curves over Q) and are associated with families of irreducible -adic Galois representations following work by Deligne and Serre (see [3,Sect. 9.6]).
While there exist simple, explicit expressions for Fourier coefficients of Eisenstein series, such as in [4], basis elements for spaces of cusp forms are trickier to compute. In this paper we derive a formula for the trace of Hecke operators on spaces of so-called 'twist-minimal' forms (Theorem 1), and demonstrate that this is sufficient to recover the Fourier coefficients of basis elements of cusp form spaces (Theorem 2). We also give a result enabling the extraction of Fourier coefficients of newforms from twist-minimal representatives (Theorem 3).
The value of this paper is twofold. Firstly, the formula is given in an explicit, classical form, and hence the terms are readily computable. Secondly, the sieving of the formula to twist-minimal spaces results in much simpler expressions to compute these spaces, and allows us to calculate a smaller number of twist-minimal spaces in order to recover a large number of different cusp form spaces at once. This efficiently provides explicit data for aforementioned problems on representation of integers and Galois representations. The results can also be incorporated into Schaeffer's method for computing weight-1 spaces, given in [5]. Further work implementing the results of this paper for the efficient computation of all forms of weight 1 and level at most 10,000 is currently in progress. A full explanation and discussion of the advantages of this method is given at the end of Sect. 2.
The idea of using trace formulae to derive information on modular forms dates back to the 1950s. Following the construction of certain non-holomorphic cusp forms, now called Maass cusp forms, by Maass in [6], Selberg sought to demonstrate the infinitude of such forms and provide an asymptotic expression for their count, which he achieved with a trace formula in [7]. He and Eichler continued work in this direction, leading to a trace formula for the action of Hecke operators T n on spaces of holomorphic cusp forms for the full modular group and square-free n in [8]. Following work by others in [9] and [10], this formula is now available in full generality for traces of any Hecke operator on spaces of any level and character. Although this formula can be used to find Fourier coefficients for basis elements of cusp form spaces (as done in [11], with a newform sieve, for the generation of LMFDB data), our formula contains much simpler expressions, and is more efficient for this task in the case that the level N is not square-free.
On the structural side, twists of modular forms were studied in [12] and the concept of a twist-minimal form 1 was explored. In [13], Palm presented an adèlic trace formula for twist-minimal forms. This approach was also taken in the classical context in [14] for Hecke operators T ±1 and Maass cusp forms. The pathway between the adèlic and classical contexts is shown in [15]. This motivates us to use the structural idea of restricting to twist-minimal forms in a classical setting for holomorphic cusp forms. The result will be a computable analogue of the adèlic formula in [13].
The structure of the paper is as follows. Section 2 covers the required background theory leading to the presentation of two results. The formula for the trace of Hecke operators on spaces of twist-minimal modular forms is given in Theorem 1, and the extraction of basis elements for twist-minimal, newform or cusp form spaces from this trace formula is given in Theorem 2. A formula for the Fourier coefficients of an arbitrary newform given Fourier coefficients of a suitable twist-minimal form is given in Theorem 3. The subsequent two sections explain how these theorems are arrived at, with Sect. 3 addressing the structural theory, and Sect. 4 deriving the trace formula. Finally, in Sect. 5 we give three examples of practical computations using these results.

Preliminary theory
For any N ∈ N, a Dirichlet character χ is a homomorphism: The domain of χ is extended to Z by the canonical mod-N homomorphism, with χ(x) = 0 whenever (N, x) = 1. The number N in this construction is called the level of the character. The conductor of If f(χ) = N then we say χ is primitive. For any N , if f(χ) = 1 then we say χ is trivial mod N . This character will be written as 1 with the level taken from context. Dirichlet characters factor over primes in the following sense. Let ν p (x) be the maximum a ∈ Z ≥0 such that p a | x. For all p | N there exists a unique χ p with level p ν p (N ) such that: We use this factorisation to define χ p for any prime p | N . The order of a non-trivial character is the least n ∈ N such that χ n = 1. This is denoted Ord(χ). We define twistminimal characters as in [14,Definition 1.5].
In general, we define a twist-minimal character χ as a product χ p where all of the χ p are twist-minimal.
This completes the prerequisites on Dirichlet characters, and we move onto modular forms. Let H be the Poincaré upper half plane H = z ∈ C : (z) > 0 . There exists an action on H by Möbius transformations with notation: For any complex-valued function f the weight k slash action of γ = a b c d ∈ GL 2 (R) + on f is defined as: For any N ∈ N, the congruence subgroup Γ 0 (N ) is defined to be the following subgroup of 2 (Z): Fix k, N ∈ N and fix χ a Dirichlet character mod N . A modular form of weight k, level N and character χ is a complex-valued, holomorphic function f , defined on H, which satisfies: is polynomially bounded as (z) → ∞ for all γ ∈ 2 (Z).

Modular forms have Fourier expansions:
f (z) = n≥0 a n e 2π inz . (2.6) Such an expansion completely describes the form, and hence it is these a n coefficients which we seek to compute. All modular forms of weight k, level N and character χ constitute a vector space, denoted M k (N, χ). An important family of operators on this space are the Hecke operators. We follow the definition and notation of [16,Sect. 6.5]. Let f be a modular form of weight k. For any n ∈ N we define the generic Hecke operator 2 as: Hecke operators are commutative and multiplicative, so that T χ nm = T χ n T χ m whenever (n, m) = 1. We give an explicit expression for the trace of these operators on subspaces of modular forms, and we show that these traces can be used to generate basis elements.
A cusp form is a modular form for which f | k γ (z) → 0 as (z) → ∞ for all γ ∈ 2 (Z). Cusp forms of weight k, level N and character χ form a subspace of modular forms, denoted S k (N, χ). This subspace is stable under the action of Hecke operators, and is a Hilbert space with respect to the Petersson inner product, defined as: This inner product is used to decompose the space of cusp forms. Fix M|N and d| N M , then it is verified that for any level M cusp form f the function g(z) = f (dz) is a level N cusp form with the same weight and character as f . Forms of level N which arise in this manner from any level M < N are called oldforms, and the orthogonal complement (with respect to the Petersson inner product) of the space spanned by oldforms is called the newform space. It is denoted S new k (N, χ), and was studied in detail in [17] and [12]. The utility of this decomposition is that one can recover data on cusp form spaces solely from data on newform spaces, and thus the scope of any study into cusp forms is reduced.
A cusp form which is an eigenfunction for all Hecke operators is called a Heckeeigenform. Many results about the corresponding Hecke-eigenvalues are given in [12] and [18]. We say that two Hecke-eigenforms in S k (N, χ) are equivalent if they have the same eigenvalues for all but finitely many T p . We thus normalise these forms by setting their first Fourier coefficient to 1. For a normalised Hecke-eigenform f , the eigenvalue of T χ n is equal to the n-th Fourier coefficient of f . By [17,Lemma 18] there is a basis for S new k (N, χ) consisting of representatives from distinct equivalence classes of Hecke-eigenforms. In the other direction, by [17,Lemmas 21,22], every Hecke-eigenform of a given level N is equivalent to a normalised newform of some level M | N .
We define the twist of a form f by a Dirichlet character ψ by the following action on its Fourier expansion: ψ(n)a n e 2π inz . (2.9) We observe the following interaction between twists and Hecke operators.
Lemma 1 Let f be a Hecke-eigenform of weight k, level N and character χ. Let ψ be a character of level M. If (M, n) = 1 then T This relation, first stated in [12], is checked by expanding out the definitions of the operators involved. We also verify that f ψ is a cusp form with character χψ 2 . This means that the twist of any Hecke-eigenform is equivalent to a newform. We can use twists to pass between Hecke-eigenforms of different levels and characters. We define a twist-minimal form as a normalised newform which is not equivalent to any form arising as the twist of a newform from a lower level. Twist-minimal forms of weight k, level N and character χ again span a Hilbert space, stable under Hecke operators, which we denote S min k (N, χ). As in the newform decomposition, the immediate utility here is from further restriction to the scope of study.

Results
It will be shown in Theorem 2 that basis elements for subspaces of cusp forms (given as Fourier expansions) can be computed from the trace of Hecke operators acting on spaces of twist-minimal forms with twist-minimal character. Our first result is an explicit formula for this trace.
where the terms are defined as follows: In all terms, e = ν p (N ) and s = ν p (f(χ)) for a given prime p, and δ is the characteristic function of the condition underneath it.
We give details of each term in this formula. In C 1 , χ f is the primitive character which induces χ, φ(n) is Euler's totient function, the size of (Z/nZ) * , and χ f ( √ n) is 0 if n is not square. In C 3 we define α = ν p (n/d − d) and the dash on the sum indicates that we have an additional factor of 1 2 In C 2 , ρ is a root of the polynomial x 2 − tx + n. We define d as the fundamental discriminant of t 2 − 4n, and ∈ N such that t 2 − 4n = d 2 . The functions h and w are the class number and roots of unity of a quadratic extension with that fundamental discriminant. For the S min p terms, set v = ν p (t 2 − 4n), and let · p denote the Kronecker symbol, then if p > 2, (p, n) = 1 and s < e we have: (2.12) If p > 2, (p, n) = 1 and s = e we have: (2.13) If p = 2, (p, n) = 1, s < e and e > 2, then S min 2 (2 e , χ, t, n) = 0 if v < e − 1, and otherwise we have: else. (2.14) If p = 2, (p, n) = 1 and e ≤ 2 we have: If p = 2, (p, n) = 1 and e = s we have: In all cases, u ≡ √ d is defined mod p e if p > 2 and p e+2 if p = 2. Finally, we have: Section 4 constitutes the derivation of this formula.
Definition 2 Fix a weight k, level N and character χ. The trace form of S k (N, χ), denoted T N,χ , is given by the following series on z ∈ H: The trace forms T new N,χ and T min N,χ are defined similarly by traces on S new k (N, χ) and S min k (N, χ) respectively.
We can extract basis elements for S min k (N, χ) from its trace form by the following lemma. Proof Let f i be a normalised Hecke-eigenform basis for S min k (N, χ), where the i-th element has n-th Fourier coefficient a i,n . Then we have: Further, if we let B denote the matrix with columns given by f i coefficients, then M = BB . Therefore, the ranks of B and M are equal.

Definition 3 Fix a level N and a character χ.
A 'twist pair', denoted M, ψ , is a tuple of level M and character ψ satisfying at least one of the following for every prime p: Two twist pairs M 1 , ψ 1 and M 2 , ψ 2 are considered equivalent if M 1 = M 2 and ψ 2 = χψ 1 . Basis elements for spaces of cusp forms can subsequently be generated as using the following result: Theorem 2 Fix a level N and weight k, and let χ be any (not necessarily twist-minimal) character of level N . The space S k (N, χ) has a basis of cusp forms given by:  (N, χ), and fixing ψ = 1 gives a basis for S min k (N, χ), provided χ is twist-minimal. If χ is twist-minimal, then basis elements for any of these spaces are given by letting M and ψ run over inequivalent twist pairs for level N and character χ. If χ is not twist-minimal, then we can generate basis elements from bijection with a twist-minimal space. Further, for any of these spaces, there exists a subset of m ∈ N with m at most the Sturm bound of the space such that the resultant basis matrix has zero nullity.
Finally, we extend Lemma 1 to a result giving the Fourier expansion of a newform whenever an appropriate twist-minimal form is known. ) be a twist-minimal form with Fourier coefficients a n . Let ψ be a primitive Dirichlet character, and let ψ be the primitive character that induces χψ.
for all primes p.
Theorems 3 and 2 are proven at the start of Sect. 3. These results give us an efficient way of computing basis elements of cusp form spaces. The most computationally expensive term in Theorem 1 is C 2 in which the multiplicative function is a simple case statement, and the relevant class numbers can be pre-calculated and stored in a table. This formula is thus simpler to compute than the respective formula for full cusp form spaces, given in (4.2).
Further, if we let d new denote the dimension of a newform space S new k (N, χ), then the complexity of generating a full-rank matrix of leading coefficients of basis elements from the trace formula is O(d 3 new ). On the other hand, letting d min denote the dimension of a particular twist-minimal space S min k (M, χψ 2 ) which twists into S new k (N, χ) then the complexity of computing the same number of coefficients for just these basis elements is O (d min d new ) 3 2 . As a result, as well as the individual terms being easier to compute, the overall complexity for computing spaces of newforms (or, in turn, full cusp form spaces) is reduced by recovering these spaces from twist-minimal spaces.
The restriction that the weight be at least 2 means that weight 1 forms are missed in this approach. 3 This is a meaningful issue as, since the proof of Serre's modularity conjecture in [20], these forms are known to categorise all odd, two-dimensional, irreducible Galois representations over finite fields. The Fourier coefficients of these forms give the traces of Frobenius elements in the associated representations.
The computation presented in [5], however, involves first computing weight 2 basis elements, before manipulating these in a manner to find weight 1 forms. In future work, we will adapt this approach, utilising the benefits of the twist-minimal weight 2 computation presented here to perform more efficient computations of weight 1 forms.

Structural theory
We study the structural theory of cusp forms, which leads to the proof of Theorem 2. In [17, Theorem 5] a decomposition of cusp form spaces is given in terms of newforms and lifts of newforms. To decompose these newform spaces in terms of twist-minimal spaces we draw on previous results on twists of cusp forms. These come from applying the results in [12] to the specific case of twist-minimal forms with twist-minimal character. The multiplicativity present in both the expression for the level of f ψ and the definition of twist-minimal characters will allow us to decompose newform spaces multiplicatively. Fix a level N . For any χ, there exists a character ψ such that χψ 2 is twist-minimal, and twisting by ψ gives a bijection between S min k (N, χ) and S min k (N, χψ 2 ). This is shown in [14,Lemma 1.6], with a construction of the required ψ. As a result, we need only consider twist-minimal spaces with twist-minimal character, and need study only the decomposition of newform spaces with twist-minimal character.
The Fourier coefficients of a newform with arbitrary character can be recovered by applying Theorem 3, which is proven as follows: Proof First, let Q be the product of primes p such that ψ p = χ p , and let ψ Q be the product of the respective ψ p . Twisting by ψ Q is equivalent to applying the Atkin and Lehner W Q operator. The twist f ψ Q is equivalent to a newform of level N with coefficients as in (2.22) by [12, (1.1)].
Denote this newform g, and note that g is itself twist-minimal. This means we can apply Lemma 3 to g ψ N Q , whereupon the rest of Theorem 3 follows.

Explicit decomposition of a specified newform space
In this section, we will identify which twist-minimal spaces are required to obtain all basis elements of any given newform space with twist-minimal character.

Lemma 5
Let p be an odd prime, and χ 2 a twist-minimal character with f(χ 2 ) = p f , then there does not exist any twist from S min k (p e , χ 1 ) to S new k (p f , χ 2 ) with e < f and χ 1 a non-trivial twist-minimal character.
Proof Assume for contradiction that ψ is a character providing such a twist from S min k (p e , . This means e = s which is a contradiction. Alternatively 1 ψ), and hence f(χ 1 ) f(χ 1 ψ) > f(χ 1 ψ 2 ). This means, by Lemma 3 part 2, that the level p f > f(χ 1 ψ 2 ), which contra- Finally, if f( , with the contradiction as before.
While this shows there are no twists into spaces S new , which we enumerate with the following lemma:

Lemma 6
Let p be an odd prime, f an even integer, and χ 2 a twist-minimal character of Having covered all twists when p is odd, we now study p = 2.

Lemma 7
Let χ 1 and χ 2 be twist-minimal characters for 2 e and 2 f respectively with f > e. There does not exist any twist from S min k (2 e , χ 1 ) to S new k (2 f , χ 2 ).
We can summarise the above lemmas to give a decomposition of newform spaces with twist-minimal character into twists from twist-minimal spaces. Let a twist pair be defined as in Definition 3, then we have: with the union being taken over all twist pairs for level N and character χ. Here, the twist notation means that all forms in the space have been twisted by the subscript character. This is sufficient for Theorem 2 but in order to use this decomposition in computations we need it to be disjoint.

Removing twist-equivalent forms
Definition 4 Let S min k (N 1 , χ 2 ψ 2 1 ) and S min k (N 2 , χ 2 ψ 2 2 ) be two spaces in the decomposition given in (3.1). We say that f ∈ S min k (N 1 , χ 2 ψ 2 1 ) and g ∈ S min k (N 2 , Twist-equivalent forms are thus twist-minimal forms which give rise to the same newform. We see immediately that two forms are twist-equivalent only if N 1 = N 2 , and the following lemma lets us consider twist-equivalence as a condition on twist pairs.
In general for level N , we see that two forms are twist-equivalent if and only if this condition holds for all p | N . This allows us to write down a disjoint decomposition. Let M, ψ be as before, and let ∼ denote the relation by this lemma giving rise to twist-equivalence, then we have the decomposition: We now quantify the equivalence classes. This is also done multiplicatively, and we can find the number of elements in an equivalence class based on which case we are in for each p | N in the definition of twist pairs (Definition 3). Proof By following Lemma 8 we see that if ψ p = 1, or χ p = 1 and ψ p = · p , then the pair is in a singleton equivalence class. For all other cases, the lemma gives us a second, distinct twist pair.

Lemma 9 Fix a level N and
The condition that p | N M is equivalent to requiring ψ p = 1. Hence, we have two classes whenever ψ p = · p or χ p = 1. The result then follows from multiplicativity.

Deriving the trace formula
In [21,Theorem 12.4.11] we see the following explicit formula for the trace of a Hecke operator on an arbitrary full cusp form space. The specific details of functions and variables are as in Theorem 1. Fix a level N , character χ and weight k ≥ 2 such that χ(−1) = (−1) k . Then TrT χ n |S k (N, χ) is given by: where the individual terms are given by: N, (f, N ), χ),

(4.2)
In addition to the details from Theorem 1 , the variable x 1 is defined by the congruences x 1 ≡ d (mod c) and x 1 ≡ n/d (mod N /c). These congruences are seen to uniquely fix x 1 modulo f(χ), meaning that the term χ(x 1 ) in A 3 is well-defined.
From the structural theory, we write the cusp form space trace formula in terms of newform spaces, and subsequently in terms of twist-minimal spaces. We then invert these formulae to obtain a formula for the twist-minimal space in terms of full cusp form spaces.
The newform trace is written as a sum over the set: From now on, the notation P will always refer to this set.

Lemma 10
For any m ∈ N, define β m to be a multiplicative function, defined on powers of primes as: Proof We use the following relation from [21,Theorem 13.5.7]: , χ), (4.6) where N 1 is the squarefree component of N (the product of primes p || N ). In [12] we see that if p | n, N f(χ ) with p 2 | N then the p-th Hecke eigenvalue for any form in S new k (N, χ) is 0. Using [17, Theorem 3, (ii)'] for any multiple of p we conclude that TrT where ψ 3 = ψ 1 ψ 2 . Note that: Extending this multiplicatively gives the inverted formula: (4.10) where ω N M is the number of primes dividing N M . We now see that multiplicativity results in the trace formula carry through to the twistminimal trace formula . Let f (N, χ) be a function defined on N ∈ N and χ level N . Fix n, m ∈ N and define f new and f min as: We will show that each component of (4.2) can be written as linear combinations of multiplicative functions. Thus, we construct the twist-minimal trace formula by computing each component's value at each prime for arbitrary twist-minimal character χ. From this, we obtain an expression of the form: We then perform a further, similar sieve on n to deduce an expression of the form: as in Theorem 1.
Due to the definition of β m in Lemma 10, the formulas for p | (N, n) and p (N, n) differ greatly, and we treat them separately. We now derive explicit formulas for the B i terms in both cases, and then use these in turn to derive the C i terms in Theorem 1.

Formula for p (N, n) cases
We start by establishing an explicit formula for f min in terms of f in the case that χ is twist-minimal.
Let δ C be the characteristic function taking 1 when C is true and 0 when C is false. Let χ be a twist-minimal character of level p e and conductor p s , as in Definition 1. From the definition of f new we deduce the relation:  This is what we will refer to as the decomposition formula. We factor a multiplicative function from each term in (4.2), and then apply the decomposition formula to give explicit expressions for B i in (4.13). In this section, when evaluating the multiplicative function at a prime p | N , we will assume that p n, with the cofactor case being handled in the next section. Note that this means we can assume d = 1 in (4.13).
At many points it will be useful to have the following lemma.

Lemma 11
For any prime p and x, a ∈ N we have: (4.17)

A 1 term
This simple case will be useful in demonstrating the general sieving approach. We let R denote the multiplicative factor of the component (which is different for each component, so any specific R will not persist). In (4.2) we have already that: χ p , n), (4.18) where: Note that the following expression in the decomposition formula simplifies: (p e , χ, n). (4.20) This makes it easy to evaluate R min , defined in (4.16). This is separated into cases for each possible twist-minimal character. Applying Lemma 11 to all resultant expressions for R min gives: if s < e, e ≥ 3, Here, φ is Euler's totient function. This is then rewritten more succinctly, giving B 1 in (4.13) as follows: (4.22)

A 3 term
We will focus on the A 3 in (4.2) next, returning to A 2 as the most complicated component later. Again we extract a multiplicative factor R which is distinct from the R of the previous section. We write: (4.23) where, setting α = ν p (n/d − d) we have: Applying the decomposition formula (4.16) to R we see considerable cancellationalthough the expression for R new is quite complicated, the expression for R min is very simple. Let denote the conditions p = 2, 2 | e, α ≥ e 2 − 1 and s < e 2 . Then applying the decomposition formula to R gives: (4.25) Therefore, B 3 is given by: if , 0 e l s e . (4.26) The dash on the sum here holds the same meaning as in the A 3 term in (4.2).

A 4 term
The A 4 term is 0 whenever k > 2. As the weight does not change in the decomposition formula, we can assume that k = 2. We rewrite A 4 as: where: (4.28) Note that for this section we are always in the top case. We again sieve R through the decomposition formula, giving: (4.29)

A 2 term
We follow the work of [22] to rewrite the A 2 term multiplicatively. For t 2 < 4n, write t 2 − 4n = d 2 with fundamental discriminant d and ∈ N. Note that the sum over f 2 |(t 2 − 4n) is equivalent to taking the sum over f | , discarding cases where ν 2 (f ) = ν 2 ( + 1) and 4 | d, in which the sum over x (mod N ) is zero.
The class number formula (see [23]) gives the following: Thus, the A 2 term becomes: (4.31) Let g be a divisor of (N, ). Define the function: where Ω (N, g, t, n) is the set of solutions mod N to x 2 + tx − n ≡ 0 (mod Ng). Using the Chinese Remainder Theorem, we can write Ω as a Cartesian product: Otherwise, if f(χ) ≤ 2 v 2 then J is given by: (4.41) (4.42) Proof We will use the following orthogonality result on Dirichlet characters. Let f(χ) = p s and fix r ∈ N with r < s (and r > 1 if p = 2). Let A = {1 ≤ a < p s : a ≡ 1 (mod p r )}, then A consists of all powers mod p s of a single element g and so A χ(a) = 0. Further, let Assume that p > 2, so that 4 is invertible mod p. By completing the square, the condition x 2 − tx + n ≡ 0 (mod p e+β ) becomes (2x − t) 2 ≡ t 2 − 4n (mod p e+β ). If v ≥ e + β then t 2 −4n ≡ 0 (mod p e+β ) and hence the condition is 2x ≡ t (mod p e+β 2 ). This gives p e−β 2 elements of Ω(p e , p β , t, n). If f(χ) ≤ p e+β 2 then χ(x) is equal for all x ∈ Ω. Otherwise, the orthogonality result above applies, hence the sum is 0.
If v ≥ e +β +2 then Ω is 2 e−β 2 elements, of the form t +kp . If instead we have v < e + β then 2 v means Ω = ∅. Otherwise, let u i be any root of The result follows from summation over these elements together with orthogonality. Now that we have expressions for J we move on to evaluating R. We use the following formula, arrived at through application of the standard geometric series sum. Let p > 2. In the sum defining R we study the cases when e < ν p ( ), ν p ( ) < e ≤ v and v < e separately. In each case, the resultant sum is evaluated using (4.43). We end up with the following summary of all cases. Set h = max(2s − 1, e), and m = p e+ν p ( )−1− h 2 + p e+ν p ( )−1− h−1 2 , then when v ≥ h we deduce the following expression for R p : and when v < h, R p is given by: p ν p (l)+min(e−s,ν p (l)) χ(t/2 + up ν p (l) ) + χ(t/2 − up ν p (l) ) . When p = 2 we again separate into cases depending on the relation between v and h. In each case, we obtain a different expression based on the congruence class of d mod 8. Applying (4.43), we arrive at the following expressions for all cases. If v < h we obtain: If v = max(h, 2s) then R 2 is given by:  Finally, if v > max(h, 2s) then we obtain: (4.48) The expressions for R p are now sieved using the decomposition formula in (4.16). We treat the odd and even prime cases separately, and sieve to R new p first, given by: For other cases with e = 2, we obtain: e l s e . and when p = 2 this is: where u ≡ l √ d (mod 2 e+ν 2 ( )+2 ) 0 e l s e . (4.58)

Formula for p | (N, n)
We now address the case that p divides both n and N , finding expressions for the multiplicative part of each term (which we have denoted R in each case). From Lemma 10 we see that if s < e and e > 1 then the trace is 0. As such, we only consider terms when s = e or e = 1. Noting that β m is now equivalent to the Möbius μ function, we obtain the much simpler decomposition formula: For the A 1 term, if s > 0 then as p | n we necessarily have χ( √ n) = 0. Thus, we only have a non-zero A 1 term when e = 1 and s = 0. In this case, we evaluate R min p (p, χ, n) = −1 and hence:  Finally for the A 4 term we compute: R min p (p, n) = δ s=0 p ν p (n) σ (p ν p (n) ) − 1 . (4.63)

Factorising over n
The preceding subsections allow us to produce a formula in the form of (4.13), where each B i is evaluated on the arguments ( N d , χ, k, n d 2 ). We now incorporate the outer sum by interchanging the sum and product in each term.
For the B 1 term, interchanging gives: Then, using multiplicativity of P as defined in (4.3), we evaluate the inner sum, giving:  where the conditions are p = 2, 2 | e, α ≥ e 2 − 1 ≥ s, 2 n d 2 and e > 2. Noting that if p n then the product is identical for all d, and using multiplicativity of P we obtain: