Post Reply 
challenge HP50G RPL
09-24-2016, 04:02 AM
Post: #1
challenge HP50G RPL
Hello
Reduce the number of orders for the algorithm roots quadratic equation

Code:
«
  @ HALT
  @  1. -3. 2.
  @  1. 2. 3.
  @  A B C
  -4 *      @ Multiplica por -4 el primer nivel → A B -4*C
  PICK3 *   @ copia nivel 3 (A) al nivel 1, y luego lo multiplica con el nivel 2 → A B -4*C A → A B -4*C*A
  OVER SQ + @ copia nivel 2 (B) a 1, lo eleva al cuadrado y suma → A B -4*C*A+B^2
  √         @ saca raíz cuadrada → A B '√(B^2-4*A*C)'
  UNROT     @ Sube al 3cer nivel → '√(B^2-4*A*C)' A B
  SWAP 2 *  @ Invierte y multiplica * 2  → '√(B^2-4*A*C)' B 2*A
  SWAP NEG  @ Invierte y negativo → '√(B^2-4*A*C)' 2*A -B
  DUP2      @ duplica los dos primeros niveles → '√(B^2-4*A*C)' 2*A -B 2*A -B
  5 PICK +  @ copia nivel 5 (√B^2-4*A*C) a 1 y suma → '√B^2-4*A*C' 2*A -B 2*A '-B+√(B^2-4*A*C)'
  SWAP /    @ Invierte y divide → '√B^2-4*A*C' 2*A -B '-B+√(B^2-4*A*C)/2*A'
  4 ROLLD   @ Sube al 4to nivel → '-B+√(B^2-4*A*C)/2*A' '√(B^2-4*A*C)' 2*A -B
  ROT       @ baja el 3cer nivel al 1er nivel → '-B+√(B^2-4*A*C)/2*A' 2*A -B '√(B^2-4*A*C)'
  - SWAP /  @ resta, invierte y divide → '-B+√(B^2-4*A*C)/2*A' '-B-√(B^2-4*A*C)/2*A'
»

the previous code have 26 instructions
Visit this user's website Find all posts by this user
Quote this message in a reply
09-24-2016, 07:36 AM
Post: #2
RE: challenge HP50G RPL
How about two instructions?

« →V3 PROOT »

X<> c
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
09-24-2016, 01:04 PM (This post was last modified: 09-26-2016 01:10 AM by compsystems.)
Post: #3
RE: challenge HP50G RPL
(09-24-2016 07:36 AM)Joe Horn Wrote:  How about two instructions?
« →V3 PROOT »

« →V3 PROOT » 'QUADRATIC1' STO

1. -3. 2. QUADRATIC1 returns [1, 2] // ok
1. 2. 3. QUADRATIC1 returns [(-1.,1.41421356238) (-1., -1.41421356238)] // ok
'A' 'B' 'C' QUADRATIC1 returns "→V3 ERROR" =(

with local vars & 8 instructions
Code:
« 
  → 
  A 
  B 
  C 
  '(-B+√(B^2-4*A*C))/(2*A)' EVAL 
  '(-B-√(B^2-4*A*C))/(2*A)' EVAL  
»
'QUADRATIC0' STO


with 12 instructions
Code:
«
  'R1(A,B,C)=(-B+√(B^2-4*A*C))/(2*A)' DEFINE
  'R2(A,B,C)=(-B-√(B^2-4*A*C))/(2*A)' DEFINE
  3 DUPN R1 EVAL 4 ROLLD R2 EVAL
» 
'QUADRATIC2' STO

/!\ First value, different from zero
1. -3. 2. QUADRATIC2 returns 1 2 // ok
1. 2. 3. QUADRATIC2 returns (-1.,1.41421356238) (-1., -1.41421356238) // ok
(1,0) (-1,-1) (0,1) QUADRATIC2 returns (1, 0) (0, 1)
'a' 'b' 'c' QUADRATIC2 EVAL returns
'(-b+√(c*-4.*a+SQ(b)))/(a*2.)'
'(-b-√(c*-4.*a+SQ(b)))/(a*2.)'


/!\ but the idea is to use the formula '-B±√(B^2-4*A*C)/2*A' with RPN commands (SWAP DUP ROLL ROLLD etc), also without unused local variables or globales and EVAL as in the first post


with the code of firts post
... 'QUADRATIC' STO
1. -3. 2. QUADRATIC returns 1 2 // ok
1. 2. 3. QUADRATIC returns (-1.,1.41421356238) (-1., -1.41421356238) // ok
(1,0) (-1,-1) (0,1) QUADRATIC returns (1, 0) (0, 1)
'a' 'b' 'c' QUADRATIC returns
'(-b+√(c*-4.*a+SQ(b)))/(a*2.)'
'(-b-√(c*-4.*a+SQ(b)))/(a*2.)'



PD: Someone who can port it to RPN HP-PRIME

Thanks
Visit this user's website Find all posts by this user
Quote this message in a reply
09-24-2016, 05:15 PM
Post: #4
RE: challenge HP50G RPL
Code:
« PICK3 / UNROT
  SWAP / -2 / DUP SQ
  ROT - √ DUP2 - 
  UNROT +
»

16 instructions, 50 bytes.
Find all posts by this user
Quote this message in a reply
09-24-2016, 10:20 PM (This post was last modified: 09-25-2016 03:56 PM by compsystems.)
Post: #5
RE: challenge HP50G RPL
It works fine, but it seems you're using another formula? and not '-B±√(B^2-4*A*C)/2*A'

Someone break to lower the Record of Gerson =), SysRPL is accepted

