Skip to content

Commit 564179a

Browse files
committed
increment 1
1 parent 718b99a commit 564179a

File tree

131 files changed

+16252
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+16252
-0
lines changed

analysis/Compression_Analysis/PSNR.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import numpy as np
2+
import math
3+
import cv2
4+
5+
def Representational(r,g,b):
6+
return (0.299*r+0.287*g+0.114*b)
7+
8+
def calculate(img):
9+
b,g,r = cv2.split(img)
10+
pixelAt = Representational(r,g,b)
11+
return pixelAt
12+
13+
def main():
14+
15+
#Loading images (orignal image and compressed image)
16+
orignal_image = cv2.imread('orignal_image.png',1)
17+
compressed_image = cv2.imread('compressed_image.png',1)
18+
19+
#Getting image height and width
20+
height,width = orignal_image.shape[:2]
21+
22+
orignalPixelAt = calculate(orignal_image)
23+
compressedPixelAt = calculate(compressed_image)
24+
25+
diff = orignalPixelAt - compressedPixelAt
26+
error = np.sum(np.abs(diff) ** 2)
27+
28+
error = error/(height*width)
29+
30+
#MSR = error_sum/(height*width)
31+
PSNR = -(10*math.log10(error/(255*255)))
32+
33+
print("PSNR value is {}".format(PSNR))
34+
35+
36+
if __name__ == '__main__':
37+
main()
38+
Loading
29.3 KB
Loading
81.9 KB
Loading

arithmetic_analysis/bisection.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import math
2+
3+
4+
def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano
5+
6+
start = a
7+
end = b
8+
if function(a) == 0: # one of the a or b is a root for the function
9+
return a
10+
elif function(b) == 0:
11+
return b
12+
elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative,
13+
# then his algorithm can't find the root
14+
print("couldn't find root in [a,b]")
15+
return
16+
else:
17+
mid = (start + end) / 2
18+
while abs(start - mid) > 0.0000001: # until we achieve precise equals to 10^-7
19+
if function(mid) == 0:
20+
return mid
21+
elif function(mid) * function(start) < 0:
22+
end = mid
23+
else:
24+
start = mid
25+
mid = (start + end) / 2
26+
return mid
27+
28+
29+
def f(x):
30+
return math.pow(x, 3) - 2*x - 5
31+
32+
33+
print(bisection(f, 1, 1000))

arithmetic_analysis/intersection.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import math
2+
3+
def intersection(function,x0,x1): #function is the f we want to find its root and x0 and x1 are two random starting points
4+
x_n = x0
5+
x_n1 = x1
6+
while True:
7+
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n)))
8+
if abs(x_n2 - x_n1)<0.00001 :
9+
return x_n2
10+
x_n=x_n1
11+
x_n1=x_n2
12+
13+
def f(x):
14+
return math.pow(x,3)-2*x-5
15+
16+
print(intersection(f,3,3.5))
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import numpy
2+
3+
def LUDecompose (table):
4+
#table that contains our data
5+
#table has to be a square array so we need to check first
6+
rows,columns=numpy.shape(table)
7+
L=numpy.zeros((rows,columns))
8+
U=numpy.zeros((rows,columns))
9+
if rows!=columns:
10+
return
11+
for i in range (columns):
12+
for j in range(i-1):
13+
sum=0
14+
for k in range (j-1):
15+
sum+=L[i][k]*U[k][j]
16+
L[i][j]=(table[i][j]-sum)/U[j][j]
17+
L[i][i]=1
18+
for j in range(i-1,columns):
19+
sum1=0
20+
for k in range(i-1):
21+
sum1+=L[i][k]*U[k][j]
22+
U[i][j]=table[i][j]-sum1
23+
return L,U
24+
25+
26+
27+
28+
29+
30+
31+
matrix =numpy.array([[2,-2,1],[0,1,2],[5,3,1]])
32+
L,U = LUDecompose(matrix)
33+
print(L)
34+
print(U)

