# Revision history [back]

### "Replacing" function calls in special cases

I'm currently trying to implement some number field computations on a very particular number field (specifically, $K = \mathbb{Q}(\zeta_{n-1},n^{1/(n-1)})$). The code I've written so far has some bottlenecks that profiling via %prun has identified, namely:

1. {method '_nf_rnfeq' of 'sage.libs.cypari2.gen.gen' objects}, which seems to compute the minimal polynomial of $K$ (currently I'm defining $K$ via F.<u> = CyclotomicField(n-1), and then K.<a> = F.extension(x^(n-1) - n), so $K$ is a relative extension. _nf_rnfeq seems to compute the absolute minimal polynomial of a given relative extension (via PARI).

2. {method 'discriminant' of 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint' objects}, which appears to just be the polynomial discriminant computation.

Say that I'm able to find the minimal polynomial for $K$, $f(x)$, and the discriminant for this, $\Delta(f)$. How could I go about "redirecting" these calls to the (slow, general) methods to my specific method?

### "Replacing" function calls in special cases

I'm currently trying to implement some number field computations on a very particular number field (specifically, $K = \mathbb{Q}(\zeta_{n-1},n^{1/(n-1)})$). The code I've written so far has some bottlenecks that profiling via %prun has identified, namely:

1. {method '_nf_rnfeq' of 'sage.libs.cypari2.gen.gen' objects}, which seems to compute the minimal polynomial of $K$ (currently I'm defining $K$ via F.<u> = CyclotomicField(n-1), and then K.<a> = F.extension(x^(n-1) - n), so $K$ is a relative extension. _nf_rnfeq seems to compute the absolute minimal polynomial of a given relative extension (via PARI).

2. {method 'discriminant' of 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint' objects}, which appears to just be the polynomial discriminant computation.

Say that I'm able to find the Assume I've done analysis by hand gives me a much faster computation of the absolute minimal polynomial for $K$, $f(x)$, and the discriminant for this, $\Delta(f)$. How could I go about "redirecting" these calls to the (slow, general) methods to this particular number field. I'd then want a version of _nf_rnfeq that:

1. Checks if it's being called on my specific method?

special case, where better algorithms exist. If so, run that.
2. Otherwise, run the traditional algorithm.

Of course, I'd prefer to make this modification without modifying the source code, as that seems to be an especially ugly solution.

One way to implement this is to change is by modifying the relevant method at run-time, via a technique known as "Monkey-Patching" (see here).

The issue I'm having now is that attempting to monkey-patch the PARI call gives me the error:

TypeError: can't set attributes of built-in/extension type 'sage.libs.cypari2.gen.gen'

The code I'm using is below:

import sage.libs.cypari2.gen
orig_nf_rnfeq = sage.libs.cypari2.gen.gen._nf_rnfeq
def _nf_rnfeq(*args, **kwargs):
print("Rnfeq works!")
return orig_nf_rnfeq(*args, **kwargs)

sage.libs.cypari2.gen.gen._nf_rnfeq = _nf_rnfeq


### "Replacing" function calls in special cases

I'm currently trying to implement some number field computations on a very particular number field (specifically, $K = \mathbb{Q}(\zeta_{n-1},n^{1/(n-1)})$). The code I've written so far has some bottlenecks that profiling via %prun has identified, namely:

1. {method '_nf_rnfeq' of 'sage.libs.cypari2.gen.gen' objects}, which seems to compute the minimal polynomial of $K$ (currently I'm defining $K$ via F.<u> = CyclotomicField(n-1), and then K.<a> = F.extension(x^(n-1) - n), so $K$ is a relative extension. _nf_rnfeq seems to compute the absolute minimal polynomial of a given relative extension (via PARI).

2. {method 'discriminant' of 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint' objects}, which appears to just be the polynomial discriminant computation.

Assume I've done analysis by hand gives me a much faster computation of the absolute minimal polynomial for this particular number field. I'd then want a version of _nf_rnfeq that:

1. Checks if it's being called on my special case, where better algorithms exist. If so, run that.
2. Otherwise, run the traditional algorithm.

Of course, I'd prefer to make this modification without modifying the source code, as that seems to be an especially ugly solution.

One way to implement this is to change is by modifying the relevant method at run-time, via a technique known as "Monkey-Patching" (see here).

The issue I'm having now is that attempting to monkey-patch the PARI call gives me the error:

TypeError: can't set attributes of built-in/extension type 'sage.libs.cypari2.gen.gen'

The code I'm using (in the first cell of my IPython notebook) is below:

import sage.libs.cypari2.gen
orig_nf_rnfeq = sage.libs.cypari2.gen.gen._nf_rnfeq
def _nf_rnfeq(*args, **kwargs):
print("Rnfeq works!")
return orig_nf_rnfeq(*args, **kwargs)

sage.libs.cypari2.gen.gen._nf_rnfeq = _nf_rnfeq


### "Replacing" function calls in special cases

I'm currently trying to implement some number field computations on a very particular number field (specifically, $K = \mathbb{Q}(\zeta_{n-1},n^{1/(n-1)})$). The code I've written so far has some bottlenecks that profiling via %prun has identified, namely:

1. {method '_nf_rnfeq' of 'sage.libs.cypari2.gen.gen' objects}, which seems to compute the minimal polynomial of $K$ (currently I'm defining $K$ via F.<u> = CyclotomicField(n-1), and then K.<a> = F.extension(x^(n-1) - n), so $K$ is a relative extension. _nf_rnfeq seems to compute the absolute minimal polynomial of a given relative extension (via PARI).

2. {method 'discriminant' of 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint' objects}, which appears to just be the polynomial discriminant computation.

Assume I've done analysis by hand gives me a much faster computation of the absolute minimal polynomial for this particular number field. I'd then want a version of _nf_rnfeq that:

1. Checks if it's being called on my special case, where better algorithms exist. If so, run that.
2. Otherwise, run the traditional algorithm.

Of course, I'd prefer to make this modification without modifying the source code, as that seems to be an especially ugly solution.

One way to implement this is to change is by modifying the relevant method at run-time, via a technique known as "Monkey-Patching" (see here).

The issue I'm having now is that attempting to monkey-patch the PARI call gives me the error:

TypeError: can't set attributes of built-in/extension type 'sage.libs.cypari2.gen.gen'

The code I'm using (in the first cell of my IPython notebook) is below:

import sage.libs.cypari2.gen
orig_nf_rnfeq = sage.libs.cypari2.gen.gen._nf_rnfeq
def _nf_rnfeq(*args, **kwargs):
print("Rnfeq works!")
return orig_nf_rnfeq(*args, **kwargs)

sage.libs.cypari2.gen.gen._nf_rnfeq = _nf_rnfeq