summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2022-08-06 21:43:18 +0800
committerzhang <zch921005@126.com>2022-08-06 21:43:18 +0800
commit1a83481394812c9f8dbebd60a04b3b66655cd01b (patch)
tree2dfae87852dfe4d3c0a4b08e6d73aa09c1eedc22
parent6ff967aaa317073b43c8764386823191cdf8656c (diff)
finance
-rw-r--r--finance/equal_principal.py17
-rw-r--r--finance/equal_principal_interest.py19
-rw-r--r--numerical/image_inter.py93
-rw-r--r--numerical/scipy_interp.py11
4 files changed, 140 insertions, 0 deletions
diff --git a/finance/equal_principal.py b/finance/equal_principal.py
new file mode 100644
index 0000000..97af5d2
--- /dev/null
+++ b/finance/equal_principal.py
@@ -0,0 +1,17 @@
+
+
+def monthly_analysis(p, r, n):
+ r /= 12
+ paied_p = 0
+ for i in range(n):
+ month_pay = p/n + (p - paied_p)*r
+ print(i, month_pay, p/n, (p-paied_p)*r)
+ paied_p += p/n
+
+
+if __name__ == '__main__':
+
+ p = 2000000
+ r = 0.055
+ n = 30*12
+ monthly_analysis(p, r, n)
diff --git a/finance/equal_principal_interest.py b/finance/equal_principal_interest.py
new file mode 100644
index 0000000..035fd8c
--- /dev/null
+++ b/finance/equal_principal_interest.py
@@ -0,0 +1,19 @@
+
+
+def monthly_analysis(p, r, n):
+ r = r/12
+ month_p_and_i = p*r*(1+r)**n/((1+r)**n - 1)
+ remaining_p = p
+ for i in range(n):
+ month_i = remaining_p*r
+ month_p = month_p_and_i - month_i
+ remaining_p -= month_p
+ print(i, month_p_and_i, month_p, month_i)
+
+
+if __name__ == '__main__':
+ p = 600000
+ r = 0.06
+ n = 20*12
+ monthly_analysis(p, r, n)
+
diff --git a/numerical/image_inter.py b/numerical/image_inter.py
new file mode 100644
index 0000000..97b37d9
--- /dev/null
+++ b/numerical/image_inter.py
@@ -0,0 +1,93 @@
+'''
+ use bilinear interpolation to resize an image
+ https://www.brezeale.com/?p=812
+'''
+
+import numpy as np
+from PIL import Image
+
+
+def resizeImage(name):
+ img1 = Image.open(name)
+
+ old = np.asarray(img1) # convert to Numpy array
+ rows, cols, layers = old.shape
+ new = np.zeros((2 * rows - 1, 2 * cols - 1, layers))
+ print("original dimensions:", old.shape)
+
+ for layer in range(3):
+ new[:, :, layer] = resizeLayer(old[:, :, layer])
+
+ # convert the values to unsigned, 8-bit integers
+ new = new.astype(np.uint8)
+ print(" new dimensions:", new.shape)
+
+ img2 = Image.fromarray(new) # convert back to Image
+ newName = "big-" + name
+ img2.save(newName)
+
+
+def resizeLayer(old):
+ rows, cols = old.shape
+
+ rNew = 2 * rows - 1
+ cNew = 2 * cols - 1
+ new = np.zeros((rNew, cNew))
+
+ # move old points
+ new[0:rNew:2, 0:cNew:2] = old[0:rows, 0:cols]
+
+ ''' alternative approach
+ # something like this would be necessary in languages
+ # that don't support slicing
+ new = np.zeros( (2*rows - 1, 2*cols - 1) )
+ for r in range(rows) :
+ for c in range(cols) :
+ new[2*r, 2*c] = old[r,c]
+
+ rows, cols = new.shape
+ '''
+
+ # produce vertical values
+ new[1:rNew:2, :] = (new[0:rNew - 1:2, :] + new[2:rNew:2, :]) / 2
+ ''' alternative approach
+ for r in range(1, rows, 2) :
+ for c in range(0, cols, 2) :
+ # top + bottom
+ new[r,c] = ( new[r-1,c] + new[r+1,c] ) // 2
+ '''
+
+ # produce horizontal values
+ new[:, 1:cNew:2] = (new[:, 0:cNew - 1:2] + new[:, 2:cNew:2]) / 2
+ ''' alternative approach
+ for r in range(0, rows, 2) :
+ for c in range(1, cols, 2) :
+ # left + right
+ new[r,c] = ( new[r,c-1] + new[r,c+1] ) // 2
+ '''
+
+ # produce center values
+ new[1:rNew:2, 1:cNew:2] = (new[0:rNew - 2:2, 0:cNew - 2:2] +
+ new[0:rNew - 2:2, 2:cNew:2] +
+ new[2:rNew:2, 0:cNew - 2:2] +
+ new[2:rNew:2, 2:cNew:2]) / 4
+ ''' alternative approach
+ for r in range(1, rows, 2) :
+ for c in range(1, cols, 2) :
+ # top + bottom + left + right
+ new[r,c] = ( new[r-1,c] + new[r+1,c] + new[r,c-1] + new[r,c+1] ) // 4
+ '''
+
+ return new
+
+
+#################### main ####################
+
+test = np.array([[10, 40], [30, 20]])
+print(test)
+test = resizeLayer(test)
+print()
+print(test)
+
+# filename = 'book_fausett_small.jpg'
+# resizeImage(filename)
diff --git a/numerical/scipy_interp.py b/numerical/scipy_interp.py
new file mode 100644
index 0000000..fb7d3e8
--- /dev/null
+++ b/numerical/scipy_interp.py
@@ -0,0 +1,11 @@
+import numpy as np
+
+from scipy import interpolate
+x = np.asarray(range(2))
+y = np.asarray(range(2))
+z = np.asarray([[10, 40], [30, 20]])
+f = interpolate.interp2d(x, y, z, kind='linear')
+
+for x in range(6):
+ for y in range(6):
+ print(x, y, f(x, y))