HP Forums
A workaround for the inefficient SUBST command - Printable Version

+- HP Forums (https://archived.hpcalc.org/museumforum)
+-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html)
+--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html)
+--- Thread: A workaround for the inefficient SUBST command (/thread-245915.html)



A workaround for the inefficient SUBST command - peacecalc - 07-02-2013

For all HP 50g users,

The SUBST command works only correct for substituting names, but not for expressions, like "X=SIN(R*T)" when you have additonal names "R" or "T" in the expression where you want to substitute the "X".

A little program is able to overcome that behavier. I wrote this because of a thread in the hp own calculator forum with that topic.

Thank you pin224466 (that person who started the thread) for clarifying that problem. I have to post here, because I left the hp own forum some time ago. I hope that is okay to go that way.


<< EQ-> 2 ->LIST -> MATCHLST
<< WHILE MATCHLST "UP"MATCH
REPEAT END
>>
>>

'CHGEX' STO

As Input you have same syntax like for SUBST, but ist works now fine without isolating to other names. And even better it can be used with placeholders as "&A"!

For example:

STACK 2:  'COS(3*X+7*SIN(X^2 - R*T))'
STACK 1: 'X=R/T'

CHGEX

STACK 1: 'COS(3*R/T+7*SIN((R/T)^2-R*T))'


Greetings
peacecalc


Re: A workaround for the inefficient SUBST command - Gilles Carpentier - 07-02-2013

Well done ...

But I think


 \<< EQ\-> 2 \->LIST \|^MATCH DROP \>>

is sufficiant ?


Re: A workaround for the inefficient SUBST command - peacecalc - 07-03-2013

Hello Gilles,

your solution is more elegant and shorter and it has the same functionalty. Thank you, for improving the code.

My fault was to think, that the MATCH command only works subtituting the pattern founded on the same level (that make sense for outputting the result code zero or one). But MATCH works in one run even for nested expressions. It seems to be a recurrence program.

Greetings
peacecalc


Re: A workaround for the inefficient SUBST command - Gilles Carpentier - 07-03-2013

The MATCH command is very powerfull. Here is an example to extend the capacity of the CAS with some "sum" functions :

@ Exact mode
@ The system flag 3 must be '->symb'

DIR

EEVAL
\<<
{
'\GS(&A=&B;&C;&D+&E)'
'\GS(&A=&B;&C;&D)+\GS(&A=&B;&C;&E)'
}
\|^MATCH DROP

{
'\GS(&A=&B;&C;&D/&A!)'
'\GS(&A=&B;&C;&D/&A!*1^&A)'
}
\|^MATCH DROP

{
'\GS(&A=&B;&C;&D/&A!*&E^&A)'
'&D*e^&E-1*Somme(&A,&B,&D/&A!*&E^&A)'
'IFTE(&B>=0 AND &C== \oo AND NOT(Dép?(&D,&A)) AND NOT(Dép?(&E,&A)),1.,0.)'
}
\|^MATCH DROP

EVAL
\>>

Dép?
\<<
\-> f v
\<<
f LNAME NIP IF DUP TYPE 5. <> THEN AXL END
v POS
\>>
\>>

Somme
\<<
\-> n b f
\<<
0
IF 'b>0' THEN
0 b 1 - FOR bb f n bb = SUBST + EVAL NEXT
END
\>>
\>>
END

If you use EEVAL instead of EVAL, the 50G now knows that :

'\GS(n=0, +\oo, (5/n!)*sin(x)^n)'
->
'5.e^sin(x)'

'\GS(n=0,+\oo,b/n!*X^n+1/(n+1)^2)'
->
'(6*b*EXP(X)+PI^2)/6'

'\GS(n=3,+\oo,1/n!*SIN(X)^n)'
->
'-((EXP(2*LN(SIN(X)))-(2*EXP(SIN(X))-(2*SIN(X)+2)))/2)'

'\GS(n=5,+\oo,b/n!*X^n+1/(n+1)^2)' EEVAL EXPAND
->
'-((1800*b^2*X^2+600*b^2*X^3+150*b^2*X^4-(3600*b*EXP(X)+600*‡^2-(3600*b^2*X+3600*b^2+5269)))/3600)'

'\GS(n=0, +\oo, (5/n!)*sin(n)^n)'
-> ?

Edited: 3 July 2013, 5:22 p.m.