Object Oriented Programming for Python

Similar documents
COMP 204. Object Oriented Programming (OOP) Mathieu Blanchette

Read all questions and answers carefully! Do not make any assumptions about the code other than those that are clearly stated.

Your Second Physics Simulation: A Mass on a Spring

Section 4.6 Negative Exponents

Tutorial Three: Loops and Conditionals

CS177 Spring Final exam. Fri 05/08 7:00p - 9:00p. There are 40 multiple choice questions. Each one is worth 2.5 points.

Python. chrysn

8.2 Examples of Classes

Lesson 39: Kinetic Energy & Potential Energy

Tou has been released!

Electrostatics Notes 1 Charges and Coulomb s Law

Chapter 17 Lecture Notes

Description of the motion using vectorial quantities

Python & Numpy A tutorial

Why Doesn t the Moon Hit us? In analysis of this question, we ll look at the following things: i. How do we get the acceleration due to gravity out

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 18 - Linear Algebra

Calculus I 2nd Partial Project Applications of Motion: Position, Velocity, and Acceleration

Lecture Notes Chapter 5 Friction

Introduction to Electricity and Magnetism

In this section we want to learn how to solve equations containing radicals, like 5 x 4 = 9. In order to do this we need the following property.

Lecture 10: Linear Multistep Methods (LMMs)

Write a simple 1D DFT code in Python

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017

What if the characteristic equation has a double root?

3 Types of Nuclear Decay Processes

CHAPTER 1. Review of Algebra

Problem Set 6: Magnetism

NP, polynomial-time mapping reductions, and NP-completeness

Faculty of Engineering and Department of Physics Engineering Physics 131 Midterm Examination Monday February 24, 2014; 7:00 pm 8:30 pm

- Why aren t there more quantum algorithms? - Quantum Programming Languages. By : Amanda Cieslak and Ahmana Tarin

CS Deep Reinforcement Learning HW2: Policy Gradients due September 19th 2018, 11:59 pm

CS177 Spring Midterm 2. April 02, 8pm-9pm. There are 25 multiple choice questions. Each one is worth 4 points.

Mathematics 96 (3581) CA (Class Addendum) 1: Commutativity Mt. San Jacinto College Menifee Valley Campus Spring 2013

Lesson 39: Kinetic Energy & Potential Energy

CLASSICAL ITERATIVE METHODS

SYDE 112, LECTURE 7: Integration by Parts

Los Altos Physics Honors. Electrostatics: Electric Fields, Electric Forces, Electric Potentials and. Electric Potential Energy.

Managing Uncertainty

Project 5: Molecular Dynamics

Forces. gravity, weight, free fall, friction

= lim. (1 + h) 1 = lim. = lim. = lim = 1 2. lim

Study Guide #2. L. Colonna-Romano/T. Keil. Electricity and Magnetism

Electric Potential and Potential Energy. A reformulation from a vector approach to a scalar approach

Appendix A Objectives Mapping

Time-Independent Perturbation Theory

Best Practices. Nicola Chiapolini. Physik-Institut University of Zurich. June 8, 2015

Rotational Motion. PHY131H1F Summer Class 10. Moment of inertia is. Pre-class reading quiz

CRASH COURSE PHYSICS EPISODE #1: Universal gravitation

9.4 Radical Expressions

Atom Model and Relativity

Computer Science Introductory Course MSc - Introduction to Java

CONSERVATIVE FORCES, POTENTIAL ENERGY AND CONSERVATION OF ENERGY

Vectorization. Yu Wu, Ishan Patil. October 13, 2017

Lab/Demo 4 Circular Motion and Energy PHYS 1800

Vector, Matrix, and Tensor Derivatives

Lesson 3-2: Solving Linear Systems Algebraically

Atoms, Molecules and Solids. From Last Time Superposition of quantum states Philosophy of quantum mechanics Interpretation of the wave function:

l Every object in a state of uniform motion tends to remain in that state of motion unless an

Gravitational Wave. Kehan Chen Math 190S. Duke Summer College

7.1 Interacting Systems p Action/reaction pairs p Newton s Third Law p Ropes and Pulleys p.

6.1 Simple circuits. Electric charge

Part A-type questions

AST1100 Lecture Notes

Weekly Update. Shaghayegh Atashi July 10, 2017

Motion. Definition a change of position

SPH4U UNIVERSITY PHYSICS

Least Mean Squares Regression. Machine Learning Fall 2018

Worksheet 1: Integrators

Problem Solving. Undergraduate Physics

Java Programming. Final Examination on December 13, 2015 Fall 2015

8.3. SPECIAL METHODS 217

Derivation of Electro Weak Unification and Final Form of Standard Model with QCD and Gluons 1 W W W 3

PHYS 1007

Classical mechanics: conservation laws and gravity

Section 2.7 Solving Linear Inequalities

