HP Forums
How can I define cas function of an operator(mathematical term)? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: How can I define cas function of an operator(mathematical term)? (/thread-20442.html)



How can I define cas function of an operator(mathematical term)? - Aleksandr94 - 09-02-2023 03:29 PM

I have hp prime v2. I want to define operator, let's say I want to create user defined CAS operator, that computes indefinite integral: limit(int(Y,X,-A,A),-infinity,+infinity).
1) I tried to do this trough shift+ define with no success.
I was able to obtain correct integrals by using CAS.int(Y,X), but was inable to compute limits via CAS.limit(Y, X). Because it was impossible to obtain right result for limit, I was inable to obtain correct indefinite integral.
2) I tried to do this thought shift + program. I was unable to compute integral.
I used
EXPORT MYFUNC(x,y);
RETURN CAS(int(y,x));
3) I also tried to do this thought
# cas;
myfunc(x,y):=
BEGIN
int(y,x);
Result it gave was incorrect.
How can I do this?


RE: How can I define cas function of an operator(mathematical term)? - robmio - 09-02-2023 04:37 PM

(09-02-2023 03:29 PM)Aleksandr94 Wrote:  I have hp prime v2. I want to define operator, let's say I want to create user defined CAS operator, that computes indefinite integral: limit(int(Y,X,-A,A),-infinity,+infinity).
1) I tried to do this trough shift+ define with no success.
I was able to obtain correct integrals by using CAS.int(Y,X), but was inable to compute limits via CAS.limit(Y, X). Because it was impossible to obtain right result for limit, I was inable to obtain correct indefinite integral.
2) I tried to do this thought shift + program. I was unable to compute integral.
I used
EXPORT MYFUNC(x,y);
RETURN CAS(int(y,x));
3) I also tried to do this thought
# cas;
myfunc(x,y):=
BEGIN
int(y,x);
Result it gave was incorrect.
How can I do this?

Try this:

Code:

#cas
MYFUNCTION(fnz,a,b):=
BEGIN
LOCAL fnz1;
vr:=lname(fnz)[1];
fnz1:=(vr)→int(fnz(vr),vr,a,b);
RETURN fnz1(vr);
END;
#end

For example:

MYFUNCTION((x)->2*x-x^2/15,0,30) equal to 300


RE: How can I define cas function of an operator(mathematical term)? - Aleksandr94 - 09-03-2023 09:03 AM

Than you for the reply!
I wanted to be able to choose integration variable explicitly.
So I tried
#CAS
MYINT(f,x):=
BEGIN
LOCAL fnz1;
vr1:=lname(f);
fnz1:=(vr1)->int(f,x);
RETURN fnz1(vr1);
END;
#end
Most likely HpPPPL mixes up "x" variables. So I need the way to tell him that "x" must be from MYINT(f,x).
These cas function didn't gave the correct result.


RE: How can I define cas function of an operator(mathematical term)? - Albert Chan - 09-03-2023 12:58 PM

(09-02-2023 04:37 PM)robmio Wrote:  vr:=lname(fnz)[1];
fnz1:=(vr)→int(fnz(vr),vr,a,b);
RETURN fnz1(vr);

If fnz is function of 1 variable, we don't need parameter's name (it is just a dummy)
I think above is equivalent to this:

LOCAL vr;
RETURN int(fnz(vr), vr, a, b);


RE: How can I define cas function of an operator(mathematical term)? - robmio - 09-03-2023 01:25 PM

(09-03-2023 12:58 PM)Albert Chan Wrote:  
(09-02-2023 04:37 PM)robmio Wrote:  vr:=lname(fnz)[1];
fnz1:=(vr)→int(fnz(vr),vr,a,b);
RETURN fnz1(vr);

If fnz is function of 1 variable, we don't need parameter's name (it is just a dummy)
I think above is equivalent to this:

LOCAL vr;
RETURN int(fnz(vr), vr, a, b);

It is true, we don't need parameter's name.

However, an easier way to program the function could be as follows:

Code:

#cas
MYFUNCTION(fnz,a,b):=
BEGIN
LOCAL x;
RETURN int(fnz(x),x,a,b);
END;
#end

To prevent the function variable from being stored in "CAS Vars" I choose "x" as the local variable. kind regards, Roberto


RE: How can I define cas function of an operator(mathematical term)? - Aleksandr94 - 09-03-2023 05:51 PM

Thank you for replies!
There is one issue with answers you gave. I most practical cases functions are multivariable. In most cases we don't know the name of variable. So I don't know wether it will be x or other variable. Say l asked this question with intention to define normalisation operator for wavefunctions. I also want to define several other operators like various Hamiltonian operators and so on. So I started with this baby example of indefinite integral. To solve this question I should be able to solve at least any arbitrary integral from CAS. So we stopped there.
So I am asking to help me construct user defined cas function for an mathematical operator (say integration), where we can insert any arbitrary variables.
I tried this:
#CAS
MYINT(f,x):=
BEGIN
RETURN int(f,x);
END;
#end

