Creating and Manipulating Symbolic Expressions
In this example, we show how to create symbolic variables and how to manipulate symbolic expressions. Suppose we wanted to define a polynomial f=x^4+5x^3+8x^2+x-15. If we attempt to type that directly into Python, we get an error “name x is not defined”. Remember that a variable normally stores a value, but we have not stored anything. To let Python know that we want x to represent the variable x, we use the symbols command. Now, when we press SHIFT/ENTER, Python defines the symbolic expression f. There are three basic tools for manipulating symbolic expressions. We can factor. We can expand our factored expression. And, for an expression like f/(x^2-1), we can simplify. Notice in each case we use the basic Python syntax of VARIABLE.COMMAND (with any options in parentheses). In addition, we can substitute values into our symbolic expressions using the subs command. Notice that if we let g=f/(x^2-1), we get an error when we try to substitute x=1, but if we simplify the expression, we can now substitute x=1 to get a y-value since x=1 is no longer restricted from the domain.