HP Forums

Full Version: [CAS] PART FUNCTION on sum of n terms
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, the part function works fine, but there is a problem. If the expression is a sum of n terms (a+b+c+d+e+f+.... n), returns the number of terms, that make within a program, the expression parser will be dificultier

part( a + b + c + d + e + f +.... n) returns n

the TI68k calculators return in a group of two terms

PHP Code:
part+.... n)  => part( (a) +(b+c+d+e+f+.... n) ) returns 2
part
+.... n)  => part( (a) +(b+c+d+e+f+.... n), returns (a)
part+.... n)  => part( (a) +(b+c+d+e+f+.... n), returns (b+c+d+e+f+.... n
when nested part (hp-prime), the sums expressions are difficult to control.

Is possible to reprogram the PART function that groups of two terms in a sum? as the ti68k

hp-prime
part( quote( 1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10 ) )
RETURNS 10

part( quote( 1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10 ), 2 )
RETURNS -1/2

TI89
part( quote( 1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10 ) ) =>
part(quote( (1/1) + (-1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10) ))
RETURNS 2


part( quote( 1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10 ), 1 )
RETURNS 1/1
part( quote( 1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10 ) , 2 )
RETURNS (-1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10)

Thanks
In short: no.
It's much better to have all arguments of + or * at the same level with a n-ary operator than to embedd some of them sometimes very deeply if + or * are binary, it reflects much better the structure of the expression (for example you can exchange two arguments easily).
HP48 to HP50 also grouped the terms of addition and multiplication by twos


PHP Code:
« 0 0 { } -> EXPRESSION XPART NPARTS OPERATOR OBJECTS
  « EXPRESSION
    IFERR OBJ
->
    
THEN ->STR 'OPERATOR' STO 0 'NPARTS' STO OPERATOR 1 ->LIST 'OBJECTS' STO
    
ELSE ->STR 'OPERATOR' STO 'NPARTS' STO NPARTS ->LIST 'OBJECTS' STO NPARTS OPERATOR OBJECTS 3 ->LIST DROP
    END
      
IF XPART NPARTS <= XPART ->= AND NOT
      THEN 
"EXPRESSION " EXPRESSION " CONTAINS ONLY " NPARTS " PARTS" +
      ELSE
        IF 
XPART 0 ==
        
THEN OPERATOR
        
ELSE
          IF 
XPART -==
          
THEN NPARTS
          
ELSE OBJECTS XPART GET
          END
        END
      END
  »
»
'PART' STO 

'x^3-6*x^2+11*x-6' -1 PART @ returns 2 (parts)
'x^3-6*x^2+11*x-6' 0 PART @ returns "-"
'x^3-6*x^2+11*x-6' 1 PART @ returns '(x^3-6*x^2+11*x)'
'x^3-6*x^2+11*x-6' 2 PART @ returns 6
'x^3-6*x^2+11*x-6' 3 PART @ returns "EXPRESSION 'x^3-6*x^2+11*x-6' CONTAINS ONLY 2 PARTS"
'x^3-6*x^2+11*x-6' -3 PART @ returns "EXPRESSION 'x^3-6*x^2+11*x-6' CONTAINS ONLY 2 PARTS"

'X' -1 PART @ returns 0 (parts)
'X' OBJ->@ returns "ERROR" =(
'X^1' EVAL OBJ-> @ returns"ERROR" =(
'X^1' EVAL -1 PART @ returns 0 (parts)

'-X' -1 PART @ returns 1
'-X' 0 PART @ returns "NEG"
'-X' 1 PART @ returns 'X'

e -1 PART @ returns 0
e 0 PART @ returns "e"

pi -1 PART @ returns 0
pi 0 PART @ returns "pi"


'1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10 ' -1 PART @ returns 2
'1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10 ' 0 PART @ returns "+"
'1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10 ' 1 PART @ '-1/10 '
'1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9 + -1/10 ' 2 PART @ '1/1 + -1/2 + 1/3 + -1/4 + 1/5 + -1/6 + 1/7 + -1/8 + 1/9'
Correct. That's precisely because I knew the limitations of having binary + and * that I designed Giac with n-ary + and *. One of the limitation (that is perhaps not obvious on a calc) is to be able to handle sums with a very large number of arguments (for example 1 million). With binary + the last argument would be so much embedded that many symbolic operations would segfault because of stack overflow during recursion.
(07-09-2016 08:03 AM)parisse Wrote: [ -> ]Correct. That's precisely because I knew the limitations of having binary + and * that I designed Giac with n-ary + and *. One of the limitation (that is perhaps not obvious on a calc) is to be able to handle sums with a very large number of arguments (for example 1 million). With binary + the last argument would be so much embedded that many symbolic operations would segfault because of stack overflow during recursion.

I agree. Symbolics in newRPL also have n-ary + and * (there's no CAS yet, but the expression format and compiler/decompiler is finished). Much easier to scan for terms or factors in a flattened tree than a binary tree.
Reference URL's