# HP Forums

Full Version: Polynomial program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

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.

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.

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

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.

```EXPORT QPOLY(v)
BEGIN
LOCAL a,b;
a:=CAS.poly2symb(v);
b:=CAS.simplify(a);
END;
```

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

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

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.

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

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.

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.

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.

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.