14-Feb-18 INSIDE STARS. Matter and Energy. Matter and Energy. Matter and Energy. What is matter?

Least Mean Squares Regression

Transformations and A Universal First Order Taylor Expansion

Multivariate Distributions

Chapter 5 Circular Motion; Gravitation

AP PHYSICS 1 Content Outline arranged TOPICALLY

Math 231E, Lecture 25. Integral Test and Estimating Sums

Temporal Difference Learning & Policy Iteration

Conceptual Physical Science 6 th Edition

Programming Languages Fall 2013

Section 1.5. Solution Sets of Linear Systems

Chapter 8 Rotational Equilibrium and Rotational Dynamics Force vs. Torque Forces cause accelerations Torques cause angular accelerations Force and

Chapter 22 : Electric potential

Dr. Ines Espinoza, USA

3) Uniform circular motion: to further understand acceleration in polar coordinates

r CM = ir im i i m i m i v i (2) P = i

Machine Learning & Data Mining Caltech CS/CNS/EE 155 Hidden Markov Models Last Updated: Feb 7th, 2017

COMP 204. Object Oriented Programming (OOP) - Inheritance. Mathieu Blanchette

Electric Potential Energy

Week 5 - Dielectrica, Resistance and Resistivity

MA 510 ASSIGNMENT SHEET Spring 2009 Text: Vector Calculus, J. Marsden and A. Tromba, fifth edition

1 Problem 1 Solution. 1.1 Common Mistakes. In order to show that the matrix L k is the inverse of the matrix M k, we need to show that

HW9 Concepts. Alex Alemi November 1, 2009

Universal gravitation

Proof Rules for Correctness Triples

Transcription:

Object Oriented Programming for Python Bas Roelenga Rijksuniversiteit Groningen Kapteyn Institute February 20, 2017 Bas Roelenga (RUG) February 20, 2017 1 / 21

Why use Object Oriented Programming? Code will be more readable and organized There is less duplicate code, i.e. a more efficient program Makes it easier to write larger programs Bas Roelenga (RUG) February 20, 2017 2 / 21

What is Object Oriented Programming? A type of programming which uses object These objects can be everything What is an object? A structure that contains data Data from this structure can be manipulated We can request the data from this structure Bas Roelenga (RUG) February 20, 2017 3 / 21

Syntax structure of Object Oriented Programming Constructing a class 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f ) : Listing 1: Creating a python class Two key things to note class def init (self) (constructor) Bas Roelenga (RUG) February 20, 2017 4 / 21

The Constructor What is a constructor? 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x, y, z ) : 3 s e l f. x = x 4 s e l f. y = y 5 s e l f. z = z Listing 2: The constructor The 4 parameters: self, this is the object itself x, y and z represent the position of the object Bas Roelenga (RUG) February 20, 2017 5 / 21

Instantiating our object 1 x = 2 2 y = 3 3 z = 1 4 How do we create our object? 5 o u r C l a s s = Y o u r C l a s s ( x, y, z ) Listing 3: Creating our object Bas Roelenga (RUG) February 20, 2017 6 / 21

Adding a method(function) to our object Why add a method to our object? 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x ) : 3 s e l f. x = x 4 5 d e f getx ( s e l f ) : 6 r e t u r n s e l f. x Listing 4: Implementing a method When calling the function getx() it will return the x value Also called a getter function Bas Roelenga (RUG) February 20, 2017 7 / 21

Using our object 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x ) : 3 s e l f. x = x 4 5 d e f getx ( s e l f ) : 6 r e t u r n s e l f. x Listing 5: Our object 1 o u r C l a s s = Y o u r C l a s s ( 2 ) 2 x = o u r C l a s s. getx ( ) 3 4 p r i n t x Listing 6: Obtaining data from our object Bas Roelenga (RUG) February 20, 2017 8 / 21

Adding more methods(functions) to our object Instead of getting a value from our object we can also change the value of an object 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x ) : 3 s e l f. x = x 4 5 d e f setx ( s e l f, x ) : 6 s e l f. x = x Listing 7: Implementing a method 1 o u r C l a s s = Y o u r C l a s s ( 2 ) 2 o u r C l a s s. setx ( 3 ) 3 4 p r i n t o u r C l a s s. getx ( ) Listing 8: Obtaining data from our object Bas Roelenga (RUG) February 20, 2017 9 / 21

Adding more methods(functions) to our object Add a function which returns an operation on data members of the class 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x, y ) : 3 s e l f. x = x 4 s e l f. y = y 5 6 d e f g e t R a d i u s ( s e l f ) : 7 r e t u r n np. s q r t ( s e l f. x 2 + s e l f. y 2) Listing 9: Implementing a method When calling the function getradius() of the object, the object will return the radius Bas Roelenga (RUG) February 20, 2017 10 / 21

