Skip to content
Snippets Groups Projects
Commit e624bac2 authored by Jennifer Rinker's avatar Jennifer Rinker
Browse files
parents 8d784129 ebcada38
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,12 @@ To help Matlab-experienced users begin coding in Python. This includes a review
of syntax differences, along with a brief introduction of the most commonly
used libraries, and a quick overview of PEP8 coding conventions.
## Who should come
This workshop is geared primarily towards new Python users that have previously
coded in Matlab. However, there are some topics that may be of use to more
experienced users, such as the package tutorials and PEP8 coding conventions.
## Date
The workshop date and location will be announced internally at DTU Risø. Please
......@@ -28,14 +34,34 @@ arranging a new workshop.
## Prerequisites
If you are attending the workshop, please do the following before attending:
1. If you do not have Anaconda installed, please [install it](https://www.anaconda.com/download/)
**with Python 3.6**
2. If you have Anaconda installed, please either
a) have your root environment be Python 3.6, or
b) [create an environment](https://conda.io/docs/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands)
that has Python 3.6
1. If you didn't attend Workshop 1, do those prerequisites ([link](https://gitlab.windenergy.dtu.dk/python-at-risoe/scientific-python-workshops/1-intro-to-conda#prerequisites)).
2. If you don't have git installed, [install it](https://git-scm.com/downloads)
with the following options:
- Default git components
- Either git bash only or git bash & Windows command prompt
- Use OpenSSH
- Open SSL
- Checkout Windows-style, commit Unix-style
- Use MinTTY
- Default extra options
3. Clone the workshop repository to your computer.
- In your git terminal (the application is called "Git bash" in Windows),
change to a directory where you want the new folder to be placed. E.g.,
`cd /c/Users/$USERNAME/Desktop`
Note that, in git bash, file paths are similar to Unix, with forward slashes
and a different way to reference the C-drive. If you have a file path
with a space in it, put a single quote at the beginning and end of the path.
- Enter this command into the terminal:
`git clone https://gitlab.windenergy.dtu.dk/python-at-risoe/scientific-python-workshops/2-getting-started.git`
4. Test that git is set up properly by trying to pull.
- Change into the newly cloned folder
`cd 2-getting-started/`
- Enter this command:
`git pull origin master`
5. Just before the workshop, update the folder by pulling the changes (same
command as in Step 4).
## Contact
Jenni Rinker
Jenni Rinker
rink@dtu.dk
\ No newline at end of file
# Comparison of Python and Matlab
Originally from [this page](http://reactorlab.net/resources-folder/matlab/P_to_M.html)
but modified to 1) be in markdown, 2) fix some errors, and 3) add more detail.
This is very much a work in progress. This re-formatting is taking some time.
<table>
<tr>
<th>Python</th>
<th>Matlab</th>
</tr>
<tr>
<td><ul>
<li>numeric variables are double precision if decimal added</li>
</ul>
<pre><code>a = 5. # this is a float
a = 5 # this is an int</code></pre>
</td>
<td><ul>
<li>numeric variables are double precision by default</li>
</ul>
<pre><code>a = 5.0; % this is a double</code></pre>
</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
</tr>
</table>
# test
numeric arrays are objects defined in NumPy package <br /> indexes start at 0 in Python <br /> structures are defined by indentation, no 'end' <br /> `A = np.empty(10) # initialize array A`| array indexes start at 1 in Matlab <br /> indentation is for readability only <br /> `for i=1:10` <br /> ` A(i) = i;`
for i in range(1,11): | end
A.append(i) | A % display contents of A
print(A[i-1]) |
|
# repeat which prints a series of |
# values | for i=0:2:10
| fprintf(' %i \n', i)
for i in range(0,11,2): | end
print(i) |
|
# initialize an identity matrix |
| % MATLAB has built-in functions for
# import the numpy library for matrix operations | % common array initializations
|
import numpy as np | B = eye(100);
|
B = np.identity(3) |
|
# declare and initialize an array, | C = [1, 2, 3]; % or C = [1 2 3];
# known as a list in Python |
|
C = [1, 2, 3] |
|
# initialize and print an array |
# array name = arange(start,stop,step) | % array name = [start:increment:end];
|
import numpy as np |
C = np.arange(2,10,2) | C = [2:2:8] % leave off ; to display value
print(C) |
|
# print an array element on screen |
# array indexes start at 0 | % array indexes start at 1
|
print(C[1]) | C(2)
|
# prints 4 using C from above table cell | % prints 4 using C from above table cell
# note square brackets C[1] | % note parentheses C(2)
|
# declare and initialize an array |
# with fixed interval between values |
import numpy as np |
C = np.linspace(2,8,4) | C = linspace(2,8,4);
# third param is optional and = # points |
# between and including 1st two points | % third param is optional and = # points
# if third param left off, default | % between and including 1st two points
# is 50 points | % if third param left off, default
| % is 100 points
# initialize a 2D array | % these three examples accomplish the
| % same thing
|
D = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] | D = [1 2 3; 4 5 6; 7 8 9];
| D = [1:3; 4:6; 7:9];
| D = [1 2 3
| 4 5 6
| 7 8 9];
# print element of 2D array | % array indexes start at 1
# array indexes start at 0 |
print(D[1][1]) # row 2, column 2 |
# prints 5 using D from above table cell | D(2,2) % row 2, column 2
| % prints 5 using D from above table cell
# print selected sub array of 2D array |
# e.g., print rows 1 to 2 of column 1 |
| D(1:2,1) % rows 1 to 2 of column 1
for i in range(0,2): |
print(D[i][0]) |
|
# print all rows of column 1 of 2D |
# array |
|
import numpy as np | D(:,1) % all rows, column 1
D = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) |
Dsub = D[0:,0:1] |
print(Dsub) |
|
# logical expression |
|
a = 1 | a = 1
b = 2 | b = 2;
if a == 1 or b == 3: | if a == 1 || b == 3
print('a = 2 or b = 3') | fprintf('a = 2 or b = 3 \n');
| end
|
# if structure |
|
if a == 1 and b != 3: | if a == 1 && b ~= 3
print('a=1 and b not 3'); | fprintf('a=1 and b not 3 \n');
print('OK?') | fprintf('OK? \n');
| end
|
# if, else structure |
| a ~= 1
if a != 1: | fprintf('a is not 1 \n')
print('a is not 1') | elseif b ~= 3
elif b != 3: | fprintf('b is not 3 \n')
print('b is not 3') | else
else: | fprintf('huh? \n')
print('huh?') | end
|
# switch structure |
| switch menuChoice
# Python doesn't have a switch structure | case 1
| % can do any actions in a case, e.g.,
# any switch structure can be | % call a user-defined function
# written as an if-else structure | myMenuFunc01();
| case 2
# switch structures may be quicker to | myMenuFunc02();
# read and write for applications such as menus | case 3
| myMenuFunc03();
| otherwise
| fprintf('invalid selection, try again')
| end
|
# program which calls a user-defined function | % main program and function definition must
| % be in separate files and function file
# define function, here I chose name myfunc | % must have same name as function name
|
def myfunc(x,y): | z = myfunc(2,3)
return x**y # ** is exponentiation operator | % prints 8 for this input
|
# call function | ----- LISTING OF FILE myfunc.m ------
|
z = myfunc(2,3) | function returnValue = myfunc(x,y)
print(z) | returnValue = x^y; % ^ is exponentiation operator
# prints 8 for this input |
| % function is a keyword
| % returnValue is arbitrary variable name
|
# matrix multiplication |
|
import numpy as np |
| A = [2,3; 3,5];
A = np.matrix( ((2,3), (3, 5)) ) | B = [1,2; 5,-1];
B = np.matrix( ((1,2), (5, -1)) ) |
| C = A * B
C = A * B |
print(C) |
|
# plotting |
|
import numpy as np |
import matplotlib.pyplot as plt |
x = np.linspace(0,2*np.pi,100) | x = linspace(0,2*pi,100);
y = np.sin(x) | y = sin(x);
plt.plot(x,y) | plot(x,y)
plt.ylabel('sin(x)') | ylabel('sin(x)')
plt.xlabel('x') | xlabel('x')
plt.show() |
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment