Polynomial with real coefficients —> array w/ real coeff - Printable Version +- HP Forums ( https://www.hpmuseum.org/forum)+-- Forum: HP Software Libraries ( /forum-10.html)+--- Forum: General Software Library ( /forum-13.html)+--- Thread: Polynomial with real coefficients —> array w/ real coeff ( /thread-17736.html) |

Polynomial with real coefficients —> array w/ real coeff - Gil - 11-22-2021 03:09 PM
I could not find a suitable program to get the real coefficients of a polynomial in form of an array. So I prepared one, indeed somewhat "bulky", but working smoothly. As the variable of the polynomial may already be defined in the current directory, the program changes its original name in the expression into another weird dummy variable X__, to be itself transformed back, at the end, to its original name after adequate string manipulations. Enter for example '2*(t+4*(5+10))-7*t^6' Press P—>ARR To get '2*(t+4*(5+10))-7*t^6' (original expression in stack level 2) '-7*t^6+2*t+120' (simplified expression in stack level 1) :[ana0]: [ -7 0 0 0 0 2 120 ] (coefficients requested with label). The program will also detect incorrect similar variable names and give a corresponding message warning (as only 1 variable is allowed). Regards, Gil Campart RE: Polynomial with real coefficients - Albert Chan - 11-22-2021 05:12 PM
Hi, Gil This may be what you wanted. (10-26-2020 04:25 PM)Albert Chan Wrote:(10-24-2020 02:03 PM)John Keith Wrote: 'X^5-19*X^4+79*X^3+146*X^2-1153*X+1222' RE: Polynomial with real coefficients - Gil - 11-22-2021 05:51 PM
Your given code : great and most compact. Incredible! But it does not work for non integers coefficients an to a0 in a polynomial like 'an × X^n +... +a0 × X^0'. Finally, both methods, now, with the correct coefficients, do give the correct answers, as expected. Here is my corrected version 2a of P—>ARRAY. Regards, Gil RE: Polynomial with real coefficients - Gil - 11-23-2021 11:23 AM
Version 2b As a special "polynomial", now you can introduce such non simplified expressions '2*x-2*x', '3*x-x^2+4-x^2-4+-3*x' and alike always equal to zero. The output will be, for the first one: '2*x-2*x' (original input expression at stack level 3) 0 (simplified, "cleanest" expression, here always without a dot, at stack level 2) an...a0: [0] (smallest possible array, here always without a dot, with a tag label, at stack level 1). This version, as the previous one, accepts, as input, just a variable name (then never evaluated), existing or not in the directory, or only one or several real/integer numbers. 't' P—>ARR returns 't' (original input expression, here the variable t, in stack level 3) 't' (simplified, cleanest" expression, but never evaluated for a single variable, here then the variable name t again) an...a0: [1 0] (smallest possible array, with a tag label, at stack level 1). '120-20.' P—>ARR returns '120-20.' (original input expression in stack level 3) 100 (simplified expression/value, if possible without a dot like here, in stack level 2) an...a0: [100] (smallest possible array, if possible without dots like here, with a tag label, at stack level 1). '0.' P—>ARR returns 0. (original input expression/value in stack level 3) 0 (simplified expression/value in stack level 2, if possible without a dot like here) an...a0: [0] (smallest possible array, if possible without a dot like here, with a tag label). Just in case, here is the full code: \<< "\[] Version 2b 2021.11.23 \[] 1 ALG Arg (''): '(ai*X^i +) * + + (ak*X^k +) * + ' \[] Order not important \[] ai, ak : reals \[] i, k : integers \[] Any var name(already existing! or not) \[] By default: Polynom. degree is n.MAX=15 (to change it: 1st instruction after DROP, 6 lines below) \[] Output: [an an-1a0], an \=/ 0 " DROP 15 RCLF 0 0 0 0 \-> d.MAX fg s STR p v \<< DUP d.MAX XQ 'd.MAX' STO LVAR DUP TYPE 5 \=/ IF THEN DUP OBJ\-> OBJ\-> DROP XQ \-> nv \<< nv 1 > IF THEN nv \->LIST \->STR nv "Here " SWAP + " var " + SWAP + ": Use only 1 !" + SWAP ROT DROP2 DOERR END \>> SWAP DROP "" + 'v' STO \->STR v "X\175\175" SREPL DROP OBJ\-> -105 SF EXPAND -105 CF \->STR "+-" "-" SREPL DROP "-(" "+" SREPL 0 > IF THEN ")" "" SREPL DROP "E-" "E\175" SREPL DROP "-" "\175\|>" SREPL DROP "+" "-" SREPL DROP "\175\|>" "+" SREPL DROP "E\175" "E-" SREPL DROP END ".+" "+" SREPL DROP ".-" "-" SREPL DROP ".*" "*" SREPL DROP ".'" "'" SREPL DROP "\|>" + ".\|>" "" SREPL DROP "\|>" "" SREPL DROP DUP "X\175\175" v SREPL DROP OBJ\-> SWAP "+X\175\175" "+1*X\175\175" SREPL DROP "-X\175\175" "-1*X\175\175" SREPL DROP "'X\175\175" "'+1*X\175\175" SREPL DROP "'" "" SREPL DROP DUP SIZE 's' STO 'STR' STO { } d.MAX 2 XQ FOR i STR "^" i + POS 'p' STO p 0 \=/ IF THEN STR 1 p 5 - SUB OBJ\-> STR p i \->STR SIZE + 1 + s SUB DUP SIZE 's' STO 'STR' STO ELSE 0 END + -1 XQ STEP STR DUP "X" POS DUP 'p' STO 2 - 1 SWAP SUB DUP "" \=/ IF THEN OBJ\-> ELSE DROP 0 END + STR p DUP 0 \=/ 3 1 IFTE + s SUB DUP "" \=/ IF THEN OBJ\-> ELSE DROP 0 END + DUP SIZE { 0 } \-> l s l0 \<< 1 d.MAX 1 + FOR i l i GET 0 \=/ IF THEN l 'l0' STO d.MAX 1 + 'i' STO END NEXT l0 l SAME IF THEN WHILE l0 HEAD 0 == REPEAT l0 TAIL 'l0' STO END END l0 OBJ\-> \>> ELSE DROP EVAL \->STR "'" + ".'" "" SREPL DROP OBJ\-> DUP 1 END \->ARRY "[ana0]" \->TAG fg STOF \>> \>> Other format (with non readable characters ): « " Version 2b 2021.11.23 1 ALG Arg (''): '(ai*X^i +) * + + (ak*X^k +) * + ' Order not important ai, ak : reals i, k : integers Any var name(already existing! or not) By default: Polynom. degree is n.MAX=15 (to change it: 1st instruction after DROP, 6 lines below) Output: [an an-1a0], an 0 " DROP 15 RCLF 0 0 0 0 d.MAX fg s STR p v « DUP d.MAX XQ 'd.MAX' STO LVAR DUP TYPE 5 IF THEN DUP OBJ OBJ DROP XQ nv « nv 1 > IF THEN nv LIST STR nv "Here " SWAP + " var " + SWAP + ": Use only 1 !" + SWAP ROT DROP2 DOERR END » SWAP DROP "" + 'v' STO STR v "X¯¯" SREPL DROP OBJ -105 SF EXPAND -105 CF STR "+-" "-" SREPL DROP "-(" "+" SREPL 0 > IF THEN ")" "" SREPL DROP "E-" "E¯" SREPL DROP "-" "¯" SREPL DROP "+" "-" SREPL DROP "¯" "+" SREPL DROP "E¯" "E-" SREPL DROP END ".+" "+" SREPL DROP ".-" "-" SREPL DROP ".*" "*" SREPL DROP ".'" "'" SREPL DROP "" + "." "" SREPL DROP "" "" SREPL DROP DUP "X¯¯" v SREPL DROP OBJ SWAP "+X¯¯" "+1*X¯¯" SREPL DROP "-X¯¯" "-1*X¯¯" SREPL DROP "'X¯¯" "'+1*X¯¯" SREPL DROP "'" "" SREPL DROP DUP SIZE 's' STO 'STR' STO { } d.MAX 2 XQ FOR i STR "^" i + POS 'p' STO p 0 IF THEN STR 1 p 5 - SUB OBJ STR p i STR SIZE + 1 + s SUB DUP SIZE 's' STO 'STR' STO ELSE 0 END + -1 XQ STEP STR DUP "X" POS DUP 'p' STO 2 - 1 SWAP SUB DUP "" IF THEN OBJ ELSE DROP 0 END + STR p DUP 0 3 1 IFTE + s SUB DUP "" IF THEN OBJ ELSE DROP 0 END + DUP SIZE { 0 } l s l0 « 1 d.MAX 1 + FOR i l i GET 0 IF THEN l 'l0' STO d.MAX 1 + 'i' STO END NEXT l0 l SAME IF THEN WHILE l0 HEAD 0 == REPEAT l0 TAIL 'l0' STO END END l0 OBJ » ELSE DROP EVAL STR "'" + ".'" "" SREPL DROP OBJ DUP 1 END ARRY "[ana0]" TAG fg STOF » » RE: Polynomial with real coefficients —> array w/ real coeff - Gil - 11-24-2021 03:02 PM
Version 2c Deletion of some unnecessary instructions. RE: Polynomial with real coefficients —> array w/ real coeff - Gil - 11-27-2021 03:33 PM
Suppose you execute RCLVX (to get the current variable) and you get the variable name 't'. Suppose that somewhere the 't' variable was already defined, then you will be requested, in the previous program version, to delete that variable 't'. The question/request might be confusing and there is indeed no reason to delete that variable right now. That's the purpose of changing, provisory, the current working variable VX into Y__. See then, right below, the added part in bold: \<< "\[] Long Version 2g (w/ use of strings) 2021.11.27 \[] 1 ALG Arg (''): '(ai*X^i +) * + + (ak*X^k +) * + ' \[] Order not important \[] ai, ak : reals \[] i, k : integers \[] Any var name(already existing! or not) \[] By default: Polynom. degree is n.MAX=15 (to change it: 1st instruction after DROP, 6 lines below) \[] Output: [an an-1a0], an \=/ 0 " DROP 15 RCLF 0 0 0 0 \-> d.MAX fg s STR p v \<< DUP d.MAX XQ 'd.MAX' STO LVAR DUP TYPE 5 \=/ IF THEN DUP OBJ\-> OBJ\-> DROP XQ \-> nv \<< nv 1 > IF THEN nv \->LIST \->STR nv "Here " SWAP + " var " + SWAP + ": Use only 1 !" + SWAP ROT DROP2 DOERR END \>> SWAP DROP "" + 'v' STO \->STR v "X\175\175" SREPL DROP OBJ\-> RCLVX DUPDUP \-> vx \<< EVAL SAME NOT IF THEN 'Y\175\175' STOVX END -105 SF EXPAND vx STOVX \>> -105 CF \->STR "+-" "-" SREPL DROP "-(" "+" SREPL 0 > IF THEN ")" "" SREPL DROP "E-" "E\175" SREPL DROP "-" "\175\|>" SREPL DROP "+" "-" SREPL DROP "\175\|>" "+" SREPL DROP "E\175" "E-" SREPL DROP END ".+" "+" SREPL DROP ".-" "-" SREPL DROP ".*" "*" SREPL DROP ".'" "'" SREPL DROP DUP "X\175\175" v SREPL DROP OBJ\-> SWAP "+X\175\175" "+1*X\175\175" SREPL DROP "-X\175\175" "-1*X\175\175" SREPL DROP "'X\175\175" "'+1*X\175\175" SREPL DROP "'" "" SREPL DROP DUP SIZE 's' STO 'STR' STO { } d.MAX 2 XQ FOR i STR "^" i + POS 'p' STO p 0 \=/ IF THEN STR 1 p 5 - SUB OBJ\-> STR p i \->STR SIZE + 1 + s SUB DUP SIZE 's' STO 'STR' STO ELSE 0 END + -1 XQ STEP STR DUP "X" POS DUP 'p' STO 2 - 1 SWAP SUB DUP "" \=/ IF THEN OBJ\-> ELSE DROP 0 END + STR p DUP 0 \=/ 3 1 IFTE + s SUB DUP "" \=/ IF THEN OBJ\-> ELSE DROP 0 END + DUP SIZE { 0 } \-> l s l0 \<< 1 d.MAX 1 + FOR i l i GET 0 \=/ IF THEN l 'l0' STO d.MAX 1 + 'i' STO END NEXT l0 l SAME IF THEN WHILE l0 HEAD 0 == REPEAT l0 TAIL 'l0' STO END END l0 OBJ\-> \>> ELSE DROP EVAL \->STR "'" + ".'" "" SREPL DROP OBJ\-> DUP 1 END \->ARRY "[ana0]" \->TAG fg STOF \>> \>> As it appears, this version is quite heavy. A simpler/compacter version 3a is shown below, with as output the initial argument and the final, resulting array. \<< "\[] Compact Version 3a (no use of strings) 2021.11.24 \[] 1 ALG Arg (''): '(ai*X^i +) * + + (ak*X^k +) * + ' \[] Order not important \[] ai, ak : reals \[] i, k : integers \[] Any var name(already existing! or not) \[] Output: [an an-1a0] " DROP RCLF 0 \-> fg v \<< DUP LVAR DUP TYPE 5 \=/ IF THEN DUP OBJ\-> OBJ\-> DROP XQ \-> nv \<< nv 1 > IF THEN nv \->LIST \->STR nv "Here " SWAP + " var " + SWAP + ": Use only 1 !" + SWAP ROT DROP2 DOERR END \>> SWAP DROP "" + 'v' STO \->STR v "X\175\175" SREPL DROP OBJ\-> RCLVX DUPDUP \-> vx \<< EVAL SAME NOT IF THEN 'Y\175\175' STOVX END -105 SF EXPAND -105 CF 'X\175\175' * SIMPLIFY DUP { } \-> f l \<< DEGREE 1 START f 'X\175\175' / SIMPLIFY DUP 0 'X\175\175' STO \->NUM DUP "\|>" + ".\|>" "" SREPL DROP "\|>" "" SREPL DROP OBJ\-> l + 'l' STO - 'f' STO 'X\175\175' PURGE -1 STEP l OBJ\-> \>> vx STOVX \>> ELSE DUP -105 CF DROP2 EVAL "\|>" + ".\|>" "" SREPL DROP "\|>" "" SREPL DROP OBJ\-> 1 END \->ARRY "[ana0]" \->TAG fg STOF \>> \>> RE: Polynomial with real coefficients —> array w/ real coeff - gor1060 - 05-05-2023 07:57 AM
[attachment=12059] Update: [attachment=12061] [attachment=12062] RE: Polynomial with real coefficients —> array w/ real coeff - rprosperi - 05-05-2023 12:03 PM
If the goal is for someone to use your program, please consider sharing it either typed or in a text file, etc. it can't be copied from a screen grab. Showing these is better than nothing at all, but most folks won't copy from an image. Just trying to help you share in a better way. RE: Polynomial with real coefficients —> array w/ real coeff - Gil - 05-05-2023 02:03 PM
Very nice indeed. Thanks. Perhaps we could simplify your tests by a single one as follows: « 0 { } —> N L « DUP DEGREE 'N' STO N 1 < IF THEN 1 —>LIST ELSE 0 N 1 - START 0 HORNER 'L' STO+ DROP NEXT L + END AXL » » And to have your program work without being asked if we want to go to real, add the following 3 instructions -105 SF for real, PUSH to save the initial flags and POP to restore the initial flags: « PUSH -105 SF 0 { } —> N L « DUP DEGREE 'N' STO N 1 < IF THEN 1 —>LIST ELSE 0 N 1 - START 0 HORNER 'L' STO+ DROP NEXT L + END AXL POP » » RE: Polynomial with real coefficients —> array w/ real coeff - gor1060 - 05-08-2023 12:07 PM
(05-05-2023 02:03 PM)Gil Wrote: Very nice indeed. There is no limit to perfection. Enabling the PUSH and POP instructions with -105 SF is a questionable idea, because in exact mode you can't get an array with exact polynomial coefficients. My original program gives an approximate result in approximate mode and an exact result in exact mode. With real coefficients of the polynomial in the approximate mode, the program does not ask about changing the mode. I didn't think about any optimization of my program, but if you want, then let the next iteration be like this: [attachment=12079] [attachment=12080] [attachment=12081] [attachment=12082] |