-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBasic_vector_manipulation.py
126 lines (86 loc) · 2.67 KB
/
Basic_vector_manipulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# lists
[0, 4, 5]
[0, 0.1, 1+3j]
x = 1
y = 2
r = [x,y]
print r
# if x or y changes later on, the list doesn't change
r[0] = 2
#expressions for x, y coordinates
x = 1
y = 2
r = [ x*2 , y+1/sqrt(y) ]
# find the magnitude of vector a:
a = [1.0, 3.0, 5.6]
magnitude = sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2])
sum[1,2,3] # returns the sum of the elements in common type --> 6
max[1,2,3] #returns max value --> 3
min[1,2,3] #returns min value --> 1
len[1,2,3] # returns the length or number of elements --> 3
#map : apply an operatino to ALL the elements of a list : NOTE in python 2, returns a list, but in python 3, returns an "iterator" where you need to convert to a list
logr = map(log,r) #python 2
map(lambda x: x+1, [1,2,3]) # --> [2,3,4]
logr = list(map(log,r)) # python 3
# append something to our list r
r = [1.0, 1.5, 2.1, 3.6]
r.append(5.6)
print r
# append --> create a list from scratch
# remove an element "pop"
r = [1.0, 1.5, 2.1, 3.6]
r.pop() # note r.pop(n) removes n element, but SLOW
print r
'''
2) arrays (different than lists!!)
** Used for vectors, matrices, images, etc **
- number of elements is FIXED
- all elements must be the same type
- arrays can be 2D, 3D, or higher
- can do arithmetic on WHOLE arrays at once
- array operatinos are faster
- printout of arrays is different
- creating arrays
- numpy functions
- ones, empty
- convert list to array
- reading arrays from a single/multiple column file
- array operations
- dot product
- max, min, len
- size, shape
'''
# zeros array [0,0,0,0] fixedsize
from numpy import zeros, ones, empty # note differences in array formations
a = zeros(4, float)
print a
#convert list to array
r = [1.0, 2.0, 4.0]
a = array(r, float) # array name and type
# multi-dimensional array
from numpy import array
a = array([ [1,2,3] , [4,5,6] ], int)
print a
# mutli-dimensional array - elements indexed in order row, then column
a = zeros([2,2], int) #prints out [[0 0][0 0]]
a [0,1] = 1
a [1,0] = -1
print (a) # prints out [[ 0 1][-1 0]]
# note need to account for fact that indexed starts at 0
# read arrays from a single/mulitple column file
a = loadtxt("values.txt",float)
# perform operations on the whole array at once
a = array( [1.0,2.0,3.0] , float)
b = array([4.0,5.0,6.0], float)
c = a + b
print c
# Dot product ( to multiply the individual elements)
from numpy import array, dot
a = array( [1.0,2.0,3.0] , float)
b = array([4.0,5.0,6.0], float)
c = dot(a,b)
print c #32
# Check the size and shape of an array ( instead of max, min, len)
a = array([[1.0, 2.0, 3.0], [4.0,5.0,6.0]], float)
print a.size # --> 6
print a.shape # --> (2,3) prints out row-column