concept expression in category python

This is an excerpt from Manning's book Math for Programmers: 3D graphics, machine learning, and simulations with Python.
Because linear transformations preserve vector sums and scalar multiples, they also preserve a broader class of vector arithmetic operations. The most general operation is called a linear combination. A linear combination of a collection of vectors is a sum of scalar multiples of them. For instance, one linear combination of two vectors u and v would be 3u − 2v. Given three vectors u, v, and w, the expression 0.5u − v + 6w is a linear combination of u, v, and w. Because linear transformations preserve vector sums and scalar multiples, these preserve linear combinations as well.
In the last chapter, you learned that the derivative of x3 is 3x2. By the time we’re done in this chapter, you’ll be able to write a Python function that takes an algebraic expression and gives you an expression for its derivative. Our data structure for an algebraic formula will be able to represent variables, numbers, sums, differences, products, quotients, powers, and special functions like sine and cosine. If you think about it, we can represent a huge variety of different formulas with that handful of building blocks, and our derivative will work on all of them (figure 10.5).
Figure 10.5: A goal is to write a derivative function in Python that takes an expression for a function and returns an expression for its derivative.
![]()
We’ll get started by modeling expressions as data structures instead of functions in Python code. Then, to warm up, we can do some simple computations with the data structures to do things like plugging in numbers for variables or expanding products of sums. After that, I’ll teach you some of the rules for taking derivatives of formulas, and we’ll write our own derivative function and perform them automatically on our symbolic data structures.
A first observation is that f is an arbitrary name for this function. For instance, the right-hand side of this equation expands the same way regardless of what we call it. Because of this, we can focus only on the expression that defines the function, which in this case is (3x2 + x) sin(x). This is called an expression in contrast to an equation, which must contain an equals sign (=). An expression is a collection of mathematical symbols (numbers, letters, operations, and so on) combined in some valid ways. Our first goal, therefore, is to model these symbols and the valid means of composing this expression in Python.
Let’s write a function that takes an expression and returns a list of distinct variables that appear in it. For instance, h(z) = 2z + 3 is defined using the input variable z, while the definition of g(x) = 7 contains no variables. We can write a Python function, distinct_variables, that takes an expression (meaning any of our elements or combinators) and returns a Python set containing the variables.
If our expression is an element, like z or 7, the answer is clear. An expression that is just a variable contains one distinct variable, while an expression that is just a number contains no variables at all. We expect our function to behave accordingly:
>>> distinct_variables(Variable("z")) {'z'} >>> distinct_variables(Number(3)) set()The situation is more complicated when the expression is built from some combinators like y · z + xz. It’s easy for a human to read all the variables, y, z, and x, but how do we extract these from the expression in Python? This is actually a Sum combinator representing the sum of y · z and xz. The first expression in the sum contains y and z, while the second has x and z. The sum then contains all of the variables in these two expressions.
This suggests we should use a recursive solution: the distinct_variables for a combinator are the collected distinct_variables for each of the expressions it contains. The end of the line has the variables and numbers, which obviously contain either one or zero variables. To implement the distinct_variables function, we need to handle the case of every element and combinator that make up a valid expression:

This is an excerpt from Manning's book Get Programming: Learn to code with Python.
An expression is a line of code that can be reduced to a value.
To get the value, you substitute the values for all other known variables in the expression and do the calculation. For example, if a = 1 and b = 2, then c = a + b = 1 + 2 = 3. In programming, the only thing you’re allowed to have to the left of the equal sign is the name of a variable.