python22.sci2u.dk Active sci2u
Loading...
Login
Prefer latest booklet Prefer latest booklet Login
Link to booklet is copied to clipboard!
Saved to booklet!
Removed from booklet!
Started capturing to booklet.
Stopped capturing to booklet.
Preferring latest booklet.
No longer prefers latest booklet.
Entered edit mode.
Exited edit mode.
Edit mode

Click on the booklet title or a chapter title to edit (max. 30 characters)


How To's

How to get python How to plot points and lines How to use loops How to draw general shapes How to draw curves How to use functions
6.1 Area of triangle
6.2 Plain code
6.3 A function
How to manipulate Polygons How to create animations How to do mathematical analysis How to fit How to go from SymPy to NumPy How to solve a single differential equation How to do coupled and 2nd order ODEs How to combine vectors and ODEs How to do ODE events Add Chapter..

How to use functions

6.1 Area of triangle

Often, when coding some problem in Python, you face rather complex mathematical expressions that cannot be written clearly as one-line lambda-functions. You would then use the standard Python function as introduced in this how-to.
As an example, consider that you have three points defining a triangle:
and would like a function that receives the coordinates for the three points, and returns the area of the triangle they form.
The mathematical operation you'll need is the following: Setup the two vectors from one of the points to each of the other points:
Now evaluate the cross-product of the two vectors:
and take the length of that vector, and you have the area of the parallelogram that two vectors define.
Now, the area of the triangle is half the area of the parallelogram:


6.2 Plain code

Consider we would like to find the areas of these two triangles:

Figure 1

The first triangle has these three corners:
P0 = [2,1]
P1 = [8,1]
P2 = [5,5]
and its area can be calculated with these five lines:
v1 = np.array(P1) - np.array(P0)
v2 = np.array(P2) - np.array(P0)
v1_cross_v2 = [0, 0, v1[0] * v2[1] - v2[0] * v1[1]]
A = np.linalg.norm(v1_cross_v2)
A_tri = A/2
Providing the result:
A_tri
12.0
The five lines doing the calculation are nicely readably, but clearly, they would not easily be written as a lambda function. Hence, when calculating the area of the second triangle we would be tempted to copy-paste the five lines, which for many reasons would be a bad idea.


6.3 A function

In stead of copy-pasting code sections and reusing them, one would rather want to introduce a Python function:
def area_tri(P0,P1,P2):
    v1 = np.array(P1) - np.array(P0)
    v2 = np.array(P2) - np.array(P0)
    v1_cross_v2 = [0, 0, v1[0] * v2[1] - v2[0] * v1[1]]
    A = np.linalg.norm(v1_cross_v2)
    A_tri = A/2
    return A_tri
The function declaration starts with def and is followed by the function name, here area_tri. Then comes some arguments in parentheses, ( ), here P0, P1, and P2. Finally, the first line of the function declaration ends with :. The following code lines will belong to the function as long as they are indented (by some amount, typically 4 spacings). The function may contain a statement with a return keyword followed by a single or a list of variables.
As with the lambda function, a function declared with the above method using def-:-indentation-return syntax is called by supplying values (or variables) as arguments. For the first triangle we have:
area_tri([2,1],[8,1],[5,5])
12.0
So, the function returns the value of A_tri as this is what was written after return in the function declaration.
And now, with practically no extra code, we get the area of the second triangle:
area_tri([4,1],[2,5],[8,3])
10.0


Sci2u Assignment: 820
Delete "How To's"?
Once deleted this booklet is gone forever!
Block is found in multiple booklets!

Choose which booklet to go to:

© 2019-2022 Uniblender ApS