Code:
« 
  PICK3 / UNROT
  SWAP / -2 / DUP SQ
  ROT - √ DUP2 -
  UNROT +
» 
'QUADRATIC3' STO

1. -3. 2. QUADRATIC3 returns 1 2 // ok

1. 2. 3. QUADRATIC3 returns (-1.,1.41421356238) (-1., -1.41421356238) // ok

(1,0) (-1,-1) (0,1) QUADRATIC3 returns (1, 0) (0, 1)

'A' 'B' 'C' QUADRATIC3 EVAL returns

Code:
'B/A/-2-1/(2*ABS(A))*√-(4*C*A-B^2)' 
'B/A/-2+1/(2*ABS(A))*√-(4*C*A-B^2)'
Visit this user's website Find all posts by this user
Quote this message in a reply
09-25-2016, 08:14 AM (This post was last modified: 09-25-2016 08:24 AM by peacecalc.)
Post: #6
RE: challenge HP50G RPL
Hello all,

Code:

\<< \-> A B C \<< A B C \->V3 PROOT \>> \>>

I tried the method of Joe Horn, and... it works. Even if the solutions are complex.

The three coefficients have to be integer or real numbers, then everything is fine.

Thank you Joe, for your lucid two command gem!
Find all posts by this user
Quote this message in a reply
09-25-2016, 03:40 PM (This post was last modified: 09-26-2016 12:57 AM by compsystems.)
Post: #7
RE: challenge HP50G RPL
PROOT It is a very primitive command (HP48 series) only accepts real an complex coefficients, does not allow symbolic coefficients, the code « →V3 PROOT » fails with symbolic and complex coefficients

Code:
« →V3 PROOT » 'QUADRATIC1' STO

