he function simplify above can perform all numerical computations, but stops whenever it encounters a variable. It cannot do any better, in fact, because it does not know the values of variables. If we specify values for variables, we can then use those values in the computation, replacing each variable whose value is specified with the value itself.
Question 1: Evaluating expressions with respect to a variable valuation.
The function simplify above can perform all numerical computations, but stops whenever it encounters a variable. It cannot do any better, in fact, because it does not know the values of variables.
If we specify values for variables, we can then use those values in the computation, replacing each variable whose value is specified with the value itself.
A variable valuation is a mapping from variables to their values; we can represent it simply as a dictionary associating to each variable a number:
You can extend the evaluation function to take as input a variable valuation. The idea is that, when you find a variable, you try to see whether its value is specified in the variable valuation. If it is, you can replace the variable with the value, and carry on. If it is not, you leave the variable as it is, since you cannot evaluate it.
To check if a variable (a string) s is in a dictionary d, you can test
s in d
and to get the value, in case it is present, you can just do d[s] of course.
WRITE CODE IN PYTHON FOR:
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images