Skip to content

Commit a649d73

Browse files
committed
upload diagonal script
1 parent c5f459e commit a649d73

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

get_diagonals.ipynb

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Script to obtain diagonals of arrays, including rectangular arrays."
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import numpy as np"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 2,
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"np.random.seed(536)"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 3,
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"test_wide = np.random.randint(0, 10, (4,10))\n",
35+
"test_long = np.random.randint(0, 10, (10,4))"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 4,
41+
"metadata": {},
42+
"outputs": [
43+
{
44+
"data": {
45+
"text/plain": [
46+
"array([[5, 2, 3, 5, 5, 1, 5, 9, 4, 0],\n",
47+
" [9, 1, 4, 0, 6, 1, 4, 5, 0, 9],\n",
48+
" [9, 7, 2, 1, 0, 8, 8, 0, 3, 4],\n",
49+
" [6, 9, 0, 3, 7, 1, 5, 8, 8, 3]])"
50+
]
51+
},
52+
"execution_count": 4,
53+
"metadata": {},
54+
"output_type": "execute_result"
55+
}
56+
],
57+
"source": [
58+
"test_wide"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 5,
64+
"metadata": {},
65+
"outputs": [
66+
{
67+
"data": {
68+
"text/plain": [
69+
"array([[4, 5, 0, 5],\n",
70+
" [0, 8, 2, 2],\n",
71+
" [4, 5, 7, 6],\n",
72+
" [3, 4, 4, 1],\n",
73+
" [1, 6, 8, 6],\n",
74+
" [2, 5, 4, 6],\n",
75+
" [1, 5, 3, 8],\n",
76+
" [8, 5, 4, 6],\n",
77+
" [9, 0, 5, 4],\n",
78+
" [4, 3, 1, 2]])"
79+
]
80+
},
81+
"execution_count": 5,
82+
"metadata": {},
83+
"output_type": "execute_result"
84+
}
85+
],
86+
"source": [
87+
"test_long"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 6,
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"data": {
97+
"text/plain": [
98+
"array([5, 2, 6])"
99+
]
100+
},
101+
"execution_count": 6,
102+
"metadata": {},
103+
"output_type": "execute_result"
104+
}
105+
],
106+
"source": [
107+
"np.diag(test_long, 1) # compare with numpy's inbuilt function"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 7,
113+
"metadata": {},
114+
"outputs": [
115+
{
116+
"data": {
117+
"text/plain": [
118+
"array([1, 5])"
119+
]
120+
},
121+
"execution_count": 7,
122+
"metadata": {},
123+
"output_type": "execute_result"
124+
}
125+
],
126+
"source": [
127+
"test_wide[[0,3], [5,6]]"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": 11,
133+
"metadata": {},
134+
"outputs": [],
135+
"source": [
136+
"def get_diag(arr, k=0, from_element=None):\n",
137+
" '''from element = coordinates of an array element,\n",
138+
" use if want to calculate the offset from coords\n",
139+
" instead of giving the offset itself'''\n",
140+
" N,M = arr.shape\n",
141+
" if from_element != None:\n",
142+
" k = from_element[1] - from_element[0] # same as numpy\n",
143+
" if k > 0:\n",
144+
" row_lim = min(N, M-k)\n",
145+
" possible_rows = np.arange(0, row_lim)\n",
146+
" col_lim = min(M, k+N)\n",
147+
" possible_cols = np.arange(k, col_lim)\n",
148+
" elif k =< 0:\n",
149+
" row_lim = min(N, -k+M)\n",
150+
" possible_rows = np.arange(-k, row_lim)\n",
151+
" col_lim = min(M, N-(-k))\n",
152+
" possible_cols = np.arange(0, col_lim)\n",
153+
" return(arr[possible_rows, possible_cols])"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 26,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"data": {
163+
"text/plain": [
164+
"array([4, 8, 7, 1])"
165+
]
166+
},
167+
"execution_count": 26,
168+
"metadata": {},
169+
"output_type": "execute_result"
170+
}
171+
],
172+
"source": [
173+
"get_diag(test_long, k=0)"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 27,
179+
"metadata": {},
180+
"outputs": [
181+
{
182+
"data": {
183+
"text/plain": [
184+
"array([8, 0, 1])"
185+
]
186+
},
187+
"execution_count": 27,
188+
"metadata": {},
189+
"output_type": "execute_result"
190+
}
191+
],
192+
"source": [
193+
"get_diag(test_long, k=-7)"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": 30,
199+
"metadata": {},
200+
"outputs": [
201+
{
202+
"data": {
203+
"text/plain": [
204+
"array([9, 0, 4])"
205+
]
206+
},
207+
"execution_count": 30,
208+
"metadata": {},
209+
"output_type": "execute_result"
210+
}
211+
],
212+
"source": [
213+
"get_diag(test_wide, k=7)"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": null,
219+
"metadata": {},
220+
"outputs": [],
221+
"source": []
222+
}
223+
],
224+
"metadata": {
225+
"kernelspec": {
226+
"display_name": "Python 3.7.4 64-bit",
227+
"language": "python",
228+
"name": "python37464bitdff059f72f8b417fb86b0d43a0194990"
229+
},
230+
"language_info": {
231+
"codemirror_mode": {
232+
"name": "ipython",
233+
"version": 3
234+
},
235+
"file_extension": ".py",
236+
"mimetype": "text/x-python",
237+
"name": "python",
238+
"nbconvert_exporter": "python",
239+
"pygments_lexer": "ipython3",
240+
"version": "3.7.4"
241+
}
242+
},
243+
"nbformat": 4,
244+
"nbformat_minor": 2
245+
}

0 commit comments

Comments
 (0)