1. -3. 2. QUADRATIC1 returns [1, 2] // ok
1. 2. 3. QUADRATIC1 returns [(-1.,1.41421356238) (-1., -1.41421356238)] // ok
'A' 'B' 'C' QUADRATIC1 returns "→V3 ERROR Bad Argument Type" =(
(1,0) (-1,-1) (0,1) QUADRATIC1 returns "→V3 ERROR Bad Argument Type" =(

Now are 4 instructions or objects with PROOT

Code:

« 
   3 →LIST
   AXL PROOT
»
 'QUADRATIC4' STO

1. -3. 2. QUADRATIC4 returns [1, 2] // ok
1. 2. 3. QUADRATIC4 returns [(-1.,1.41421356238) (-1., -1.41421356238)] // ok
(1,0) (-1,-1) (0,1) QUADRATIC4 returns [(1, 0) (0, 1)] // OK

Using a more powerful predefined function SOLVE CAS, real/complex and/or symbolic entry

12 instructions or objects
Code:
«
  3 →LIST                     @ crea una lista de 3 elementos con los coeficientes ingresados
  'X^(N-1)' 'N' 3 1 -1 SEQ    @ crea una lista de monomios de un polinomio de grado 2 en X
  *                           @ multiplica paralelamente o elemento a elemento las dos listas anteriores 
  ∑LIST                       @ crea el polinomio, como la suma de monomios
  'X' SOLVE                   @ halla las raíces      
»
 'QUADRATIC5' STO


or

only 6 instructions or objects, perhaps the minimal instructions for full function (HP50) to get the roots of a quadratic equation, from the coefficients in stack
Code:
«
  3 →LIST { 'X^2' 'X' 1 } * ∑LIST SOLVEVX
»
'QUADRATIC6' STO

1. -3. 2. QUADRATIC6 returns { 'X=1' 'X=2' }// ok
1. 2. 3. QUADRATIC6 returns { 'X=(-1, -1.41421356237)' 'X=(-1, 1.41421356237)' } // ok
'A' 'B' 'C' QUADRATIC6 returns { 'X=-(b+√-(4*c*a-b^2.))/(2*a)' 'X=-(b-√-(4*c*a-b^2.))/(2*a)' } // ok
(1,0) (-1,-1) (0,1) QUADRATIC6 { 'X=(7.5E-15,1.)' 'X=(1.,-1.E-14)' } ~ 'X=(0,1)' 'X=(1,0)'

// ok
Visit this user's website Find all posts by this user
Quote this message in a reply
09-25-2016, 05:03 PM
Post: #8
RE: challenge HP50G RPL
(09-25-2016 08:14 AM)peacecalc Wrote:  Hello all,

Code:

\<< \-> A B C \<< A B C \->V3 PROOT \>> \>>

I tried the method of Joe Horn, and... it works. Even if the solutions are complex.

The three coefficients have to be integer or real numbers, then everything is fine.

As you say, that won't accept complex coefficients.

Another simple program that takes coefficients from the stack:
Code:

« →V3 'X' PEVAL 'X' QUAD
»

This won't accept complex coefficients either. Also, it won't find complex solutions.
Find all posts by this user
Quote this message in a reply
09-25-2016, 10:41 PM
Post: #9
RE: challenge HP50G RPL
is better to use the commands 3 ->LIST AXL

Code:
«
  3 ->LIST AXL 'X' PEVAL 'X' QUAD
»
'QUADRATIC6' STO

(1,0) (-1,-1) (0,1) // == 1 -1-i, i
QUADRATIC6 returns { 'X=(7.5E-15, 1.)' 'X=(1., -1.E-14)' }

== { 'X=i' 'X=1' }
Visit this user's website Find all posts by this user
Quote this message in a reply
09-26-2016, 12:46 AM
Post: #10
RE: challenge HP50G RPL
(09-24-2016 05:15 PM)Gerson W. Barbosa Wrote:  
Code:
« PICK3 / UNROT
  SWAP / -2 / DUP SQ
  ROT - √ DUP2 - 
  UNROT +
»

16 instructions, 50 bytes.

If you replace "DUP2 - UNROT +" for "- LASTARG +" you save one instruction. Do you have the code for the HP 42s?
Find all posts by this user
Quote this message in a reply
09-26-2016, 01:51 AM
Post: #11
RE: challenge HP50G RPL
(09-26-2016 12:46 AM)Juan14 Wrote:  
(09-24-2016 05:15 PM)Gerson W. Barbosa Wrote:  
Code:
« PICK3 / UNROT
  SWAP / -2 / DUP SQ
  ROT - √ DUP2 - 
  UNROT +
»

16 instructions, 50 bytes.

If you replace "DUP2 - UNROT +" for "- LASTARG +" you save one instruction.

Additionally this lowers the byte count to 47.5. A potential problem with LASTARG might be the status of flag -55, but I don't think we should worry about that on the 50g, where there is plenty of RAM and LASTARG is always enabled.

(09-26-2016 12:46 AM)Juan14 Wrote:  Do you have the code for the HP 42s?

http://www.hpmuseum.org/forum/thread-1156.html
Find all posts by this user
Quote this message in a reply
09-26-2016, 11:56 PM
Post: #12
RE: challenge HP50G RPL
http://www.hpmuseum.org/forum/thread-1156.html
[/quote]

Thank you Gerson, I'll try it in the free42.
Find all posts by this user
Quote this message in a reply
10-05-2016, 12:28 AM (This post was last modified: 10-05-2016 12:46 AM by compsystems.)
Post: #13
RE: challenge HP50G RPL
hp48gx: 18 objects minimum (with→LIST cmd). Code by: ElkAr
Code:
≪ 2 →LIST SWAP / EVAL SWAP -2  / DUP SQ ROT - √  DUP2 + ROT ROT - ≫

hp48gx: 19 objects minimum. Code by: Gerson W. Barbosa
Code:
« 3 PICK / 3 ROLLD
  SWAP / -2 / DUP SQ
  ROT - √ DUP2 -
  3 ROLLD +
»

hp50g: 16 objects minimum. Code by: Gerson W. Barbosa
Code:
« PICK3 / UNROT
  SWAP / -2 / DUP SQ
  ROT - √ DUP2 - 
  UNROT +
»


What is the minimum number of objects in other RPN calculators?
Visit this user's website Find all posts by this user
Quote this message in a reply
10-05-2016, 04:13 PM
Post: #14
RE: challenge HP50G RPL
(10-05-2016 12:28 AM)compsystems Wrote:  What is the minimum number of objects in other RPN calculators?

Creo que quieres decir otras calculadoras RPL. I think you mean other RPL calculators :-)
Regarding RPN calculators, nothing beats the wp34s:

SLVQ

Nice saving of one command by using →LIST and EVAL in the HP-48 code above!

Regards,

Gerson.
Find all posts by this user
Quote this message in a reply
Post Reply 




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