Post Reply 
Problem on displaying user defined function as output
11-01-2022, 06:24 AM
Post: #1
Problem on displaying user defined function as output
Hello! I am new to HP Prime and I start to explorer programming on HP Prime.

With the aid of Stirling numbers of the second kind, I could expand any polynomials in x in terms of the falling factorial (x^n) and the output as a list of the corresponding coefficients in ascending order. I had also implement the falling factorial as a function FALL(n). Here is the problem, any chance that I could refine the program such that the output could be like " 5+3*FALL(1)-2*FALL(2)" instead of a list {5 3 -2}. Many thanks!
Find all posts by this user
Quote this message in a reply
11-02-2022, 08:20 PM (This post was last modified: 11-02-2022 09:20 PM by ftneek.)
Post: #2
RE: Problem on displaying user defined function as output
Assuming the coefficients are stored in L0, you could try this:
Code:

return Σ(L0(I)*FALL(I-1),I,1,DIM(L0));

It may need to be a cas program to give a symbolic result. Let me know if it works

- neek
Find all posts by this user
Quote this message in a reply
11-03-2022, 09:09 AM
Post: #3
RE: Problem on displaying user defined function as output
(11-02-2022 08:20 PM)ftneek Wrote:  Assuming the coefficients are stored in L0, you could try this:
Code:

return Σ(L0(I)*FALL(I-1),I,1,DIM(L0));

It may need to be a cas program to give a symbolic result. Let me know if it works

Thank you for replying, I have tried similar idea but as FALL(n) has been implemented, it would directly simplify to some products like: FALL(4,x) ---->x*(x-1)*(x-2)*(x-3).

So my idea is to use string manipulation, may be a bit clumsy as follows:
Code:

#cas
//convert a list of coeff with unknown x into a string of FALL(n,x)
coeff2fall(coeff,x):=
BEGIN
LOCAL Q, n;
Q:="";
n:=length(coeff);
FOR K FROM 0 TO n-1 STEP 1 DO
CASE
IF coeff[K+1]>0 THEN Q:= Q + "+" + coeff[K+1] + "*FALL(" + K + "," + x + ")" END;
IF coeff[K+1]<0 THEN Q:= Q + coeff[K+1] + "*FALL(" + K + "," + x + ")" END; 
DEFAULT Q:=Q;
END;
END;
RETURN Q;
END;
#end

The result is good,
input: COEFF2FALL(x,{0,1,7,6,1})
output:"+1*FALL(1,x)+7*FALL(2,x)+6*FALL(3,x)+1*FALL(4,x)"

The only downside is the additional quotation marks, expr is need to further use. I am not sure if the output could be produced without "".
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)