generate his own symbolic math function
Hi
If I would like to generate my own symbolic math function : general_falling_factorial(x, j, n) . What I should add and modify in the code below ? (I do not know what to do with the two lines below I know it is ok for the first one, but what must I write for the second one ?)
return prod((((x)-(k*(j))) for k in (0..(n-1))), z=x.parent().one())
return prod((((x)-(k*(j))) for k in (0..(n-1))), z=x.parent().one())
j,n=var('j,n')
N=11
show(LatexExpr(r"B_n=\sum_{j=1}^{n+1}\frac{(-1)^{j-1}}{j}{n+1\choose j}\sum_{i=0}^{j-1}(i|j)_n"))
show(LatexExpr(r"\text{with }\, \,(i|j)_n:=i(i-j)\cdots(i-(n-1)j)= \
\text{general_falling_factorial() is not the falling_factorial() SageMath Function} \
\, = \, "),falling_factorial(x, n))
print "https://math.stackexchange.com/questions/497616/is-there-an-explicit-formula-for-the-bernoulli-numbers-that-doesnt-implicitly-r"
print "http://youngp.people.cofc.edu/factbern.pdf"
show(LatexExpr(r"B_n=\sum_{j=1}^{n+1}\frac{(-1)^{j-1}}{j}{n+1\choose j}\sum_{i=0}^{j-1}(i|j)_n"))
show(LatexExpr(r"\text{with }\, \,(i|j)_n:=i(i-j)\cdots(i-(n-1)j)"))
def general_falling_factorial_latex(self, x,j,n):
return '{' + '('+str(x) + ' | ' + str(j) + ')'+'}_{' + str(n) + '}'
general_falling_factorial = function('general_falling_factorial', nargs=3, print_latex_func=general_falling_factorial_latex)
def general_falling_factorial(x, j, n):
r""" to fill up """
from sage.symbolic.expression import Expression
from sage.structure.coerce import py_scalar_to_element
x = py_scalar_to_element(x)
j = py_scalar_to_element(j)
n = py_scalar_to_element(n)
if ((isinstance(j, Integer) or
(isinstance(j, Expression) and
j.is_integer())) and j >= 0) and \
((isinstance(n, Integer) or
(isinstance(n, Expression) and
n.is_integer())) and n >= 0) :
return prod((((x)-(k*(j))) for k in (0..(n-1))), z=x.parent().one())
return prod((((x)-(k*(j))) for k in (0..(n-1))), z=x.parent().one())
def bernoulliExplicit(N) :
bernoulliSL=[]
for n in range(0,N) :
bernoulliSLt=[]
for j in range (1,n+2):
#show("j : ",j)
bernoulliSLt.append((((-1)^(j-1)/j) *(factorial(n+1))/((factorial(j)*(factorial((n+1-j))))))* \
sum([general_falling_factorial(i,j,n) for i in range(0,j)]))
bernoulliSL.append(sum(bernoulliSLt))
return bernoulliSL
show("Explicit Bernoulli List : ",bernoulliExplicit(N))
bernoulliL=[]
for i in range(0,N) :
bernoulliL.append(bernoulli(i))
show("SageMath Bernoulli List : ",bernoulliL)
In my attempt to generate my own general_falling_factorial, I simply copy and slightly modified falling_factorial() .
I suppose it is a naive way ! ;-)
Hello, @ortollj. Could you please define what you mean by "symbolic function"? Concerning the
return
s in your code, the first one and the second one are exactly the same, so theif
statement in the middle of the definition is useless. Also, the use of thez
argument makes it equal to $1$, meaning multiply by $1$, which is useless. On the other hand, thepy_scalar_to_element
function seems to do exactly the same job as the Sage preparser: it transforms42
intoInteger(42)
,2,3
intoRealNumber('2.3')
, etc. Using this function should be unnecessary in 99.9% of the cases. Finally, there are unnecessary parenthesis.I would rewrite the function as follows (see next comment)
Sorry, I run out of space. Here is the code:
I think I did not express myself well, (but maybe my question does not make sense?)
what I asked was simply to get the same displayed thing when I call :
with only symbolics variables x,j,n as if I call
Hello, @ortollj. Sorry about the misunderstanding. I am still not sure, but does this code is what you are looking for? It can evaluate the function numerically, and has a LaTeX representation.
I am deleting my previous answer. If this code is correct, I'll add it as answer.