arithmetic_analysis/newton_method.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x)
2+
x_n=startingInt
3+
while True:
4+
x_n1=x_n-function(x_n)/function1(x_n)
5+
if abs(x_n-x_n1)<0.00001:
6+
return x_n1
7+
x_n=x_n1
8+
9+
def f(x):
10+
return (x**3)-2*x-5
11+
12+
def f1(x):
13+
return 3*(x**2)-2
14+
15+
print(newton(f,f1,3))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Implementing Newton Raphson method in Python
2+
# Author: Haseeb
3+
4+
from sympy import diff
5+
from decimal import Decimal
6+
7+
def NewtonRaphson(func, a):
8+
''' Finds root from the point 'a' onwards by Newton-Raphson method '''
9+
while True:
10+
c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) )
11+
12+
a = c
13+
14+
# This number dictates the accuracy of the answer
15+
if abs(eval(func)) < 10**-15:
16+
return c
17+
18+
19+
# Let's Execute
20+
if __name__ == '__main__':
21+
# Find root of trigonometric function
22+
# Find value of pi
23+
print ('sin(x) = 0', NewtonRaphson('sin(x)', 2))
24+
25+
# Find root of polynomial
26+
print ('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
27+
28+
# Find Square Root of 5
29+
print ('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
30+
31+
# Exponential Roots
32+
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
33+
34+
35+
36+

boolean_algebra/quine_mc_cluskey.py

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
def compare_string(string1, string2):
2+
l1 = list(string1); l2 = list(string2)
3+
count = 0
4+
for i in range(len(l1)):
5+
if l1[i] != l2[i]:
6+
count += 1
7+
l1[i] = '_'
8+
if count > 1:
9+
return -1
10+
else:
11+
return("".join(l1))
12+
13+
def check(binary):
14+
pi = []
15+
while 1:
16+
check1 = ['$']*len(binary)
17+
temp = []
18+
for i in range(len(binary)):
19+
for j in range(i+1, len(binary)):
20+
k=compare_string(binary[i], binary[j])
21+
if k != -1:
22+
check1[i] = '*'
23+
check1[j] = '*'
24+
temp.append(k)
25+
for i in range(len(binary)):
26+
if check1[i] == '$':
27+
pi.append(binary[i])
28+
if len(temp) == 0:
29+
return pi
30+
binary = list(set(temp))
31+
32+
def decimal_to_binary(no_of_variable, minterms):
33+
temp = []
34+
s = ''
35+
for m in minterms:
36+
for i in range(no_of_variable):
37+
s = str(m%2) + s
38+
m //= 2
39+
temp.append(s)
40+
s = ''
41+
return temp
42+
43+
def is_for_table(string1, string2, count):
44+
l1 = list(string1);l2=list(string2)
45+
count_n = 0
46+
for i in range(len(l1)):
47+
if l1[i] != l2[i]:
48+
count_n += 1
49+
if count_n == count:
50+
return True
51+
else:
52+
return False
53+
54+
def selection(chart, prime_implicants):
55+
temp = []
56+
select = [0]*len(chart)
57+
for i in range(len(chart[0])):
58+
count = 0
59+
rem = -1
60+
for j in range(len(chart)):
61+
if chart[j][i] == 1:
62+
count += 1
63+
rem = j
64+
if count == 1:
65+
select[rem] = 1
66+
for i in range(len(select)):
67+
if select[i] == 1:
68+
for j in range(len(chart[0])):
69+
if chart[i][j] == 1:
70+
for k in range(len(chart)):
71+
chart[k][j] = 0
72+
temp.append(prime_implicants[i])
73+
while 1:
74+
max_n = 0; rem = -1; count_n = 0
75+
for i in range(len(chart)):
76+
count_n = chart[i].count(1)
77+
if count_n > max_n:
78+
max_n = count_n
79+
rem = i
80+
81+
if max_n == 0:
82+
return temp
83+
84+
temp.append(prime_implicants[rem])
85+
86+
for i in range(len(chart[0])):
87+
if chart[rem][i] == 1:
88+
for j in range(len(chart)):
89+
chart[j][i] = 0
90+
91+
def prime_implicant_chart(prime_implicants, binary):
92+
chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))]
93+
for i in range(len(prime_implicants)):
94+
count = prime_implicants[i].count('_')
95+
for j in range(len(binary)):
96+
if(is_for_table(prime_implicants[i], binary[j], count)):
97+
chart[i][j] = 1
98+
99+
return chart
100+
101+
def main():
102+
no_of_variable = int(raw_input("Enter the no. of variables\n"))
103+
minterms = [int(x) for x in raw_input("Enter the decimal representation of Minterms 'Spaces Seprated'\n").split()]
104+
binary = decimal_to_binary(no_of_variable, minterms)
105+
106+
prime_implicants = check(binary)
107+
print("Prime Implicants are:")
108+
print(prime_implicants)
109+
chart = prime_implicant_chart(prime_implicants, binary)
110+
111+
essential_prime_implicants = selection(chart,prime_implicants)
112+
print("Essential Prime Implicants are:")
113+
print(essential_prime_implicants)
114+
115+
if __name__ == '__main__':
116+
main()

