VPython MapReduceFilter

From Physics Book
Jump to navigation Jump to search

Introduction

In Python, map, reduce, and filter are all different types of functions users can operate on sequences of elements. These functions help for making coding in VPython functional. All three of the functions take in a mathematical function and a sequence that should be run through the mathematical function.



Inputs

map(), filter(), and reduce() take in two parameters. The first parameter is the function that the user would like to use. Typical functions include squaring a number, dividing a number, or creating some type of function that performs mathematical operations. The second parameter is the list the user would like to input into the function. If the function is mathematical, then a list of numbers is needed to be passed through. The types of the function must match the types of the list. Example:

        def cubed(x): return x**3
        items = [1, 2, 3, 4]
        map(cubed, items)

As you can see here, the cubed function went in first, and then then items list came in next. These functions also use lambdas which are used to make code shorter and replace having to making a function.

Lambda Function

The lambda function is used to replace the need to make a new function.

The format is:

      lambda x: condition.

Lambda x is used in replacement of a function. Instead of:

      def cubed(x): return x**3
      items = [1,2,3]
      map(cubed, items)

a user can do:

      items = [1,2,3]
      map(lambda x: x**3, items)


Map()

Map() is a function used to apply a function that is passed in to a list of items that must have the function performed on each of the items. The format is:

                      map(function, alist)

Each item in the list gets passed through the function, and a new list gets created with each item that had went through the function. Example:

       def timestwo(x): return x*2
       numlist = [1,2,3,4,5]
       map(timestwo, numlist)
       # your result will be [2,4,6,8,10]

Map is extremely useful when someone would like to perform a certain action to each thing in their list. It helps short code because rather than performing the math function for each item on a new line, the code becomes shortened to a few lines.


Filter()

Filter is a function used to extract items from a list if they return true to the function given. It goes through a list and sees if the item in the list can be applied to the function. Lambdas are more commonly used when it comes to filter. The format is:

       filter(afunction, alist)
       filter(lambda x: condition, alist)

Here is an example:

       list = [3, 7, 5, 2, 1, 6]  
       filter(lambda x: x > 3, list)
       # this returns [7, 5, 6]

This is very useful because a for loop isn't needed to constantly check through each item in the list. Instead, filter shortens the amount of code needed to be written and finds the item that fit the condition. Iterations are then not needed.


Reduce()

Reduce is a function used to pass the whole list into a function, and after one item is passed through, the result of it becomes the next thing that is put into the mathematical function.

Example:

              list = [1, 2, 3, 4]
              reduce(lambda x, y: x*y)
              # The result is 24
              #Step 1: 1*2 = 2, the first element is x, and the second element is y
              #Step 2: 2*3 = 6, the result of step one is now x, and y the next element after that
              #Step 3: 6*4= 24

This function is useful if a person needs to pass numbers through and wants to use the new values again in the same function.


Use In Physics

These functions can be used in VPython for certain things. If a person would like to calculate the force of gravity on different objects, a person can make a list of masses and then multiply by 9.8. Example:

       masslist = [1, 3, 5, 10, 20]
       map(lambda x: x*9.8, masslist)

This example allows for the whole lists of masses to find out their gravitational forces on earth. These functions can make solving physics problems on multiple items easier instead of changing the value each time something new needs to be calculated.


References

[1] [2] [3]