2.2. Declaring arrays#

NumPy arrays can be declared using the np.array() function.

a = np.array([ a1, a2, ..., an ])

In Spyder, create a new Python file called 2_Arrays.py and save it to your OneDrive folder. We are going to declare an array for the vector \(\vec{a} = (1, 2, 3)\). Enter the code below into your program.

# 2. Arrays

import numpy as np

# Declaring arrays
a = np.array([1, 2, 3])
print(f"a = {a}")

Note that the elements in the row vector are contained within square bracket and elements are separated using commas. Run your program and you should see the following.

a = [1 2 3]

To declare a 2D array we input multiple row vectors separated by commas.

A = np.array([[ a11, a12, .. , a1n ],
              [ a21, a22, .. , a2n ],
                 :    :         :
              [ am1, am2, .. , amn ]])

Lets declare a 2D array for the matrix

\[\begin{split}A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}.\end{split}\]

Enter the following into your program.

# 2D arrays
A = np.array([ [1, 2,] , [3, 4] ])
print(f"A = \n{A} \n")

Run your program and you should see the following added to the console.

A = 
[[1 2]
 [3 4]]

2.2.1. Range of numbers#

A 1D array containing a sequence of numbers can be generated using the numpy.arange() command.

np.arange(start : stop : step)

start is the first number in the range, stop is 1 more than the last number in the range and step is the difference between the numbers. If start or step aren’t specified Python uses default values of start = 0 and step = 1.

To generate an array containing the numbers 1 to 9 we use start = 1, stop = 10 (1 more than the last number 9) and step = 1. Enter the following code into your program.

# Range of numbers
b = np.arange(1, 10, 1)
print(f"b = {b} \n")

Run your program and you should see the following added to the console.

b = [1 2 3 4 5 6 7 8 9]

Since Python uses the default values of step = 1 we don’t need to specify the step value. Change the range command to np.arange(1, 10) and rerun your program, you should see that the output has not changed.

To generate an array containing the even numbers between 0 and 20 inclusive we use start = 0, stop = 21 and step = 2.

c = np.arange(0, 21, 2)
print(f"c = {c} \n")

Run your program and you should see the following added to the console.

c = [ 0  2  4  6  8 10 12 14 16 18 20]

Note that 0 is an even number since the definition of an even number is a number that has a remainder of 0 when divided by 2.

We can also use negative step values. To generate an array containing the numbers 10 to 1 in descending order we have start = 10, stop = 0 (1 less than the last number 0) and step = -1. Enter the following code into your program.

d = np.arange(10, 0, -1)
print(f"d = {d} \n")

Run your program and you should see the following added to the console.

d = [10  9  8  7  6  5  4  3  2  1]

2.2.2. Generating special matrices#

The NumPy library contains functions that can be used to generate arrays for some special matrices (Table 2.1).

Table 2.1 NumPy functions for generating special matrices#

Function

Description

np.zeros((m, n))

An \(m \times n\) array of 0s

np.ones((m, n))

An \(m \times n\) array of 1s

np.eye(n)

An \(n \times n\) identity matrix

To print a \(1\times 6\) array of zeros add the following code to your program.

# Special matrices
zeros_1x6 = np.zeros(6)
print(f"1x6 array of zeros = {zeros_1x6} \n")

Run your program and you should see the following added to the console.

1x6 array of zeros = [0. 0. 0. 0. 0. 0.] 

To print a \(4 \times 4\) array of ones add the following code to your program.

ones_4x4 = np.ones((4,4))
print(f"4x4 array of ones = \n\n {ones_4x4} \n")

Run your program and you should see the following added to the console.

4x4 array of ones = 

 [[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]] 

To print a \(5 \times 5\) identity matrix add the following code to your program.

I_5 = np.eye(5)
print(f"5x5 identity matrix = \n\n {I_5} \n")

Run your program and you should see the following added to the console.

5x5 identity matrix = 

 [[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]] 

2.2.3. Exercise#

Create a new Python file called 2_Arrays_exercises.py and save it to your OneDrive folder. Use it to answer the following exercises.

Exercise 2.1

Define and print arrays corresponding to the following vectors and matrices

  1. \(\vec{a} = (6, 3, 4, -1)\)

  2. \(B = \begin{pmatrix} 3 & 5 & -2 \\ 2 & 4 & 3 \\ 7 & 2 & -1 \end{pmatrix}\)

  3. \(C = \begin{pmatrix} 2 & 0 & -1 & 4 \\ 7 & -3 & 9 & -5 \end{pmatrix}\)

  4. \(D = \begin{pmatrix} -4 & 4 & 2 \\ 7 & 5 & -3 \\ 5 & 1 & 6 \end{pmatrix}\)

  5. An array of odd numbers up to and including 31

  6. An array containing the multiples of 6 that between 0 and 100 inclusive

  7. An array containing the multiples of 9 between 900 and 1000 in reverse order

  8. A 4 \(\times\) 8 array of 3s

  9. A 10 \(\times\) 10 identity matrix