ciphers/brute_force_caesar_cipher.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from __future__ import print_function
2+
def decrypt(message):
3+
"""
4+
>>> decrypt('TMDETUX PMDVU')
5+
Decryption using Key #0: TMDETUX PMDVU
6+
Decryption using Key #1: SLCDSTW OLCUT
7+
Decryption using Key #2: RKBCRSV NKBTS
8+
Decryption using Key #3: QJABQRU MJASR
9+
Decryption using Key #4: PIZAPQT LIZRQ
10+
Decryption using Key #5: OHYZOPS KHYQP
11+
Decryption using Key #6: NGXYNOR JGXPO
12+
Decryption using Key #7: MFWXMNQ IFWON
13+
Decryption using Key #8: LEVWLMP HEVNM
14+
Decryption using Key #9: KDUVKLO GDUML
15+
Decryption using Key #10: JCTUJKN FCTLK
16+
Decryption using Key #11: IBSTIJM EBSKJ
17+
Decryption using Key #12: HARSHIL DARJI
18+
Decryption using Key #13: GZQRGHK CZQIH
19+
Decryption using Key #14: FYPQFGJ BYPHG
20+
Decryption using Key #15: EXOPEFI AXOGF
21+
Decryption using Key #16: DWNODEH ZWNFE
22+
Decryption using Key #17: CVMNCDG YVMED
23+
Decryption using Key #18: BULMBCF XULDC
24+
Decryption using Key #19: ATKLABE WTKCB
25+
Decryption using Key #20: ZSJKZAD VSJBA
26+
Decryption using Key #21: YRIJYZC URIAZ
27+
Decryption using Key #22: XQHIXYB TQHZY
28+
Decryption using Key #23: WPGHWXA SPGYX
29+
Decryption using Key #24: VOFGVWZ ROFXW
30+
Decryption using Key #25: UNEFUVY QNEWV
31+
"""
32+
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33+
for key in range(len(LETTERS)):
34+
translated = ""
35+
for symbol in message:
36+
if symbol in LETTERS:
37+
num = LETTERS.find(symbol)
38+
num = num - key
39+
if num < 0:
40+
num = num + len(LETTERS)
41+
translated = translated + LETTERS[num]
42+
else:
43+
translated = translated + symbol
44+
print("Decryption using Key #%s: %s" % (key, translated))
45+
46+
def main():
47+
message = raw_input("Encrypted message: ")
48+
message = message.upper()
49+
decrypt(message)
50+
51+
if __name__ == '__main__':
52+
import doctest
53+
doctest.testmod()
54+
main()

ciphers/onepad_cipher.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import print_function
2+
3+
import random
4+
5+
6+
class Onepad:
7+
def encrypt(self, text):
8+
'''Function to encrypt text using psedo-random numbers'''
9+
plain = [ord(i) for i in text]
10+
key = []
11+
cipher = []
12+
for i in plain:
13+
k = random.randint(1, 300)
14+
c = (i+k)*k
15+
cipher.append(c)
16+
key.append(k)
17+
return cipher, key
18+
19+
def decrypt(self, cipher, key):
20+
'''Function to decrypt text using psedo-random numbers.'''
21+
plain = []
22+
for i in range(len(key)):
23+
p = (cipher[i]-(key[i])**2)/key[i]
24+
plain.append(chr(p))
25+
plain = ''.join([i for i in plain])
26+
return plain
27+
28+
29+
if __name__ == '__main__':
30+
c, k = Onepad().encrypt('Hello')
31+
print(c, k)
32+
print(Onepad().decrypt(c, k))

0 commit comments

Comments
 (0)