Adding a class variable Class variables are variables that are a part of the class (not of the object) These are static elements 1 c l a s s Y o u r C l a s s : 2 3 c = 3E8 4 5 d e f i n i t ( s e l f, x, y ) : 6 s e l f. x = x 7 s e l f. y = y Listing 10: Adding static variable When creating 2 objects from this class c will remain the same for both, thus if we change c it will change for both objects Bas Roelenga (RUG) February 20, 2017 11 / 21

Exercise 1: Question Code 10 particles which have the following parameters and store them in a list: A 3 dimensional position A mass It doesn t matter what the actual values of the positions or mass end up being. Bas Roelenga (RUG) February 20, 2017 12 / 21

Exercise 1: Answer 1 c l a s s P a r t i c l e : 2 d e f i n i t ( s e l f, x, y, z, mass ) : 3 4 s e l f. x = x 5 s e l f. y = y 6 s e l f. z = z 7 s e l f. mass = mass 8 9 p a r t i c l e l i s t = [ ] 0 1 f o r i i n range ( 0, 10) : 2 3 p = P a r t i c l e ( i, i, i, i 10) 4 p a r t i c l e l i s t. append ( p ) Listing 11: Answer Bas Roelenga (RUG) February 20, 2017 13 / 21

Inheritance What is inheritance? When is inheritance useful, i.e why do we want to use it? There are 2 kinds of classes now: Superclass (can also be called an abstract class) Subclass Bas Roelenga (RUG) February 20, 2017 14 / 21

Using inheritance: superclass The superclass looks like any other class This class will serve as a abstract for our subclasses 1 c l a s s S u p e r c l a s s : 2 3 d e f i n i t ( s e l f, x, y ) : 4 5 s e l f. x = x 6 s e l f. y = y 7 8 d e f getx ( s e l f ) : 9 r e t u r n s e l f. x 0 1 d e f gety ( s e l f ) : 2 r e t u r n s e l f. y Listing 12: Our superclass Bas Roelenga (RUG) February 20, 2017 15 / 21

Using inheritance: subclass The subclass looks a little bit different 1 2 c l a s s S u b c l a s s ( S u p e r c l a s s ) : 3 4 d e f g e t S u b c l a s s S p e c i f i c P r o p e r t i e ( s e l f ) : 5 r e t u r n s e l f. x s e l f. y Listing 13: Our subclass Notice that this subclass does not contain a constructor Bas Roelenga (RUG) February 20, 2017 16 / 21

Using inheritance: putting it all together We create the a class in the following way: 1 2 x = 1 3 y = 3 4 5 o u r C l a s s = S u b c l a s s ( x, y ) 6 7 p r i n t o u r C l a s s. getx ( ) 8 p r i n t o u r C l a s s. g e t S u b c l a s s S p e c i f i c P r o p e r t i e ( ) Listing 14: Putting it together We can use all the function and variables from the superclass Only our subclass can use its specific property method!! Bas Roelenga (RUG) February 20, 2017 17 / 21

Overloading What is overloading? 1 2 c l a s s S u b c l a s s ( S u p e r c l a s s ) : 3 4 d e f i n i t ( s e l f, x, y, mass ) : 5 s e l f. x = x 6 s e l f. y = y 7 8 s e l f. mass = mass 9 0 d e f g e t S u b c l a s s S p e c i f i c P r o p e r t i e ( s e l f ) : 1 r e t u r n s e l f. x s e l f. y Listing 15: Overloading The constructor function now overloads the constructor function of the superclass Bas Roelenga (RUG) February 20, 2017 18 / 21

Exercise 2: Question Create 3 classes of particles (call them electrons, protons and neutrons) Every class has the following properties: A position An energy Every class also has a specific method which can only be used by the class itself: Electron: A function that returns the spin (hint: Overload the electron class constructor) Proton: A function that returns the kinetic energy Neutron: A function that returns the mass Bas Roelenga (RUG) February 20, 2017 19 / 21

Exercise 2: Answer First of we create the superclass Particle We create a subclass for each particle For electrons we overload the constructor For protons and neutrons we give there mass as a static variable We create the function that does each of the operations Bas Roelenga (RUG) February 20, 2017 20 / 21

If there is time left Create a simple N-body simulation using object oriented programming The following steps can be followed: Create a 100 particles with x, y, z and mass parameters (just assume everything starts with no velocities at all) Create a loop which does a 100 iterations of our N-Body simulation In each iterations each particles needs to update its position, use the gravitational force between the particles to calculate the acceleration, then use this acceleration to calculate to update the velocity of each particle and thus update the position. Hints: It doesn t really matter what values are used for the simulation, we only want to train our object oriented part. Give every particle an update function, so you only have to call the update function and put in the acceleration to update the position of the particle. Bas Roelenga (RUG) February 20, 2017 21 / 21