Polynomial with real coefficients —> array w/ real coeff

11222021, 03:09 PM
(This post was last modified: 11242021 12:21 AM by Gil.)
Post: #1




Polynomial with real coefficients —> array w/ real coeff
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 

11222021, 05:12 PM
Post: #2




RE: Polynomial with real coefficients
Hi, Gil
This may be what you wanted. (10262020 04:25 PM)Albert Chan Wrote:(10242020 02:03 PM)John Keith Wrote: 'X^519*X^4+79*X^3+146*X^21153*X+1222' 

11222021, 05:51 PM
(This post was last modified: 11222021 06:56 PM by Gil.)
Post: #3




RE: Polynomial with real coefficients
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 

11232021, 11:23 AM
(This post was last modified: 11232021 01:44 PM by Gil.)
Post: #4




RE: Polynomial with real coefficients
Version 2b
As a special "polynomial", now you can introduce such non simplified expressions '2*x2*x', '3*xx^2+4x^24+3*x' and alike always equal to zero. The output will be, for the first one: '2*x2*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). '12020.' P—>ARR returns '12020.' (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 an1a0], 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 an1a0], 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 » » 

11242021, 03:02 PM
Post: #5




RE: Polynomial with real coefficients —> array w/ real coeff
Version 2c
Deletion of some unnecessary instructions. 

11272021, 03:33 PM
(This post was last modified: 11272021 03:42 PM by Gil.)
Post: #6




RE: Polynomial with real coefficients —> array w/ real coeff
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 an1a0], 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 an1a0] " 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 \>> \>> 

« Next Oldest  Next Newest »

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