1 | initial version |
Hello, @ortollj! You have to define two functions: one to take care of the numeric evaluation, and the other for taking care of the LaTeX representation. The important thing is that you LaTeX output function must return a LatexExpr
string. Let me explain.
First, we define the numeric evaluation function:
def general_falling_factorial_numeric(self, x, j, n, parent=None, algorithm=None):
r""" to fill up """
if (j < 0) or (n < 0):
raise RuntimeError('j and n should be nonnegative!')
return int(prod(x-k*j for k in (0..n-1)))
Notice the signature of the function? I have used self
, parent=None
and algorithm=None
. I am not completely sure why these are needed in this case of defining symbolic functions, but they are. Maybe you could post a question about this if you are interested.
Now, we define the LaTeX function:
def general_falling_factorial_latex(self, x, j, n):
return LatexExpr('{' + '('+str(x) + ' | ' + str(j) + ')'+'}_{' + str(n) + '}')
Notice that this function doesn't only return a string, but it encloses it in a LatexExpr
type, which is what is rendered as LaTeX output by the show
function (below). If you don't use that, you will get only a non-LaTeX string.
Finally, we have to put both functions under the same name:
general_falling_factorial = function('general_falling_factorial', nargs=3, evalf_func=general_falling_factorial_numeric, print_latex_func=general_falling_factorial_latex)
This new function is called general_falling factorial
, and you can call it in your code by that name. Besides defining a name for the function, I have indicated that it takes three arguments with nargs=3
. The argument evalf_func
specifies what subroutine should be called when you want a numerical result (general_falling_factorial_numeric
, in this case). The argument print_latex_func
specifies what subroutine to call when you want LaTeX output (general_falling_factorial_latex
, in this case).
Now you can use your new function to compute a result like:
general_falling_factorial(2, 1, 1).n()
which returns 2
(I used int
in the first subroutine to return integers only). The .n()
part tells Sage that you want a numeric result, so it calls general_falling_factorial_numeric
.
On the other hand, you can also obtain a LaTeX output with
var('x j n')
general_falling_factorial(x, j, n).show()
which will return $(x | j)_n$. The .show()
part indicates Sage that you want a LaTeX output, so it has to call general_falling_factorial_latex
.
Finally, if you don't use .n()
nor .show()
, you will get a purely symbolic result:
general_falling_factorial(2, 1, 1)
returns general_falling_factorial(2, 1, 1)
.