So I tried this new cas function in CAS mode
MYINT(y^2,y)
And I have obtained this:
Int(f(x)dx)


RE: How can I define cas function of an operator(mathematical term)? - Albert Chan - 09-03-2023 06:48 PM

HP Prime cannot generate unique symbol, like lisp gensym. (correct this if I was wrong ...)
Here, we use a fixed-name global symbol as a bridge.

If symbol is then used for other purpose, we may get into trouble.
See thread, HP Prime undef for Limes. Maybe a Bug?

Code:
#CAS
MYINT(f,x) :=
BEGIN
subst(int(f(x=x_), x_), x_=x);
END;
#END

CAS> MYINT(x*sin(y), x)      → 1/2*x^2*sin(y)
CAS> MYINT(x*sin(y), y)      → -x*cos(y)
CAS> MYINT(x*sin(y), z)      → x*z*sin(y)

Update: removed ASSUME(x_, symbol).


RE: How can I define cas function of an operator(mathematical term)? - Aleksandr94 - 09-04-2023 02:08 PM

(09-03-2023 06:48 PM)Albert Chan Wrote:  HP Prime cannot generate unique symbol, like lisp gensym. (correct this if I was wrong ...)
Here, we use a fixed-name global symbol as a bridge.

If symbol is then used for other purpose, we may get into trouble.
See thread, HP Prime undef for Limes. Maybe a Bug?

Code:
#CAS
MYINT(f,x) :=
BEGIN
subst(int(f(x=x_), x_), x_=x);
END;
#END

CAS> MYINT(x*sin(y), x)      → 1/2*x^2*sin(y)
CAS> MYINT(x*sin(y), y)      → -x*cos(y)
CAS> MYINT(x*sin(y), z)      → x*z*sin(y)

Update: removed ASSUME(x_, symbol).
Thank you for the reply!
I have aso tried something like

#CAS
NORM1(fn,a,b):=
BEGIN
LOCAL x
RETURN int(fn(x=x_),x_,a,b);
END;
#end
And tis didn't work.
I also tried this.

#CAS
NORM1(fn,a,b):=
BEGIN
LOCAL x
RETURN int(fn(x),x,a,b);
END;
#end
And this also didn't work.

How do I find this, assuming that there may be several variables and variable of integration is unknown?
It is a bit dissaponying thing about this calculator, that one just can't define mathematical operators by simply writing some formula. It is dissapoinnting, that there is no good queck start manual about defining user defined CAS functions. I think that a user defined function is the most important feature of calculator like this.


RE: How can I define cas function of an operator(mathematical term)? - Albert Chan - 09-04-2023 03:40 PM

(09-04-2023 02:08 PM)Aleksandr94 Wrote:  How do I find this, assuming that there may be several variables and variable of integration is unknown?

It may be best not to "find this" yourself, whatever it is.
Just show with a concrete example, and what is needed.


RE: How can I define cas function of an operator(mathematical term)? - roadrunner - 09-04-2023 04:59 PM

Aleksandr94,

Try this:

Code:
#cas
MYINT(f,var):=
BEGIN
 local fnc;
 fnc:="int(" + f + "," + var + ")";
 RETURN expr(fnc);
END;
#end

I "think" this is what you are looking for.

-road


RE: How can I define cas function of an operator(mathematical term)? - Aleksandr94 - 09-04-2023 04:59 PM

(09-04-2023 03:40 PM)Albert Chan Wrote:  
(09-04-2023 02:08 PM)Aleksandr94 Wrote:  How do I find this, assuming that there may be several variables and variable of integration is unknown?
It may be best not to "find this" yourself, whatever it is.
Just show with a concrete example, and what is needed.
Thank you for the reply.
I would like define several quantum mechanical operators as CAS functions.
First of all I want to define normalization operator for wavefunction with single coordinate variable.
Operator is like this: f(x)/int(f(x)f(x)^*,x, a, b). This operator converts a wavefunction to its normalized variant.
I also would like to define eigenvalue operator for differential equations.
I thought that it is batter to start with baby examples, so I asked about infinite integral, indefinite integral and definite integral.
I hope, that if enough examples of CAS UDF-s will be shown, one will be able to write any other quantum mechanical operator.


RE: How can I define cas function of an operator(mathematical term)? - Albert Chan - 09-04-2023 06:26 PM

Code:
NORM1(f,a,b) :=
BEGIN
LOCAL c := int(f(¬x)*conj(f(¬x)), ¬x, a, b);
unapply( f(¬x)/sqrt(c), ¬x);
END;

Cas> NORM1(t -> sin(2*pi*t), 0, 1)

(¬x) -> sin(2*π*¬x)/√2*2

Cas> NORM1((x)->x*(LL-x),0,LL)

(¬x) -> ¬x*(LL-¬x)/√(LL^5/30)

Examples taken from video, How to Normalize a Wave function in Quantum Mechanics

Update:
CyperAngel had suggested "¬x", to reduce chances of name conflict.
If variables used always start with alphabet, we are safe.