Calculator for polynomials
This is a project for my university.
This calculator allows you to operate on multivariable polynomials. You can enter polynomials on the input console and then perform operations such as check if the last added polynomial is equal to zero or multiply two last added polynomials and then print the result.
In this progam, a polynomial has a recurrent definition. It is either a coefficient (that is, an integer value) or a sum of monomials. A monomial is a product of a variable raised to a power and a polynomial. Variables in a polynomial are numbered x_i, where i is the depth of the recursion. You'll find more about the definition of a polynomial in the next paragraph and in the examples below.
The polynomials entered within the console are added on top of the stack which is empty at the beginning of the program. If you want to add a polynomial to the stack you need to enter it on the console in a format specified by following rules:
POLYNOMIAL → COEFFICIENT | MONOMIALS
MONOMIALS → (MONOMIAL) | (MONOMIAL)+MONOMIALS
MONOMIAL → POLYNOMIAL,EXPONENT
COEFFICIENT and EXPONENT need to be integers, like 2 or -193. EXPONENT needs to be positive.
Example inputs:
(3,2)
= x02*3
(2,1)+(1,4)
= x0*2 + x04
((5,2),3)
= x03*x12*5
((4,1)+(2,3),1)+(1,2)
= x0*(x1*4 + x13*2) + x02
Apart from adding a new polynomial to the stack, you can enter following commands on the console:
- ZERO - inserts a polynomial equal to zero at the top of the stack
- IS_COEFF - checks if the polynomial at the top of the stack is a coefficient, prints 0 or 1 adequately
- IS_ZERO - checks if the polynomial at the top of the stack is equal to zero, prints 0 or 1
- CLONE - inserts a copy of the polynomial at the top to the top of the stack
- ADD - adds two polynomials at the top of the stack, removes these two polynomials from the stack and inserts the sum
- MUL - multiplies two polynomials at the top of the stack, removes them and inserts the product
- NEG - negates the polynomial at the top of the stack
- SUB - subtracts second polynomial (from the top) from the polynomial at the top, removes them and inserts the difference
- IS_EQ - checks if two polynomials at the top of the stack are equal, prints 0 or 1
- DEG - prints the degree of the polynomial at the top (or -1 if it is equal to zero)
- DEG_BY x - prints the degree of the polynomial at the top taking into account the variable x
- AT y - computes the value of the polynomial at y, removes the polynomial from the stack and puts the result of the operation on the stack
- PRINT - prints the polynomial at the top of the stack
- POP - removes the polynomial at the top of the stack
- COMPOSE - performs top-of-stack polynomial composite with k consecutive top-of-stack polynomials, removes these k + 1 polynomials from stack, and inserts the result of composite on top of stack