The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

Polynomial program
Message #1 Posted by Michael Carey on 19 Nov 2013, 8:19 p.m.

I have written a program to automate the process of entering a polynomial. But I am having trouble with the variables, the old variable being used previously problem I think? Here is the code:

EXPORT QPOLY(V)
BEGIN
LOCAL A,Y;

A:=CAS.poly2symb(V); Y:=CAS.simplify(A);

END;

It gets upset infrequently and evaluates the whole expression to a number. For example QPLOY([1,1,1]) currently gives: 57, where it should give x^2+x+1.

I have tried to clear X, but cannot seem to do so, and there is no x (lower case) in the variable list.

Thank you, Michael Carey

Edited: 19 Nov 2013, 8:21 p.m.

      
Re: Polynomial program
Message #2 Posted by Michael de Estrada on 19 Nov 2013, 8:24 p.m.,
in response to message #1 by Michael Carey

A and Y are predefined real variables, so you can't store symbolic results in them. Try lower case a and y. Same for V.

Edited: 19 Nov 2013, 8:36 p.m.

            
Re: Polynomial program
Message #3 Posted by Michael Carey on 19 Nov 2013, 8:40 p.m.,
in response to message #2 by Michael de Estrada

So I have:

EXPORT QPOLY(v)

a:=CAS.poly2symb(v); b:=CAS.simplify(a);

END;

I get an error at b.

Thank you Michael Carey

                  
Re: Polynomial program
Message #4 Posted by Michael de Estrada on 19 Nov 2013, 9:08 p.m.,
in response to message #3 by Michael Carey

OK. The problem is that you can't pass a string type expression like [1,1,1] directly into the function parameter list. It is just evaluating a to 0.0, such that simplify(0) gives an error. What you need to do is first store [1,1,1] in v and then enter QPOLY(v). Also, you must create v in CAS view for this to work.

Edited: 19 Nov 2013, 9:09 p.m.

                  
Re: Polynomial program
Message #5 Posted by Eddie W. Shore on 19 Nov 2013, 10:27 p.m.,
in response to message #3 by Michael Carey

Try adding a LOCAL statement:

EXPORT QPOLY(v)
BEGIN
LOCAL a,b;
a:=CAS.poly2symb(v);
b:=CAS.simplify(a);
END;
                        
Re: Polynomial program
Message #6 Posted by Michael de Estrada on 19 Nov 2013, 10:36 p.m.,
in response to message #5 by Eddie W. Shore

That will just make things worse, and the result will be 0.

      
Re: Polynomial program
Message #7 Posted by cyrille de Brébisson on 20 Nov 2013, 3:21 a.m.,
in response to message #1 by Michael Carey

Hello,

Make this a CAS program, not a numerical program, it will help..

enter QPOLY(V):=begin simplify(poly2symb(V)); end; in CAS command line to create it (assuming that this is what you want).

cyrille

            
Re: Polynomial program
Message #8 Posted by Michael de Estrada on 20 Nov 2013, 9:44 a.m.,
in response to message #7 by cyrille de Brébisson

Problem is that will only work inside CAS view. A better way that works everywhere is to write a program as follows:

EXPORT QPOLY(v)
BEGIN
CAS("b:=simplify(poly2symb(v))");
RETURN b;
END;

If v=[1,1,1], then b=x^2+x+1

Edited: 20 Nov 2013, 9:57 a.m.

                  
Re: Polynomial program
Message #9 Posted by Michael Carey on 20 Nov 2013, 3:17 p.m.,
in response to message #8 by Michael de Estrada

I seem to have gotten side tracked here, I should mention that that the original program *did* work, its just not working any more and I cannot work out why. I am thinking that something has stomped on the X variable, but I am not sure.

Thank you Michael Carey

      
Re: Polynomial program
Message #10 Posted by Han on 20 Nov 2013, 3:33 p.m.,
in response to message #1 by Michael Carey

Quote:
I have written a program to automate the process of entering a polynomial. But I am having trouble with the variables, the old variable being used previously problem I think? Here is the code:
EXPORT QPOLY(V)
BEGIN
LOCAL A,Y;

A:=CAS.poly2symb(V); Y:=CAS.simplify(A);

END;

It gets upset infrequently and evaluates the whole expression to a number. For example QPLOY([1,1,1]) currently gives: 57, where it should give x^2+x+1.

I have tried to clear X, but cannot seem to do so, and there is no x (lower case) in the variable list.

Thank you, Michael Carey


Clearing X will have no effect on this because X is never used in the program or in any of the CAS commands used in the program. Did you mean to write lower case x? That variable, on the other hand, does affect the program. Also, x defined from CAS is different from x created in Home -- yeah, I know... what a mess ;_;

I am willing to bet that there is a variable called 'x' (lower case) whose value is 7. This variable likely appears in the CAS view -- try typing: purge(x) in CAS view.

      
Re: Polynomial program
Message #11 Posted by CR Haeger on 20 Nov 2013, 4:28 p.m.,
in response to message #1 by Michael Carey

May I ask why use a PROGRAM to automate this?

Why not just use poly2symb({1,1,1}) in HOME* or CAS?

In CAS poly2symb({a,2,c}) seems to allow for a combination of numbers/letters.

Best, Carl

* excluding RPN?

Edited: 20 Nov 2013, 4:29 p.m.

            
Re: Polynomial program
Message #12 Posted by Michael Carey on 20 Nov 2013, 8:30 p.m.,
in response to message #11 by CR Haeger

Poly2sym() doesn’t simplify. It makes it look cleaner.

Ok I messed around last night before going to bed and cleared out x and now it works as intended. I am sure that I tried to do that previously though and it didn’t work. I distinctly remember getting a "No such variable x" error. Which it does now.

                  
Re: Polynomial program
Message #13 Posted by CR Haeger on 20 Nov 2013, 8:43 p.m.,
in response to message #12 by Michael Carey

In HOME view, you are right that poly2symb({1,1,1}) does not seem to simplify by itself.

In CAS view, settings/simplify:maximum seems to give the simplified expression.

Edited: 20 Nov 2013, 8:43 p.m.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall